Search code examples
cdo-climate

How to calculate variable for based on interval conditon using expr cdo-climate


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 tas is greater than 275.15. In the second condition, I want fsa to be equal to (1-0.5*( tas -273.15)) when tas is between 273.15 and 275.15. Here is my first try using expr, any help is appreciated. any approach will be ok it doesn't have to be expr. my file size is big so a concise method is preferred.

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>

Solution

  • Here is the answer by Ralf from CDO community main forum.

    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 && !