0

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

Thessla7
  • 17
  • 3
  • Just wondering why you have time dimension of size 1460. Was this derived from another 6 hourly dataset ? If so, you may be better off going back to the source data and using `cdo yearmean` and `cdo mergetime`. – Robert Davy Feb 03 '22 at 01:36
  • You are right, the data is already manipulated in terms of time to 6-hourly values. I need some of the variables still in this temporal resolution. Everything is fine with the NetCDF I have for the 10 years respectively. I just would need another one which takes the SMB yearly value from each of the ten NetCDFs and combine it into another NetCDF. – Thessla7 Feb 03 '22 at 08:08
  • Since you only have 10 files it should be ok to do 10x ncks commands to extracts the SMB vaiable. (Either that or write a bash loop). Then you will need to add a time dimension, make it a record dimension, and concatenate, similar to this https://stackoverflow.com/questions/66117320/how-to-add-time-dimension-when-concatenating-daily-trmm-netcdf-files-using-nco – Robert Davy Feb 04 '22 at 06:22

1 Answers1

2

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

ncecat -u time -v SMB Rhone_AWS_199*.nc out.nc
Charlie Zender
  • 5,929
  • 14
  • 19