-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgof_data_sim.py
More file actions
289 lines (246 loc) · 8.86 KB
/
gof_data_sim.py
File metadata and controls
289 lines (246 loc) · 8.86 KB
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
#!/usr/bin/env python
"""
# =============================================================================
# The program processes data reference and simulation signals.
# Including rotation, making equal dt, and synchronization
# of starting and ending time.
# =============================================================================
"""
from __future__ import division, print_function
import math
import numpy as np
from scipy import interpolate
from seism import seism_psignal, s_filter
from stools import seism_cutting, seism_appendzeros
# import matplotlib.pyplot as plt
def reverse_up_down(station):
"""
reverse up down component
"""
# station has 3 components [ns, ew, ud]
# only need to flip the 3rd one
station[2].accel *= -1
station[2].velo *= -1
station[2].displ *= -1
return station
# end of reverse_up_down
def scale_from_m_to_cm(station):
# scales timeseries from meters to centimeters
for i in range(0, len(station)):
station[i].accel *= 100
station[i].velo *= 100
station[i].displ *= 100
return station
# end of scale_data
def get_azimuth():
"""
Get the azimuth for rotation from user.
"""
azimuth = ''
while not azimuth:
a = raw_input('== Enter azimuth for rotation (optional): ')
# if user choose not to rotate
if not a:
return azimuth
try:
azimuth = float(a)
except ValueError:
print("[ERROR]: invalid azimuth.")
return azimuth
# end of get_azimuth
def rotate(station, azimuth):
"""
Station = [psignal_ns, psignal_ew, psignal_up]
"""
# checking instance
if len(station) != 3:
return station
for s in station:
if not isinstance(s, seism_psignal):
return station
psignal_ns = station[0]
psignal_ew = station[1]
psignal_up = station[2]
# Nothing to do
if not azimuth:
return station
# rotate data in North and East
matrix = np.array([(math.cos(math.radians(azimuth)),
-math.sin(math.radians(azimuth))),
(math.sin(math.radians(azimuth)),
math.cos(math.radians(azimuth)))])
[psignal_ns.accel, psignal_ew.accel] = matrix.dot([psignal_ns.accel,
psignal_ew.accel])
[psignal_ns.velo, psignal_ew.velo] = matrix.dot([psignal_ns.velo,
psignal_ew.velo])
[psignal_ns.disp, psignal_ew.disp] = matrix.dot([psignal_ns.displ,
psignal_ew.displ])
station = [psignal_ns, psignal_ew, psignal_up]
return station
# end of rotate
# ============================================================================
def get_dt():
dt = ''
while not dt:
d = raw_input("== Enter common dt of two signals: ")
try:
dt = float(d)
except ValueError:
print("[ERROR]: invalid dt.")
return dt
# end of get_dt
def get_fmax():
fmax = ''
while not fmax:
f = raw_input("== Enter the maximum frequency for decimation: ")
try:
fmax = float(f)
except ValueError:
print("[ERROR]: invalid fmax.")
return fmax
# end of get_fmax
def interp(data, samples, old_dt, new_dt):
"""
Call interpolate on given data
"""
old_t = np.arange(0, samples*old_dt, old_dt)
if old_t.size == samples+1:
old_t = old_t[:-1]
f = interpolate.interp1d(old_t, data, 'linear', bounds_error=False)
new_t = np.arange(0, samples*old_dt, new_dt)
new_data = f(new_t)
# eliminate NaN values
for i in range(1, new_data.size-1):
if np.isnan(new_data[i]):
if not np.isnan(new_data[i+1]):
new_data[i] = (new_data[i-1] + new_data[i+1])/2
else:
new_data[i] = new_data[i-1]
if np.isnan(new_data[-1]):
new_data[-1] = new_data[-2]
# using plot to test
# plt.plot(t,data,'r',new_t,new_data,'b')
# plt.show()
return new_data
# end of interpolate
def process_signal_dt(signal, dt, fmax):
"""
Processes signal with common dt and fmax.
"""
# call low_pass filter at fmax
signal.accel = s_filter(signal.accel, signal.dt, type='lowpass',
family='butter', fmax=fmax,
N=4, rp=0.1, rs=100)
signal.velo = s_filter(signal.velo, signal.dt, type='lowpass',
family='butter', fmax=fmax,
N=4, rp=0.1, rs=100)
signal.displ = s_filter(signal.displ, signal.dt, type='lowpass',
family='butter', fmax=fmax,
N=4, rp=0.1, rs=100)
# interpolate
signal.accel = interp(signal.accel, signal.samples, signal.dt, dt)
signal.velo = interp(signal.velo, signal.samples, signal.dt, dt)
signal.displ = interp(signal.displ, signal.samples, signal.dt, dt)
signal.samples = signal.accel.size
signal.dt = dt
return signal
# end of process
def process_dt(station1, station2, dt, fmax):
"""
Process all signals in two stations to have common dt
"""
# process signals in stations
for i in range(0, 3):
station1[i] = process_signal_dt(station1[i], dt, fmax)
station2[i] = process_signal_dt(station2[i], dt, fmax)
return station1, station2
# end of process_dt
# ============================================================================
def get_earthq():
"""
Get the earthquake start time
"""
time = raw_input("== Enter the earthquake start time (#:#:#.#): ")
time = time.split(':')
if len(time) < 3:
print("[ERROR]: invalid time format.")
return get_earthq()
else:
for i in range(0, len(time)):
try:
time[i] = float(time[i])
except ValueError:
print("[ERROR]: invalid time format.")
return get_earthq()
# time = [hour, min, sec, frac]
return time
# end of get_earthq
def get_leading():
"""
Get the simulation leading time
"""
lt = ''
while not lt:
t = raw_input("== Enter the simulation leading time (sec): ")
try:
lt = float(t)
return lt
except ValueError:
print("[ERROR]: invalid leading time.")
# return lt
# end of get_leading
def synchronize(station1, station2, stamp, eqtimestamp, leading):
"""
synchronize the stating time and ending time of data arrays in two signals
signal1 = data signal; signal2 = simulation signal
"""
if not stamp:
return station1, station2
# time in sec = hr*3600 + min*60 + sec + frac*0.1
start = stamp[0]*3600 + stamp[1]*60 + stamp[2]
eq_time = eqtimestamp[0]*3600 + eqtimestamp[1]*60 + eqtimestamp[2]
sim_start = eq_time - leading
for i in range(0, 3):
signal1 = station1[i]
signal2 = station2[i]
samples1 = signal1.samples
samples2 = signal2.samples
dt = signal1.dt # same dt of two signals
# synchronize the start time
if start < sim_start:
# data time < sim time < earthquake time; cutting data array
signal1 = seism_cutting('front', (sim_start - start),
20, signal1, False)
elif start > eq_time:
# sim time < earthquake time < data time; adding zeros in front
signal1 = seism_appendzeros('front', (start - eq_time),
20, signal1)
signal2 = seism_cutting('front', (eq_time - sim_start),
20, signal2, False)
else:
# sim time < data time < earthquake time; adding zeros
signal1 = seism_appendzeros('front', (start - sim_start),
20, signal1)
# synchronize the ending time
data_time = dt * samples1 # total time of data signal
end = start + data_time
sim_time = dt * samples2 # total simulation time
sim_end = sim_start + sim_time
if sim_end < end:
# adding zeros in simulation signal
# signal2 = seism_appendzeros('end', (end - sim_end), 20, signal2)
signal1 = seism_cutting('end', (end - sim_end), 20, signal1, False)
elif end < sim_end:
# cutting from simulation signal
signal2 = seism_cutting('end', (sim_end - end), 20, signal2, False)
else:
pass
# scale the data if they have one sample in difference after synchronizing
if signal1.samples == signal2.samples+1:
seism_appendzeros('end', signal2.dt, 20, signal2)
elif signal2.samples == signal1.samples+1:
seism_appendzeros('end', signal1.dt, 20, signal1)
station1[i] = signal1
station2[i] = signal2
return station1, station2
# end of synchronize