CDO Examples

In this part a few helpful tips and common used CDO examples are given.

Look at your data


Before start to do anything with your data file have a closer look what's in the file. Only if you know your data you can handle it correctly.

 GRIB files:     cdo sinfon infile.grb    - variable, grid and time information
                 cdo info infile.grb      - data information for each timestep
 
 netCDF files:   cdo sinfon infile.nc    - variable, grid and time information
                 cdo info infile.nc       - data information for each timestep
 
                 ncdump -h infile.nc      - shows metadata
                 ncview infile.nc        - quick look

Notice: If only infile or outfile without a suffix is given in the examples, it can be srv, ieg, grb, grb1, grb2,nc, nc1, nc4, nc4c, and nc5.

Convert a GRIB file to netCDF

cdo -f nc copy infile.grb outfile.nc

Show me the variables in my file

cdo showname infile

cdo showname rectilinear_grid_2D.nc
 tsurf precip u10 v10 qvi slp
cdo showname: Processed 6 variables ( 0.01s 16MB )

To suppress the cdo process information use the -s (silent) option:

cdo -s showname rectilinear_grid_2D.nc
 tsurf precip u10 v10 qvi slp

Show the time steps of a file

cdo -s showtimestamp rectilinear_grid_2D.nc
  2001-01-01T00:00:00  2001-01-01T06:00:00  2001-01-01T12:00:00  2001-01-01T18:00:00  2001-01-02T00:00:00
  2001-01-02T06:00:00  2001-01-02T12:00:00  2001-01-02T18:00:00  2001-01-03T00:00:00  2001-01-03T06:00:00
  2001-01-03T12:00:00  2001-01-03T18:00:00  2001-01-04T00:00:00  2001-01-04T06:00:00  2001-01-04T12:00:00
  2001-01-04T18:00:00  2001-01-05T00:00:00  2001-01-05T06:00:00  2001-01-05T12:00:00  2001-01-05T18:00:00
  2001-01-06T00:00:00  2001-01-06T06:00:00  2001-01-06T12:00:00  2001-01-06T18:00:00  2001-01-07T00:00:00
  2001-01-07T06:00:00  2001-01-07T12:00:00  2001-01-07T18:00:00  2001-01-08T00:00:00  2001-01-08T06:00:00
  2001-01-08T12:00:00  2001-01-08T18:00:00  2001-01-09T00:00:00  2001-01-09T06:00:00  2001-01-09T12:00:00
  2001-01-09T18:00:00  2001-01-10T00:00:00  2001-01-10T06:00:00  2001-01-10T12:00:00  2001-01-10T18:00:00

Lets have a look at the attributes of the variables

cdo -s showatts rectilinear_grid_2D.nc
tsurf:
  long_name = "surface temperature"
  units = "K"
  missing_value = "-9.000000e+33"
precip:
  long_name = "total precipitation"
  units = "kg/m^2s"
  missing_value = "-9.000000e+33"
u10:
  long_name = "10m u-velocity"
  units = "m/s"
  missing_value = "-9.000000e+33"
v10:
  long_name = "10m v-velocity"
  units = "m/s"
  missing_value = "-9.000000e+33"
qvi:
  long_name = "vertically integrated water vapor"
  units = "kg/m^2"
  missing_value = "-9.000000e+33"
slp:
  long_name = "mean sea level pressure"
  units = "Pa"
  missing_value = "-9.000000e+33"
Global:

Select a variable at a given timestep

E.g. variable precip at the first timestep

cdo -seltimestep,1 -selvar,precip infile outfile 

Select a time range

E.g. all variables in the range 1 to 12 timesteps:

cdo -seltimestep,1/12 infile outfile 

Revert the latitudes from N-S to S-N

Notice: not the other way round

cdo invertlat infile outfile

Change the reference time units or add a missing time dimension

Use the settaxis operator to do this for you, e.g. set the reference time units to "2000-01-01,12:00:00,1day":

cdo -r -f nc -settaxis,2000-01-01,12:00:00,1day infile outfile

To overwrite the level description use the setzaxis operator

Notice: the zaxistype shouldn't contain blanks!
Here is an example for a description of 7 pressure level in hPa:

zaxistype = pressure
size      = 7
name      = lev
longname  = pressure
units     = hPa
levels    = 850 500 200 100 50 20 10

Store the above lines in a file, e.g. myzaxis.txt and pass it to setzaxis:

  cdo setzaxis,myzaxis.txt  infile  outfile

For MPI-OM TP6M the z-axis description file looks like:

zaxistype : depth_below_sea
size      : 80
name      : depth
longname  : "depth below sea level"
units     : "m"
levels    : -6, -17, -27, -37, -47, -57, -67, -77.5, -88.5, -100, -112.5, -125.5, -139, -153,
            -167.5, -183, -199, -215.5, -233, -251.5, -271, -291.5, -312.5, -334, -357, -381.5,
            -407, -433.5, -461, -489.5, -519.5, -551, -584, -618.5, -654.5, -692.5, -732, -773,
            -816, -861, -908, -957, -1008.5, -1062.5, -1119, -1178, -1239.5, -1304, -1371.5,
            -1442, -1516, -1593.5, -1674.5, -1759.5, -1848.5, -1941.5, -2038.5, -2140, -2246,
            -2356.5, -2472.5, -2594, -2721, -2854, -2993, -3138.5, -3290.5, -3449.5, -3616,
            -3790, -3972, -4162.5, -4362, -4570.5, -4788.5, -5016.5, -5255, -5504.5, -5765.5,
            -6038.5

Operator chaining: to use more than one operator in one command

E.g. extract the variable tas in a time range and compute the field mean (result is a time series):

cdo -fldmean -seltimestep,1/12 -selvar,precip infile outfile

Notice: the data will be processed using the operators given from the right to the left!
In this example:

  1. extract variable tsurf
  2. select timesteps 1 to 12
  3. compute the field mean

 

 

 

Document Actions