[gradsusr] If Statements and Drawing Variables

Becker, Bernd bernd.becker at metoffice.gov.uk
Wed Apr 19 04:23:35 EDT 2017


check this for something similar, ignore the odd bits:


* drought duration
dur=10
dur=4

* probability threshold
thresh1=3
thresh2=6
*
*  where flood forecast signal for 2 consecutive days is at least thresh percent (higher than  zero)
*  where 2 consecutive days of rain > 5 mm/day are predicted.
*
'define p=const(const(maskout(cdf ,cdf -'thresh1'),1),0,-u)'
*
*  where -no- drought forecast signal for dur consecutive days is at least thresh percent
*  where dur consecutive days of no rain are -not- predicted.
*
'define d=const(const(maskout(cdf.2(t='dur'),cdf.2(t='dur')-'thresh2'),1),0,-u)'
*
*  where rain    forecast  has positive tendency, is increasing with time from week1 to week 2.
*  where more rain falls in forecast week 2 than in forecast week 1.
*
'define i=const(const(maskout(prec.3(t=2    ),prec.3(t=13)-prec.3(t= 6)),1),0,-u)'
*
*  where rain    forecast  has positive tendency, is increasing with time from week2 to week 3&4.
*  where more rain falls in forecast week 3&4 than in forecast week 2.
*
'define j=const(const(maskout(prec.3(t=2    ),prec.3(t=20)-prec.3(t=13)),1),0,-u)'
* where tendency for rainfall during the fx period is positve
'define k=const(const(maskout(i              ,j-1                      ),0),1)'
*
*  where temperature    forecast  has positive tendency, is increasing with time from week1 to week 2.
*  where temperature    in forecast week 2 is warmer than in forecast week 1.
*
'define l=const(const(maskout(tmean.3(t=2    ),tmean.3(t=13)-tmean.3(t= 6)),1),0,-u)'
'define m=const(const(maskout(tmean.3(t=2    ),tmean.3(t=20)-tmean.3(t=13)),1),0,-u)'
'define n=const(const(maskout(l              ,m-1                      ),0),1)'
* apply only in temperate areas, where Tmean<14C
tcut=14
'define n=const(const(maskout(tmean.3(t=2    ), tmean.3(t=2) - 'tcut'        ),0),1)'

'set grads off'
'set warn off'
'set datawarn off'
'set gxout grfill'
'set ccolor 10'
'set ccols 0 10 0 '
'set clevs   0  1 '
'set black -1 .00000001'
'd p*d*k*n'

hth
From: gradsusr-bounces at gradsusr.org [mailto:gradsusr-bounces at gradsusr.org] On Behalf Of Carter Humphreys - Mods for GRX
Sent: 18 April 2017 23:14
To: GrADS Users Forum; jeffduda319 at gmail.com
Subject: Re: [gradsusr] If Statements and Drawing Variables

Jeff,

Thank you for the help! I got that working. So I've gotta a few different fields that I am working with here. What I want to be able to do is after performing the maskout()and const() on those fields, combine the resulting index value from all fields into one image. So in other words, taking the lowest in each field and outputting that as the final image.

I would image you could use the same process [ maskout) + const() ]but wouldn't that require a variables? (And I can't draw variables)

Thank you!


--

Sincerely,

Carter J. Humphreys

Developer



Mods for GRX

carter.humphreys at mods-for-grx.com<mailto:carter.humphreys at mods-for-grx.com> | Support Email: support at mods-for-grx.com<mailto:support at mods-for-grx.com> | Website: www.mods-for-grx.com<http://www.mods-for-grx.com/>

On Mon, Apr 17, 2017 at 10:50 PM, Jeff Duda <jeffduda319 at gmail.com<mailto:jeffduda319 at gmail.com>> wrote:
Unfortunately you can't define plottable fields like that. You need to use a combination of maskout() and const() to get the desired field. Nesting of const((maskout)) usually gets you what you want. maskout() works like an if statement, filtering out values you don't want to plot based on an inequality. The way it works is maskout(field,mask), where field is the raw field you want to plot, and mask is an expression which may include other fields and/or constants. For example, if you wanted to just plot EHI where it was > 1, you could do 'd maskout(ehi,ehi-1)'. The result of maskout is a field plotted only where the mask expression is > 0 (might be >= 0, I forget, but if you're dealing with a continuous field, the end result is the same). Anywhere the mask field does not satisfy the equation, the raw field is treated like missing data. This is where const() comes in. You can nest maskout() within const() to tell grads what to do with the missing data (like the ELSE part of the if-else block). If you wanted to fill the rest of the EHI-masked field with 0s, for example, you could do 'd const(maskout(ehi,ehi-1),0,-u)', where the -u flag specifies const() to only apply the else value to undefined values (created by the maskout).
While maskout works best for one-sided (i.e., semi-infinite) inequalities (e.g., x > 5 or x <= -10), you can use it for bounded inequalities as well using the absolute value function. You'll have to get a little clever with your masking expression, but some simple algebra will do it for you. You can also sum values and take an average (where the bounded inequality is valid, you'll sum the same field twice, so taking the average will restore the original values to the field. For example, for your first expression:
'd 0.5*(maskout(ehi,ehi-0.25)+maskout(ehi,0.5-ehi))' would plot a band of EHI values at grid points where 0.25 < EHI < 0.5. You could wrap this in a const() function to change those values to 1: 'd const(0.5*(maskout(ehi,ehi-0.25)+maskout(ehi,0.5-ehi)),1)'. You can then do that for all your bands. There will not be any overlap.
Of course, that's a longhand way of doing it. You can also just use repeated multiplied instances of nested const(maskout()) functions to get the same result in one grand command, although it will be quite involved. I'll give you a start and leave you to figure the rest out:
'd const(const(maskout(ehi,0.125 - abs(ehi-0.375)),1),0,-u)...
This first chunk gives your first condition where index would be set to 1. Use multiplication for the other terms.
Good luck!
Jeff Duda


On Mon, Apr 17, 2017 at 4:59 PM, Carter Humphreys - Mods for GRX <carter.humphreys at mods-for-grx.com<mailto:carter.humphreys at mods-for-grx.com>> wrote:
Hello,

I'm fairly new to GrADS and am trying use a script to search through a model product using if statements and assign a value to a certain range of that product. (I.e. if temp is between 45 and 55, x = 2) I then try to have GrADS draw that variable but all I end up with is a blank map.

Any help would be appreciated, it's probably simple but I've been pretty much lost after trying this for a few days now.

My Script:

'clear'
'set mpdset hires'
'sdfopen http://nomads.ncep.noaa.gov/dods/nam/nam20170417/nam_12z'

'define ehi = (capesfc*hlcy1000_0m)/160000'
'define index = 0'

if (ehi>=0.25 & ehi<0.50)
index = 1
endif

if (ehi>=0.50 & ehi<0.75)
index = 2
endif

if (ehi>=0.75 & ehi<1.00)
index = 3
endif

if (ehi>=1.00)
index = 4
endif

'set gxout shaded'

'd index'

'cbar'
'draw title GrADS Script Test'
'printim C:/GrADS/GrADS_script_test.png png white'


--

Sincerely,

Carter J. Humphreys

Developer



Mods for GRX

carter.humphreys at mods-for-grx.com<mailto:carter.humphreys at mods-for-grx.com> | Support Email: support at mods-for-grx.com<mailto:support at mods-for-grx.com> | Website: www.mods-for-grx.com<http://www.mods-for-grx.com/>

_______________________________________________
gradsusr mailing list
gradsusr at gradsusr.org<mailto:gradsusr at gradsusr.org>
http://gradsusr.org/mailman/listinfo/gradsusr



--
Jeff Duda
Post-doctoral research fellow
University of Oklahoma School of Meteorology

_______________________________________________
gradsusr mailing list
gradsusr at gradsusr.org<mailto:gradsusr at gradsusr.org>
http://gradsusr.org/mailman/listinfo/gradsusr

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://gradsusr.org/pipermail/gradsusr/attachments/20170419/6e6d0491/attachment-0001.html 


More information about the gradsusr mailing list