/
Surface_waves.py
308 lines (269 loc) · 13.9 KB
/
Surface_waves.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import instaseis
import numpy as np
from obspy.signal.filter import envelope
from obspy.geodetics import kilometer2degrees
from obspy.geodetics.base import gps2dist_azimuth
from obspy.core.stream import Stream
from obspy.core.trace import Trace
import matplotlib.pylab as plt
from scipy.signal import hilbert
import os
## All different classes:
from Get_Parameters import Get_Paramters
from Create_observed import Create_observed
class Surface_waves:
def __init__(self, PRIOR):
self.prior = PRIOR
def get_R_phases(self,time_at_rec):
phases = []
phases.append(dict(starttime=lambda dist, depth: time_at_rec + dist / 2.8,
endtime=lambda dist, depth: time_at_rec + dist / 2.6,
comp='Z',
fmin=1. / 20.,
fmax=1. / 10.,
dt=5.0,
name='R1_10_20'))
phases.append(dict(starttime=lambda dist, depth:time_at_rec+ dist / 2.8,
endtime=lambda dist, depth: time_at_rec + dist / 2.6,
comp='Z',
fmin=1. / 16.,
fmax=1. / 8.,
dt=4.0,
name='R1_08_16'))
phases.append(dict(starttime=lambda dist, depth: time_at_rec + dist / 2.8, # 2.7,
endtime=lambda dist, depth:time_at_rec + dist / 2.6, # 2.5,
comp='Z',
fmin=1. / 32.,
fmax=1. / 16.,
dt=8.0,
name='R1_16_32'))
phases.append(dict(starttime=lambda dist, depth: time_at_rec + dist / 2.8, # /2.7,
endtime=lambda dist, depth: time_at_rec + dist / 2.5, # /2.5,
comp='Z',
fmin=1. / 48.,
fmax=1. / 24.,
dt=12.0,
name='R1_24_48'))
return phases
def get_L_phases(self,time_at_rec):
phases = []
phases.append(dict(starttime=lambda dist, depth: time_at_rec+ dist / 3.2, # /3.15,
endtime=lambda dist, depth:time_at_rec + dist / 2.9, # /2.95,
comp='T',
fmin=1. / 48.,
fmax=1. / 24.,
dt=5.0,
name='G1_24_48'))
phases.append(dict(starttime=lambda dist, depth:time_at_rec + dist / 3.2, # /3.2,
endtime=lambda dist, depth: time_at_rec + dist / 2.9, # /2.95,
comp='T',
fmin=1. / 32.,
fmax=1. / 16.,
dt=8.0,
name='G1_16_32'))
phases.append(dict(starttime=lambda dist, depth:time_at_rec + dist / 3.2, # /3.15,
endtime=lambda dist, depth: time_at_rec + dist / 2.9, # /2.95,
comp='T',
fmin=1. / 24.,
fmax=1. / 12.,
dt=6.0,
name='G1_12_24'))
phases.append(dict(starttime=lambda dist, depth: time_at_rec+ dist / 3.2, # /3.1,
endtime=lambda dist, depth: time_at_rec + dist / 2.9, # /3.00,
comp='T',
fmin=1. / 16.,
fmax=1. / 8.,
dt=4.0,
name='G1_08_16'))
return phases
def rayleigh_pick(self, Z_trace, la_s, lo_s, depth, save_directory, time_at_rec, npts,filter = True ,plot_modus = False):
if plot_modus == True:
dir_R = save_directory + '/Rayleigh_waves'
if not os.path.exists(dir_R):
os.makedirs(dir_R)
Rayleigh_st = Stream()
evla = la_s
evlo = lo_s
rec = instaseis.Receiver(latitude=self.prior['la_r'], longitude=self.prior['lo_r'])
dist, az, baz = gps2dist_azimuth(lat1=evla,
lon1=evlo,
lat2=self.prior['la_r'],
lon2=self.prior['lo_r'], a=self.prior['radius'], f=0)
# For now I am just using the Z-component, because this will have the strongest Rayleigh signal:
Z_comp = Z_trace.copy()
if plot_modus == True:
Z_comp.plot(outfile=dir_R + '/sw_entire_waveform.pdf')
phases = self.get_R_phases(time_at_rec)
for i in range(len(phases)):
if plot_modus == True:
dir_phases = dir_R + '/%s' % phases[i]['name']
if not os.path.exists(dir_phases):
os.makedirs(dir_phases)
trial = Z_trace.copy()
if filter == True:
trial.detrend(type="demean")
trial.filter('highpass', freq=phases[i]['fmin'], zerophase=True)
trial.filter('lowpass', freq=phases[i]['fmax'], zerophase=True)
trial.detrend()
if plot_modus == True:
start_vline = int(
(phases[i]['starttime'](dist, depth).timestamp - time_at_rec.timestamp) / trial.stats.delta)
end_vline = int(
(phases[i]['endtime'](dist, depth).timestamp - time_at_rec.timestamp) /trial.stats.delta)
plt.figure(4)
ax = plt.subplot(111)
plt.plot(trial.data, alpha=0.5)
ymin, ymax = ax.get_ylim()
plt.plot(trial.data)
plt.vlines([start_vline, end_vline], ymin, ymax)
plt.xlabel(time_at_rec.strftime('%Y-%m-%dT%H:%M:%S + sec'))
plt.tight_layout()
plt.savefig(dir_phases + '/sw_with_Rayleigh_windows.pdf')
# plt.show()
plt.close()
if filter == True:
trial.detrend(type="demean")
env = envelope(trial.data)
trial.data = env
trial.trim(starttime=phases[i]['starttime'](dist, depth), endtime=phases[i]['endtime'](dist, depth))
else:
env = trial.data
if plot_modus == True:
plt.figure(5)
plt.plot(trial,label = '%s' % phases[i]['name'])
plt.legend()
plt.tight_layout()
plt.savefig(dir_phases + '/Rayleigh_envelope_filter_%s.pdf' % phases[i]['name'])
# plt.show()
plt.close()
zero_trace = Trace(np.zeros(npts),
header={"starttime": phases[i]['starttime'](dist, depth), 'delta': trial.meta.delta,
"station": trial.meta.station,
"network": trial.meta.network, "location": trial.meta.location,
"channel": phases[i]['name']})
total_trace = zero_trace.__add__(trial, method=0, interpolation_samples=0,
fill_value=trial.data,
sanity_checks=False)
Rayleigh_st.append(total_trace)
if plot_modus == True:
plt.figure(6)
plt.plot(Rayleigh_st.traces[0].data, label='%s' % Rayleigh_st.traces[0].meta.channel)
plt.plot(Rayleigh_st.traces[1].data, label='%s' % Rayleigh_st.traces[1].meta.channel)
plt.plot(Rayleigh_st.traces[2].data, label='%s' % Rayleigh_st.traces[2].meta.channel)
plt.plot(Rayleigh_st.traces[3].data, label='%s' % Rayleigh_st.traces[3].meta.channel)
plt.legend()
plt.tight_layout()
plt.savefig(dir_R + '/diff_Rayleigh_freq.pdf')
plt.close()
return Rayleigh_st
def love_pick(self, T_trace, la_s, lo_s, depth, save_directory, time_at_rec, npts, filter = True, plot_modus= False):
if plot_modus == True:
dir_L = save_directory + '/Love_waves'
if not os.path.exists(dir_L):
os.makedirs(dir_L)
Love_st = Stream()
evla = la_s
evlo = lo_s
rec = instaseis.Receiver(latitude=self.prior['la_r'], longitude=self.prior['lo_r'])
dist, az, baz = gps2dist_azimuth(lat1=evla,
lon1=evlo,
lat2=self.prior['la_r'],
lon2=self.prior['lo_r'], a=self.prior['radius'], f=0)
# For now I am just using the Z-component, because this will have the strongest Rayleigh signal:
T_comp = T_trace.copy()
if plot_modus == True:
T_comp.plot(outfile=dir_L + '/sw_entire_waveform.pdf')
phases = self.get_L_phases(time_at_rec)
for i in range(len(phases)):
if plot_modus == True:
dir_phases = dir_L + '/%s' % phases[i]['name']
if not os.path.exists(dir_phases):
os.makedirs(dir_phases)
trial = T_trace.copy()
if filter == True:
trial.detrend(type="demean")
trial.filter('highpass', freq=phases[i]['fmin'], zerophase=True)
trial.filter('lowpass', freq=phases[i]['fmax'], zerophase=True)
trial.detrend()
if plot_modus == True:
start_vline = int(
(phases[i]['starttime'](dist, depth).timestamp - time_at_rec.timestamp) / trial.stats.delta)
end_vline = int(
(phases[i]['endtime'](dist, depth).timestamp - time_at_rec.timestamp) / trial.stats.delta)
plt.figure(1)
ax=plt.subplot(111)
plt.plot(trial.data, alpha=0.5)
ymin, ymax = ax.get_ylim()
# plt.plot(trial.data)
plt.vlines([start_vline, end_vline], ymin, ymax)
plt.xlabel(time_at_rec.strftime('%Y-%m-%dT%H:%M:%S + sec'))
plt.savefig(dir_phases + '/sw_with_Love_windows.pdf')
plt.tight_layout()
plt.close()
if filter == True:
trial.detrend(type="demean")
env = envelope(trial.data)
trial.data = env
trial.trim(starttime=phases[i]['starttime'](dist, depth), endtime=phases[i]['endtime'](dist, depth))
else:
env = trial.data
if plot_modus == True:
plt.figure(2)
plt.plot(trial,label = '%s' % phases[i]['name'])
plt.legend()
plt.tight_layout()
plt.savefig(dir_phases + '/Love_envelope_filter_%s.pdf' % phases[i]['name'])
plt.close()
zero_trace = Trace(np.zeros(npts),
header={"starttime": phases[i]['starttime'](dist, depth), 'delta': trial.meta.delta,
"station": trial.meta.station,
"network": trial.meta.network, "location": trial.meta.location,
"channel": phases[i]['name']})
total_trace = zero_trace.__add__(trial, method=0, interpolation_samples=0,
fill_value=trial.data,
sanity_checks=False)
Love_st.append(total_trace)
if plot_modus == True:
plt.figure(3)
plt.plot(Love_st.traces[0].data, label='%s' % Love_st.traces[0].meta.channel)
plt.plot(Love_st.traces[1].data, label='%s' % Love_st.traces[1].meta.channel)
plt.plot(Love_st.traces[2].data, label='%s' % Love_st.traces[2].meta.channel)
plt.plot(Love_st.traces[3].data, label='%s' % Love_st.traces[3].meta.channel)
plt.legend()
plt.tight_layout()
plt.savefig(dir_L + '/diff_Love_freq.pdf')
plt.close()
return Love_st
def filter(self,stream,time_at_rec,la_s,lo_s,depth,Rayleigh = True):
env_stream = Stream()
dist, az, baz = gps2dist_azimuth(lat1=la_s,
lon1=lo_s,
lat2=self.prior['la_r'],
lon2=self.prior['lo_r'], a=self.prior['radius'], f=0)
if Rayleigh == True:
phases = self.get_R_phases(time_at_rec)
else:
phases = self.get_L_phases(time_at_rec)
for i,v in enumerate(stream.traces):
npts = len(v.data)
trace = stream.traces[i].copy()
trace.detrend(type="demean")
trace.interpolate(
sampling_rate=10. / phases[i]['dt']) # No method specified, so : 'weighted_average_slopes' is used
trace.filter('highpass', freq=phases[i]['fmin'], zerophase=True)
trace.filter('lowpass', freq=phases[i]['fmax'], zerophase=True)
trace.detrend()
trace.detrend(type="demean")
env = envelope(trace.data)
zero_trace = Trace(np.zeros(npts),
header={"starttime": phases[i]['starttime'](dist, depth), 'delta': trace.meta.delta,
"station": trace.meta.station,
"network": trace.meta.network, "location": trace.meta.location,
"channel": trace.meta.channel, "instaseis": trace.meta.instaseis})
env_trace = Trace(env,
header={"starttime": phases[i]['starttime'](dist, depth), 'delta': trace.meta.delta,
"station": trace.meta.station,
"network": trace.meta.network, "location": trace.meta.location,
"channel": trace.meta.channel, "instaseis": trace.meta.instaseis})
env_stream.append(env_trace)
return env_stream