Extract specific particle id variable from a netCDF file - netcdf

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.

Related

How to extract a single variable using NCO. Associated variable problem

I am trying extract to a single variable kd_490 from a NetCDF file (over thredds) using NCO.
My code is below:
ncks -v kd_490 -d lat,40.0,70.0 -d lon,-20.0,15.0 https://rsg.pml.ac.uk/thredds/dodsC/cci/v4.2-release/geographic/daily/kd/1998/ESACCI-OC-L3S-K_490-MERGED-1D_DAILY_4km_GEO_PML_KD490_Lee-19980102-fv4.2.nc out.nc
However, along with kd_490 it also extracts kd_490_bias and kd_490_rmsd. I know that ncks extracts "associated" variables. See here. However, I am not clear on why NCO is identifying these as "associated" variable.
I cannot figure out a way to only select the variable kd_490. Adding "-C" to the code results in the grid being wrong. Does anyone know how?
Note: CDO can solve this problem. However, CDO is less efficient at extracting the spatial subset in the code, so NCO is more appropriate.
NCO identifies kd_490_bias and kd_490_rmsd as associated variables because of the ancillary_variables attribute in kd_490:
float kd_490(time,lat,lon) ;
kd_490:_FillValue = 9.96921e+36f ;
kd_490:long_name = "Downwelling attenuation coefficient at 490nm, derived using Lee 2005 equation and bbw from Zhang 2009 (following the SeaDAS Kd_lee algorithm)" ;
kd_490:units = "m-1" ;
kd_490:ancillary_variables = "kd_490_rmsd kd_490_bias" ;
kd_490:grid_mapping = "crs" ;
kd_490:parameter_vocab_uri = "http://vocab.ndg.nerc.ac.uk/term/P071/19/CFSN0064" ;
kd_490:standard_name = "volume_attenuation_coefficient_of_downwelling_radiative_flux_in_sea_water" ;
kd_490:units_nonstandard = "m^-1" ;
kd_490:_ChunkSizes = 1, 270, 270 ;
as documented here. To extract kd_490 without the ancillary variables, but with the grid variables (which are other "associated" variables), this works for me:
ncks -O -C -v kd_490,crs,lat,lon -d lat,40.0,70.0 -d lon,-20.0,15.0 https://rsg.pml.ac.uk/thredds/dodsC/cci/v4.2-release/geographic/daily/kd/1998/ESACCI-OC-L3S-K_490-MERGED-1D_DAILY_4km_GEO_PML_KD490_Lee-19980102-fv4.2.nc ~/foo2.nc

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

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