2025-05-12 12:11:41 +02:00

79 lines
2.5 KiB
Python

#----------------------------------------------------------------------------
# Python class for a seismic trace
#
import numpy as np
class SeismicTrace(object):
"""
Class defining a seismic trace by station name, network name, component,
number of samples, sampling interval and start time
"""
def __init__(self, staname, comp, dt, tanf, vals):
"""
Initialize seismic trace object from data:
:param staname: station name
:param comp: component
:param dt: sampling interval
:param tanf: start time
:param vals: values of trace (1D numpy array)
"""
self.staname = staname
self.comp = comp
self.dt = dt
self.tanf = tanf
self.vals = vals
self.nsamp = vals.size
self.tlen = self.dt*self.nsamp
@classmethod
def readFromAscii(cls, fid, comp='None'):
"""
Class method as second constructor to initialize trace from an Ascii file
:param fid: file descriptor of already open Ascii file
:param comp: optional component selection
:return: an instance of the class
"""
staname = fid.readline().strip()
if not staname:
print("No station name found. Is file really an Ascii gather file")
return None
compf = fid.readline().strip()
if comp != 'None' and comp != compf:
print("Requested component %s not found.", comp)
return None
h = fid.readline().strip().split()
vals = np.array([float(v) for v in fid.readline().strip().split()])
dt = float(h[1]); tanf = float(h[2])
return cls(staname, compf, dt, tanf, vals)
def absoluteMaximum(self):
"""
return absolute maximum of trace
"""
return abs(max(self.vals))
def writeToOpenAsciiFile(self, fid):
"""
write a seismic trace to an open Ascii file
"""
fid.write(self.staname + '\n')
fid.write(self.comp + '\n')
fid.write("{:12d} {:15.9f} {:20.9f}\n".format(self.nsamp, self.dt, self.tanf))
for n in range(self.nsamp):
fid.write("{:17.8e}".format(self.vals[n]))
fid.write("\n")
def printInfo(self):
"""
Print out some information about the trace
"""
print("Identification: ", self.staname, self.comp)
print("Sampling interval: ", self.dt)
print("Number of samples: ", self.nsamp)
print("Start time after midnight: ", self.tanf)
print("Length of trace: (N*DT) ", self.tlen)