problem with formula

Post any questions, ideas, or topics related to Jython and Python scripting.
User avatar
kbedka1
Posts: 428
Joined: Wed Jan 28, 2009 7:27 pm

problem with formula

Post by kbedka1 »

I'm trying to use this formula in my work. When I apply it, I get a visAd.UnitException. How can I get this to work? Also, is exp the correct way to do e^power in jython?

def HIWC_PROB(iwp,T11,T67):
b=mask(iwp,'lt',0,0);
c=mask(T11,'gt',233,0);
d=5.678+(.0006*iwp)-(.0369*T11)+(.0252*(T67-T11));
e=1/(1+exp(-d));
e=e*b*c;
return e;
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: problem with formula

Post by tomw »

Usually this error occurs when doing an arithmetic combination involving add or subtract, and the units of each operand are not convertible. You cannot, for example, add a pressure to a temperature. Depending on the structure of your data, you might be able to use the

Code: Select all

noUnit(data)

function to strip the unit

Regarding exp() -- yes that is the right one; however, because there is no built-in bridge from Jython to VisAD (as there is for add, subtract, etc), you will need to be sure that you get the right one. You'll need something like:

Code: Select all

from visad.python.JPythonMethods import exp;
...
e=1/(1+exp(-d));
User avatar
kbedka1
Posts: 428
Joined: Wed Jan 28, 2009 7:27 pm

Re: problem with formula

Post by kbedka1 »

Here's my new , I'm now getting the error (see attached) "RangeUnits must be convertible with RangeType default Units"

def HIWC_PROB(iwp,T11,T67):
from visad.python.JPythonMethods import exp;
a=mask(iwp,'lt',0,0);
b=mask(T11,'gt',233,0);
c=5.678+(.0006*noUnit(iwp))-(.0369*noUnit(T11))+(.0252*(noUnit(T67)-noUnit(T11)));
d=1/(1+exp(-c));
e=d*b*a;
return e;
Attachments
picture 2015-03-02 at 8.50.36 AM.png
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: problem with formula

Post by tomw »

While I'm not familiar with that specific error, I'm going to assume that the variables that start with "T" are temperature and the variable "iwp" is not -- and that's the issue.

Also, if you can isolate which statement is causing the error that might help. Here are some suggestions:

I think that you should leave the "T" variables as temperature (that is, don't do a noUnit() on them, but make a new variable that is like

Code: Select all

noiwp = noUnit(iwp)

and then use it both in the "mask()" and in the "c=" formula. (The "mask()" method returns an object that has units of the original data, by the way.)

Also, I suggest that you rearrange the expressions so that this new variable is not the first one, since the rule for making a new field is to take on the unit of the first variable encountered.

Worse case is that you'll have to extract the "array" of numbers from the variables, do your arithmetic and then put the result into a new FlatField.

As far as I know, there is no way to simply "turn off" the unit checking when using these complex objects....
User avatar
kbedka1
Posts: 428
Joined: Wed Jan 28, 2009 7:27 pm

Re: problem with formula

Post by kbedka1 »

Here's what I have now but am still getting the same error. You're right, T are temperature units, iwp is not. When I removed all traces of iwp from the formula, I had no problem. As soon as iwp is introduced, the problem reappears. I have iwp declared 2nd in the formula but that did not seem to make a difference

def HIWC_PROB(iwp,T11,T67):
from visad.python.JPythonMethods import exp;
b=mask(T11,'gt',233,0);
noiwp=noUnit(iwp)
c=5.678+(.0006*noiwp)-(.0369*T11)+(.0252*(T67-T11));
d=1/(1+exp(-c));
e=d*b;
return e;
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: problem with formula

Post by tomw »

As Rick will attest, these are "interesting" problems!

Try re-arranging the c= to be:

c=(.0252*(T67-T11))-(.0369*T11)+5.678+(.0006*noiwp);

This "should" keep the units in temperature...should...

Also, stick some "print" statements between the lines to isolate which line of code it's still complaining about...

(You could also try to "fool" it by forcing the unit on "iwp" to be "degK" using "newUnit()"....you'd have to look it up, as I don't know all the arguments for the call.)
User avatar
kbedka1
Posts: 428
Joined: Wed Jan 28, 2009 7:27 pm

Re: problem with formula

Post by kbedka1 »

That didn't work either, I see newUnit(field, varname, unit name), what would "field" be in my example. varname would be iwp, correct?

Also, print statements don't show output since I'm creating this in my local python library and running via a formula call in the Data Explorer window.
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: problem with formula

Post by tomw »

I believe the field would be "iwp" (without the quotes), the varname would be "iwp" (with the quotes), and the unit would be "degK".

You might need to run this via the Jython shell in order to help diagnose the issues. Also, if you're trying to display it as an "Image", try a different display type (like "color shaded plan view")....it could be the error is now not in the computation but in what follows (hence, running the shell to isolate where the error is happening...).
User avatar
kbedka1
Posts: 428
Joined: Wed Jan 28, 2009 7:27 pm

Re: problem with formula

Post by kbedka1 »

newUnit requires 3 inputs, what is the "field" name?

When I start typing this into the shell, i.e. " from visad.python.JPythonMethods import exp;", I get syntax errors…

Color shaded plan view did not work either.

I put an example of my file on your ftp server at: pub/incoming/MTS01V04.0.RS_HIWC.2014048.2259.PX.04K.CDF

iwp=Liquid or Ice Water Path
T11=Infrared Channel Temperature (10.8 um)
T67=IR Mid-level Water Vapor 6.8
User avatar
tomw
Posts: 296
Joined: Tue Dec 23, 2008 3:40 pm

Re: problem with formula

Post by tomw »

newUnit requires 3 inputs, what is the "field" name?


Your variable, "iwp" (without the quotes). the "variable" is the name of the _new_ quantity, so something like "iwpt" (with the quotes).

I'll try to get your file and have a look -- might be a while. I'm hoping someone at the Center knows more about this stuff and will jump in...
Post Reply