How to delete a group variable while keeping the group structure of a netcdf in python or bash? - netcdf

I have a .nc file with a group structure, one of the groups containing a variable I need to delete.
Using xarray, if I want to delete the variable I can only extract its group as a new .nc file.
ds = xr.load_dataset(path_test,group='/data_01/ku')
ds = ds.drop_vars(["ssh"])
ds.to_netcdf(path_test, mode="a", group='/data_01/ku')
Using bash command ncks (from nco) doing this :
ncks -x -g data_01/ku -v ssh in.nc out.nc
I get a memory error.
Does anyone know how to delete one specific variable while keeping the complete group structure of the file ?
Thanks guys

The ncks command you tried looks correct, and such commands work for me.
Try adding the -C switch just in case:
ncks -O -x -C -g g1/g1g1 -v ppc_dbl ~/nco/data/in_grp.nc ~/foo.nc
Seems like you got unlucky, or possibly are employing an old NCO version?

Related

How to add variable from nc1 to nc2 file without deleting/removing the variables in nc2?

I have two kinds of variables in two different nc files. The dimension and other things are same, I just have to add one more variable in the existing nc file, How can I do this (using CDO or R or any other)
I used the command line (cdo selvar,varname in.nc out.nc) but it doesn't help. This command does work but deletes the existing variables. Any suggestions on how can I add new variables without deleting the variable inside the nc file?
Many thanks.
cdo solution
From your comment of clarification, I think the cdo command you need is cat
cdo cat aaa.nc bbb.nc output.nc
This will concatenate the fields in bbb.nc to the ones in aaa.nc and put the result in output.nc
nco solution
As an alternative you can also use ncrcat:
ncrcat aaa.nc bbb.nc output.nc
The NCO solution is
ncks -A -v yyy bbb.nc aaa.nc
as documented here. (Adrian's suggested NCO command would concatenate the files in time, not append one variable to the other file)

multiply variables in two NetCDF files in single command

I have two netcdf files:
file_1.nc with variables qty_1 and qty_2 and
file_2.nc with variables qty_3, qty_4 and qty_5.
I want a file with 3 variables qty_3=qty_3*qty_2; qty_4=qty_4+qty_2 and qty_5.
Now I am first copying the variables to file_2 using
ncks -A -v qty_1,qty_2 file_1.nc file_2.nc
then I am doing math operation as,
ncap2 -A -s 'qty_3=qty_3*qty_2' -s 'qty_4=qty_4+qty_2' file_2.nc
This works, however, take some time.
Is there a way I can do this calculation in a single command ?
If you aren't totallly dependent on NCO, you could do this with CDO:
cdo -selname,qty_3,qty_4,qty_5 -aexpr,'qty_3=qty_3*qty_2;qty_4=qty_4+qty_2' -merge file_1.nc file_2.nc out.nc

unmerge netcdf ensemble from .nc4 file

I have a -nc4 that contains three .nc files. The files were merged with ncecat in the following way:
ncecat --ovr -M -4 -u ensemble_member1.nc ensemble_member2.nc ensemble_member3.nc merged_ensemble.nc4
I would like to unmerge the .nc4 file in the containing folder to recover the three .nc files. Is there a simple command for this?
When ncecat merges ensembles with the command you show, it creates a record variable to glue them together as explained in the manual here. To disaggregate, hyperslab each ensemble out from the combined file and (optionally) average over the degenerate record dimension leftover from the glue, e.g.,
ncwa -a record -d record,0 in.nc ensemble1.nc
ncwa -a record -d record,1 in.nc ensemble2.nc
ncwa -a record -d record,2 in.nc ensemble3.nc

force NCKS to try to append when used in batch script

I want to use ncks in a batch script to select a variable an append it to a file:
ncks -v ${var} $infile $outfile
But interactively it asks me whether I want to exit, overwrite or append.
On the help page of the command there is this tip:
"Some users may wish to avoid interactive ncks queries about whether to overwrite existing data. For example, batch scripts will fail if ncks does not receive responses to its queries. Options -O and -A are available to force overwriting existing files and variables, respectively."
but I don't want to overwrite, I wish to append, is there any way to do this?
Try:
ncks -A -v ${var} $infile $outfile
For me it appended selected variable $var to the $outfile without the prompt.

Select data along non-conventional dimension with CDO or NCO

I have a large number of NetCDF files from which I would like to extract a small number of variables for one location, and merge them into a new NetCDF file. The dimensions of the files are:
dimensions:
time = 18 ;
level = 65 ;
levelh = 66 ;
domain = 36 ;
I can subtract/merge the files for all domains with something like:
cdo select,name=u,v file1.nc file2.nc out.nc
But all other operators seem to be related to selections in space (e.g. sellonlatbox) or time (e.g. seltimestep), but I can't find a way to select only 1 domain from the NetCDF files. Is this possible with CDO's or NCO's?
Not sure I fully understand the question/intent. NCO treats all dimensions equally. If you want domain #17 then try
ncrcat -v u,v -d domain,17 file1.nc file2.nc out.nc
If file1.nc and file2.nc are not sequential in a record coordinate then try
ncecat -v u,v -d domain,17 file1.nc file2.nc out.nc
ADDED 20180929:
or if you don't like that, and the files do not have a record dimension yet are time-sequential then before using ncrcat turn the temporal dimension into a record coordinate for each file with
ncks -O --mk_rec_dmn time file1.nc file1.nc
ncks -O --mk_rec_dmn time file2.nc file2.nc
...
etc. and proceed as above. That may be the best way forward with NCO.

Resources