You are on page 1of 4

capping field range - setting a maximum or minimum value - CDO - Project Ma... https://code.mpimet.mpg.

de/boards/1/topics/4014

Forums » Support »

capping field range - setting a maximum or minimum value


Added by Adrian Tompkins over 4 years ago

Hi

I want to set all values below C to C in a netcdf file:

This question has been asked before:


https://code.zmaw.de/boards/1/topics/1589?r=1591#message-1591

And the response suggests using gec and lec.

The solution using these operators would be

cdo mul -gec,$c file.nc file.nc t1.nc


cdo add -mulc,$c -ltc,$c file.nc t1.nc output.nc
rm -f t1.nc

But is there a neater/shorter way to do this?

It would be nice if there were a maxc and minc operator to do it in one command

Adrian

Replies (6)
RE: capping field range - setting a maximum or minimum value - Added by Jaison-Thomas Ambadan over 4 years ago

Hi,

you could try with NCO operator ncap2,

For example, to set the variable "temp < 270K" to 270

ncap2 -s "where(temp < 270.0) temp = 270.0" ifile.nc ofile.nc

It would be nice if there were a maxc and minc operator to do it in one command

or it may be a good idea to include conditional assignments in CDO expr operator

Cheers,
J

RE: capping field range - setting a maximum or minimum value - Added by Ralf Mueller over 4 years ago

you could also use the expr operator an the if-then-else shortcut:

cdo -expr,'temp=(temp>270.0)?5.0:temp;' temp.nc maskedTemp.nc

1 of 4 1/26/21, 4:27 PM
capping field range - setting a maximum or minimum value - CDO - Project Ma... https://code.mpimet.mpg.de/boards/1/topics/4014

RE: capping field range - setting a maximum or minimum value - Added by Ralf Mueller over 4 years ago

the expr operator has learnt some new things in cdo-1.7.1:

NAME
expr, exprf, aexpr, aexprf - Evaluate expressions

SYNOPSIS
expr,instr ifile ofile
exprf,filename ifile ofile
aexpr,instr ifile ofile
aexprf,filename ifile ofile

DESCRIPTION
This module arithmetically processes every timestep of the input dataset.
Each individual assignment statement have to end with a semi-colon.
Unlike regular variables, temporary variables are never written to the output stream.
To define a temporary variable simply prefix the variable name with an underscore (e.g. _varname)
when the variable is declared.

The following operators are supported:

Operator & Meaning & Example & Result


= & assignment & x = y & Assigns y to x
+ & addition & x + y & Sum of x and y
- & subtraction & x - y & Difference of x and y
* & multiplication & x * y & Product of x and y
/ & division & x / y & Quotient of x and y
^ & exponentiation & x ^ y & Exponentiates x with y
== & equal to & x == y & 1, if x equal to y; else 0
!= & not equal to & x != y & 1, if x not equal to y; else 0
> & greater than & x > y & 1, if x greater than y; else 0
< & less than & x < y & 1, if x less than y; else 0
>= & greater equal & x >= y & 1, if x greater equal y; else 0
<= & less equal & x <= y & 1, if x less equal y; else 0
<=> & less equal greater & x <=> y & -1, if x less y; 1, if x greater y; else 0
&& & logical AND & x && y & 1, if x and y not equal 0; else 0
|| & logical OR & x || y & 1, if x or y not equal 0; else 0
?: & ternary conditional & x ? y : z & y, if x not equal 0, else z

The following functions are supported:

Math intrinsics:

abs(x) Absolute value of x


floor(x) Round to largest integral value not greater than x
ceil(x) Round to smallest integral value not less than x
int(x) Integer value of x
nint(x) Nearest integer value of x
sqr(x) Square of x
sqrt(x) Square Root of x
exp(x) Exponential of x
log(x) Natural logarithm of x

2 of 4 1/26/21, 4:27 PM
capping field range - setting a maximum or minimum value - CDO - Project Ma... https://code.mpimet.mpg.de/boards/1/topics/4014

log10(x) Base 10 logarithm of x


sin(x) Sine of x, where x is specified in radians
cos(x) Cosine of x, where x is specified in radians
tan(x) Tangent of x, where x is specified in radians
asin(x) Arc-sine of x, where x is specified in radians
acos(x) Arc-cosine of x, where x is specified in radians
atan(x) Arc-tangent of x, where x is specified in radians
rad(x) Convert x from degrees to radians
deg(x) Convert x from radians to degrees

Coordinates:

clon(x) Longitude coordinate of x (available only if x has geographical coordinates)


clat(x) Latitude coordinate of x (available only if x has geographical coordinates)
gridarea(x) Grid cell area of x (available only if x has geographical coordinates)
clev(x) Level coordinate of x (0, if x is a 2D surface variable)

Constants:

ngp(x) Number of horizontal grid points


nlev(x) Number of vertical levels
size(x) Total number of elements (ngp(x)*nlev(x))
missval(x) Returns the missing value of variable x

Statistical values over a field:

fldmin(x), fldmax(x), fldsum(x), fldmean(x), fldavg(x), fldstd(x), fldstd1(x), fldvar(x), fldvar1(x)

Vertical statistical values:

vertmin(x), vertmax(x), vertsum(x), vertmean(x), vertavg(x), vertstd(x), vertstd1(x), vertvar(x), vertvar1(x)

Miscellaneous:

sellevel(x,k) Select level k of variable x


sellevidx(x,k) Select level index k of variable x
remove(x) Remove variable x from output stream

OPERATORS
expr Evaluate expressions
The processing instructions are read from the parameter.
exprf Evaluate expressions script
Contrary to expr the processing instructions are read from a file.
aexpr Evaluate expressions and append results
Same as expr, but keep input variables and append results
aexprf Evaluate expression script and append results
Same as exprf, but keep input variables and append results

PARAMETER
instr STRING Processing instructions (need to be 'quoted' in most cases)
filename STRING File with processing instructions

I showed some of them here but I missed the comparison stuff ;-(

3 of 4 1/26/21, 4:27 PM
capping field range - setting a maximum or minimum value - CDO - Project Ma... https://code.mpimet.mpg.de/boards/1/topics/4014

RE: capping field range - setting a maximum or minimum value - Added by Jaison-Thomas Ambadan over 4 years ago

cool! Thanks Ralf!

RE: capping field range - setting a maximum or minimum value - Added by Andrea Gierisch almost 4 years ago

Hi,

For example, to set the variable "temp < 270K" to 270

ncap2 -s "where(temp < 270.0) temp = 270.0" ifile.nc ofile.nc

I found your thread, thanks for the hints! However, I wanted to apply a similar limitation to all variables in my file. Thus, I finally used:
cdo setrtoc,rmin,rmax,c ifile ofile
with a very small number for rmin, and rmax=c.
This works as well.

Andrea

RE: capping field range - setting a maximum or minimum value - Added by Andrea Gierisch over 2 years ago

cdo setrtoc,rmin,rmax,c ifile ofile


with a very small number for rmin, and rmax=c.

I recently learned that cdo also accepts "inf" and "-inf" as arguments for rmin and rmax. Hence, this is a better solution:

cdo setrtoc,-inf,270,270 ifile ofile

(1-6/6)

4 of 4 1/26/21, 4:27 PM

You might also like