Project

General

Profile

Calculate complex expression

Added by Ilias Moysidis almost 2 years ago

What is the cleanest way of calculating the expression:

h = 5/9 * (e-10), where
e = 6.112 * 10 ^ (7.5 * tas / (237.7+tas)) * hurs / 100.

I only want to keep the h variable.


Replies (3)

RE: Calculate complex expression - Added by Karin Meier-Fleischer almost 2 years ago

Hi Ilias,

you can use the expr operator, see https://code.mpimet.mpg.de/projects/cdo/embedded/cdo.pdf#subsection.2.7.1

cdo -expr,'h = ((5/9) * ((6.112 * 10 ^ (7.5 * tas / (237.7+tas)) * hurs / 100.)-10))' infile outfile

RE: Calculate complex expression - Added by Ilias Moysidis almost 2 years ago

Thank you very much.

When I run the line of code that you gave me:
cdo -expr,'h = ((5/9) * ((6.112 * 10 ^ (7.5 * tas / (237.7+tas)) * hurs / 100)-10))' temp.nc temp2.nc
from the command line, it runs smoothly.

However, when I ran the code from the python wrapper:
Cdo().expr('h = ((5/9) * ((6.112 * 10 ^ (7.5 * tas / (237.7+tas)) * hurs / 100)-10))', input='temp.nc', output='temp2.nc')
I am getting the following error:
Error in calling operator expr with:

/usr/bin/cdo -O -s -expr,h = ((5/9) * ((6.112 * 10 ^ (7.5 * tas / (237.7+tas)) * hurs / 100)-10)) temp.nc temp2.nc<<<

STDOUT:
STDERR:/bin/sh: 1: Syntax error: "(" unexpected

Do you know why?

RE: Calculate complex expression - Added by Karin Meier-Fleischer almost 2 years ago

Try

Cdo().expr("'h = ((5/9) * ((6.112 * 10 ^ (7.5 * tas / (237.7+tas)) * hurs / 100)-10))'", input='temp.nc', output='temp2.nc')
    (1-3/3)