Project

General

Profile

Frequently Asked Questions

How to Get Help?

We encourage users to use both Forums and Issues tracking system. If you are not sure about using Forums or Issues list, use the Forums first especially the Support list.

To be most helpful, we recommend the following:

Please include the version of CDO in your postings. Use

cdo -V

If possible, check your calls with the latest CDO release - problems may be solved already.

Input Files: Almost all problems have to do with files. To face such problems the original data is needed. This does not mean, that the full data set is needed.

Please use following methods to shrink the data before uploading:
  • Select a single variable from the file, which causes problems. This can be with CDO using selname
  • If your problem arises with a single timestep, send us only one! Use operators from the seltime
  • Remap to a coarse grid with CDO's remapping facilities.
  • If the file is still too large, there might be a public ftp server, where you can upload it.
  • If the data cannot be uploaded, include the output from ncdump -h of your input NetCDF data.
  • Building Problem: Please submit the config.log file created by the configure call.
  • No cross-posting: Do NOT create Forums entries and Issues on the same topic. This is annoying and does not solve your problem.
Tips: ;)
  • Sometimes a simple internet search beginning with cdo followed by your question will show you the answers of similar questions very fast.
  • If you get an email with the keyword Feedback, its on you to answer.
     

How do I reference the CDO Users Guide in publications?

See https://code.mpimet.mpg.de/projects/cdo/wiki/Cite

How can I see what's in my file?

cdo sinfon infile

Example:

cdo sinfon rectilinear_grid_2D.nc
   File format : NetCDF
    -1 : Institut Source   T Steptype Levels Num    Points Num Dtype : Parameter name
     1 : unknown  unknown  v instant       1   1     18432   1  F32  : tsurf         
     2 : unknown  unknown  v instant       1   1     18432   1  F32  : precip        
     3 : unknown  unknown  v instant       1   1     18432   1  F32  : u10           
     4 : unknown  unknown  v instant       1   1     18432   1  F32  : v10           
     5 : unknown  unknown  v instant       1   1     18432   1  F32  : qvi           
     6 : unknown  unknown  v instant       1   1     18432   1  F32  : slp           
   Grid coordinates :
     1 : gaussian                 : points=18432 (192x96)  np=0
                              lon : -180 to 178.125 by 1.875 degrees_east  circular
                              lat : 88.57217 to -88.57217 degrees_north
   Vertical coordinates :
     1 : surface                  : levels=1
   Time coordinate :  40 steps
     RefTime =  2001-01-01 00:00:00  Units = hours  Calendar = standard
  YYYY-MM-DD hh:mm:ss  YYYY-MM-DD hh:mm:ss  YYYY-MM-DD hh:mm:ss  YYYY-MM-DD hh:mm:ss
  2001-01-01 00:00:00  2001-01-01 06:00:00  2001-01-01 12:00:00  2001-01-01 18:00:00
  2001-01-02 00:00:00  2001-01-02 06:00:00  2001-01-02 12:00:00  2001-01-02 18:00:00
  2001-01-03 00:00:00  2001-01-03 06:00:00  2001-01-03 12:00:00  2001-01-03 18:00:00
  2001-01-04 00:00:00  2001-01-04 06:00:00  2001-01-04 12:00:00  2001-01-04 18:00:00
  2001-01-05 00:00:00  2001-01-05 06:00:00  2001-01-05 12:00:00  2001-01-05 18:00:00
  2001-01-06 00:00:00  2001-01-06 06:00:00  2001-01-06 12:00:00  2001-01-06 18:00:00
  2001-01-07 00:00:00  2001-01-07 06:00:00  2001-01-07 12:00:00  2001-01-07 18:00:00
  2001-01-08 00:00:00  2001-01-08 06:00:00  2001-01-08 12:00:00  2001-01-08 18:00:00
  2001-01-09 00:00:00  2001-01-09 06:00:00  2001-01-09 12:00:00  2001-01-09 18:00:00
  2001-01-10 00:00:00  2001-01-10 06:00:00  2001-01-10 12:00:00  2001-01-10 18:00:00
cdo sinfon: Processed 6 variables over 40 timesteps ( 0.00s 20MB )

How can I see the timesteps in my file?

cdo showtimestamp rectilinear_grid_2D.nc

Example:

cdo 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
cdo showtimestamp: Processed 6 variables over 40 timesteps ( 0.00s 20MB )

How many timesteps are in my file?

cdo ntime rectilinear_grid_2D.nc

Example:

cdo ntime rectilinear_grid_2D.nc
40
cdo ntime: Processed 6 variables over 40 timesteps ( 0.01s 20MB )

Tip: If you want to suppress the cdo execution information use the -s option:

cdo -s ntime rectilinear_grid_2D.nc
40

How can I select variables and save it to a new file?

cdo -selname,variable infile outfile

Example: Extract variable tsurf

cdo -selname,tsurf rectilinear_grid_2D.nc tsurf_rectilinear_grid_2D.nc

Extract variables tsurf and precip
cdo -selname,tsurf,precip rectilinear_grid_2D.nc tsurf_precip_rectilinear_grid_2D.nc

How can I extract timesteps?

cdo seltimestep,t infile outfile

Example: Select only time step 1 and 5

cdo -seltimestep,1,5 infile outfile

Select time steps 1 to 12

cdo -seltimestep,1/12 infile outfile

How can I reverse the order of the latitudes from N-S to S-N? (Notice: NOT the other way round)

cdo invertlat infile outfile

How to convert temperature units from K to degC?

cdo addc,-273.15 infile outfile

How can I set a constant value to missing value?

cdo setctomiss,value infile outfile

Example:

cdo setctomiss,-9999.9 infile outfile            

How to set a value range to missing value?

cdo setrtomiss,rmin,rmax infile outfile

Example: To set all values less than 273.15 to missing value

cdo setrtomiss,0,273.15 infile outfile

How to extract a sub-region using lat/lon values (degrees)?

cdo -sellonlatbox,lonmin,lonmax,latmin,latmax infile outfile

Example: To extract only the northern hemisphere

cdo -sellonlatbox,-180.0,180.0,0.0,90.0  infile outfile

How can I change the attributes of a variable?

Use the setattribute operator.

Example: Change the units of variable tas to Kelvin

cdo -setattribute,tas@units="K" infile outfile

How to compute the grid cell area in square meter?

cdo gridarea infile outfile

How to interpolate/remap my data to another grid?

Use one of the remap operators.

Example: You want to interpolate your data to the grid described in the grid description file gridfile.txt

gridtype  = lonlat
xsize     = 300
ysize     = 207
xfirst    = 47.51
xinc      = 0.033333333
yfirst    = 23.61
yinc      = 0.033333333

To remap (bilinear method) to the new grid using the gridfile.txt
cdo remapbil,gridfile.txt infile outfile

How can I convert an ASCII file to netCDF?

Assume the ASCII file contains your data where the columns represent longitude and the rows latitude.
To convert the data to a correct netCDF file, you must know the time dimension, latitude and longitude values.

First, create a grid description file, e.g. gridfile.txt:

gridtype = lonlat
xsize = 32
ysize = 64
xfirst = 0.0
xinc   = 1.0
yfirst = 0.0
yinc   = 1.0

Then use the grid description file, set the variable name, and define the time:

cdo -f nc -settaxis,2000-01-01,00:00:00,1day -setname,mask -input,gridfile.txt outfile.nc < infile.txt

How can I extract data from netCDF/GRIB file and write it to an ASCII file?

The output operators output , outputf , outputtab provide the possibility to extract data from a netCDF file which will be send to stdout.
The output can be redirected to an output file of format ASCII.

To output all data from the input file to outfile.txt (not recommended)

cdo -outputtab,name,date,lon,lat,value infile.nc > outfile.txt

It is better to make a more accurate selection beforehand to keep the output low, like

cdo -outputtab,name,date,lon,lat,value -sellevel,100000 -selname,variable infile.nc > variable_lev100000.txt

How can I remap my data to global 1x1 degree grid?

cdo remapbil,r360x180 infile outfile

How to create a weights file once when I have to remap multiple files to Gaussian N32 grid?

Create weights file for remapping once:

cdo genbil,n32 infile weights.nc

Then do the remaping:

cdo remap,n32,weights.nc infile outfile    

Tip:

If an warning occurs like

cdo remap (Warning): Remap weights from weights.nc not used, lonlat (1440x400) grid with mask (575319) not found!

the "with source mask" means that the input file contains missing values. These missing values are 
ignored during the remapping process. Thats why the remap weights are different for those files.

A workaround could be to set the missing values to zero. This should work at least for precip data:

cdo -R remap,N32,weights.nc -setmisstoc,0 infile outfile

How can I remap my data to a different grid of another data file?

Assume, your data file is called infile and you want to remap it to the same grid as the file other_data.nc.
Use the other_data.nc as grid template to remap your infile to the same grid:

cdo -remapycon,other_data.nc infile outfile

How can I add coordinate variables latitude and longitude if missing in the data file?

If your data is on a 1x1 degrees grid with lon = 0 to 359 and lat = -89.5 to 89.5:

cdo -f nc -setgrid,r360x180 infile outfile

If your data is not on a predefined grid you can create a grid description file, e.g. gridfile.txt:

gridtype  = lonlat
xsize     = 360
ysize     = 180
xfirst    = 10.0
xinc      = 0.125 
yfirst    = 0.0
yinc      = 0.125

Add the coordinates and write the data to e.g. 'outfile_with_latlon.nc'

cdo -f nc -setgrid,grid_latlon_global.txt infile outfile_with_latlon.nc

How can I add a missing time coordinate variable?

Assume you want to add a relative time axis with units 'days since 2000-1-1 12:00:00'

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

How can I remove dimensions of size 1?

cdo --reduce_dim infile outfile

How can I set or change the missing value (or change NaN to missing value)?

Set missing value to a constant value, e.g. -9999

cdo setmisstoc,-9999 infile outfile

Set constant value e.g. -999.9 to missing value
cdo setctomiss,-999.9 infile outfile

Set NaN to missing value and change the missing value to -9999.9
cdo setmissval,nan infile outfile
cdo setmissval,-9999.9 -setmissval,nan ifile ofile

How to set all missing values to the nearest non missing value?

cdo -setmisstonn infile outfile

How can I create a 1x1 degree land/sea mask?

You can use the built-in topography data of the CDOs using the topo operator.

Create a file with masked land:

cdo -f nc -setrtomiss,0,10000 -topo masked_land.nc

Create a file with masked ocean:

cdo -f nc -setrtomiss,-20000,0 -topo masked_ocean.nc

How to use a mask file?

In the forum section 'Operator News' there is an article 'Masking - more general' which describes the
creation and use of mask files in more detail. https://code.mpimet.mpg.de/boards/53/topics/10933

How can I interpolate my model levels to pressure levels?

cdo ml2pl,92500,85000,50000,20000 <input file> <output file>

How to convert my data on a reduced Gaussian grid to a regular grid?

cdo -R copy infile outfile

or

cdo -setgridtype,regular infile outfile

How to split a file containing many years into single yearly files?

You can do it manually year by year, e.g. to select the years 1960-2100:

cdo -selyear,1960 infile 1960_data.nc
cdo -selyear,1961 infile 1961_data.nc
...
cdo -selyear,2100 infile 2100_data.nc

or more comfortable - do it in one command line

for year in $(seq -w 1961 1 2100); do cdo -f nc -selyear,$year infile ${year}_data.nc ; done

In case you don't know the years, you can also use the splityear operator.

How can I extract a special season like JFMAM from my data?

To chose user defined seasons use the select operator with the parameter season
where SEASON is a comma separated list of seasons (substring of DJFMAMJJA- SOND or ANN):

cdo select,season=SEASON infile outfile

here you have to do

cdo select,season=JFMAM infile outfile

How to extract the last timestep without knowing how many timesteps are in my file?

cdo select,timestep=-1 infile outfile

How can I extract the same variables on the same levels from multiple files?

Assume you have 3 input files. Each input file contains the same variables for a different
time period. To select the variable T,U and V on the levels 200, 500 and 850 from all 3
input files, use:

cdo select,name=T,U,V,level=200,500,850 infile1 infile2 infile3 outfile

How to change the default season DJF to JFM for seasavg?

Change the season months for e.g. seasavg (default DJF, MAM, JJA, SON) to e.g. JFM, AMJ, JAS and OND

export CDO_SEASON_START=JAN

Check the used seasons with -v option

cdo -v seasavg infile outfile

How can I convert a GrADS binary file to netCDF?

You can convert a GrADS binary file to netCDF using the associated descriptor file and CDOs import_binary operator:

cdo -f nc import_binary infile.ctl outfile

How can I create a quick look plot of my variable?

CDO has the plot types graph (xy), contour, shaded, grfill and vector.

Examples:

CDO automatically append the variable name and the suffix of the selected graphic format to the plotfile name, e.g.

cdo shaded,device="pdf" -seltimestep,25 -selname,tas in.nc plotfile_t25

produces a PDF file named plotfile_t25_tas.pdf.

Examples:

cdo contour,device="png" -seltimestep,25 -selname,tas in.nc plotfile_t25

Tip:

Vector: no change of projection !!! Variables must have the names u and v !!!

cdo vector,device="png" -seltimestep,13 -chname,u10,u,v10,v -selvar,u10,v10 infile plot_vector

How to retrieve frost days where temperature is less than 0

cdo -f nc -timsum -ltc,0 infile outfile
or
cdo -f nc -timsum -expr,'t2min = ((t2min < 0.0)) ? 1.0 : (t2min/0.0)' infile outfile

How to calculate the bias of two files for the first 31 timesteps?

general: bias = sum(obs(file1)-sim(file2))/ntimes

cdo -divc,31 -timsum -sub -seltimestep,1/31 file1 file2 outfile

How can I delete the February, 29th?

cdo delete,month=2,day=29 infile outfile

How can I remap ICON data when the grid information is stored in a separated file?

Assume you want to remap the ICON data to a global 1x1 degree grid.
First, compute the weights using the separated grid file

cdo genbil,r360x180 ICON_grid_information.nc weights.nc
then remap ICON data
cdo remap,r360x180,weights.nc -setgrid,ICON_grid_information.nc infile outfile

Both commands can be executed in a single call like (in a more general form):

cdo remapbil,localgrid.nc:N1 -selname,FIELDS -setgrid,ingrid.nc:N2 data-file.nc out-file.nc

where inputs are are:

  • localgrid.nc Target grid to which the data is remapped
  • N1 Position of the target sub-grid in localgrid.nc
  • ingrid.nc Source grid from which the data is remapped
  • N2 Position of the source sub-grid in ingrid.nc
  • data-file.nc Source data file with data corresponding to ingrid.nc
  • out-file.nc Output of remapped data corresponding to localgrid.nc
  • FIELDS Short names of fields to be remapped

Positions N1 or N2 are parameter values of gridID when the files are checked with operators like sinfo or griddes. Note that using a single call instead of pre-computing the interpolation weights is not always preferable.

How can I change variable and global file attributes in one step?

You can use the setattribute operator with the FILE attribute name to read the attribute settings from a file.

cdo setattribute,FILE=myattributes.txt infile outfile

E.g. myattributes.txt:

institute="My institute" 
source="my_original_file.nc" 
tas@coordinates="lat lon" 
tas@units="degC" 
pres@units="hPa" 

Why can't I change the attributes and names of the latitude, longitude and levels variables?

Latitude(lat), longitude(lon) and levels (lev) are coordinate variables (dimensions) containing the grid information.
CDO is designed to handle data variables. Plain dimensions are some kind of meta information of data variables and
hence handled automatically.

For renaming and changing the dimension attributes, use the *NCOs* instead.

Examples:

Rename LATS and LONS to lat and lon

ncrename -v LATS,lat -v LONS,lon infile outfile

Change the attributes of lat and lon

ncatted -a units,lat,o,c,"degrees_north" -a units,lon,o,c,"degrees_east" infile outfile

How can I convert my levels values and units from centimeter to meter?

You can use the setzaxis operator to change the z-axis by a descriptor file. Assume your input file contains levels in 'cm' and you want to convert it to values in 'm'. Solution: write a shell script which retrieves the levels from the input file, creates the z-axis descriptor file and generates the new file with z-axis units 'm' and correct values.

#!/usr/bin/env bash

infile="infile.nc" 

levels=$(cdo -s showlevel $infile)
echo "levels = $levels" 

i=0
for value in $levels; do
    v=$(echo "scale=6; $value/100" | bc -l)
    echo "$value cm = $v m " 
    if [[ $i -gt 0 ]]; then
       levelsm=$(echo "$levelsm, $v")
    else
       levelsm=$v
    fi
    i=`expr $i + 1`
done

cat > zaxis_meter.txt << EOF
zaxistype = depth_below_land
size      = $i
name      = olevel
units     = "m" 
levels    = $levelsm
EOF

cdo -f nc setzaxis,zaxis_meter.txt $infile outfile.nc

Why did I get an error when converting GRIB2 to netCDF but CDO lists grb2 as supported?

"Error (decodeIS) : GRIB version 2 unsupported!"

Some model output e.g. from ECMWF (OpenIFS) have mixed GRIB1 and GRIB2 records which is not acceptable for CDO.

Use the grib_api/eccodes tool grib_set to fix it:

grib_set -s editionNumber=1 infile.grb new_infile.grb

Why do I get a much larger file when extracting only one timestep from netCDF4 file?

Most netCDF4 files are compressed and becomes decompressed when using CDO. The option '-z zip' is needed to
compress the CDO output data, too.

cdo -z zip -seltimestep,7 infile outfile

How to interpolate 6-hourly data to 1-hourly data?

cdo inttime,6 infile outfile

or

cdo inttime,2011-01-01,00:00,1hour infile outfile

How to speed up my scripts?

Please have a look at this Operator News thread

.


 

FAQ python-cdo

 

How to import the python-cdo module?

from cdo import Cdo
cdo = Cdo()

How do I write the following command line in Python?

Command:

    cdo -ymonmean -selyear,2010/2019 -selname,tas infile.nc outfile.nc

Same command in Python:

    cdo.ymonmean(input='-selyear,2010/2019 -selname,tas infile.nc', output='outfile.nc')

What kind of data output formats are available?

Parameter to output

Files:               output=<outfile>
Xarray Dataarray:    returnXArray=<variable name>
Xarray Dataset:      returnXDataset=True
Numpy masked array:  returnMaArray=<variable name>

Examples:

    ofile = cdo.selname('t2m', input='mydata.nc')

    cdo.selname('t2m', input='mydata.nc', output='outfile.nc')

    temp = cdo.selname('t2m', input='mydata.nc', returnXArray='t2m')

    ds = cdo.remapbil('global_0.5', input='mydata.nc', returnXDataset=True)

    t_array = cdo.fldmean(input='-sellonlatbox,-180.,180.,60.,90. -selname,t2m mydata.nc', 
                          options='--reduce_dim', returnMaArray='t2m')

How can I use the operator chaining in CDO Python?

Example:

cdo.selgridcell('640417,691450',input="-select,name=u10,v10" + infile, output=outfile)

Why do I get an error using expr in CDO Python?

The computing command has to be set in double quotes AND single quotes, too:

cdo.expr("'ws=sqrt(u10*u10+v10*v10)'",input=infile, output=outfile)