Академический Документы
Профессиональный Документы
Культура Документы
by Zhigang Peng
This website contains a brief tutorial on Seismic Analysis Code (SAC). It is part of the mini course series offered to
the incoming Geophysics graduate students at GT. This tutorial consists of the following sections:
Part 1: SAC Basics
SAC overview
How to run it at GT geophysics laboratory
SAC data format
Read/Write/Plot seismic data in SAC
SAC Header
SAC MACRO file
Other basic tools available in SAC
It it working.
If it is not working for some reason, check the following two variables.
% echo $SACAUX
/usr/local/geophysics/sac/sac/aux
% echo $PATH
You should have SACAUX defined as "/usr/local/geophysics/sac/sac/aux", and "/usr/local/geophysics/bin/" in your path. If not, please add the
following two lins in your ${HOME}/.bashrc files to make it work properly.
# added for Seismic Analysis Code (SAC)
export SACAUX=/usr/local/geophysics/sac/sac/aux
export PATH=/usr/local/geophysics/bin:/usr/local/geophysics/sac/local_bin:/usr/local/geophysics/sac/sac/bin:$PATH
For users in other institutions that are using tcsh, please use the following examples to add corresponding lines in your ${HOME}/.tcshrc.
# added for Seismic Analysis Code (SAC)
setenv SACHOME /usr/local/geophysics/sac/sac
setenv SACAUX $SACHOME/aux
set path=($SACHOME/bin $path)
For USArray short course user, please untar the following file to your home directory, or any subdirectory where you would like to put the
tutorial material.
%
%
%
%
%
%
For outside user, please download the example files from the following link:
% wget http://geophysics.eas.gatech.edu/people/zpeng/Teaching/Sac_Tutorial_2013.tar.gz
% tar zxvf Sac_Tutorial_2013.tar.gz
% cd SAC_Tutorial/SAC_Tutorial_WF
Next, we will read some seismograms recorded during a temporary PASSCAL deployment in Turkey right after the 1999 Mw7.4 Izmit and
Mw7.1 Duzce earthquakes ( Ben-Zion et al., GJI, 2003) , and play with them. The seismograms shown below were also used in Peng &
Ben-Zion (GJI, 2004) to document crustal anisotropy near the Karadere-Duzce branch of the North Anatolian Fault that ruptured during
those two earthquakes.
% sac
SAC> r YJ.BV.EHZ.SAC
# read the vertical-component data recorded by station BV`
SAC> p
# plot the data
SAC> r YJ.BV.EH?.SAC
# read all three component data recorded at station BV
SAC> p1
# plot multiple data in one page
SAC> xlim 0 7
# zoom in at 0 - 7 s relative to the waveform reference time (typically the origin time)
SAC> p1
# you have to plot it again to reflect the change
SAC> xlim a -2 t0 +4
# zoom in at 2 s before P, and 4 s after S arrivals
SAC> ylim all
# enforce that y axis has the same scale
SAC> p1
# you have to plot it again to reflect the change
SAC> bd sgf
# Begins plotting to the SAC Graphics File device driver
SAC> p1
# no X graphic is shown. But a file named f001.sgf is created
SAC> sgftops f001.sgf BV_3c.ps # Convert the SGF file into a postscript file
SAC> quit
% gs BV_3c.ps
# ghostview your postscript file
% gs -c '-100 600 translate 270 rotate' -f BV_3c.ps # ghostview by rotating it into landscape, may not work
To write SAC data back to your disk, you have the following three options. See below for examples.
% cp
%sac
SAC>
SAC>
SAC>
SAC>
SAC>
SAC>
SAC>
SAC>
% rm
% cp
SAC Header
SAC header consists of important information about the data, such as the sampling interval, start time, length, station location, event
location, components, phase arrivals, etc. There parameters will be used by various SAC command to process the data. It is important to
keep your header information complete and updated.
To list the SAC header information, you can open the data in SAC, and use lh (listhdr) command. To list a specific header only, use lh
header_name (e.g., lh delta). To go back to the default listing (all headers), use lh default.
% sac
SAC> r YJ.BV.EHZ.SAC
SAC> lh
FILE: YJ.BV.EHZ.SAC - 1
----------
NPTS
B
E
IFTYPE
LEVEN
DELTA
IDEP
DEPMIN
DEPMAX
DEPMEN
OMARKER
AMARKER
T0MARKER
KZDATE
KZTIME
IZTYPE
KSTNM
CMPAZ
CMPINC
STLA
STLO
STEL
STDP
EVLA
EVLO
EVDP
DIST
AZ
BAZ
GCARC
LOVROK
USER7
USER8
NVHDR
SCALE
NORID
NEVID
NWFID
LPSPOL
LCALDA
KCMPNM
MAG
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
2409
-9.992996e+00
1.408700e+01
TIME SERIES FILE
TRUE
1.000000e-02
VELOCITY (NM/SEC)
-2.073471e+04
1.584818e+04
5.137106e+01
0
1.848
3.192
NOV 20 (324), 1999
00:12:55.840
GMT DAY
BV
0.000000e+00
0.000000e+00
4.075520e+01
3.101490e+01
2.470000e+02
0.000000e+00
4.079930e+01
3.100330e+01
8.150000e+00
4.994444e+00
1.686886e+02
3.486961e+02
4.492941e-02
TRUE
0.000000e+00
0.000000e+00
6
1.000000e+00
0
0
2
FALSE
TRUE
EPZ_01
2.310000e+00
A detailed description of the SAC data file format, including the header, can be found at the SAC Users Guide v101.6 - June 2013, and SAC
Data Format .
Another way to list SAC header is to use a C program saclst (originally written by Professor Lupei Zhu at SLU. The binary code is in
/usr/local/geophysics/bin. Outside users can download the source code at sac_msc.tar.gz" . This program should also be included in the
latest version of SAC.
% saclst
Usage: saclst header_lists f file_lists
% saclst evla evlo stla stlo cmpaz cmpinc f YJ.BV.EH?.SAC
YJ.BV.EHE.SAC
40.7993
31.0033
40.7552
31.0149
YJ.BV.EHN.SAC
40.7993
31.0033
40.7552
31.0149
YJ.BV.EHZ.SAC
40.7993
31.0033
40.7552
31.0149
$KEYS sta
$default sta BV
* define keys, and default value
sc rm -f *.sgf
* remove all the previous sgf files
setbb sacfile "*.$sta$.*.SAC"
* setbb sacfile "*.$sta$.*.SAC"
setbb psfile "$sta$_3c.ps"
setbb pdffile "$sta$_3c.pdf"
* set three component data
qdp off
* turn off quick dirty plot (qdp)
r %sacfile%
* read the data
rmean
* remove the mean
bp p 2 n 4 c 1 10
* use two way, 4th order bandpass butterworth filter with
* with pass band of 1 - 10 Hz
setbb ds '( concatenate ' &1,kstnm ' ' &1,kzdate ' ' &1,kztime ' ) '
evaluate to r &1,dist
title '( concatenate ' %ds% ' ', dist = ' ' %r km ') '
* get the stn name, reference time, and distance from SAC header
* and put them into the title
fileid l ul
fileid t l kcmpnm
* display the component name
* fileid Controls the file id display found on most SAC plots.
* TYPE LIST hdrlist : Define a list of header fields to display in the
*
fileid.
* LOCATION UL : Place file id in upper left hand corner.
title on
xlabel "Time @(s@)"
ylabel "Velocity @(nm/s@)"
* set the xlabel and ylabel
ylim all
* set the y-axis to be the same
xlim o o +8
* set the time axis to be from origin time to 8 sec after the origin time
p1
* plot on the x window first
bd sgf
* start the SAC Graphics File device driver
p1
* plot it again to f001.sgf
sc sgftops f001.sgf %psfile%
* convert it to postscript, done
sc rm -f *.sgf
* remove all the previous sgf files
sc gs %psfile%
* view the outout eps file
sc ps2pdf %psfile% %pdffile%
* convert the ps to pdf file
quit
* quit SAC
To run the SAC Macro file, use one of the following three ways:
%sac
SAC> m plt_3c.mac sta BV
# or
% printf "m plt_3c.mac sta BV\nq\n" | sac
# or
% sac plt_3c.mac sta BV
Again, a detailed description of the SAC Macro can be found online at the SAC2000 User's manual http://www.iris.edu/manuals
/sac/SAC_Manuals/SACMacros.html
Phase pick.
Phase picking can be easily done in SAC via automatic phase picker "apk", or manually phase picker "ppk".
% sac
SAC> cut 0 10
# cut the data
SAC> r YJ.BV.EH?.SAC
# read three component data recorded at station BV
SAC> qdp off
# turn off quick dirty plot
SAC> p1
# plot the data
SAC> ch a -12345 t0 -12345
# remove the original P and S arrival pick
SAC> apk
# use the auto picker
SAC> p1
# see the auto picker result
SAC> ppk
# use the manual picker
# cheat sheet,
zoom in: type "x" to define left side time window,
followed by a left click of mouse to define the right side time window
zoom out: type "o"
p arrival: type "a", or "p" at the time
where you think the p wave arrival in.
s arrival: type "t0", or "s" at the time
where you think the s wave arrival in.
quit: type "q"
SAC> ppk p 3 m on bell off
# pick all 3 component together with the same time, turn the annoying bell off
SAC> wh
# to save the picked arrival, you can use the command "wh". But this command could
# mess up the data if you have used the cut command. So be careful.
In this exercise, we will use WILBER II to download waveform data generated by the 2002 Mw7.9 Denali Fault earthquake.
You can download waveforms by clicking on the following web page: http://www.iris.edu/wilber .
WILBER II is scheduled to be retired on September 30, 2013. It will be replaced by Wilber 3 . I will update the tutorial later this year.
Next choose Q4 2002, and the Alaska. Then you will get a page with all events with 5 deg (or other specfic range) of the clicked point. Click
on the following M6.3 event:
DATE
TIME
2002/11/03 22:12:41.0
SOURCE MAG
FARM 7.9
LAT
LON
63.52 -147.44
DEPTH DESCRIPTION
4.90 CENTRAL ALASKA
Next, you need to define the network, channel, distance, and other parameters to select the data you would like to request from IRIS. To
make the request relatively modest, let's use the following parameters: NETWORK - AK, AZ, BK, CI, and UW, and click on PROCEED. Next,
choose CHANNEL - BHZ, BHN, BHE, DISTANCE from 0 to 180 deg.
Next, we need to select data format, time window and user identifition. We will choose the SEED format, 5 minutes before, and 60 minutes
(1 hour) after P.
After a few minutes, you should get an email notice saying that you data is read to pick up somewhere in an FTP site. Please download the
file and put it inside Sac_Tutorial/Denali_2002 directory.
To save time, you can use the SEED data I have already downloaded from the IRIS DMC.
To extract SAC waveform data from the SEED volume, use the following command:
% cd Denali_2002
% rdseed -d -o 1 -f 20021103_Denali.seed
Use shell script (e.g., awk, sed, Makefile, Perl) to interact with SAC
Seismological research has changed significantly in the past few decades, partially due to the digital revolution. In most case, we will not
be able to publish a paper based on only one or two seismograms (I was told that people in the early 1950-1960s can do that). In addition,
some of the most exciting findings within seismology come from the analysis of massive continous seismic waveforms that are otherwise
considered as noise (e.g., non-volcanic tremor, ambient noise tomography, etc). Hence, we need to analyze hundreds and thousands
(sometimes even more) of seismic data first. How can we do that?
Basically we have to reply on computers to do most of the dirty job. 8-) Shell script language (e.g., awk, sed, Makefile and Perl) will help
you a lot in this case. Below are some examples to show how you can interact with SAC using shell script.
Example 1: rename the SAC filename to something like NET.STN.BH[ZNE].SAC
%
%
#
%
#
%
#
mkdir backup
ls *.SAC | awk -F"." '{print $7,$8}' | sort | uniq -c | awk '{if ($1 == 3) print $2,$3}' | sort | uniq > stn_3c.id
find the station name which has three seismograms (three components)
ls `awk '{print "*"$1"."$2".*SAC"}' stn_3c.id` | awk -F"." '{print "mv "$0,$7"."$8"."$10"."$12}' | sh
rename the SAC filename to NET.STN.BH[ZNE].SAC
mv 2002*.SAC backup
move the rest into backup for now
Example 2: put earthquake and station component information into the SAC header.
%
#
%
#
%
%
%
%
%
#
gsact 2002 11 03 22 12 41 518 f *.SAC | awk '{print "r "$1"\nch o "$2"\nwh"} END{print "quit"}' | sac
put the eq. origin time into the SAC header
printf "r *.SAC\nch evlo -147.45290 evla 63.51410 evdp 4.2 mag 7.9\nwh\nq\n" | sac
put the earthquake location information into the SAC header
saclst o f *.SAC | awk '{printf "r %s\nch allt %.5f\nwh\n",$1,$2*(-1)} END{print "quit"}' | sac
shift the origin time to be at 0 s
printf "r *.BHE.SAC\nch cmpaz 90 cmpinc 90\nwh\nq\n" | sac
printf "r *.BHN.SAC\nch cmpaz 0 cmpinc 90\nwh\n\q\n" | sac
printf "r *.BHZ.SAC\nch cmpaz 0 cmpinc 0\nwh\n\q\n" | sac
put the component information into the SAC header
Example 3: cut the two horizontal component data so that they will have the same length, which are needed for rotation in SAC.
% saclst e f `awk '{print $1"."$2".BH[EN].SAC"}' stn_3c.id` # list the end of the two horizontal component data
% saclst e f `awk '{print $1"."$2".BH[EN].SAC"}' stn_3c.id` |\
paste - - | awk '{if ($2> $4) print $4;else print $2}' > BH_EN_end_time.dat
% saclst b f `awk '{print $1"."$2".BH[EN].SAC"}' stn_3c.id` |\
paste - - | awk '{if ($2< $4) print $4;else print $2}' > BH_EN_start_time.dat
# find out the minimum of the end time and save it
% paste -d" " stn_3c.id BH_EN_start_time.dat BH_EN_end_time.dat |\
awk '{print "cut "$3,$4"\nr "$1"."$2".BH[EN].SAC\nrmean\nrtrend\nrotate to GCP\nw "$1"."$2".BHR.SAC "$1"."$2".BHT.SAC"} END {print "q"}' | sac
# cut the two horizontal component data, remove mean and trend,
# rotate them to great circle path, save the output
% awk '{print "r "$1"."$2".BH[ZNE].SAC\nrmean\nrtrend\nw over"} END {print "q"}' stn_3c.id | sac
# remove mean and trend for the original three component data.
An important step in data processing is to document what we have done to the data, so that other people can reproduce your results. Also
sometimes you need to reprocess the data, but maybe you have forgot the commands and parameters you have typed a few months ago.
There are two ways to document your scripts. The first way is simply to write a shell script. This could be assigned as a potential
homework for you. You can define different steps using flags, and simply copy the command line from the above examples into each step.
Another way is to write a Makefile, and put each step seperately. Makefile was originally used for compiling a collection of computer
codes. It can be used to store simple commands if puting them in a seperate shell script file is not needed.
See Makefile in your working directory. For example, if you want to redo step 3, simply type "make step3" in your command line, it will
automatically execute what is written underneath the flag step3. If you want to clean up all you have done here, simply type "make clean".
If you want to reprocess all the steps, type "make all".
Note that to make the Makefile work, you need to change "$" to "$$" when you refer to variables in awk.
Additional tutorial on Makefile can be found online, such as Make a tutorial .
An alternative would be to use the following commands (sacdump and sacdump_slice) developed by us (originally from Dr. Lupei Zhu at
SLU).
%
%
%
%
%
%
%
%
%
%
%
%
%
%
%
gmt4 # command line to use the GMT version 4 (which can run pssac, only for GT user), or type 'source /usr/local/geophysics/etc/gmt4rc'
pssac -R0/2000/5/38 -JX6i/9i -Edt-3 -M0.3 -K -P -Ba200f100:"Time (s)":/a5f1":Distance (deg)":WeSn *.BHZ.SAC > Denali_BHZ_record_section.eps
printf "0 2000\n" | awk '{printf "%f %f\n%f %f\n",$1,$1*4,$2,$2*4/111.19}' | psxy -R -JX -K -O -P -W1tap/255/0/0 >> Denali_BHZ_record_section.eps
printf "1000 39 20 0 4 MC 11/02/2002 Denali Fault Earthquake\n" | pstext -R -JX -O -P -N -G0/0/0 >> Denali_BHZ_record_section.eps
gs Denali_BHZ_record_section.eps
pssac -R300/1800/0/6 -JX6i/4i -Ent-3 -M1 -K -P -Ba500f100:"Time (s)":S AZ.RDM.*.SAC > Denali_AZ_RDM_BH.eps
ls AZ.RDM.*.SAC | awk -F"." '{printf "%f %f 20 0 4 ML %s %s\n",300,NR+0.2,$2,$3}' | pstext -JX -R -O -P >> Denali_AZ_RDM_BH.eps
gs Denali_AZ_RDM_BH.eps
You can also type make Denali_BHZ_record_section.eps Denali_AZ_RDM_BH.eps to generate these two plots.
There is a simple matlab script named gen_bp_PKD.m in your working directory. It will use fget_sac to read transverse component data at
station PKD, and plot both the broadband and band-pass-filtered data.
cd SAC_Tutorial/SAC_Tutorial_WF
matlab
matlab -nojvm
>> gen_bp_PKD
#
#
#
#
Below please find a link to another matlab script called gen_spectrogram.m . It will generate the third panel that shows the spectrogram
(i.e., frequency versus time) of the recorded signal (mainly teleseismic P wave, its coda, and triggered tremor at Parkfield). It needs a
subroutine called eqfiltfilt.m , which is a two-pass, zero-phase butterworth filter.
cd SAC_Tutorial/SAC_Tutorial_WF # go to the first tutorial example
wget http://geophysics.eas.gatech.edu/people/zpeng/Software/matlab/gen_spectrogram.m
wget http://geophysics.eas.gatech.edu/people/zpeng/Software/matlab/eqfiltfilt.m
matlab -nojvm
# launch matlab without the java machine
>> gen_spectrogram
# run the matlab script to compute spectrogram, which calls fget_sac command.
>> gen_spectrogram('PKD','HH','T','BK');
# or be specific about the parameters
Finally please find a link to another matlab script called sac2wav.m . It will read the raw seismic data, apply a 0.5 Hz high-pass filter (See
Peng et al. (SRL, 2011) for why doing this), and convert the seismic data into audible sounds. If it works, you will generate an audible
seismogram like this BK.PKD.HHT.SAC.wav .
cd SAC_Tutorial/SAC_Tutorial_WF # go to the first tutorial example
wget http://geophysics.eas.gatech.edu/people/zpeng/Software/matlab/sac2wav.m
matlab -nojvm
# launch matlab without the java machine
>> sac2wav
# run the matlab script to convert SAC to wave, which calls fget_sac command.
>> sac2wav('BK.PKD.HHZ.SAC',100,1,0,2000);
# or be specific about the parameters
Another example is to use the sac_wfcc command (modified from the code written by Dr. Lupei Zhu at SLU) to align the P wave (or any
other phases), and compute the correlation coefficients. For outside users, the source code for sac_wfcc can be downloaded at the
following link sac_wfcc.tar.gz .
% saclst a f *.NC.CCO.EHZ.SAC | sac_wfcc -D-0.2/0.8/0.5
# list the original P wave arrival, and perform waveform cross-correlations using 0.2 s before
# and 0.8 s after the P arrival. The output is the wf, the P arrival after time shift to best align
# with the first trace, and the correlation coefficient.
% saclst a f *.NC.CCO.EHZ.SAC | sac_wfcc -D-0.2/0.8/0.5 | awk '{print "r "$1"\nch t6 "$2"\nwh"} END {print "q"}' | sac
# put the aligned P wave arrival into the SAC header t6
% saclst t6 f *.NC.CCO.EHZ.SAC | awk '{printf "r %s\nch allt %.5f\nwh\n",$1,$2*(-1)+5} END {print "q"}' | sac
# shift the trace so that they are aligned at 5 s.
SAC> cut 4.5 5.5
SAC> r *.NC.CCO.EHZ.SAC
SAC> qdp off
SAC > p1
SAC> p2
# as you see, the waveforms after alignment are more coherent than the original data.
Stacking
Stacking helps to enhance the coherent signal and suppress the non-coherent random noise. Many advanced array processing tools are
based on the idea of stacking. Here we only use the SAC command to perform direct stacking (i.e., no time shifts). There are many existing
tools available for advanced array analysis (e.g., stant stack).
SAC> cut 4 6
# cut the data from 4 to 6 s (around the P waves)
SAC> r *.NC.CCO.EHZ.SAC
# read all the data
SAC> rmean
# remove the mean
SAC> w append .P
# save the cut data
% ls *.NC.CCO.EHZ.SAC.P | awk '{if (NR == 1) print "r "$1;else print "addf "$1} END {print "div "NR"\nw CCO.stack.SAC.P\nq"}' | sac
# read the first data, add the rest data, divide with the total number, and save the output
SAC> r *.NC.CCO.EHZ.SAC.P
# read the P wave data
SAC> color black inc # set the color to start from black
SAC> p1
# plot them
SAC> p2