Next: P.2 Using both GMT Up: P. Special Operations Previous: P. Special Operations Contents Index

#
P.1 Running GMT in isolation mode

In Chapter 4 it is described how GMT creates several (temporary) files to communicate between the different commands that make up the script that finally creates a plot. Among those files are:

.gmtdefaults4.
  This file covers about 100 different settings that influence the layout of your plot, from font sizes to tick lengths and date formats (See Section 4.2). Those settings can be altered by editing the file, or by running the gmtset command. A problem may arise when those settings are changed half-way through the script: the next time you run the script it will start with the modified settings and hence might alter your scripts results. It is therefore often necessary to revert to the original .gmtdefaults4 file. Isolation mode avoids that issue.
.gmtcommands4.
  This file is created to communicate the command line history from one command to the next (Section 4.5) so that shorthands like -R or -J can be used once it has been set in a previous GMT command. The existence of this file makes if impossible to run two GMT scripts simultaneously in the same directory, since those .gmtcommand4 files may clash (contain different histories) and adversely affect the results of both scripts.
.gmt_bb_info.
  This file contains the information about the BoundingBox (Section C.1) of the PostScript output. This information too has to be transferred from one GMT command to the next in a script. Again, running two commands simultaneously in the same directory may have disastrous effects on that file.

A cure to all these woes is the isolation mode introduced in GMT version 4.2.2. This mode allows you to run a GMT script without leaving any traces other than the resulting PostScript or data files, and not altering the .gmtdefaults4 or .gmtcommands4 files. Those files will be placed in a temporary directory instead. And if properly set up, this temporary directory will only be used by a single script, even if another GMT script is running simultaneously. This also provides the opportunity to create any other temporary files that the script might create in the same directory.

The example below shows how isolation mode works.

    #!/bin/sh
    #               GMT Appendix P, example 1
    #
    # Purpose:      Illustrates the use of isolation mode
    # GMT progs:    gmtset, grdimage, grdmath, makecpt, pscoast
    # Unix progs:   mktemp, rm
    #
    ps=GMT_App_P_1.ps

    # Create a temporary directory. $GMT_TMPDIR will be set to its pathname.
    # XXXXXX is replaced by a unique random combination of characters.
    export GMT_TMPDIR=`mktemp -d /tmp/gmt.XXXXXX`

    # These settings will be local to this script only since it writes to
    # $GMT_TMPDIR/.gmtdefaults4
    gmtset COLOR_MODEL rgb ANNOT_FONT_SIZE_PRIMARY 14p

    # Make grid file and color map in temporary directory
    grdmath -Rd -I1 Y = $GMT_TMPDIR/lat.nc
    makecpt -Crainbow -T-90/90/60 -Z > $GMT_TMPDIR/lat.cpt

    # The grdimage command creates the history file $GMT_TMPDIR/.gmtcommands4
    grdimage $GMT_TMPDIR/lat.nc -Sl -JK6.5i -C$GMT_TMPDIR/lat.cpt -P -K > $ps
    pscoast -R -J -O -Dc -A5000 -Gwhite -B60g30/30g30 >> $ps

    # Clean up all temporary files and the temporary directory
    rm -rf $GMT_TMPDIR

Figure P.1: Example created in isolation mode

The files .gmtdefaults4 and .gmtcommands4 are automatically created in the temporary directory $GMT_TMPDIR. The script is also adjusted such that the temporary grid file lat.nc and colormap lat.cpt are created in that directory as well. To make things even more easy, GMT now provides a set of handy shell functions in gmt_shell_functions.sh: simply include that file in the script and the creation and the removal of the temporary directory is reduced to a single command.

    #!/bin/sh
    #               GMT Appendix P, example 2
    #
    # Purpose:      Illustrates the use of isolation mode
    # GMT progs:    gmtset, grdimage, grdmath, makecpt, pscoast
    # GMT funcs:    gmt_init_tmpdir, gmt_remove_tmpdir
    #
    ps=GMT_App_P_2.ps

    # Make GMT shell functions accessible the the script
    . gmt_shell_functions.sh

    # Create a temporary directory. $GMT_TMPDIR will be set to its pathname.
    gmt_init_tmpdir

    # These settings will be local to this script only since it writes to
    # $GMT_TMPDIR/.gmtdefaults4
    gmtset ANNOT_FONT_SIZE_PRIMARY 14p

    # Make grid file and color map in temporary directory
    grdmath -Rd -I1 Y = $GMT_TMPDIR/lat.nc
    makecpt -Crainbow -T-90/90/60 -Z > $GMT_TMPDIR/lat.cpt

    # The grdimage command creates the history file $GMT_TMPDIR/.gmtcommands4
    grdimage $GMT_TMPDIR/lat.nc -Sl -JK6.5i -C$GMT_TMPDIR/lat.cpt -P -K > $ps
    pscoast -R -J -O -Dc -A5000 -Gwhite -B60g30/30g30 >> $ps

    # Clean up all temporary files and the temporary directory
    gmt_remove_tmpdir

Next: P.2 Using both GMT Up: P. Special Operations Previous: P. Special Operations Contents Index