if statement in grads

Bernd Becker bernd.becker at METOFFICE.GOV.UK
Fri Mar 13 05:22:25 EDT 2009


This will slow your code down dramatically!

May I suggest to work with
maskout and set const to define 0/1 pointer fields
where the if conditions hold true and
use the pointer (global) fields to
do
what ever is done inside the if statement.

This can be incredibly fast, compared to dealing
with grid point values individually.

Bernd


On Thu, 2009-03-12 at 15:48 -0400, Eric Altshuler wrote:
> John,
>
> One problem I immediately see is that in your if statement:
>
> 'define aa=rain.2*24'
> if (rainlow <= aa & aa < rainhigh)
>
> you are trying to compare the script variables rainlow and rainhigh with 'aa', which is a GrADS defined data variable, not a script variable. This will not work. You need to assign a particular single value to the script variable aa. An example might be the following, with the dimension environment set to all dimensions fixed:
>
> ...
> ...
> 'set t 't1
> ...
> 'set z 'z1
> ...
> 'set x 'x1
> 'set y 'y1
> 'd 24*rain.2'
> aa=subwrd(result,4)
> if (rainlow <= aa & aa < rainhigh)
> ...
> ...
>
> where x1, y1, z1 and t1 are the values (assigned to these script variables) you want for the fixed dimensions. You already have a looping structure set up for Z, and I assume you also have one for T, but you will also need to loop over the values of X and Y so that the script variable aa is assigned a single value in each iteration of the loop.
>
> Best regards,
>
> Eric L. Altshuler
> Assistant Research Scientist
> Center for Ocean-Land-Atmosphere Studies
> 4041 Powder Mill Road, Suite 302
> Calverton, MD 20705-3106
> USA
>
> E-mail: ela at cola.iges.org
> Phone: (301) 902-1257
> Fax: (301) 595-9793
>
> ----- Original Message -----
> From: "John Guhin" <johnguhin at GMAIL.COM>
> To: GRADSUSR at LIST.CINECA.IT
> Sent: Thursday, March 12, 2009 12:31:00 PM GMT -05:00 US/Canada Eastern
> Subject: Re: if statement in grads
>
> Thanks Stephen. You are correct for more than one time step. But can we read
> a 2D variable at all inside the if statement? Consider the following:
>
> 'open /raid/pray/processed_data/IWC/iwc.ctl'
> 'open /raid/pray/processed_data/AMSRE/rain-amsre_15jun06-31aug08.ctl'
> rainlow=0 ; rainhigh=5;
> lon1=0 ; lon2=360
> lat1=-30 ; lat2=30
> undef=-1e+34
> z1=1
> while(z1<19)
> 'set z 'z1
> 'define aa=rain.2*24'
> if (rainlow <= aa & aa < rainhigh)
> 'd aave(iwc,lon='lon1',lon='lon2',lat='lat1',lat='lat2')'
> else
> 'd aave(iwc*0+'undef',lon='lon1',lon='lon2',lat='lat1',lat='lat2')'
> endif
> z1=z1+1
> endwhile
>
> iwc is 3D. rain.2 is actually a 2D variable, so aa is  also 2D. I think
> thats where the problem is.  Anybody has faced similar problem?
> thanks.
>
>
>
> On Thu, Mar 12, 2009 at 11:39 AM, Stephen R McMillan <
> smcmillan at planalytics.com> wrote:
>
> >
> > John,
> > The if statement you quoted--the one that Jennifer suggested--is valid, as
> > is the nested method that Pablo suggested.  However, your unexpected results
> > appear to indicate the "if..else" conditions (in particular are not changing
> > during the second time loop iteration.  Since your variable 'aa' would be
> > the same at each timestep, then I suppose 'iwc' may be the culprit. Have you
> > tried echo-displaying the conditional results at each timestep to confirm
> > their values?
> > Stephen Mc
> >
> >
> > *John Guhin <johnguhin at GMAIL.COM>*
> > Sent by: GRADSUSR at LIST.CINECA.IT
> >
> > 03/11/2009 08:03 PM
> > Please respond to
> > GRADSUSR at LIST.CINECA.IT
> >
> > To
> > GRADSUSR at LIST.CINECA.ITcc
> > Subject
> > Re: if statement in grads
> >
> >
> >
> >
> > Thanks Stephen.
> >  I simplified the script by considering only one time (t). The problem is
> > indeed if statement.
> > It seems if statement like this  if (rainlow <=aa & aa<rainhigh)
> > does not work in grads. Is it true? or there is another way to solve my
> > problem?
> >
> > thanks in advance.
> >
> >
> > On Wed, Mar 11, 2009 at 5:07 PM, Stephen R McMillan <*
> > smcmillan at planalytics.com* <smcmillan at planalytics.com>>wrote:
> > John,
> > Check the position of your "t1=1" line.  As it is, it would appear to
> > repeat the 20 days once.  Did you intend to put "t1=1" before the outermost
> > while loop?
> > Stephen Mc
> >
> >
> > *John Guhin <**johnguhin at GMAIL.COM* <johnguhin at GMAIL.COM>*>*
> > Sent by: *GRADSUSR at LIST.CINECA.IT* <GRADSUSR at LIST.CINECA.IT>
> >
> > 03/11/2009 03:48 PM
> > Please respond to
> > *GRADSUSR at LIST.CINECA.IT* <GRADSUSR at LIST.CINECA.IT>
> >
> >
> > To
> > *GRADSUSR at LIST.CINECA.IT* <GRADSUSR at LIST.CINECA.IT>cc
> > Subject
> > Re: if statement in grads
> >
> >
> >
> >
> >
> >
> > Hi,
> >    I tried with
> >  if (rainlow <=aa & aa<rainhigh)
> >
> >  but didn't work. Am I mising something?
> >
> > thanks in advance.
> >
> >
> > On Wed, Mar 11, 2009 at 1:28 PM, Jennifer Adams <*jma at cola.iges.org*<jma at cola.iges.org>
> > >wrote:
> > Try  changing your if statement to:
> > if (rainlow <=aa & aa<rainhigh)
> > --Jennifer
> >
> > On Mar 11, 2009, at 1:19 PM, John Guhin wrote:
> >
> >
> > Hi,
> >     I am trying to use if statement in a grads script... which doesn't seem
> > to work. I am
> > attaching  the gs file. iwc is a 3D variable, and rain is 2D variable
> > exactly in the same grids. I am trying to extract iwc when rain is within
> > certain range. The script executes without any error, but the output repeats
> > itself every 20 days. This means that the " if" statement is not working!!
> > Any help would be appreciated.
> >
> >  'reinit'
> > 'c'
> > 'open iwc.ctl'
> > 'open /rain.ctl'
> > 'set gxout fwrite'
> > 'set fwrite iwc_ppt_binned.dat'
> >
> > lon1=40 ; lon2=180
> > lat1=-20 ; lat2=20
> > undef=-1e+34
> > rainlow=0 ; rainhigh=5;
> >
> > while(rainhigh<11)
> > t1=1
> > while(t1<21)
> > 'set t 't1
> > z1=1
> > while(z1<19)
> > 'set z 'z1
> > 'define aa=24*rain.2(z=1)'
> > if (rainlow <= aa < rainhigh)
> > 'd aave(iwc,lon='lon1',lon='lon2',lat='lat1',lat='lat2')'
> > else
> > 'd aave(iwc*0+'undef',lon='lon1',lon='lon2',lat='lat1',lat='lat2')'
> > endif
> > z1=z1+1
> > endwhile
> > t1=t1+1
> > endwhile
> > rainlow=rainlow+5
> > rainhigh=rainhigh+5
> > endwhile
> >
> > 'close 2'
> > 'close 1'
> > 'disable fwrite'
> >
> > --
> > John Guhin
> > --
> > Jennifer M. Adams
> > IGES/COLA
> > 4041 Powder Mill Road, Suite 302
> > Calverton, MD 20705
> > *jma at cola.iges.org* <jma at cola.iges.org>
> >
> >
> >
> >
> >
> >
> >
> > --
> > John Guhin
> >
> >
> >
> > ***************************************************
> > The information contained in this e-mail message is intended only for the
> > use of the recipient(s) named above and may contain information that is
> > privileged, confidential, and/or proprietary.  If you are not the intended
> > recipient, you may not review, copy or distribute this message.  If you have
> > received this communication in error, please notify the sender immediately
> > by e-mail, and delete the original message.
> > ***************************************************
> >
> >
> >
> > --
> > John Guhin
> >
> >
> >
> > ***************************************************
> > The information contained in this e-mail message is intended only for the
> > use of the recipient(s) named above and may contain information that is
> > privileged, confidential, and/or proprietary. If you are not the intended
> > recipient, you may not review, copy or distribute this message. If you have
> > received this communication in error, please notify the sender immediately
> > by e-mail, and delete the original message.
> > ***************************************************
> >
>
>
>
> --
> John Guhin
--
Bernd Becker   The Monthly Outlook
Met Office  FitzRoy Road  Exeter   Devon EX1 3PB  United Kingdom
Tel.: +44 (0) 1392 884511 Fax: +44 (0)870 900 5050
E-mail:bernd.becker at metoffice.com  -  http://www.metoffice.com



More information about the gradsusr mailing list