All seismic hazard assessments rely to one degree or another on earthquake catalogs. The goal of this lesson is to plot earthquake data (e.g., epicenters shaded by depth or magnitude) derived from an on-line earthquake catalog. In this lesson, earthquake date will be plotted on a map. Perl scripts will be explored in more detail, and modified to filter the earthquake catalog data in various ways.
Many earthquake catalogs are now on-line. Visit the following websites to obtain an earthquake catalog for some region of interest to you using the CNSS site:
CNSS worldwide earthquake catalog
Southern California Earthquake Data Center
The perl script used for filtering the CNSS file is equake_read.pl
This perl script is called as part of script7.gmt, which is used to draw the entire map, using GMT commands.
Take another look at the perl script used to filter the catalog data: equake_read.pl. The key lines in the perl script used to filter the data are:
Look at the structure carefully. The set of lines:
while (< FILE1 >) {
.
.
.
} # this is the end of the loop that started with "while (
close (FILE1);
are a loop, called a while-loop. The loop structure begins with a statement
"while (< FILE1 >)", that means everthing inside the loop will be executed repeatedly while there is more data in the input file called FILE1. FILE1 is just an alias for whatever input file name was specified earlier in the code. In this case, for every line in the file referred to as FILE1, the steps inside the while-loop are executed. The program exits the loop when there are no more data lines in FILE1. The brackets "{....}" delineate the section of the computer code that is part of the while-loop.
Now look at the commands that are executed within the loop. The first pair of commands are:
In perl, the symbols "$_" refer to "everything on one line of the input file". So the command $line = $_, means that everything on one line of the file is assigned to a variable called $line. This variable contains all the information on the line. If you printed $line the first time through the while-loop, you would see the first line of the file duplicated exactly. The next step is to extract the information we need from the line. In this case, we only need the longitude, latitude, and depth of the earthquake focus. Perl is particularly good at extracting this kind of information with a simple command. The line:
creates a set of variables from the single line. Every time the computer code encounters a blank (white space) in the line, it creates a new variable. This set of variables is stored in the array called "parse". The "@" in front of "parse" tells indicates that "parse" is an array and will contain sets of variables, each extracted from the line. The array can be called anything you wish, but it helps to be informative. In this case the array is called parse because we are parsing information in the line.
In the input CNSS earthquake catalog file, the lines look like:
When the command:
is executed, the array "parse" is filled with information from the line. Different information is stored in elements of "parse" as follows:
$parse[0] = 1980/01/08
$parse[1] = 00:54:24.20
$parse[2] = 12.6660
$parse[3] = -88.6140
$parse[4] = 33.00
$parse[5] = 4.90
$parse[6] = 22
$parse[7] = 0.00
$parse[8] = NEI
$parse[9] = 198001084002
After executing the command, subsequent lines can refer to elements of the array. The longitude is stored in the element $parse[3], the latitude is stored in $parse[2], and the depth is stored in $parse[4]. Now that the line is parsed, we only have to print the information that we actually need to make the map. The line:
prints the longitude, latitude, and depth. In this example, the output looks like:
The symbols "\n" puts a carriage return at the end of the new line that is printed. If instead of "longitude latitude depth", the "longitude latitude magnitude" should be printed, the print statement would be modified to look like:
It is possible to parse lines that have other symbols, like commas, separating the information that needs to be parsed. For example, the input file might contain lines that look like:
In this case the command to do the parsing would look like:
Perhaps a comma followed by a space separate data contained in the line:
will successfully parse this line.
An advantage of using perl and similar computer languages is that it is easy to filter data sets with a few commands. Suppose the earthquake catalog is searched for all earthquakes within a specific geographic region. Later, you want to make a plot of only magnitude 6.0 or greater earthquakes. One approach would be to re-search the catalog. It is much more efficient just to write a filter in perl. This can be accomplished by adding a "if...then" statement to the while-loop within the perl script:
Now, only when the value of $parse[5] is greater than or equal to 6.0 will output be printed. Notice that brackets "{}" delineate the statements that are including in the "if ... then" statement, and "then" is implied. A common programming error is to forget to match brackets. Without matching brackets, the program normally will not run at all. Unfortunately, programs will occasionally run with brackets that do not match as the programmer intends, leading to very muddled output.