-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathautopick.py
308 lines (239 loc) · 8.42 KB
/
autopick.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from obspy.signal.trigger import recursive_sta_lta
from filter import *
import warnings
warnings.filterwarnings("ignore", category=RuntimeWarning)
def kurtosis(data_tr, window=100):
"""
Kurtosis method
INPUT:
data_tr: Trace data, 1D array
window: Sliding/rolling window. Default is 100.
OUTPUT:
Kurtosis value, 1D array
"""
kurt = []
for i in range(len(data_tr)):
a = data_tr[i:i+window]
std, mean = np.std(data_tr), np.mean(data_tr)
y = np.sum((a - mean)**4) / window
k = y / std**4
kurt.append(k)
return kurt
def AIC(data_tr):
"""
Akaike Information Criterion Method
INPUT:
data_tr: Trace data, 1D array
OUTPUT:
AIC value, 1D array
"""
len_data = len(data_tr)
# X Axis length
x_axis = np.arange(len_data)
len_x = len(x_axis)
# AIC Formula
AIC = np.zeros((len_data))
for i in range(0, len_data - 1):
a = i * np.log(np.var(data_tr[0:i]))
b = (len_data - i - 1) * (np.log(np.var(data_tr[i + 1: len_data])))
AIC[i] = a + b
len_AIC = len(AIC)
# Differential AIC with time series
Diff_AIC = np.zeros((len_AIC))
for i in range(len_AIC - 1):
Diff_AIC[i] = ((AIC[i + 1] - AIC[i])/(x_axis[i+1] - (x_axis[i])))**2
for i in range(len_AIC - 1):
if Diff_AIC[i] == np.inf:
Diff_AIC[i] = 0
new_AIC = np.nan_to_num(Diff_AIC)
# max_diff_data = new_AIC.max()
# norm_AIC = np.zeros((len_AIC))
# for i in range(len_AIC - 1):
# norm_AIC[i] = Diff_AIC[i] / max_diff_data
# new_Norm_AIC = np.nan_to_num(norm_AIC)
return new_AIC
def STA_LTA(data_tr, nsta=int(5*50), nlta=(10*200)):
"""
Short-Term Average and Long-Term Average Ratio Method
INPUT:
data_tr: Trace data, 1D array
nsta: Length of short time average window in samples
nlta: Length of long time average window in samples
OUTPUT:
RSL: STA/LTA value, 1D array
"""
RSL = recursive_sta_lta(data_tr, nsta, nlta)
# max_RSL = RSL.max()
# len_tr = len(data_tr)
# norm_RSL = np.zeros((len_tr))
# for i in range(len_tr - 1):
# norm_RSL[i] = RSL[i] / max_RSL
return RSL
# def cutTrace(event, no_trace, cut_trace):
# """
# Cut trace given a specified window
# INPUT:
# event: Event data (TDMS object)
# no_trace: Trace number
# cut_trace: Cut trace window. Specify as tuple (start,end) with start is the
# start time and end is the end time
# OUTPUT:
# tcut: Trace time samples after cut operation
# ycut: Amplitude after cut operation
# """
# # Cut trace
# t, z = event.tt, event.zz # axes
# index_cut = [int(np.where(t==cut_trace[0])[0]), int(np.where(t==cut_trace[1])[0])]
# tcut = t[index_cut[0]:index_cut[1]]
# fibre_data = event.data
# y = fibre_data[:,no_trace]
# ycut = y[index_cut[0]:index_cut[1]]
# return tcut, ycut
def kurtosisFindArrival(event, no_trace, cut_trace, win, lpf=None,
window=100, plot=False, pick_color=None, title=None):
"""
Autopicking arrival times using Kurtosis and plot
INPUT:
event: Event data (TDMS object)
cut_trace: Cut trace window. Specify as tuple (start,end) with start is the
start time and end is the end time
win: Searching window for time arrivals. It is a LIST of TUPLES, for instance
[(x0,y0), (x1,y1), ..., (xn, yn)] where n is the number of arrivals you want
to find, xn is the the starting range, and yn is the ending range. In case
you want to find P and S arrival, then your win will be [(xp,yp), (xs, ys)]
lpf: Low-pass filter.
* Default is None: No filter is used
* If used, specify as dictionary of f (low pass frequency) and fs (sampling
frequency)
window: Kurtosis calculation window. Default is 100.
plot: Option to plot the result. Default is False, so no plot is produced, but
the following OUTPUTS will be produced.
OUTPUT:
t: Autopicked arrival times
A: Autopicked arrival kurtosis
NOTE: Both t and A are LIST, with size that depends on your specified "win"
For two arrivals i.e. P and S arrivals, LIST will have shape (2,)
"""
# Cut trace
tcut, ycut = cutTrace(event, no_trace, cut_trace)
if lpf!=None:
# Low pass filter is applied
f, fs = lpf["f"], lpf["fs"]
ycut = butter_lowpass_filter(ycut, f, fs, order=5)
# Kurtosis calculation
ycut_kurt = kurtosis(ycut, window=window)
assert type(win) is list, "Your window must be a list. Example: [(10,20)] if only one window, or [(10,20),(60,70)] if two windows."
t, A = [], []
for i in range(len(win)):
# For every window
win1 = np.where((tcut >= win[i][0]) & (tcut <= win[i][1]))[0]
win1_start, win1_end = win1[0], win1[-1]
win1_trace = ycut_kurt[win1_start:win1_end]
# Search the highest point
max_win1 = max(win1_trace)
Ap = max_win1
# Search time at the highest point (arrivals)
tp = np.where(ycut_kurt == max_win1)[0]
tp = tcut[tp][0]
t.append(tp)
A.append(Ap)
if plot==True:
# Plot seismogram and kurtosis
plt.figure(figsize=(11,8))
plt.subplot(2,1,1)
plt.plot(tcut, ycut, color="black")
plt.title("Trace Cut No. {} of {}".format(no_trace, title))
plt.xlabel("Time [sec]")
plt.ylabel("Amplitude")
plt.xlim(min(tcut), max(tcut))
plt.grid()
# Plot autopicked arrivals
for i in range(len(t)):
if pick_color==None:
plt.axvline(t[i])
if pick_color!=None:
plt.axvline(t[i], color=pick_color[i])
plt.subplot(2,1,2)
plt.title("Kurtosis of Trace No. {}".format(no_trace))
plt.plot(tcut, ycut_kurt, color="red")
plt.xlabel("Time [sec]")
plt.ylabel("Kurtosis")
plt.xlim(min(tcut), max(tcut))
plt.grid()
plt.tight_layout(1.1)
plt.show()
return t, A
# def kurtosisHeatmap(kurt, time_axis, n_traces, cmap='jet', figsize=(7,5),
# vmin=None, vmax=None):
# """
# Plot heatmap of kurtosis of all traces
# INPUT:
# kurt: 2D array of calculated kurtosis (output of running "pickAllTraces")
# time_axis: Time samples (list)
# n_traces: Number of traces
# """
# # Plot kurtosis heatmap
# plt.figure(figsize=figsize)
# plt.imshow(kurt, aspect='auto', extent=(time_axis[0], time_axis[-1], n_traces-1, 0),
# cmap=cmap, vmin=vmin, vmax=vmax)
# plt.colorbar()
# plt.title("Kurtosis")
# plt.xlabel("Time [sec]")
# plt.ylabel("Trace")
# # if plot_arrival==True:
# # # Plot arrivals
# # plt.scatter(np.array(tp), np.arange(n_traces), marker='|', s=1, color='red')
# # plt.scatter(np.array(ts), np.arange(n_traces), marker='|', s=1, color='red')
def pickAllTraces(event, cut_trace, win, lpf=None, window=100,
save_file=None):
"""
PS picking of all traces in the event data
INPUT:
event: Event data (TDMS object)
cut_trace: Cut trace window
win: Searching window for time arrivals. It is a LIST of TUPLES, for instance
[(x0,y0), (x1,y1), ..., (xn, yn)] where n is the number of arrivals you want
to find, xn is the the starting range, and yn is the ending range. In case
you want to find P and S arrival, then your win will be [(xp,yp), (xs, ys)]
lpf: Low-pass filter.
* Default is None: No filter is used
* If used, specify as dictionary of f (low pass frequency) and fs (sampling
frequency)
window: Kurtosis calculation window. Default is 100.
save_file: Saving arrivals to CSV file
* Default is None: No file is saved
* If saved, specify the filename to save
OUTPUT:
t: Autopicked arrival times
A: Autopicked arrival kurtosis
NOTE: Both t and A are LIST, with size that depends on your specified "win"
For two arrivals i.e. P and S arrivals, LIST will have shape (2,)
"""
n_traces = len(event.zz)
t, A = [], []
for i in range(n_traces):
x = kurtosisFindArrival(event=event, no_trace=i, cut_trace=cut_trace,
win=win, lpf=lpf, window=window)
t_, A_ = x
t.append(t_)
A.append(A_)
if i%10==0:
print("Finished picking until trace {}".format(i))
if save_file!=None:
# Save result to file
df = pd.DataFrame({'D': event.zz})
t, A = np.array(t), np.array(A)
m, n = t.shape
for i in range(n):
# Record arrival times
colname_t = 't'+'{}'.format(i+1)
df[colname_t] = t[:,i]
# Record arrival kurtosis
colname_A = 'A'+'{}'.format(i+1)
df[colname_A] = A[:,i]
df.to_csv(save_file, index=False)
print("Saved file to {}".format(save_file))
return t, A