unmerge netcdf ensemble from .nc4 file - netcdf

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

Related

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

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?

Extract specific particle id variable from a netCDF file

I have a netCDF file output from a particle dispersion model (GNOME).
As it is a particle dispersion model, I have every particle identified by a particle id variable:
int id(data) ;
id:description = "particle ID" ;
id:units = "1" ;
I need to extract only some specific particle id and their locations. I have tried with cdo and nco operators and I get these errors:
ncks -v longitude,latitude -d id,62001. infile.nc outputfile.nc
ncks: ERROR dimension id is not in input file
cdo -select,name=latitude,longitude,id=62968 infile.nc outputfile.nc
cdo select (Abort): Unsupported selection keyword: 'id'!
I hope someone could help me. Thanks
The dimension is actually named "data". I suggest you rename the dimension to "id". Then your command should work:
ncrename -d data,id in.nc
ncks -v longitude,latitude -d id,62001. in.nc out.nc
or you could leave the names alone, and if the id is really the data index, then this should work:
ncks -v longitude,latitude -d data,62001 in.nc out.nc
NB: no decimal point this time since data is not a coordinate, as explained here.
EDIT: 20210921 in response to comment below, unless I am missing something, the dataset would need to have a variable traj dimensioned traj(time,data) in order for the suggested commands to have the result you desire. The header of your file shows no such variable.

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

How to merge 2 separate netcdf files into 1 and add a time dimension

I have two NetCDF files of the Greenland ice sheet velocities, one from 2015 and one from 2016. These files contain grided data where the velocity is plotted with x,y coordinates. However, no time dimension is included. How can I merge these two files into 1, where the final file has a time dimension? So in stead of two separate x,y,z grids, I would like to have one x,y,z,t data structure, where time = 2.
Thanks!
If the files contain the same variables and are the same size, try ncecat
ncecat -u time file1.nc file2.nc out.nc
You can add a time dimension to a file with ncap2:
ncap2 -s 'defdim("time",1);time[time]=74875.0;time#long_name="Time"; etc.etc.etc.' -O ~/nco/data/in.nc ~/foo.nc
I suggest reading this thread for more details: https://sourceforge.net/p/nco/discussion/9830/thread/cee4e1ad/
After you have done that you can merge them together either using the ncrcat command (see https://linux.die.net/man/1/ncrcat) or also with cdo
cdo mergetime file1.nc file2.nc combined_file.nc

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