forked from JohnVidale/Array_codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pro6_plot_stacked_seis.py
440 lines (392 loc) · 17.9 KB
/
pro6_plot_stacked_seis.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/usr/bin/env python
# Read in 2D stacks for two events
# Compute tdiff, ave_amp, amp_ratio
# Plot radial and transverse cuts through stack, plus beam sum
# Write out tdiff, ave_amp, amp_ratio results
# John Vidale 3/2019
def pro6stacked_seis(eq_file1, eq_file2, plot_scale_fac = 0.03, slow_delta = 0.0005,
slowR_lo = -0.1, slowR_hi = 0.1, slowT_lo = -0.1, slowT_hi = 0.1,
start_buff = -50, end_buff = 50, norm = 0, freq_corr = 1.0,
plot_dyn_range = 1000, fig_index = 401, get_stf = 0, ref_phase = 'blank',
ARRAY = 0, max_rat = 1.8, min_amp = 0.2, turn_off_black = 0,
R_slow_plot = 0, T_slow_plot = 0, tdiff_clip = 1, event_no = 0):
import obspy
import obspy.signal
from obspy import UTCDateTime
from obspy import Stream, Trace
from obspy import read
from obspy.geodetics import gps2dist_azimuth
import numpy as np
import os
from obspy.taup import TauPyModel
import obspy.signal as sign
import matplotlib.pyplot as plt
model = TauPyModel(model='iasp91')
from scipy.signal import hilbert
import math
import time
import statistics
#%% Get info
#%% get locations
print('Running pro6_plot_stacked_seis')
start_time_wc = time.time()
dphase = 'PKiKP'
sta_file = '/Users/vidale/Documents/GitHub/Array_codes/Files/events_good.txt'
with open(sta_file, 'r') as file:
lines = file.readlines()
event_count = len(lines)
print(str(event_count) + ' lines read from ' + sta_file)
# Load station coords into arrays
station_index = range(event_count)
event_names = []
event_index = np.zeros(event_count)
event_year = np.zeros(event_count)
event_mo = np.zeros(event_count)
event_day = np.zeros(event_count)
event_hr = np.zeros(event_count)
event_min = np.zeros(event_count)
event_sec = np.zeros(event_count)
event_lat = np.zeros(event_count)
event_lon = np.zeros(event_count)
event_dep = np.zeros(event_count)
event_mb = np.zeros(event_count)
event_ms = np.zeros(event_count)
event_tstart = np.zeros(event_count)
event_tend = np.zeros(event_count)
event_gcdist = np.zeros(event_count)
event_dist = np.zeros(event_count)
event_baz = np.zeros(event_count)
event_SNR = np.zeros(event_count)
event_Sflag = np.zeros(event_count)
event_PKiKPflag = np.zeros(event_count)
event_ICSflag = np.zeros(event_count)
event_PKiKP_radslo = np.zeros(event_count)
event_PKiKP_traslo = np.zeros(event_count)
event_PKiKP_qual = np.zeros(event_count)
event_ICS_qual = np.zeros(event_count)
iii = 0
for ii in station_index: # read file
line = lines[ii]
split_line = line.split()
event_index[ii] = float(split_line[0])
event_names.append(split_line[1])
event_year[ii] = float(split_line[2])
event_mo[ii] = float(split_line[3])
event_day[ii] = float(split_line[4])
event_hr[ii] = float(split_line[5])
event_min[ii] = float(split_line[6])
event_sec[ii] = float(split_line[7])
event_lat[ii] = float(split_line[8])
event_lon[ii] = float(split_line[9])
event_dep[ii] = float(split_line[10])
event_mb[ii] = float(split_line[11])
event_ms[ii] = float(split_line[12])
event_tstart[ii] = float(split_line[13])
event_tend[ii] = float(split_line[14])
event_gcdist[ii] = float(split_line[15])
event_dist[ii] = float(split_line[16])
event_baz[ii] = float(split_line[17])
event_SNR[ii] = float(split_line[18])
event_Sflag[ii] = float(split_line[19])
event_PKiKPflag[ii] = float(split_line[20])
event_ICSflag[ii] = float(split_line[21])
event_PKiKP_radslo[ii] = float(split_line[22])
event_PKiKP_traslo[ii] = float(split_line[23])
event_PKiKP_qual[ii] = float(split_line[24])
event_ICS_qual[ii] = float(split_line[25])
# print('Event ' + str(ii) + ' is ' + str(event_index[ii]))
if event_index[ii] == event_no:
iii = ii
if iii == 0:
print('Event ' + str(event_no) + ' not found')
else:
print('Event ' + str(event_no) + ' is ' + str(iii))
# find predicted slowness
arrivals1 = model.get_travel_times(source_depth_in_km=event_dep[iii],distance_in_degree=event_gcdist[iii]-0.5,phase_list=[dphase])
arrivals2 = model.get_travel_times(source_depth_in_km=event_dep[iii],distance_in_degree=event_gcdist[iii]+0.5,phase_list=[dphase])
dtime = arrivals2[0].time - arrivals1[0].time
event_pred_slo = dtime/111. # s/km
# convert to pred rslo and tslo
sin_baz = np.sin(event_baz[iii] * np.pi /180)
cos_baz = np.cos(event_baz[iii] * np.pi /180)
pred_Nslo = event_pred_slo * cos_baz
pred_Eslo = event_pred_slo * sin_baz
# rotate observed slowness to N and E
obs_Nslo = (event_PKiKP_radslo[iii] * cos_baz) - (event_PKiKP_traslo[iii] * sin_baz)
obs_Eslo = (event_PKiKP_radslo[iii] * sin_baz) + (event_PKiKP_traslo[iii] * cos_baz)
print('PR '+ str(pred_Nslo) + ' PT ' + str(pred_Eslo) + ' OR ' + str(obs_Nslo) + ' OT ' + str(obs_Eslo))
# find observed back-azimuth
# bazi_rad = np.arctan(event_PKiKP_traslo[ii]/event_PKiKP_radslo[ii])
# event_obs_bazi = event_baz[ii] + (bazi_rad * 180 / np.pi)
if ARRAY == 1:
goto = '/Users/vidale/Documents/PyCode/LASA/EvLocs'
os.chdir(goto)
file = open(eq_file1, 'r')
lines=file.readlines()
split_line = lines[0].split()
t1 = UTCDateTime(split_line[1])
date_label1 = split_line[1][0:10]
file = open(eq_file2, 'r')
lines=file.readlines()
split_line = lines[0].split()
t2 = UTCDateTime(split_line[1])
date_label2 = split_line[1][0:10]
#%% read files
# #%% Get saved event info, also used to name files
# date_label = '2018-04-02' # date for filename
if ARRAY == 1:
goto = '/Users/vidale/Documents/PyCode/LASA/Pro_files'
os.chdir(goto)
fname1 = 'HD' + date_label1 + '_2dstack.mseed'
fname2 = 'HD' + date_label2 + '_2dstack.mseed'
st1 = Stream()
st2 = Stream()
st1 = read(fname1)
st2 = read(fname2)
tshift = st1.copy() # make array for time shift
amp_ratio = st1.copy() # make array for relative amplitude
amp_ave = st1.copy() # make array for relative amplitude
print('Read in: event 1 ' + str(len(st1)) + ' event 2 ' + str(len(st2)) + ' traces')
nt1 = len(st1[0].data)
nt2 = len(st2[0].data)
dt1 = st1[0].stats.delta
dt2 = st2[0].stats.delta
print('Event 1 - First trace has ' + str(nt1) + ' time pts, time sampling of '
+ str(dt1) + ' and thus duration of ' + str((nt1-1)*dt1))
print('Event 2 - First trace has ' + str(nt2) + ' time pts, time sampling of '
+ str(dt2) + ' and thus duration of ' + str((nt2-1)*dt2))
if nt1 != nt2 or dt1 != dt2:
print('nt or dt not does not match')
exit(-1)
#%% Make grid of slownesses
slowR_n = int(1 + (slowR_hi - slowR_lo)/slow_delta) # number of slownesses
slowT_n = int(1 + (slowT_hi - slowT_lo)/slow_delta) # number of slownesses
print(str(slowT_n) + ' trans slownesses, hi and lo are ' + str(slowT_hi) + ' ' + str(slowT_lo))
# In English, stack_slows = range(slow_n) * slow_delta - slow_lo
a1R = range(slowR_n)
a1T = range(slowT_n)
stack_Rslows = [(x * slow_delta + slowR_lo) for x in a1R]
stack_Tslows = [(x * slow_delta + slowT_lo) for x in a1T]
print(str(slowR_n) + ' radial slownesses, ' + str(slowT_n) + ' trans slownesses, ')
#%% Loop over slowness
total_slows = slowR_n * slowT_n
global_max = 0
for slow_i in range(total_slows): # find envelope, phase, tshift, and global max
if slow_i % 200 == 0:
print('At line 101, ' +str(slow_i) + ' slowness out of ' + str(total_slows))
if len(st1[slow_i].data) == 0: # test for zero-length traces
print('%d data has zero length ' % (slow_i))
seismogram1 = hilbert(st1[slow_i].data) # make analytic seismograms
seismogram2 = hilbert(st2[slow_i].data)
env1 = np.abs(seismogram1) # amplitude
env2 = np.abs(seismogram2)
amp_ave[slow_i].data = 0.5 * (env1 + env2)
amp_ratio[slow_i].data = env1/env2
angle1 = np.angle(seismogram1) # time shift
angle2 = np.angle(seismogram2)
phase1 = np.unwrap(angle1)
phase2 = np.unwrap(angle2)
dphase = (angle1 - angle2)
# dphase = phase1 - phase2
for it in range(nt1):
if dphase[it] > math.pi:
dphase[it] -= 2 * math.pi
elif dphase[it] < -1 * math.pi:
dphase[it] += 2 * math.pi
if dphase[it] > math.pi or dphase[it] < -math.pi:
print(f'Bad dphase value {dphase[it]:.2f} {it:4d}')
freq1 = np.diff(phase1) #freq in radians/sec
freq2 = np.diff(phase2)
ave_freq = 0.5*(freq1 + freq2)
ave_freq_plus = np.append(ave_freq,[1]) # ave_freq one element too short
# tshift[slow_i].data = dphase / ave_freq_plus # 2*pi top and bottom cancels
tshift[slow_i].data = dphase/(2*math.pi*freq_corr)
local_max = max(abs(amp_ave[slow_i].data))
if local_max > global_max:
global_max = local_max
#%% Extract slices
tshift_full = tshift.copy() # make array for time shift
for slow_i in range(total_slows): # ignore less robust points
if slow_i % 200 == 0:
print('At line 140, ' +str(slow_i) + ' slowness out of ' + str(total_slows))
for it in range(nt1):
if ((amp_ratio[slow_i].data[it] < (1/max_rat)) or (amp_ratio[slow_i].data[it] > max_rat) or (amp_ave[slow_i].data[it] < (min_amp * global_max))):
tshift[slow_i].data[it] = np.nan
#%% If desired, find transverse slowness nearest T_slow_plot
lowest_Tslow = 1000000
for slow_i in range(slowT_n):
if abs(stack_Tslows[slow_i] - T_slow_plot) < lowest_Tslow:
lowest_Tindex = slow_i
lowest_Tslow = abs(stack_Tslows[slow_i] - T_slow_plot)
print(str(slowT_n) + ' T slownesses, index ' + str(lowest_Tindex) + ' is closest to input parameter ' + str(T_slow_plot) + ', slowness diff there is ' + str(lowest_Tslow) + ' and slowness is ' + str(stack_Tslows[lowest_Tindex]))
# Select only stacks with that slowness for radial plot
centralR_st1 = Stream()
centralR_st2 = Stream()
centralR_amp = Stream()
centralR_ampr = Stream()
centralR_tdiff = Stream()
for slowR_i in range(slowR_n):
ii = slowR_i*slowT_n + lowest_Tindex
centralR_st1 += st1[ii]
centralR_st2 += st2[ii]
centralR_amp += amp_ave[ii]
centralR_ampr += amp_ratio[ii]
centralR_tdiff += tshift[ii]
#%% If desired, find radial slowness nearest R_slow_plot
lowest_Rslow = 1000000
for slow_i in range(slowR_n):
if abs(stack_Rslows[slow_i] - R_slow_plot) < lowest_Rslow:
lowest_Rindex = slow_i
lowest_Rslow = abs(stack_Rslows[slow_i] - R_slow_plot)
print(str(slowR_n) + ' R slownesses, index ' + str(lowest_Rindex) + ' is closest to input parameter ' + str(R_slow_plot) + ', slowness diff there is ' + str(lowest_Rslow) + ' and slowness is ' + str(stack_Rslows[lowest_Rindex]))
# Select only stacks with that slowness for transverse plot
centralT_st1 = Stream()
centralT_st2 = Stream()
centralT_amp = Stream()
centralT_ampr = Stream()
centralT_tdiff = Stream()
#%% to extract stacked time functions
event1_sample = Stream()
event2_sample = Stream()
for slowT_i in range(slowT_n):
ii = lowest_Rindex*slowT_n + slowT_i
centralT_st1 += st1[ii]
centralT_st2 += st2[ii]
centralT_amp += amp_ave[ii]
centralT_ampr += amp_ratio[ii]
centralT_tdiff += tshift[ii]
#%% compute timing time series
ttt = (np.arange(len(st1[0].data)) * st1[0].stats.delta + start_buff) # in units of seconds
#%% Plot radial amp and tdiff vs time plots
fig_index = 6
# plt.close(fig_index)
plt.figure(fig_index,figsize=(30,10))
plt.xlim(start_buff,end_buff)
plt.ylim(stack_Rslows[0], stack_Rslows[-1])
for slowR_i in range(slowR_n): # loop over radial slownesses
dist_offset = stack_Rslows[slowR_i] # trying for approx degrees
ttt = (np.arange(len(centralR_st1[slowR_i].data)) * centralR_st1[slowR_i].stats.delta
+ (centralR_st1[slowR_i].stats.starttime - t1))
plt.plot(ttt, (centralR_st1[slowR_i].data - np.median(centralR_st1[slowR_i].data))*plot_scale_fac /global_max + dist_offset, color = 'green')
plt.plot(ttt, (centralR_st2[slowR_i].data - np.median(centralR_st2[slowR_i].data))*plot_scale_fac /global_max + dist_offset, color = 'red')
# extract stacked time functions
if get_stf != 0:
if np.abs(stack_Rslows[slowR_i]- 0.005) < 0.000001: # kludge, not exactly zero when desired
event1_sample = centralR_st1[slowR_i].copy()
event2_sample = centralR_st2[slowR_i].copy()
# plt.plot(ttt, (centralR_amp[slowR_i].data) *plot_scale_fac/global_max + dist_offset, color = 'purple')
if turn_off_black == 0:
plt.plot(ttt, (centralR_tdiff[slowR_i].data)*plot_scale_fac/1 + dist_offset, color = 'black')
plt.plot(ttt, (centralR_amp[slowR_i].data)*0.0 + dist_offset, color = 'lightgray') # reference lines
plt.xlabel('Time (s)')
plt.ylabel('R Slowness (s/km)')
plt.title(ref_phase + ' seismograms and tdiff at ' + str(T_slow_plot) + ' T slowness, green is event1, red is event2')
# Plot transverse amp and tdiff vs time plots
fig_index = 7
# plt.close(fig_index)
plt.figure(fig_index,figsize=(30,10))
plt.xlim(start_buff,end_buff)
plt.ylim(stack_Tslows[0], stack_Tslows[-1])
for slowT_i in range(slowT_n): # loop over transverse slownesses
dist_offset = stack_Tslows[slowT_i] # trying for approx degrees
ttt = (np.arange(len(centralT_st1[slowT_i].data)) * centralT_st1[slowT_i].stats.delta
+ (centralT_st1[slowT_i].stats.starttime - t1))
plt.plot(ttt, (centralT_st1[slowT_i].data - np.median(centralT_st1[slowT_i].data))*plot_scale_fac /global_max + dist_offset, color = 'green')
plt.plot(ttt, (centralT_st2[slowT_i].data - np.median(centralT_st2[slowT_i].data))*plot_scale_fac /global_max + dist_offset, color = 'red')
# plt.plot(ttt, (centralT_amp[slowT_i].data) *plot_scale_fac/global_max + dist_offset, color = 'purple')
if turn_off_black == 0:
plt.plot(ttt, (centralT_tdiff[slowT_i].data)*plot_scale_fac/1 + dist_offset, color = 'black')
plt.plot(ttt, (centralT_amp[slowT_i].data)*0.0 + dist_offset, color = 'lightgray') # reference lines
plt.xlabel('Time (s)')
plt.ylabel('T Slowness (s/km)')
plt.title(str(event_no) + ' ' + date_label1 + ' ' +ref_phase + ' seismograms and tdiff ' + str(R_slow_plot) + ' R slowness, green is event1, red is event2')
os.chdir('/Users/vidale/Documents/PyCode/LASA/Quake_results/Plots')
# plt.savefig(date_label1 + '_' + str(start_buff) + '_' + str(end_buff) + '_stack.png')
#%% R-T tshift averaged over time window
fig_index = 8
stack_slice = np.zeros((slowR_n,slowT_n))
for slowR_i in range(slowR_n): # loop over radial slownesses
for slowT_i in range(slowT_n): # loop over transverse slownesses
index = slowR_i*slowT_n + slowT_i
num_val = np.nanmedian(tshift[index].data)
# num_val = statistics.median(tshift_full[index].data)
stack_slice[slowR_i, slowT_i] = num_val # adjust for dominant frequency of 1.2 Hz, not 1 Hz
# stack_slice[0,0] = -0.25
# stack_slice[0,1] = 0.25
# tdiff_clip = 0.4/1.2
tdiff_clip_max = tdiff_clip # DO NOT LEAVE COMMENTED OUT!!
tdiff_clip_min = -tdiff_clip
y1, x1 = np.mgrid[slice(stack_Rslows[0], stack_Rslows[-1] + slow_delta, slow_delta),
slice(stack_Tslows[0], stack_Tslows[-1] + slow_delta, slow_delta)]
fig, ax = plt.subplots(1, figsize=(7,6))
# fig, ax = plt.subplots(1, figsize=(9,2))
# fig.subplots_adjust(bottom=0.3)
# c = ax.pcolormesh(x1, y1, stack_slice, cmap=plt.cm.bwr, vmin = tdiff_clip_min, vmax = tdiff_clip_max)
c = ax.pcolormesh(x1, y1, stack_slice, cmap=plt.cm.coolwarm, vmin = tdiff_clip_min, vmax = tdiff_clip_max)
ax.axis([x1.min(), x1.max(), y1.min(), y1.max()])
circle1 = plt.Circle((0, 0), 0.019, color='black', fill=False)
ax.add_artist(circle1)
circle2 = plt.Circle((0, 0), 0.040, color='black', fill=False)
ax.add_artist(circle2) #outer core limit
fig.colorbar(c, ax=ax)
plt.ylabel('R Slowness (s/km)')
plt.title(ref_phase + ' time shift')
# plt.title('T-R average time shift ' + date_label1 + ' ' + date_label2)
plt.show()
#%% R-T amplitude averaged over time window
fig_index = 9
stack_slice = np.zeros((slowR_n,slowT_n))
smax = 0
for slowR_i in range(slowR_n): # loop over radial slownesses
for slowT_i in range(slowT_n): # loop over transverse slownesses
index = slowR_i*slowT_n + slowT_i
num_val = np.nanmedian(amp_ave[index].data)
stack_slice[slowR_i, slowT_i] = num_val
if num_val > smax:
smax = num_val
# stack_slice[0,0] = 0
y1, x1 = np.mgrid[slice(stack_Rslows[0], stack_Rslows[-1] + slow_delta, slow_delta),
slice(stack_Tslows[0], stack_Tslows[-1] + slow_delta, slow_delta)]
# fig, ax = plt.subplots(1)
fig, ax = plt.subplots(1, figsize=(7,6))
# c = ax.pcolormesh(x1, y1, stack_slice/smax, cmap=plt.cm.gist_yarg, vmin = 0.5)
c = ax.pcolormesh(x1, y1, stack_slice/smax, cmap=plt.cm.gist_rainbow_r, vmin = 0)
# c = ax.pcolormesh(x1, y1, stack_slice, cmap=plt.cm.gist_rainbow_r, vmin = 0)
ax.axis([x1.min(), x1.max(), y1.min(), y1.max()])
circle1 = plt.Circle((0, 0), 0.019, color='black', fill=False)
ax.add_artist(circle1) #inner core limit
circle2 = plt.Circle((0, 0), 0.040, color='black', fill=False)
ax.add_artist(circle2) #outer core limit
c = ax.scatter(pred_Eslo, pred_Nslo, color='blue', s=100, alpha=0.75)
c = ax.scatter(obs_Eslo, obs_Nslo, color='black', s=100, alpha=0.75)
fig.colorbar(c, ax=ax)
plt.xlabel('Transverse Slowness (s/km)')
plt.ylabel('Radial Slowness (s/km)')
plt.title(str(event_no) + ' ' + date_label1 + ' ' + ref_phase + ' beam amplitude')
# plt.title('Beam amplitude ' + date_label1 + ' ' + date_label2)
os.chdir('/Users/vidale/Documents/PyCode/LASA/Quake_results/Plots')
plt.savefig(date_label1 + '_' + str(start_buff) + '_' + str(end_buff) + '_beam.png')
plt.show()
#%% Save processed files
if ARRAY == 0:
goto = '/Users/vidale/Documents/PyCode/Hinet'
if ARRAY == 1:
goto = '/Users/vidale/Documents/PyCode/LASA/Pro_Files'
os.chdir(goto)
fname = 'HD' + date_label1 + '_' + date_label2 + '_tshift.mseed'
tshift_full.write(fname,format = 'MSEED')
fname = 'HD' + date_label1 + '_' + date_label2 + '_amp_ave.mseed'
amp_ave.write(fname,format = 'MSEED')
fname = 'HD' + date_label1 + '_' + date_label2 + '_amp_ratio.mseed'
amp_ratio.write(fname,format = 'MSEED')
#%% Option to write out stf
if get_stf != 0:
event1_sample.taper(0.1)
event2_sample.taper(0.1)
fname = 'HD' + date_label1 + '_stf.mseed'
event1_sample.write(fname,format = 'MSEED')
fname = 'HD' + date_label2 + '_stf.mseed'
event2_sample.write(fname,format = 'MSEED')
elapsed_time_wc = time.time() - start_time_wc
print('This job took ' + str(elapsed_time_wc) + ' seconds')
os.system('say "Done"')