GrADS Scripting Language

Introduction to GrADS scripts
Elements of the Language:
      say / prompt / pull
      if / else / endif
      while / endwhile
      Intrinsic Functions
Commands that complement the scripting language
Widgets (N.B. The widgets have been temporarily disabled for version 2.1.a1)
Script Library
Scripting Language Reference Card (Requires Adobe Acrobat Reader)

Introduction to GrADS Scripts

Scripts offer users the facility to program GrADS operations. Although it is relatively easy for users to produce sophisticated GrADS graphics without ever writing a script, there are occasions where the programming capability makes things even easier. This section explains the general capabilities of scripts, how to run them, and suggests a strategy for users who may wish to write their own.

What scripts can do

The GrADS scripting language, used via the GrADS run command, provides a similar capability to the exec command, except that scripts also have flow control, defined variables, and access to GrADS command output. Scripts may be written to perform a variety of functions, such as allowing a user to interact with Grads via point and click interface, animating any desired quantities, and annotating plots with information obtained from GrADS query commands.

The scripting language is similar to REXX in implementation. All variables are of type STRING. Mathematical operations are supported on script variables. Flow control is achieved via if/else/endif and while/endwhile constructs. Loop flow may be modified by the continue or break commands. Strings contained in variables or generated via an expression may be issued to GrADS as commands. The output from those commands (i.e., the text that GrADS would have output to the terminal) is put into a variable and made available to the script. The language includes support for functions.

Before writing your own scripts, it is recommended that you read the rest of this section and then try to run some of the scripts in the library. Study these example scripts, referring to this page for information on syntax etc., and you will soon be equipped to write scripts of your own.

Running scripts

The command to execute a script is the run command:

This command runs the script contained in the named file, which generally has a ".gs" tag at the end. Optional arguments are passed to the script as a string variable. You may issue any GrADS command from a script, including the run command. When calling scripts recursively, be sure that you can back out of the recursion and return to your main script.

Automatic script execution

You may have a simple script automatically executed before every display command:

This script would typically be used to set an option that by default gets reset after each display command, for example:

You can issue any GrADS command from this script, but the interactions are not always clear. For example, if you issued a display command from this script, you could easily enter an infinite recursion loop.

The argument to the script is the expression from the display command.

Storing GrADS scripts

It is convenient to put all your GrADS "utility" scripts in one directory (e.g., /usr/local/grads/lib/scripts).

To simplify running these scripts, GrADS first looks in the current directory for the script and then, if it can't find it, appends the ".gs" extension and tries again. For example, suppose you are working on a test script called You would run it in GrADS by,

If after the first two tries, the script still can't be located, then GrADS looks in the directory defined by the environment variable GASCRP. In the t(csh), for example,

or in ksh,

Note the if the / is not added to the end of the directory name, it is automatically added by UNIX. However, it'll still work if you type

If the script cannot be found, then .gs is appended and GrADS tries yet again. Thus,

simplifies to,

Elements of the Language

A script file is made up of records. The end of a script record is determined by either a newline character or a semicolon (where the semicolon is not contained within a constant string).

Each script record may be one of the following types:

Many of the above record types will contain expressions. Script expressions are composed of operands and operators. Operands are strings constants, variables, or function calls; operators are mathematical, logical, or concatenation operations. Further discussion of these record types and the expressions they may contain is given below.


Comments in GrADS scripts must contain an asterisk (*) in the first column.


The statement record consists only of an expression:

The expression is evaluated, and the resulting string is then submitted to GrADS as a command for execution. The script variable rc will contain the return code from the GrADS command (this will always be an integer value). In addition, any text output from GrADS in response to the command is put in the variable result for examination by the script. A GrADS error resulting from an invalid command WILL NOT terminate execution of the script.

The simplest type of expression is a string constant, which is just a character string enclosed in single or double quotes. Here's an example of simple script containing a comment plus statements comprised of string constants:


Assignment records are used to define variables and assign them values. The format of the assignment record is:

The expression is evaluated, and the result is assigned to be the value of the indicated variable. The same example from above can be rewritten to include assignment statements. Note the use of explicit and implied concatenation:

say / prompt

To present information or questions to the GrADS user via the terminal (standard output), use the say or prompt commands:

The result of the expression is written to the terminal. The prompt command works the same way as the say command but does not append a carriage return. It is possible to combine variables and string constants when writing to standard output:

For example:



To retrieve information provided by the GrADS user via the terminal (standard input), use the pull command:

The script will pause for user input from the keyboard (ending with the carriage return), and then the string entered by the user is assigned to the indicated variable name. To elaborate on a previous example:

if / else / endif

One way to control the flow of script execution is via the if/else/endif construct. The format is as follows:

The else block is optional, but the endif record must be present. The script records following if expression are executed if the expression evaluates to a string containing the character 1. If the expression evaluates to 0, then the script records in the if block are not executed and the script continues with the else block (if it is present) or the record following endif. The if expression record must be separated from the script records that follow it. For example, the following script record would be invalid:

The correct syntax requires three separate script records. This is achieved by putting each record on one line:

Alternatively, the three records could be on the same line separated by a semicolon:

N.B. There is no elseif construct in GrADS.

while / endwhile

Another method for controlling the flow of script execution is the while/endwhile construct. The format is as follows:

The script records following while expression are executed if the expression evaluates to a string containing the character 1. If the expression evaluates to 0, then the script records in the while block are not executed and the script continues with the record following endwhile. The while expression record must be separated from the script records that follow it.

Two additional script commands may be used to modify the while loop execution: break and continue. Inserting the break statement will immediately end execution of the loop and the script will move on to the records following endwhile. The continue statement will immediately end execution of the loop, but the script will then branch immediately back to the top of the loop, and the expression will be re-evaluated.

While loops are often used as counters. For example:


The contents of a script variable is always a character string. However, if the contents of a variable represent a number in the correct format, certain operators may perform numeric operations on that variable, giving a string result which will also be a number.

Variable names can have from 1 to 8 characters, beginning with an alphabetic character and containing letters or numbers only. The name is case sensitive. If a variable has not yet been assigned, its value is its name.

String variables or string constants are enclosed in either single or double quotes. An example of an assignment statement that defines a string variable is as follows:

Numeric variables may be entered without quotes, but are still considered strings.

Predefined script variables

Some variable names are predefined; it is a good idea to avoid assigning values to these variables. The following are predefined script variables -- their values will change with every execution of a GrADS command from the script:

lat, lon, lev, and time are also used as predefined variables in GrADS. Although using them within a script is okay, in order to avoid confusion it is not recommended.

Global string variables

String variables are usually local to the functions they are contained in. Global string variables are also available. They are specified via the variable name. Any variable name starting with an underscore (_) will be assumed to be a global variable, and will keep its value throughout an entire script file. An example of an assignment statement that defines a global string variable is as follows:

N.B. Global variables cannot be used in function headers. For example:

wouldn't make sense, since _var is a global variable, and would be invalid if it were the only argument.

Compound string variables

Compound variables are used to construct arrays in scripts. A compound variable has a variable name with segments separated by periods. For example:

In this case, when the variable contents are accessed, i and j will be looked up to see if they are also variables (non-compound). If they are, the i and j will be replaced by the string values of i and j. For example:

In the above example, the assignment is equivalent to:

Note that the string values of i and j may be anything, but the variable name specification in the script must follow the rules for variable names: letters or numbers, with a leading letter. The variable name after substitution may be any string:

The above is valid. However, we cannot refer to this variable name directly:

would be invalid.

Variable names may not be longer than 16 characters, either before or after substitution.

Note that the GrADS scripting language is not particularly efficient in handling large numbers of variables. Thus compound variables should not be used to create large arrays:

The above loop will create 10000 distinct variable names. Such a large number of variables in the variable chain will slow the script down a lot.


The following operators are implemented in the scripting language:

The following operators will perform a numeric operation if the operands are numeric:

If any of the following operations are attempted with non-numeric operands, an error will result:

Arithmetical operations are done in floating point. If the result is integral, the result string will be an integer. Logical operations will give a character 0 (zero) if the result is FALSE, and a character 1 (one) if the result is TRUE.


Script expressions consist of any combination of operands, operators, and parentheses. Operands may be string constants, variables, or function calls. The precedence of the operators is:

Within the same precedence level, operations are performed left to right. Parentheses modify the order of operation according to standard convention.

All script expressions, including all function calls, etc. are evaluated and the resulting string is what gets executed as a command. For example:

For the expression following if, both sides of the logical operation must be evaluated before the entire expression can be simplified into a true or false result. In this case, the subexpression on the left is true, but the subexpression on the left is not, so the whole expressions resolves to 0 (zero) and the script will print:


In some espressions, the concatenation operator may be implied. The % operator may be omitted whenever the two operands are a string constant and a variable name. With implied concatentation, intervening blanks will be ignored.

For example, the following expressions have the same effect:

Assuming two previous statements, minlat = -30 and maxlat = 30, the resulting expression would be:

Keep in mind the order of precedence when using the concatenation operator.


Function calls take the form of:

where the function name follows the same rules as for variable names, and the arguments may be any expression. Functions may either be contained within the script file itself, or the may be intrinsic functions. Functions contained within other script files are not supported as yet (other script files may be executed via the GrADS run command).

In either case, functions are invoked as a script expression is being evaluated. Script functions always have a single string result, but may have one or more string arguments. Functions are invoked by:


If the function has no arguments, you must still provide the parentheses:


You may provide your own functions from within your script file by using the function definition record:

function name(variable, variable, ...)

To return from a function, use the return command:

return expression

The expression is optional; if not provided, a NULL string will be returned. (A null string is: '') The result of the function is the result of the expression specified on the return command.

When a function is invoked, the arguments are evaluated, then flow of control is transferred to the function. The variables contained in the list within the function definition record are initialized to the values of the passed arguments. If too few arguments where passed for the variables specified, the trailing variables are uninitialized. If too many arguments are passed, the extra arguments are discarded.

You may modify the variables from the function definition record without modifying the variables from the calling routine.

Scope of variables is normally local to the function, but can be global.

When a script file is first invoked (via the run command), execution starts at the beginning of the file. A function definition record may optionally be provided at the beginning. If it is, it should specify one variable name. This variable will be initialized to any run command options. If no options were given, the variable will be initialized to NULL.

Intrinsic Functions

Commands that complement the scripting language

There are some GrADS commands that, although not designed exclusively for scripts, are most useful in script applications. These include:

query <option> or q <option>

set gxout findstn

When using the graphics output type set gxout findstn, three arguments must be provided with the display command. The first argument is a station data expression. The 2nd and 3rd arguments are the X and Y screen coordinates of the of the desired search location. GrADS will find the station closest to the specified X and Y position, and print its stid, lon, and lat. This graphics output type should only be used when X and Y are the varying dimensions and AFTER a regular display command (that results in graphics output) is entered.

set dbuff on|off

This command sets double buffer mode on or off. This allows animation to be controlled from a script. The clear command also sets double buffer mode off.


Swaps buffers, when double buffer mode is on. If double buffer mode is off, this command has no effect.

The usual usage of these commands would be:


GrADS has the capability to implement a graphical user interface. This interface is used to draw widgets (buttons and pull down menus) that allow a "point and click" interface between the Grads user and the scripting language. (N.B. The widgets have been temporarily disabled for version 2.1.a1)


Here is a sample from a script illustrating how to draw a button:

The reference pages for set button and draw button contain information on how to specify the button characteristics and position.

A button's initial "state" is ON. If a user clicks on a button following a q pos command, then the button state will switch from ON (1) to OFF (0). A second q pos followed by a mouse click on the button will return it to the ON state. The button state may also be changed with the redraw button command.

The output from the q pos command is what makes the button widgets so useful. Here is a template of what q pos returns after a mouse click on a button:

Position = xpos ypos mousebutton widgetclass buttonnumber buttonstate


If the user did not click on a button, then widgetclass will be 0 and there will be no output for buttonnumber or buttonstate.

Drop Menus

As with button widgets, dropmenus provide a "point-and-click" interface between scripts and the GrADS user. The reference pages for set dropmenu and draw dropmenu contain information on how to specify the dropmenu characteristics and position.

The output from q pos after a click on a dropmenu is similar to that described above for buttons. Here is a template of what is returned by q pos after a mouse click on a dropmenu:

Position = xpos ypos mousebutton widgetclass menunumber inum


If no menu item is selected, then menunumber and inum will both be -1.

Here is a script sample illustrating how to use a dropmenu:

'reset events'
'set rgb 90 100 100 100'
'set rgb 91 150 150 150'
'set rgb 92 200 200 200'
'set dropmenu 1 91 90 92 0 91 90 92 1 91 90 92 90 92 6'
'draw dropmenu 1 1 8 1.5 0.5 Select a Variable | Wind | Temperature | Height | SLP '
noselect = 1
while (noselect)
  'q pos'
  menunum  = subwrd(result,7)
  menuitem = subwrd(result,8)
  if (menunum = 1)
    if menuitem = 1 ; newbase = 'Variable = Wind'   ; endif
    if menuitem = 2 ; newbase = 'Variable = Temp'   ; endif
    if menuitem = 3 ; newbase = 'Variable = Height' ; endif
    if menuitem = 4 ; newbase = 'Variable = SLP'    ; endif
    'draw dropmenu 1 1 8 1.5 0.5 'newbase' | Wind | Temperature | Height | SLP '
    noselect = 0

Here is another script sample illustrating how to use cascading dropmenus:

'set rgb 90 100 100 100'
'set rgb 91 150 150 150'
'set rgb 92 200 200 200'
'set button 1 91 -1 -1 1 91 90 92 12'
'draw button 1 1 8 1 0.5 quit'
'set dropmenu 1 91 -1 -1 1 91 90 92 1 91 90 92 90 92 6'
'draw dropmenu 1 1.5 7.5 2 0.5  Menu Base | Space | Earth >05> | Sun | Moon'
'draw dropmenu 5 cascade Ocean | Land | Atmosphere >11> | Biosphere'
'draw dropmenu 11 cascade Snow | Rain | Mist | Tornado '

while (1)
  'q pos'
  say result
  ev = subwrd(result,6)
  if (ev!=3); break; endif;

It is left to the GrADS script writer (that means you!) to run the demo and interpret the output of q pos when clicking on all the options in the cascade of dropmenus.

Rubber banding

GrADS has a widget type called rband for rubber banding. There are two rband modes: box and line. To set up the rband widget, use the following command:


In box mode, as the user clicks and drags the mouse in the active rband area a box is drawn with one corner located at the initial click and the opposite corner located at the release point. In line mode, a line is drawn between these two points.

For example, suppose you want to set up a box rubber band widget in the plot region only.

First, execute q gxinfo to get the X and Y limits of the plot area. The result from q gxinfo might look like this:

Second, set up the widget with set rband using the dimensions grabbed from the result of q gxinfo:

Finally, use q pos to activate the widget.

This freezes the system until the user clicks, drags, and then releases the mouse somewhere within the active rband area. Here is a template for the output you would get from GrADS after a mouse click and drag in the rband area:


The page coordinates can be then be used to draw a box (or a line) where the user specified, or parsed and used in the coordinate transform q xy2w to recover the lat/lon region selected by the user.