two questions about scripts

Dave Allured dave.allured at NOAA.GOV
Wed Feb 9 19:37:22 EST 2005


Ethan M Greene wrote:
> Hello-
>
> I am running PCGrADS v1.8SL11 on a MSWindows 2000 platform with Cygwin
> 1.5.12-1.
>
> I am encountered two problems while writing scripts.
>
> First, I can use the absolute value function to display data, but not on
> variables in scripts. For example, the code
>               ‘d abs(var)’
> works, but
>               x = -5
>               y = abs(-5)
> generates,
>               Function not found: abs
>               Error occurred on line 2
>               In file test.gs

Be careful here.  The Grads scripting language and the Grads command
language are two almost completely independent languages.  The
documentation does not make this entirely obvious.  Don't get the two
confused.

Think of the Grads scripting language as a meta language whose sole
purpose is to assemble lines of text that get passed to the main Grads
command interpreter.  I believe that the commands, syntax, variable name
spaces, function name spaces, and interpreters are completely separate
from each other.  You usually see the two languages intermingled within
Grads scripts, however.  The ones in quotes are "Grads commands", not
native script language commands.

In my limited experience, there are only three groups of functions that
can be used directly at the scripting language level:

   Built-in scripting lang. functions #1 (strings and ascii I/O):
   http://grads.iges.org/grads/gadoc/script.html#intrinsic

   Built-in scripting lang. functions #2 (math and more string funcs):
   http://grads.iges.org/grads/gadoc/mathfunctions.html

   User defined scripting language functions:
   http://grads.iges.org/grads/gadoc/script.html#functions

The list of available built-ins is short, and absolute value is not in
there.  You could add this yourself as a very short user defined
function at the top of your script if you wish.

> Second, I would like to open files were the address is a variable that I
> read in from a text file. The code,
>                         name = test.ctl
>                         ‘open ‘name
> works fine, but
>                         dummy = read(‘filename.txt’)
>                   name = sublin(result,2)
>                         ‘open ‘name
> does not work (even though ‘say name’ will output the correct address).

There is a problem with the read function on some platforms.  It
invisibly captures the carriage return character at the end of the line,
and includes it in the input string.  My guess is that this is your
problem.  For a robust script, conditionally delete the final carriage
return character with this method:

    read_str = read (filename)
    name = sublin (read_str, 2)

    len1 = math_strlen (name)
    if len1 > 0
       lastchar = substr (name, len1, 1)
       if (lastchar < ' ') ; name = substr (name, 1, len1-1) ; endif
    endif

Also note that in your example #2 above, your use of script variables
"dummy" and "result" are inconsistent and will not work as shown.
"result" is a special reserved name that should be used with Grads
commands, not with Grads script functions.  Use a uniquely named script
variable to hold the results from the read function.

--Dave A.
CDC/NOAA/CIRES



More information about the gradsusr mailing list