Jellyse
Jellyse

Reputation: 863

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?

Upvotes: 1

Views: 2018

Answers (4)

user12920237
user12920237

Reputation: 1

I edited the code above according to the dimensions I wanted (lat, lon)

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

and the feedback was this:

HINT: If operation fails, try multislabbing (http://nco.sf.net/nco.html#msa) wrapped dimension using ncks first, and then apply ncrcat to the resulting file

Upvotes: 0

Charlie Zender
Charlie Zender

Reputation: 6352

@Packard is right on both counts. Moreover, the stride of 1 is default and thus not needed. Hence

ncrcat -C -F -d nj_u,151,152 -d ni_u,234,235 -v vel_u 2009120800000${x}.nc testU${x}.nc

Upvotes: 1

Packard CPW
Packard CPW

Reputation: 339

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

Upvotes: 2

Poshi
Poshi

Reputation: 5762

I see a couple errors in your script, but nothing that could lead to your actual error.

  • The shebang line should not contain space and the path should be absolute
  • There's a comma in the for condition that should be a semicolon

    #!/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
    

When I prepend echo to the command you want to run, I get this result:

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

Three invocations with a single file each. That code is working. It looks like there's something else. Are you simplifying your code or showing us the full code?

Upvotes: 1

Related Questions