Project

General

Profile

exprf long equation that combines constants with variables

Added by Anne Moree about 3 years ago

Hello,

I would like to make a calculation which relies on 2 variables (temp,salt), which I have put into one file using 'cdo merge'.
However, I find that 'cdo aexprf' needs a specific order. For example 'constant + variable' is not OK, while 'variable + constant' goes as expected.

In my exprfile I would like to define a few constants and then calculate like so:

--
  1. Some constants:
    GSmv = 22.3916
    KC = 273.15; # Kelvin-conversion [K]
    b1 = 2.00907;
    b2 = 3.22014;
    b3 = 4.05010;
    b4 = 4.94457;
    b5 = -0.256847;
    b6 = 3.88767;
    b7 = -0.00624523;
    b8 = -0.00737614;
    b9 = -0.010341;
    b10 = -0.00817083;
    b11 = -0.000000488682;
  1. Calculate
    _Tpot = temp - KC;
    Tscaled = ln((298.15-_Tpot)/(KC+_Tpot));
    outvar = (1000/GSmv)*exp(b1 + (b2*Tscaled) + (b3*Tscaled^2) + (b4*Tscaled^3) + (b5*Tscaled^4) + (b6*Tscaled^5) + salt*(b7 + (b8*Tscaled) + (b9*Tscaled^2) + (b10*Tscaled^3)) + (b11*salt^2));
    --

The output of Tscaled in this script after calling 'cdo aexprf,exprfile infile.nc outfile.nc' is correct, but variable outvar is not.
From my testing this seems to be caused by the order of the different operators (for example, Tscaled*b5 works like it should, b5*Tscaled does not).
As the equation starts with a constant, it is a bit tricky to oblige to the start-expression-with-variable.

Questions:
- Any suggestions for a better approach for my purpose?
- Is order indeed relevant for exprf files or what am I doing wrong?

Thank you in advance,

Anne


Replies (7)

RE: exprf long equation that combines constants with variables - Added by Anne Moree almost 3 years ago

Hello again,

I have solved this issue by writing a bash script, which contains a.o.:
cdo aexpr,"o2sat=(1000/${GSmv})*exp(${b1} + (${b2}*Tscaled) + (${b3}*Tscaled^2) + (${b4}*Tscaled^3) + (${b5}*Tscaled^4) + (${b6}*Tscaled^5) + salt*(${b7} + (${b8}*Tscaled) + (${b9}*Tscaled^2) + (${b10}*Tscaled^3)) + (${b11}*salt^2))" infile.nc outfile.nc

Cheers,
Anne

RE: exprf long equation that combines constants with variables - Added by Karin Meier-Fleischer almost 3 years ago

Hi Anne,

I would use the following to retain all information from the temporary variable _Tpot (tas-KC):

Tscaled = ln(((_Tpot*-1.)+298.15)/(_Tpot+KC);

CDO docu:

Please note that functions with numeric return values do not provide extra meta data and are not sufficient 
to declare an output variable.

    cdo expr,'tas=4.0' infile outfile
    cdo expr,'tas=tas+4.0-tas' infile outfile

This example shows two attempts to define a constant field based on the meta data of an input variable. 
The first call will fail because there is no information provided reagarding units or coordinates, whereas 
the second can copy all information from the original variable from the input file.

-Karin

RE: exprf long equation that combines constants with variables - Added by Karin Meier-Fleischer almost 3 years ago

You was faster than me.

To use it with the aexprf operator you can use the script exprfile

GSmv = 22.3916;
KC = 273.15;
b1 = 2.00907;
b2 = 3.22014;
b3 = 4.05010;
b4 = 4.94457;
b5 = -0.256847;
b6 = 3.88767;
b7 = -0.00624523;
b8 = -0.00737614;
b9 = -0.010341;
b10 = -0.00817083;
b11 = -0.000000488682;
_Tpot = tas - KC;
Tscaled = ln(((_Tpot*-1.)+298.15)/(_Tpot+KC);
outvar = (1000/GSmv)*exp(b1 + (b2*Tscaled) + (b3*Tscaled^2) + (b4*Tscaled^3) + (b5*Tscaled^4) + (b6*Tscaled^5) + salt*(b7 + (b8*Tscaled) + (b9*Tscaled^2) + (b10*Tscaled^3)) + (b11*salt^2));
cdo -aexprf,exprfile infile outfile

It should give the correct result.

RE: exprf long equation that combines constants with variables - Added by Anne Moree almost 3 years ago

Hi Karin, thanks for thinking along! Your last answer is indeed what I have been trying as described in my initial question, but it did not work for outvar.

RE: exprf long equation that combines constants with variables - Added by Karin Meier-Fleischer almost 3 years ago

Maybe you have to use the following for outvar:

outvar = exp((Tscaled)*b2 + (Tscaled^2)*b3 + (Tscaled^3)*b4 + (Tscaled^4)*b5 + (Tscaled^5)*b6 + b1 + salt*((Tscaled)*b8 + (Tscaled^2)*b9 + (Tscaled^3)*b10 + b7) + (salt^2)*b11) * (1000/GSmv);

RE: exprf long equation that combines constants with variables - Added by Anne Moree almost 3 years ago

Yes I have been trying in that direction, but then gave up as bash seemed easier. Is your suggestions a 'yes' to that expr is sensitive to the order of constants and variables? This would be relevant for others as well I think.

RE: exprf long equation that combines constants with variables - Added by Uwe Schulzweida almost 3 years ago

expr isn't sensitive to the order of constants and variables. I have tried all 3 variations and get the same correct result for all of them:

outvar = (1000/GSmv)*exp(b1 + (b2*Tscaled) + (b3*Tscaled^2) + (b4*Tscaled^3) + (b5*Tscaled^4) + (b6*Tscaled^5) + salt*(b7 + (b8*Tscaled) + (b9*Tscaled^2) + (b10*Tscaled^3)) + (b11*salt^2));
outvar = exp((Tscaled)*b2 + (Tscaled^2)*b3 + (Tscaled^3)*b4 + (Tscaled^4)*b5 + (Tscaled^5)*b6 + b1 + salt*((Tscaled)*b8 + (Tscaled^2)*b9 + (Tscaled^3)*b10 + b7) + (salt^2)*b11) * (1000/GSmv);
cdo aexpr,"o2sat=(1000/${GSmv})*exp(${b1} + (${b2}*Tscaled) + (${b3}*Tscaled^2) + (${b4}*Tscaled^3) + (${b5}*Tscaled^4) + (${b6}*Tscaled^5) + salt*(${b7} + (${b8}*Tscaled) + (${b9}*Tscaled^2) + (${b10}*Tscaled^3)) + (${b11}*salt^2))" infile.nc outfile.nc
Can you please send us your exact cdo command, your cdo version number and maybe the data so we can reproduce the problem.

    (1-7/7)