Working with GRASS without starting it explicitly

From GRASS-Wiki
Revision as of 23:48, 15 February 2013 by NikosA (talk | contribs) (→‎Bash examples (GNU/Linux): updated, removed parts for grass642)
Jump to navigation Jump to search

GRASS sessions

It is possible to access GRASS modules without explicitly starting a "GRASS session". GRASS libraries require certain environment variables to be set. In fact a "GRASS session" is just a set of processes (e.g. a shell and/or GUI) which have the necessary environment settings, specifically:

  • GISBASE needs to be set to the top-level directory of the GRASS installation.
  • GISRC needs to contain the absolute path to a file containing settings for GISDBASE, LOCATION_NAME and MAPSET.
  • PATH needs to include $GISBASE/bin and $GISBASE/scripts.

If the GRASS libraries are shared libraries, the loader needs to be able to find them. This normally means that LD_LIBRARY_PATH (Linux, Solaris), DYLD_LIBRARY_PATH (MacOSX) or PATH (Windows) need to contain $GISBASE/lib, although there are other means to the same end (e.g. on Linux, putting $GISBASE/lib (with $GISBASE replaced by its actual value) into /etc/ then running ldconfig).

Some libraries and modules use other variables. More information for most of them is available in the file $GISBASE/docs/html/variables.html. The display libraries used by d.* commands use additional variables, which are documented along with the individual drivers.

Accessing GRASS modules without starting a GRASS session

Python example

See GRASS Python Scripting Library

import os
import sys

gisbase = os.environ['GISBASE'] = "/usr/local/src/grass_trunk/dist.i686-pc-linux-gnu"

gisdbase = os.path.join(os.environ['HOME'], "grassdata")
location = "nc_spm_08"
mapset   = "PERMANENT"

sys.path.append(os.path.join(os.environ['GISBASE'], "etc", "python"))
import grass.script as grass
import grass.script.setup as gsetup

            gisdbase, location, mapset)

print grass.gisenv()

grass.message('Raster maps:')
for rast in grass.list_strings(type = 'rast'):
    print rast

Bash examples (GNU/Linux)

Below an example of a ~/.bash_profile script to set the required settings in order to access GRASS commands outside of a GRASS-session. Specifically it is for GRASS 7, and uses the version from the dist.<arch> directory in the GRASS source tree rather than an installed version.

# example for GRASS 7
export GISBASE=/usr/local/src/grass/svn/dist.x86_64-unknown-linux-gnu
export GRASS_GNUPLOT='gnuplot -persist'
export GRASS_WIDTH=640
export GRASS_HEIGHT=480
export GRASS_HTML_BROWSER=firefox
export GRASS_PAGER=cat
export GRASS_WISH=wish
export GRASS_PYTHON=python
#export GRASS_MESSAGE_FORMAT=silent     ### Attention: all printed messages silenced!!!
export GRASS_VERBOSE=0 ### Attention to verbosity!
export PATH="$GISBASE/bin:$GISBASE/scripts:$PATH"

export GIS_LOCK=$$
export GRASS_VERSION="7.0.svn"


export GISRC="$tmp/rc"
mkdir "$tmp"
cp ~/.grass7/rc "$GISRC"

The above script will allow GRASS commands to be used anywhere. Furthermore, if the ~/.Xsession sources the bash startup scripts, the settings aren't limited to interactive shells, but also work for e.g. M-! in XEmacs). Each interactive shell gets a separate "session" (i.e. a separate $GISRC file), while GUI programs share a common session.

Note, however, that the ~/.bash_profile file is a personal initialization startup script. Thus, it is read during the login process. Any modifications to it will be read after (re-)login or explicitly sourcing it.

At the end of the ~/.bash_profile file, the following lines can be added to ensure no duplication of entries in the PATH variable.

 PATH=`awk -F: '{for(i=1;i<=NF;i++){if(!($i in a)){a[$i];printf s$i;s=":"}}}'<<<$PATH`
 PYTHONPATH=`awk -F: '{for(i=1;i<=NF;i++){if(!($i in a)){a[$i];printf s$i;s=":"}}}'<<<$PYTHONPATH`

Important notes

Launching a grassXY session could still permit access on GRASS modules of the version that is set with the above script. The reason being is that grassXY scripts (e.g.: grass64, grass65, grass70) prepend the GRASS directories to PATH, LD_LIBRARY_PATH, etc. They won't remove any entries which are already there. A solution to this is to switch between GRASS versions by using a script like the following:

            for dir in $oldpath ; do
                case "${dir}" in
            echo "${newpath#:}"
        PATH=`strippath $PATH`
        export GISBASE=$PWD/dist.i686-pc-linux-gnu
        export PATH="$GISBASE/bin:$GISBASE/scripts:$PATH"
        export LD_LIBRARY_PATH="$GISBASE/lib"
        export PYTHONPATH="$GISBASE/etc/python"

Note: the above script needs to be "source"d; executing it won't work. It might also require some adjustments (e.g. the GISBASE variable).

Using normal grass sessions commands are recorded in $GISDBASE/$LOCATION_NAME/$MAPSET/.bash_history. While working with "pure" bash, shell entries go in ~/.bash_history. Merging existing "grass-bash_history(-ies)" with the bash history log, would probably be not a good idea. Perhaps it is wise(r) to use normal GRASS sessions when working on real projects. Nevertheless, it is upon the users preferences and expertise level to decide what is best.


  • Instructions and discussion on how to access GRASS mapsets outside of a GRASS session in the grass-user mailing list [1].
  • removing duplicate entries in $PATH taken from [2]

GRASS databases

(project file structure)

Minimal mapsets

within a functional LOCATION (see below) the minimal mapset is a subdirectory of the MAPSET's name, containing a WIND file. The WIND file can simply be copied from PERMANENT/DEFAULT_WIND. Optionally you can put a VAR file in there too to define the default database driver to use.

  • You can create a new mapset when starting GRASS with the -c flag. e.g.
grass64 -c /path/to/location/new_mapset_name

Minimal locations

Within the GISDATABASE (which is simply a subdirectory some where), the minimum LOCATION consists of a directory giving the LOCATION its name, which in turn contains a PERMANENT subdirectory for the PERMANENT mapset. The PERMANENT mapset contains a few special files that regular mapsets don't. These are:

contains the location's projection information
contains the location's map units definition
the default region (WINDow file). The format is identical to the WIND files created by g.region

See also