Scripting GRASS

Scripts

The possibility to automate individual steps is a very useful extension (even for GRASS beginners). These scripts have to be written in ASCII format as UNIX shell scripts. Within those scripts GRASS modules can be called with their respective parameters and geostatistical computations can be done automatically. Especially for novices it is a good idea to generate scripts step by step, trying the commands on command line in the terminal first. Later the UNIX command \texttt{\$~history} allows viewing and saving ("copy" - "paste" into a text editor with left and middle mouse button) the previously entered commands.

Here is an example that shall demonstrate the potential of script programming clearly: with this script you can calculate general geostatistical information for raster images[1]. Save this script as, for example, statistics.sh and set the UNIX permissions with chmod u+x statistics.sh.

This script can be used after launching GRASS, specify the name of the raster map to be analyzed as parameter. A modified version of above script exists in GRASS 5 under the name of r.univar. In here the output of the GRASS module r.stats is transferred to the UNIX program awk by UNIX piping (represented by the '|' character). The calculations are done within awk, results are printed on the screen.

The best way to understand GRASS scripting is definitely by studying the many existing examples. Just go to the scripts directory if your GRASS implementation and study some of them:

cd $GISBASE/scripts/

A few GRASS modules will help you in your scripts. Those are:

Study their use in the existing GRASS scripts to understand how they work.

For more information on general shell programming please consult the man page of your shell (on GNU/Linux this is generally the bash shell, so type man bash or follow on of the many links listed on the Programming Texts and Tutorials page.

Batch usage of GRASS

GRASS can be completely controlled externally through scripts (and therefore run automatically) by setting the correct environment variables. There doesn't exist a single "GRASS" program, in fact GRASS is a collection of modules which are run in a special environment. Even the GRASS startup is simply setting numerous variables. This can be done directly as well, here an example in "bash-shell" style.

After having run this script, all GRASS modules that can be used non-interactively can be used. Generally, you can get the list of parameters required by a module by typing "-help" as such a parameter or (always recommended) by reading the manual.

Once these variables have been set, GRASS can also be integrated into CGI, PERL and other scripts. You can visit a quite complex example on the internet: SlideLinks which is an online GIS based completely on CGI scripts, a database management system and GRASS.

Since GRASS 5.0 you can alternatively skip the startup screen by directly specifying the database, location and mapset:

grass5 /home/neteler/grassdata/katmandu/innercity

Obviously, this only works if the location and the other settings really exist.

Notes

[1]

The code example links in this chapter go to M. Neteler, H. Mitasova, 2002. Open Source GIS: A GRASS GIS Approach.