[gradsusr] Array Variable displaying as string not numbers

Jeff Duda jeffduda319 at gmail.com
Thu Feb 13 22:01:22 EST 2020

The difference between command line commands and scripting variables is
biting you here. You cannot directly access values from the arrays in your
data file in a single command in a GrADS script. You have to display the
variable first, then use the sublin, subwrd, or substr script commands to
capture the field value. For instance, let's say you have 850-mb
temperature. You would have to do something like this:

'set lon -110'
'set lat 31'
'd tc(lev=850)'
t1 = subwrd(result,4)

Notice how only the last of those commands is a true internal-GrADS
scripting command. The rest are just command line stuff.

You may want to play around with your data and script at a base level
before fully programming. There are complications depending on how you
setup your displayed grid. You did not provide any information in your
email, so I have to be generic.

If used a control file to open the data and you have a PDEF line, you are
likely going to be dealing with grid interpolation rather than exact grid
point values. In that case, GrADS will output something like
"Notice: plotting interpolated data"
when you run any display command at the command line. I would test your own
data to see if this is the case. If it is, then your script will need an
additional line to capture the field value:
line = sublin(result,2)
t1 = subwrd(line,4)

Notice the second argument of these functions. They indicate which line,
inline word, or subword string to pull out from the input (the first
argument). Your output may look different than what I have used in the past
(usually the 4th word contains the value itself), so you may need to end up
using a different index value. Also, 'result' is a fixed keyword. Pretty
much any command you issue at the command line in GrADS will have some text
output for you stored within *result*. So just adding

say result

after display commands will give you some information on what GrADS is
doing, and this is the source of where you grab the field values. So run
'say result' after some of your display commands to see 1) how many lines
are being out put (so you know how to use sublin()), 2) How many words
there are in the line you want (for subwrd()), and 3) which specific word
or substring within a word you want (if needed). Note that subwrd parses
the input line using blank characters as a delimiter, so you don't need to
specify spaces.

Now, you want to pull out an array of values, so you'll need to loop this
behavior. Ultimately, your code will need to look something like this:

start_lat = 31
end_lat = 37.5
start_lon = -115
end_lon = -108
delta_lat = 0.5
delta_lon = 0.5
ln = start_lon
while (ln <= end_lon)
 'set lon 'ln
 lt = start_lat
 while (lt <= end_lat)
  'set lat 'lt
  'd tc(lev=800)-tc(lev=700)'
  line = sublin(result,2)
  T_Diff = subwrd(line,4)
  lt = lt + delta_lat
 ln = ln + delta_lon

in order to get what it appears you want.

I know, GrADS scripting is a bit unintuitive, and has something of a
learning curve, and the notion of mixed display and script commands fools a
lot of people and can be a real turn off. But this is how this particular
application works.

Jeff Duda

On Thu, Feb 13, 2020 at 7:09 PM Jan Ising <jising at aggies.ncat.edu> wrote:

> Hello,
>     I am new to GrADS and came across an issue that has given me quite
> some frustration. I am writing a script that calculates the temperature
> difference between two levels at each point within a region and then
> displays the values in the terminal (my script is indented to do more than
> this but the root of my issue arises from here). The problem is that the
> terminal displays the literal value of T_Diff (i.e " 'tc(Lev = 800) -
> tc(Lev = 700)' ", and not 15,18,22,etc... (for example)). Here is my code:
> (open file and setup basic graphics output formatting)
> TheLat = 31
> while(TheLat >=31 & TheLat <=37.5)
> 'set Lat 'TheLat
> TheLong = -115
>     while (TheLong>=-115 & TheLong<=-108)
>         'set Lon 'TheLong
>         T_Diff = 'tc(Lev = 800) - tc(Lev = 700)'
>         say T_Diff
>         TheLong = TheLong + 0.5
>     endwhile
> TheLat = TheLat + 0.5
> endwhile
> Perhaps this issue is a simple syntax error, perhaps it is a logic error,
> either way I am here to figure that out, any help would be greatly
> appreciated!
> -Thank you,
> Jon
> _______________________________________________
> gradsusr mailing list
> gradsusr at gradsusr.org
> http://gradsusr.org/mailman/listinfo/gradsusr

Jeff Duda, Research Scientist
University of Colorado Boulder
Cooperative Institute for Research in Environmental Sciences
NOAA/OAR/ESRL/Global Systems Division
Boulder, CO
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://gradsusr.org/pipermail/gradsusr/attachments/20200213/e6f0237d/attachment.html>

More information about the gradsusr mailing list