Project

General

Profile

expr - the swiss army knife for computations with variables of a single file

Added by Ralf Mueller over 11 years ago

expr is an operator for arithmetic expressions on multiple variables. It supports many function from the C math library as show in the documentation. A classic example is the computation of absolute value of velocity from its components u and v:

cdo expr,'abs_velocity=sqrt(u*u+v*v)' <ifile> <ofile>

Units are not handled by expr, but this can be done manually
cdo setunit,'m/s' -expr,'abs_velocity=sqrt(u*u+v*v)' <ifile> <ofile>

A more complex expression is shown in the tutorial:

cdo -expr,'ps=1013.25*exp((-1)*(1.602769777072154)*log((exp(z/10000.0)*213.15+75.0)/288.15))' 3dheights.nc out.nc

This is the essential part or the pressure computation of the stdatm operator. It possible to write multiple variables at once by separating expression with a semicolon. The temperature computation can be added like this:

cdo -expr,'P=1013.25*exp((-1)*(1.602769777072154)*log((exp(topo/10000.0)*213.15+75.0)/288.15));T=213.0+75.0*exp((-1)*topo/10000.0)'  3dheights.nc out.nc

This can be combined with the topo operator to compute surface pressure and temperature for a realistic height (over land):
cdo -f nc -expr,'P=1013.25*exp((-1)*(1.602769777072154)*log((exp(topo/10000.0)*213.15+75.0)/288.15));T=213.0+75.0*exp((-1)*topo/10000.0)-273.15' -setrtomiss,-100000,-0.0001 -topo out.nc

If you already know expr, please reply your favorite example!


Replies (1)

RE: expr - the swiss army knife for computations with variables of a single file - Added by Ralf Mueller over 10 years ago

Little add on: you can use the '^' for computing arbitrary powers of your input fields:

cdo -infov -expr,'v=10^const' -const,2,r1x1

returns
  -1 :       Date     Time   Level Gridsize    Miss :     Minimum        Mean     Maximum : Parameter name
   1 : 0001-01-01 00:00:00       0        1       0 :                  100.00             : v          

Other operations are also possible:

cdo -infov -expr,'v=sin(3.1415926535/4.0)^const' -const,2,r1x1

returns
    -1 :       Date     Time   Level Gridsize    Miss :     Minimum        Mean     Maximum : Parameter name
     1 : 0001-01-01 00:00:00       0        1       0 :                 0.50000             : v          

    (1-1/1)