Search code examples
extractnetcdfncks

Extract NetCDF Variable and create New NetCDF


I need some help with manipulating NetCDF files. In total I have 10 files for 10 years respectively. Each year hast multiple (the same) variables, some of them also covering daily values. Here, I show you one example for the structure:

(base) thess2ice@local:rhone_smb_modelling $ ncdump -h Rhone_AWS_1990.nc
netcdf Rhone_AWS_1990 {
dimensions:
    x = 402 ;
    y = 852 ;
    time = 1460 ;
variables:
    double x(x) ;
        x:_FillValue = NaN ;
        x:standard_name = "x" ;
        x:long_name = "longitude" ;
        x:units = "degrees_east" ;
    double y(y) ;
        y:_FillValue = NaN ;
        y:standard_name = "y" ;
        y:long_name = "latitude" ;
        y:units = "degrees_north" ;
    float HGT(y, x) ;
        HGT:_FillValue = -9999.f ;
        HGT:units = "m" ;
        HGT:long_name = "Elevation" ;
    float ASPECT(y, x) ;
        ASPECT:_FillValue = -9999.f ;
        ASPECT:units = "degrees" ;
        ASPECT:long_name = "Aspect of slope" ;
    float SLOPE(y, x) ;
        SLOPE:_FillValue = -9999.f ;
        SLOPE:units = "degrees" ;
        SLOPE:long_name = "Terrain slope" ;
    float MASK(y, x) ;
        MASK:_FillValue = -9999.f ;
        MASK:units = "boolean" ;
        MASK:long_name = "Glacier mask" ;
    int64 time(time) ;
        time:units = "hours since 1990-01-01 00:00:00" ;
        time:calendar = "proleptic_gregorian" ;
    double T2(time, y, x) ;
        T2:_FillValue = NaN ;
        T2:units = "K" ;
        T2:long_name = "Temperature at 2 m" ;
    double RRR(time, y, x) ;
        RRR:_FillValue = NaN ;
        RRR:units = "mm" ;
        RRR:long_name = "Total precipitation (liquid+solid)" ;
    double ACC(y, x) ;
        ACC:_FillValue = -9999. ;
        ACC:units = "mm yr^-1" ;
        ACC:long_name = "Accumulation from RRR_solid" ;
    double MELT_I(y, x) ;
        MELT_I:_FillValue = -9999. ;
        MELT_I:units = "mm yr^-1" ;
        MELT_I:long_name = "Melt from PDD" ;
    double MELT_S(y, x) ;
        MELT_S:_FillValue = -9999. ;
        MELT_S:units = "mm yr^-1" ;
        MELT_S:long_name = "Melt from PDD" ;
    double SMB(y, x) ;
        SMB:_FillValue = -9999. ;
        SMB:units = "mm yr^-1" ;
        SMB:long_name = "SMB from PDD" ;

I need the data manipulated as input for a model. The variable I need to extract from each of the 10 NetCDF files is the SMB variable which is only a yearly value for each grid cell. So I'd like to build a NetCDF of the form:

(year, y, x) for the SMB variable

I know the ncks command already to extract only the SMB variable, but I can not manage to apply it on multiple files at once (let's say all nc files in the current directory) and bring them into one NetCDF file subsequently spanning all 10 years.

Can anybody help me with that? Would be great!

Theresa


Solution

  • The NCO ncecat command, documented here, does exactly what you seem to want:

    ncecat -u time -v SMB Rhone_AWS_199*.nc out.nc