Conditional calculation
Added by Riccardo Valenti 8 months ago
Hello everyone
I'm struggling with a conditional calculation and I was not able to find a suitable answer in the forum.
I need to estimate the vapor pressure (e) for every grid value using the wet-bulb temperature (Twet) and the minimum temperature (Tmin). Both Tmin and Twet are variables I have stored in a single .nc file.
I was able to carry out the calculation in R. As this is only one step of a more complex calculation that up to that point I was able to do with CDO, it would be cleaner if everything could be calculated with CDO.
The calculation should be as follows:
If Twet >= 0 ---> e=6.107 * exp(17.38*Tmin/(239+Tmin))
Else ---> e=6.107*exp(22.44*Tmin/(272.4+Tmin))
Thanks in advance for any kind of help
Replies (4)
RE: Conditional calculation - Added by Karin Meier-Fleischer 8 months ago
Use the expr operator
cdo -expr,'vapor_pressure=((Twet >= 0) ? e=6.107 * exp(17.38*Tmin/(239+Tmin)) : e=6.107*exp(22.44*Tmin/(272.4+Tmin))) -merge infile1 infile2 outfile
RE: Conditional calculation - Added by Riccardo Valenti 8 months ago
Dear Karin
Thanks a lot for your response and the provided code.
I've tried to run the code line with multiple adaptations (cdo 2.4.0), but it does not seem to work.
Here some additional follow-up questions:
- In the code you provided, you name the vapor pressure variable "vapor_pressure" at the beginning of the expression, but "e" in the formulas (e=6.107...) Why?
- Isn't there a ' missing at the end of the expression?
- If both variables are in the same file already, why use the command -merge and two infiles?
Would be grateful if you could help me
RE: Conditional calculation - Added by Karin Meier-Fleischer 8 months ago
I'd understand that the variables were stored in different file.
cdo -expr,'e=((Twet >= 0) ? 6.107 * exp(17.38*Tmin/(239+Tmin)) : 6.107*exp(22.44*Tmin/(272.4+Tmin)))' infile outfile
RE: Conditional calculation - Added by Riccardo Valenti 8 months ago
It's working now, thank you very much!