Hi Jeff,<div><br></div><div>  This is a great explanation. This would make a very good recipe for the <a href="http://cookbooks.opengrads.org/index.php?title=Main_Page">cookbooks</a>. This issue has been a very common cause of confusion among new users of GrADS.</div>
<div><br></div><div>  (If you need any help getting through the cookbooks let me know.)</div><div><br></div><div>      Arlindo</div><div><br><br><div class="gmail_quote">On Thu, Nov 3, 2011 at 7:46 PM, Jeffrey Duda <span dir="ltr">&lt;<a href="mailto:jdduda@iastate.edu">jdduda@iastate.edu</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">The problem is that there is a difference between grids that can be displayed and script variables.  They can&#39;t be handled the same way.  You can&#39;t use if statements using displayed fields.  What you need to do is to save the output of each computation in your script to a script variable using the subwrd command.  Here is an example:<div class="im">
<br>
<br>&#39;define th1 = temp * (pow(100000 / press , (0.286)))&#39;<br></div>th1 = subwrd(result,4)<br><br>Hopefully you&#39;ve noticed this before, but when you enter a display command at the Grads prompt, there will be a small amount of text output.  If you display a variable with all of the dimensions in your environment fixed, the text will include the value of the field you are displaying.  What subwrd does is grab the fourth word in the string variable &quot;result&quot;.  &quot;result&quot; is automatically assigned each time a display command is run, so that&#39;s why you don&#39;t see it being assigned anywhere else.  NOTE: for some data sets and for some environment settings, Grads will interpolate the output before displaying, so the text output from the display command will read <br>

<br>&quot;Note: interpolating variable...<br>Result value: _____&quot;<br><br>In that case you&#39;ll need to use the sublin command first:<br>line = sublin(result,2)<br>th1 = subwrd(line,4)<br>This tells Grads to nab the second line of the output from the result variable (result includes all of the text output from the display command).  Then you use subwrd to nab the 4th word of the string variable &quot;line&quot;.  When you do this, you&#39;ll have the fields you want as Grads scripting variables.  Then you can run if statements on them.  To output the value of a Grads script variable, use the say command:<br>

say th1 OR<br>say &quot;th1 = &quot;th1<br><br>ON THE OTHER HAND, you can do this without using any scripting variables.  In this case use the max/min and maxloc/minloc functions.  Then your script would look something like this:<br>

<br>&#39;set lev &#39;botlevel&#39; &#39;toplevel<div class="im"><br><div><br></div><div>
<span style="white-space:pre-wrap">        </span>&#39;define th1 = temp * (pow(100000 / press , (0.286)))&#39;</div><div><span style="white-space:pre-wrap">        </span>&#39;define u1 = u&#39;</div>
<div><span style="white-space:pre-wrap">        </span>&#39;define v1 = v&#39;</div><div><span style="white-space:pre-wrap">        </span>&#39;define z1 = geopot/9.81&#39;</div><div><br></div>
</div><div><span style="white-space:pre-wrap">        </span>&#39;define th2 = temp(z-1) * (pow(100000 / press(z-1) , (0.286)))&#39;</div><div><span style="white-space:pre-wrap">        </span>&#39;define u2 = u(z-1)&#39;</div>
<div><span style="white-space:pre-wrap">        </span>&#39;define v2 = v(z-1)&#39;</div><div><span style="white-space:pre-wrap">        </span>&#39;define z2 = geopot(z-1)/9.81&#39;</div><div class="im"><div><br></div><div><span style="white-space:pre-wrap">        </span>&#39;define ri=9.81/(0.5*th1+th2)*(th2-th1)/(pow((u2-u1),2)+pow((v2-v1),2))*(z2-z1)&#39;</div>


<div><br></div><div><span style="white-space:pre-wrap">        </span>&#39;d ri&#39;</div><div><span style="white-space:pre-wrap">        </span>say result <span style="white-space:pre-wrap">                        </span></div>
<div><br></div></div><div>&#39;d min(ri,lev=&#39;botlev&#39;,lev=&#39;toplev&#39;)&#39;<br>&#39;d minloc(ri,lev=&#39;botlev&#39;,lev=&#39;toplev&#39;)&#39;<br>&#39;d max(ri,lev=&#39;botlev&#39;,lev=&#39;toplev&#39;)&#39;<br>
&#39;d maxloc(ri,lev=&#39;botlev&#39;,lev=&#39;toplev&#39;)&#39;<br>
<br>Carefully follow the location of the single quotes and note that there is no while loop here.  Also note the (z-1) appended to the *2 variables to represent the field one level below the current level.  The whole quote thing confuses some people, so that&#39;s understandable.  Try this and see if it works.  Ask if you have any questions.  Good luck.<br>

<br>Jeff Duda<br><br></div><div class="gmail_quote"><div><div></div><div class="h5">On Thu, Nov 3, 2011 at 5:23 AM, Lippo Nester <span dir="ltr">&lt;<a href="mailto:liikkuvattaakse@gmail.com" target="_blank">liikkuvattaakse@gmail.com</a>&gt;</span> wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div></div><div class="h5">
<div>Hi!</div><div><br></div><div>I&#39;ve been trying to create my very first GrADS-script. The script should compute values of the bulk Richardson number in a specific grid point between predefined model levels and then find the minimum value of Ri and probably the model level associated with it.</div>


<div><br></div><div>I tried to do this using while and if loops, but for some reason it&#39;s not working as I expected. Maybe I can&#39;t understand handling variables in GrADS?</div><div><br></div><div>In this script the top and the bottom model layers are set to 40 and 50. Ri is computed and I get a list of the Ri at different levels, and they seem to be ok. However the variable &quot;minri&quot; gets always the value of ri at the lowest model level (here: ri at level 50). Variable &quot;minlevel&quot; is always the highest model level (here: level 40)!</div>


<div><br></div><div>When count is 40 at the first step of the while-loop, the value of ri should be saved into variable minri and the model level at minlev. After that, if ri &lt; minri at lower levels (count &gt; 40), values of minri and minlev should be varied by the second if-loop...</div>


<div><br></div><div>Is there a more simple or more correct way to do this? I thought max() can&#39;t make it...</div><div><br></div><div>Any help is much appreciated! </div><div><br></div><div>Best regards, Lippo</div><div>


<br></div><div>(version of GrADS 2.0.a1)</div><div><br></div><div><div>&#39;set lat 50.00&#39;</div><div>&#39;set lon 10.00&#39;</div><div>botlevel=50</div><div>toplevel=40</div><div><br></div><div>count=toplevel</div><div>


<br></div><div><span style="white-space:pre-wrap">        </span>while (count &lt;= botlevel)</div><div><br></div><div><span style="white-space:pre-wrap">        </span>&#39;set lev &#39;count</div><div>
<span style="white-space:pre-wrap">        </span>&#39;define th1 = temp * (pow(100000 / press , (0.286)))&#39;</div><div><span style="white-space:pre-wrap">        </span>&#39;define u1 = u&#39;</div>
<div><span style="white-space:pre-wrap">        </span>&#39;define v1 = v&#39;</div><div><span style="white-space:pre-wrap">        </span>&#39;define z1 = geopot/9.81&#39;</div><div><br></div><div><span style="white-space:pre-wrap">        </span>&#39;set lev &#39;count - 1</div>


<div><span style="white-space:pre-wrap">        </span>&#39;define th2 = temp * (pow(100000 / press , (0.286)))&#39;</div><div><span style="white-space:pre-wrap">        </span>&#39;define u2 = u&#39;</div>
<div><span style="white-space:pre-wrap">        </span>&#39;define v2 = v&#39;</div><div><span style="white-space:pre-wrap">        </span>&#39;define z2 = geopot/9.81&#39;</div><div><br></div><div><span style="white-space:pre-wrap">        </span>&#39;define ri=9.81/(0.5*th1+th2)*(th2-th1)/(pow((u2-u1),2)+pow((v2-v1),2))*(z2-z1)&#39;</div>


<div><br></div><div><span style="white-space:pre-wrap">        </span>&#39;d &#39;ri</div><div><span style="white-space:pre-wrap">        </span>say result <span style="white-space:pre-wrap">                        </span>*THIS SEEMS TO WORK AS I GET 10 DIFFERENT VALUES OF RI</div>


<div><br></div><div><span style="white-space:pre-wrap">                </span>if (count = toplevel) <span style="white-space:pre-wrap">        </span>*THESE IF-LOOPS AREN&#39;T OKAY, BUT WHY?</div><div><span style="white-space:pre-wrap">                </span>minri = ri</div>


<div><span style="white-space:pre-wrap">                </span>minlevel = count</div><div><span style="white-space:pre-wrap">                </span>endif</div><div><br></div><div><span style="white-space:pre-wrap">                </span>if (ri &lt; minri)</div>
<div><span style="white-space:pre-wrap">                </span>minri = ri</div><div><span style="white-space:pre-wrap">                </span>minlevel = count</div><div><span style="white-space:pre-wrap">                </span>endif</div>
<div><br></div><div><span style="white-space:pre-wrap">        </span>count = count+1</div><div><span style="white-space:pre-wrap">        </span>endwhile</div><div><br></div><div>&#39;d &#39;minri</div>
<div>say &quot;Min Ri &quot; result<span style="white-space:pre-wrap">                        </span>*ALWAYS THE VALUE OF RI AT LEVEL 50 (AT THE BOTTOM LEVEL)</div><div>&#39;d &#39;minlevel<span style="white-space:pre-wrap">                                </span></div>


<div>say &quot;Model level &quot; result<span style="white-space:pre-wrap">                </span>*ALWAYS LEVEL 40 (TOP LEVEL)</div></div><div><br></div><div><br></div><div><br></div>
<br></div></div>_______________________________________________<br>
gradsusr mailing list<br>
<a href="mailto:gradsusr@gradsusr.org" target="_blank">gradsusr@gradsusr.org</a><br>
<a href="http://gradsusr.org/mailman/listinfo/gradsusr" target="_blank">http://gradsusr.org/mailman/listinfo/gradsusr</a><br>
<br></blockquote></div><font color="#888888"><br><br clear="all"><br>-- <br>Jeff Duda<br>Iowa State University<br>Meteorology Graduate Student<br><a href="http://www.meteor.iastate.edu/%7Ejdduda" target="_blank">www.meteor.iastate.edu/~jdduda</a><br>


</font><br>_______________________________________________<br>
gradsusr mailing list<br>
<a href="mailto:gradsusr@gradsusr.org">gradsusr@gradsusr.org</a><br>
<a href="http://gradsusr.org/mailman/listinfo/gradsusr" target="_blank">http://gradsusr.org/mailman/listinfo/gradsusr</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br>Arlindo da Silva<br><a href="mailto:dasilva@alum.mit.edu">dasilva@alum.mit.edu</a><br>
</div>