Arrays can be an extremely useful tool when using loops in GrADS. Arrays reduce the number of lines in your code by a potentially large amount. For example, if you wanted to plot soundings at a number of different locations: Without arrays, you would either need to have a separate segment of code for each location, or put a bunch of conditional statements in a loop to determine which location to set your coordinates to. With arrays however, you can simply point to each new location using one single block of code, and no conditional statements. If you are at all familiar with computer programming, in any language, you are almost certainly aware of the benefits of arrays. This tutorial will be fairly short, and provide you with an idea of how to use the different array syntax methods in GrADS.
So basically, GrADS allows you to use arrays that are really only useful in loops, or potentially conditional statements. The arrays in GrADS can't be used in liner algebraic operations, at least not easily. I suppose you could put something together that might work, but then again what would be the point? But in terms of telling the program to point to certain array values, the array syntax in GrADS is plenty sufficient. There are two different methods to declaring and using arrays in GrADS that I work with. I will explain each method, and give a little bit of information regarding their uses, including a couple of examples.
The first array syntax in GrADS uses the period (.) to indicate array values. This is the simplest way to do arrays in GrADS, and I have found it sufficient nearly 100% of the time for what I want to do. It is important to use the period syntax, instead of other possible separators (e.g., the underscore), as the period will separate the name and number in a loop. The below example shows three different array expressions, one correct and two incorrect.
The output from the following will be:
Basically, the period allows the variable "i" to be read as the number, the other two syntax expressions simply read the "i" as part of the variable name.
A few other notes about this GrADS array syntax: It is important to know that the array syntax here is very basic, and you aren't really declaring "arrays", what you're doing is simply declaring a bunch of variables, each with a period in the suffix, and then pointing to the names separately depending on the value of the suffix. So sticking with the above example, these commands do not work:
array=[5, 4, 3, 2, 1] ;*No!
This does NOT output:
In fact, this script won't even run, you will just get an error Also, this is no good:
While this code will not give you an error if you try to run it, the output will NOT be:
5 4 3
it will instead be:
Get the idea? Basically, you are just naming stuff, not really declaring arrays in the traditional sense. That's pretty much it for this array syntax. Now, this syntax can be exploited in a number of different ways, you can do 2D, or 3D or nD arrays, as long as each value is separated by a period. Also, the values after the period do not necessarily have to be numbers. The below examples will show you a few possible array expressions.
Array Using Words:
That's it for the first method of arrays in GrADS, now onto the 2nd method!
This method is a little bit trickier to follow, but gives you a little better simulation of normal array syntax. That being said, this part of the tutorial is actually much shorter than the above, as there aren't really as many variations of it. This method, is basically to use the 'subwrd' command to point to values in a specified variable. The 'subwrd' command basically scans a string and breaks it up into values separated by the space. All you need to do is supply the function with the name of the string, and the word you are looking for. For example, the code:
array='5 10 15 20 25 30'
Would output the 4th word from this variable:
One of the benefits of using this syntax is that you can print out the array as a whole using the say command. Another benefit is that you can really reduce the number of lines in your script this way vs. the 1st method. Also, since GrADS is pretty good about automatically converting strings
to floats, you can declare a string of numbers, and still do
mathematical operations with each number. One drawback is that you can't really do multidimensional arrays this way. Also if you are using names, you can't include names with spaces in them, since the 'subwrd' command uses spaces as delimiters between each value. That being said, this method can be quite good for looping through lists. A quick example is below:
places='Home Store Work Park Earth'
say 'Place 'i': 'subwrd(places,i)
Home Store Work Park Earth
Place 1: Home
Place 2: Store
Place 3: Work
Place 4: Park
Place 5: Earth
So, I think that wraps up the array syntax in GrADS. Hopefully this tutorial gave you a good introduction and some good insight to using arrays in GrADS.
Download Array Example Script