bashnetcdfnconcks

How to extract all variables that start with specific string from netCDF file?


I am working with a netCDF file that has a lot of variables and I need to extract only those that start with the string Var_. The only answers that I found that came close to what I am asking were done by using ncdump however I need to do that with ncks.

I tried the following command but it ended up extracting every single variables that were in the input file instead of the ones starting with Var_ only.

ncks -C -v time,lon,lat,Var_* input.nc output.nc #(I also need the time, lon and lat variables)

I tried to do it that way because the numbers of variables in the input file can increase/decrease which is why I can't write the variables names one by one in my script.

Is there a way to do that? Also, this is for a bash script that I am writing.


Solution

  • Your command would have worked had you used regular expression (RX) syntax (documentation) rather than shell-globbing syntax in the variable list. NCO need RX syntax for variable/attribute/dimension lists contained in files, and only uses shell-globbing for filenames/paths that the shell sees. Re-try with something like

    ncks -C -v time,lon,lat,^Var_.? input.nc output.nc