Search code examples
linuxbashnetcdfnco

Extract data from several netcdf files using ncks in for loop


I am trying to extract certain variables from netcdf files. The following code works if I apply it to a single file:

ncks -C -F -d nj_u,151,152,1 ni_u,234,235,1 -v vel_u 20091208000001.nc testU.nc

See also question: Hyperslab of a 4D netcdf variable using ncks for explanation. Now I want to use this code on several .nc files with following names:

20091208000001.nc
20091208000002.nc
20091208000003.nc

I tried the following loop:

# !bin/bash
for ((x=1;x<=3;x+=1))
do
ncks -C -F -d nj_u,151,152,1 ni_u,234,235,1 -v vel_u 2009120800000$x.nc testU.nc
done

I get the error

ncks: ERROR received 4 filenames; need no more than two

How do I get the loop to only extract from one file at a time and then append the extracted output from all the files into a single output file?


Solution

  • I believe the words ni_u,234,235,1 were mistaken as another filename. You would need another -d before that.

    And if you are processing multiple nc files, you might want to rename testU.nc so that they don't overlap, or you could use ncrcat to concatenate into one single file. E.g.

    ncrcat -C -F -d nj_u,151,152,1 -d ni_u,234,235,1 -v vel_u 2009120800000?.nc testU.nc