Search code examples
unixdimensionsnetcdfnco

Extract part of many netcdf files using dimensions from another file in unix


I have several thousand .nc files that each need to be trimmed to match the size (lat/lon) of another group of files. I only want data from 70-90N, but the long files contain global data. Running ncdump on longFile1.nc gives

    dimensions:
    altitude = 40
    dim = 55890;
    nv = 2;

and running ncdump on shortFile1.nc gives

    dimensions:
    fakeDim14 = 8111;
    fakeDim15 = 1;

longFile1.nc needs to be cut down so that dim = 8111. The short file dimension length changes for each file. I know that I need the beginning of each long file, but the end point will be different each time.

I can trim longFile1.nc with

    ncks -C -d it,0,8010 -v lat,lon,instant_cloud_phase longFile1.nc -o trimmedLongFile1.nc

but I don't know how to do this for all of them, especially because the length changes with each file. I also have to go from indices 0-8010 to keep the right latitude bounds. Is there a way to read in the first dimension from every short file and then use it in the above nco command for each corresponding long file? Is that nco command even the best way to do this?

Thanks in advance!


Solution

  • Use the NCO filter ncdmnsz and put the whole thing in a loop over files:

    for fl in `ls longFile*.nc`; do
       dmnsz=`ncdmnsz fakeDim14 $fl`
       ncks -F -d it,,$dmnsz -v ...
    done
    

    Note the use of -F for 1-based indices.