python obspy read sac file with header
库依赖
obspy
matplotlib.pyplot
* opition
以 Stream 对象读取SAC文件
Use obspy.read('fileName')
to read file to a stream object.
Stream means that a sac file contain one or more trace data info, hence index can be used to localize different trace in a stream.
Easily, It’s a fast way to check how many trace in the sac stream:
>>> print(sac)
1 Trace(s) in Stream:
.GRN21..N | 1969-12-31T23:59:59.900000Z - 1970-01-01T00:00:02.450000Z | 100.0 Hz, 256 samples
Object
To call the data, index format as sac[0]
can be applied to the stream. then use .
operator to call the subject as sac[0].data
, which will return the raw data object of the first trace in the sac file.
The method to call the header of sac can be sac[0].stats.sac.headerObj
.
Both the data and header can be edited or changed directly.
Easy Plot
For each trace, a easy way to Plot is provided by obspy:
sac[0].plot()
Demonstration code
import obspy as obs
sacA = obs.read('CPS0001n.sac')
sacB = obs.read('CPS0002n.sac')
dataA = sacA[0].data
dataB = sacB[0].data
dataOutput = dataA - dataB
distA = sacA[0].stats.sac.dist
distB = sacB[0].stats.sac.dist
distOut = (distA + distB) / 2
sacOut = sacA
sacOut[0].data = dataOutput
sacOut[0].stats.sac.dist = distOut
sacOut.write('out.sac',formta='SAC')
# verification
sac = obs.read('out.sac')
sac[0].plot()