Lesson 2

Goals

The goal of this lesson is to familiarize you with using scripts in GMT. You will write a script that quickly creates a location map, and adds text to the base map, points, and similar features to the location map.



What is a Script?

One big advantage of using the UNIX operating system is that it is quite simple to develop long lists of commands that are executed in order. These high level computer programs are called "scripts". Scripts are useful for repeatedly executing a complicated series of commands. Basically, they are a mechanism for saving a lot of typing. There are actually a series of "shells" in unix. Different shells understand slightly different commands. In our case, we are going to stick with the "bash" shell, which is found on virtually all unix machines. Other shells you can run across include the "C" shell, the "csh" shell, and the "korn" shell.

Scripts are important in this class because you are going to make complex figures involving several or more gmt commands. Also, you are going to execute other C computer programs, and plot the results of these programs using gmt commands.



Look at Scripts and Script Output

Type ls -ll in this directory to see the directory contents.

you should see several files, including "location_map2.ps"

look at the image in location_map2.ps using ghostview

you should see an image like the following:

In this case, the location map was created using a script, rather than by typing commands directly on the command line. The script used is called "script1.gmt"; note that the script exists in a file, and that the name of the file includes the *.gmt extension. You can name a script file whatever you want. The *.gmt extension will help you remember that this particular script contains gmt commands.

Look at the contents of the script by typing more script1.gmt on the command line

if you are having trouble, view script1.gmt but make sure you learn to use "more".

a bash script always starts with the line "#! /usr/local/bin/bash" to let the computer know that a "bash" script is coming. Note that many of the gmt examples do not show this line - but it is always included!

The script also contains a lot of comment lines - these start with "#" - you can write whatever you wish on the comment line and it will not be executed by the script. As you can see, this is very useful for letting you put information into the script that you need, but the computer does not need. For example, you can put in comments about the details of GMT commands to help you remember their cryptic meanings.

if a line does not start with "#", then the computer will attempt to execute the command on that line. In this case, commands that will be executed are:

pscoast -R-90/-80/5/15 -JM6i -B5g5 -N1 -Dl -W -G180/180/180 -V -P > location_map2.ps

gv location_map2.ps

rm ./location_map2.ps

These lines are positioned (in order) throughout the script and they are the only lines in this simple script that actually execute - they tell the computer what to do. So, you can see that the script executes a gmt command, launches ghostview and displays the output file, location_map2.ps, in ghostview, and then deletes the file when you are done viewing it (quit ghostview).

To run the script, type ./script1.gmt on the command line. The "./" means - run the command found in this directory! If all goes well, the location map will appear. When you quite ghostview, type ls -ll again and you will see that the postscript file location_map2.ps is deleted from you directory!

if you get a meesage back that says "Permission Denied" or is similar, then you you must change the "permissions" on your file. To do this, type chmod a+x script1.gmt on the command line. Then try running the script again. You can read more about file permissions by typing "man chmod" - which brings up the manual page about the chmod command. You can type "man" followed by any command to get detailed information about that command, if it exists on the computer.



Viewing Scripts with xemacs

You now will want to edit the script - this allows you to change the information in the script, add information, change the gmt commands, etc. There are numerous file editors on most unix systems. One of the most user friendly on your system is "xemacs". You can also try "gedit" or "nedit". To edit script1.gmt, type xemacs script1.gmt & . This will launch emacs and load the file "script1.gmt". emacs works like most text editors you are familiar with. A few things to note:

Under "File" click on "un-split - keep this one" to get a better looking screen.

"buffer" refers to the name of the file in use by emacs - you can load numerous files into emacs and edit them all - just be sure to save the changes!

depanding on the printer - you may need to use the "pretty print" command

Experiment!



Editing Scripts with xemacs

With the "script1.gmt" file active in your emacs window, type in the following text, after the "pscoast..." line and before the "gv..." lines:

pstext -R -JM -G0 -O -P < < EOF >> location_map2.ps

-85.5 12.5 18 30 26 BL Nicaragua

EOF

to add text to your script. You can try using cut and paste to add these commands to the script - but it is safest to type them in.

Try running this new script. Do not forget to save your changes in emacs, before trying to run your script from the command line!

if you get an error, check your typing - that is where most errors occur! If your typing is rough - or you are lazy - or smart - just use the mouse to cut and paste the text!


Basic Steps in editing and running scripts!

So just to review, to build a map in GMT using a script you must:
create a file in an editor (xemacs) that contains the basic GMT commands
change the permissions on this new file so you can "execute it", chmod a+x filename.gmt
run the script to create the postscript output, e.g., ./filename.gmt
view the results in a postscript viewer, like ghostview (e.g., gv filename.ps)

you must always follow these steps. Pay attention to the file extension. Pay attention to the directory structure. Often things go wrong when (1) the file you are trying to run is located or created in a different directory, (2) you skip steps (for example people often edit the *.gmt file, but then forget to execute it and then can not understand why the map has not changed - follow the steps!, (3) the wrong file extension is used or the wrong file is used at the wrong time. For example, always use *.ps or *.eps to designate a file as postscript. Always name a GMT script with *.gmt file extension. You can execute a GMT script, but you can not execute a postscript file, (4) actually READ the error messages you see if your *.gmt script does not work properly. When you get an error, the only solution is to edit the script and save the changes.


Adding Text to Your Location Map

More details about adding text to your location map are revealed by loading the file script2.gmt in xemacs. Study the comment lines for information about plotting the text on the map. Run the script. The output from script2.gmt should look like the following:

Note the details about how to plot the text by studying the script. Try changing the details of the script to modify the look of the location map. You should rename the script before you make any changes - just so you keep this working copy. Try adding labels for Panama, Honduras, and El Salvador.



Assignment #2

Modify your two location maps from last time (assignment #1). Add text to the location maps to label important features of the location maps. Save these files as jpegs in your directory, call these files assign2_map1.jpg and assign2_map2.jpg. Get these files into a word document and save it (for example, you can copy and paste straight out of the gv viewer into word.


Chuck Connor
Last modified: Thu Jan 12 10:44:36 EST 2006