Pages

Thursday, May 2, 2013

Tutorial: Basics of Scripting

This tutorial will be very short, focusing on the very basics of scripting with GrADS. It is the goal of this tutorial to provide a few pointers and tips to help the budding GrADS user avoid various (frustrating) pitfalls encountered while scripting with GrADS.

Why Script? Easy; because it's annoying to keep typing the same commands over and over in the console. Scripting allows you to place all of your commands in one file, and then execute a number of GrADS commands in sequence with a single command. Additionally, scripting allows you to further extend the capability of the GrADS software; for example you can use tools such as mathematical functions, conditional statements and loops within a script, whereas you cannot use these tools using the command line. There are however a few important syntax issues that differ when using scripting vs. the command console.

How to start a new GrADS script: It is very simple to start a new GrADS script. All you need to do is open up a text file and start adding various GrADS commands to it, in the order that you wish to execute the commands. When you want to run the script, simply save it with the suffix ".gs", If you are using Windows, be sure to change the "save as" option from .txt to all files, and simply type .gs at the end of your filename. To run the script in GrADS, simply open GrADS, and type in the name of the script (be sure to include the path if it is in a different directory than the one you are running GrADS out of). And that's all there is to getting started with a GrADS script.

Scripting Basics: How to avoid syntax errors when writing your GrADS Script:The following list is designed to help you learn some of the basic syntax involved in using GrADS commands within a script. Because scripting allows you to use functions, and variables that fall outside of the basic GrADS command structure, you must be able to distinguish GrADS commands within your script. Below is a list of several syntax quirks and methods used in the GrADS scripting format.

GrADS commands must be inside single quotes ('')

Variables must be outside of the single quotes

NO indentations are allowed, avoid the TAB button keep at all costs

Lines can be commented out using the astrix (*)

Note: When commenting the * must be on the farthest left column of your text

Semi-colons (;) can be used in place of a new line

The results of queries are put into the default variable 'result'

Concatenating (adding) strings is very easy, just type them in sequence

The preceding script will prompt the user for a variable and output an image similar to the one here (depending onwhat variable you choose):

Output image from example 2

Hopefully these example use enough of the basic scripting functions to give you an idea of how to use them. You can see the use of the semi-colon in place of new lines to save space in the scripts, the use of the astrix to denote comments and the use of the say/pull commands. Scripting can be kind of tricky to get a handle of at first, but it's a skill that comes with practice. Good Luck!