Project

General

Profile

How to caluclate varaible for based on interval conditon using expr

Added by Fuad Yassin over 2 years ago

Hi all,
I am new to this forum. I am trying to calculate fsa variable values based on two discrete conditions. In the end, I want one output file with variable fsa. The first condition is fsa equals 1 when tsa is greater than 275.15. In the second condition, I want fsa to be equal to (1-0.5*(tas-273.15)) when tsa is between 273.15 and 275.15 . Here is my first try using expr, any help is appreciated. any approach will be ok for me it doesn't have to be expr. my file size is big so concise method is prefered.

cdo -expr, 'fsa = ((tas>275.15)) ? 1.0 : 0; fsa = ((tas<=275.15 & tas>273.15)) ? (1-0.5*(tas-273.15)) : fsa; <infile> <outfile>


Replies (7)

RE: How to caluclate varaible for based on interval conditon using expr - Added by Fuad Yassin over 2 years ago

Fuad Yassin wrote:

Hi all,
I am new to this forum. I am trying to calculate fsa variable values based on two discrete conditions. In the end, I want one output file with variable fsa. The first condition is fsa equals 1 when tsa is greater than 275.15. In the second condition, I want fsa to be equal to (1-0.5*( tas -273.15)) when tsa is between 273.15 and 275.15 . Here is my first try using expr, any help is appreciated. any approach will be ok for me it doesn't have to be expr. my file size is big so concise method is prefered.
cdo -expr, 'fsa = ((tas>275.15)) ? 1.0 : 0; fsa = ((tas<=275.15 & tas>273.15)) ? (1-0.5*(tas-273.15)) : fsa; <infile> <outfile>

RE: How to caluclate varaible for based on interval conditon using expr - Added by Ralf Mueller over 2 years ago

hi Fuad!

you can use nest the ternary operators like this:

cdo -expr,'fsa = ((tas<=275.15 && tas>273.15)) ? (1-0.5*(tas-273.15)) : ((tas>275.15) ? 1.0 : 0)' <infile> <outfile>

The logical OR is && !

Please check the output if it is what you want.

hth
ralf

RE: How to caluclate varaible for based on interval conditon using expr - Added by Fuad Yassin over 2 years ago

Thanks so much, Ralf.
Yes, it gives the output I was looking for.
Now, I clearly understand the nesting method.

Cheers,
Fuad

RE: How to caluclate varaible for based on interval conditon using expr - Added by Ralf Mueller over 2 years ago

happy to help ;-)

just in case you missed it: There are two forum articles about expr which explains other use cases. Maybe that's interesting for you

cheers
ralf

RE: How to caluclate varaible for based on interval conditon using expr - Added by Fuad Yassin over 2 years ago

Perfect.
I was also curious if it is possible to pass multiple inputs for expr. For instance, if I have two input files, is it possible to write it like this
cdo -expr, 'some expression'; <infile1.nc infile2.nc> <outfile>

RE: How to caluclate varaible for based on interval conditon using expr - Added by Ralf Mueller over 2 years ago

no, that's not possible. expr has only a single input stream, but you can always merge things together like:

cdo -O -L -s -merge [ \
  [ -expr,'arg=90.0-deg(v);arg=(arg<0)?arg+360:arg' -atan2 -selname,v veloc.nc -selname,u veloc.nc ] \
  [ -expr,'vel=sqrt(v*v+u*u)' veloc.nc ] \
] velarg.nc

or using the merge before the expr
cdo -f nc -expr,'TP=T+P' -merge stdatm_T.nc stdatm_P.nc o.nc
I think, this is more like what you suggested above

    (1-7/7)