Project

General

Profile

How to subtract 1D from 3D netcdf using CDO

Added by Lyndon Mark Olaguera 13 days ago

I have the following 3D (time x lat x lon) netcdf:


netcdf test_500hPa {
dimensions:
    time = UNLIMITED ; // (42 currently)
    bnds = 2 ;
    lon = 288 ;
    lat = 145 ;
    plev = 1 ;
variables:
    double time(time) ;
        time:standard_name = "time" ;
        time:bounds = "time_bnds" ;
        time:units = "hours since 1979-1-1 00:00:00" ;
        time:calendar = "proleptic_gregorian" ;
        time:axis = "T" ;
    double time_bnds(time, bnds) ;
    double lon(lon) ;
        lon:standard_name = "longitude" ;
        lon:long_name = "longitude" ;
        lon:units = "degrees_east" ;
        lon:axis = "X" ;
    double lat(lat) ;
        lat:standard_name = "latitude" ;
        lat:long_name = "latitude" ;
        lat:units = "degrees_north" ;
        lat:axis = "Y" ;
    double plev(plev) ;
        plev:standard_name = "air_pressure" ;
        plev:long_name = "pressure" ;
        plev:units = "Pa" ;
        plev:positive = "down" ;
        plev:axis = "Z" ;
    float gh(time, plev, lat, lon) ;
        gh:long_name = "Geopotential Height" ;
        gh:units = "gpm" ;
        gh:code = 7 ;
        gh:table = 200 ;
        gh:cell_methods = "time: mean" ;

And a 1D (time) netcdf:


netcdf test_zonal {
dimensions:
    time = UNLIMITED ; // (42 currently)
    bnds = 2 ;
    lon = 1 ;
    lat = 1 ;
    plev = 1 ;
variables:
    double time(time) ;
        time:standard_name = "time" ;
        time:bounds = "time_bnds" ;
        time:units = "hours since 1979-1-1 00:00:00" ;
        time:calendar = "proleptic_gregorian" ;
        time:axis = "T" ;
    double time_bnds(time, bnds) ;
    double lon(lon) ;
        lon:standard_name = "longitude" ;
        lon:long_name = "longitude" ;
        lon:units = "degrees_east" ;
        lon:axis = "X" ;
    double lat(lat) ;
        lat:standard_name = "latitude" ;
        lat:long_name = "latitude" ;
        lat:units = "degrees_north" ;
        lat:axis = "Y" ;
    double plev(plev) ;
        plev:standard_name = "air_pressure" ;
        plev:long_name = "pressure" ;
        plev:units = "Pa" ;
        plev:positive = "down" ;
        plev:axis = "Z" ;
    float gh(time, plev, lat, lon) ;
        gh:long_name = "Geopotential Height" ;
        gh:units = "gpm" ;
        gh:code = 7 ;
        gh:table = 200 ;
        gh:cell_methods = "time: mean" ;

I want to subtract the 1D from the 3D file (time dimension) but I am getting this error:


cdo sub test_500hPa.nc test_zonal.nc eddy_hgt_500hPa.nc
cdo    sub (Abort): Grid size of the input parameter gh do not match!

How can I do this in CDO?

Thanks in advance for the help!


Replies (5)

RE: How to subtract 1D from 3D netcdf using CDO - Added by Karin Meier-Fleischer 11 days ago

Hi Lyndon,

you can use the -expr operator for the calculation.

Example: 3d input file 'infile_3d.nc' contains variable 'tsurf'; 1d input file 'infile_1d.nc' contains also variable 'tsurf', therefore, we need to rename it on the fly.

cdo -expr,'tsub = (tsurf - t)' -merge [ -chname,tsurf,t infile_1d.nc -selname,tsurf infile_3d.nc ] outfile.nc

RE: How to subtract 1D from 3D netcdf using CDO - Added by Lyndon Mark Olaguera 8 days ago

Hi Karin,

The square bracket is not recognized:

(base) [lmolaguera@tulingan hgt_analysis]$ cdo -expr,'ghsub = (gh-g)' -merge [-chname,gh,g test_zonal.nc -selname,gh] test_500hPa.nc omg.nc
cdo(1) merge: Process started
cdo(2) selname: Process started
cdo(1) merge: Open failed on >[-chname,gh,g<
              No such file or directory
terminate called without an active exception
Aborted (core dumped)

RE: How to subtract 1D from 3D netcdf using CDO - Added by Lyndon Mark Olaguera 8 days ago

Sorry, it was a typo in the command. but when I opened the file in Grads, it cannot recognize the output.

Scanning self-describing file:  omg.nc
SDF file omg.nc is open as file 1
LON set to 0 360 
LAT set to -90 90 
LEV set to 500.99 500.99 
Time values set: 1979:7:16:9 1979:7:16:9 
E set to 1 1 
Notice: Z coordinate pressure values have been converted from Pa to mb
ga-> q file
File 1 : 
  Descriptor: omg.nc
  Binary: omg.nc
  Type = Gridded
  Xsize = 288  Ysize = 145  Zsize = 1  Tsize = 42  Esize = 1
  Number of Variables = 1
     ghsub  1  t,z,y,x  ghsub
ga-> d ghsub
Data Request Error: Invalid grid coordinates
  World coordinates convert to non-integer  grid coordinates
    Variable = ghsub  Dimension = 2 
  Error ocurred at column 1
DISPLAY error:  Invalid expression 
  Expression = ghsub

RE: How to subtract 1D from 3D netcdf using CDO - Added by Karin Meier-Fleischer 8 days ago

Without the data files I am not able to see what happened. Can you upload the files?

Assuming that

- the file test_zonal.nc contains the 1d data
- the file test_500hPa.nc contains the 3d data

than the correct command should be (without any proof):

cdo -expr,'ghsub = (gh-g)' -merge [ -chname,gh,g test_zonal.nc -selname,gh test_500hPa.nc ] omg.nc

RE: How to subtract 1D from 3D netcdf using CDO - Added by Lyndon Mark Olaguera 8 days ago

Hi Karin,

Thank you for the follow up comment. This is already solved. The problem is with Grads and not cdo.
I was able to plot the output in python.

-Lyndz

    (1-5/5)