forked from jmfee-usgs/geomag-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamConverter_test.py
312 lines (268 loc) · 12.4 KB
/
StreamConverter_test.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
"""Unit Tests for StreamConverter
Notes
-----
We are using triangle identities for variables to test with. Specifically
the hypotenuse is normally equal to 1, causing the adjacent angle length
to be cos(angle) and the opposite length to be sin(angle)
For more info on the components see the Notes in ChannelConverterTest.py.
"""
import obspy.core
import numpy
from geomagio import ChannelConverter, StreamConverter
assert_almost_equal = numpy.testing.assert_almost_equal
cos = numpy.cos
sin = numpy.sin
D2R = numpy.pi / 180
D2I = 60 * 10 # Degrees to Iaga Decbas
STARTTIME = obspy.core.UTCDateTime('2014-11-01')
def test_get_geo_from_mag():
"""StreamConverter_test.test_get_geo_from_mag()
The magnetic north stream containing the traces ''h'', ''d'', ''z'', and
''f'' converts to the geographics stream containing the traces ''x'',
''y'', ''z'' and ''f''
"""
mag = obspy.core.Stream()
# Call get_geo_from_magusing a decbas of 15 degrees, and streams with
# H = [1, 1], and D = [15 degrees, 30 degrees], expect streams of
# X = [cos(15), cos(30)] and Y = [sin(15), sin(30)]
# stats.DECBAS = 15 * D2I
mag += __create_trace('H', [1, 1])
mag += __create_trace('D', [15 * D2R, 30 * D2R])
mag += __create_trace('Z', [1, 1])
mag += __create_trace('F', [1, 1])
geo = StreamConverter.get_geo_from_mag(mag)
X = geo.select(channel='X')[0].data
Y = geo.select(channel='Y')[0].data
assert_almost_equal(X, [cos(15 * D2R), cos(30 * D2R)], 9,
'Expect X to equal [cos(15), cos(30)]', True)
assert_almost_equal(Y, [sin(15 * D2R), sin(30 * D2R)], 9,
'Expect Y to equal [sin(15), sin(30)]', True)
def test_get_geo_from_obs():
"""StreamConverter_test.test_get_geo_from_obs()
The observatory stream containing the observatory traces
''h'', ''d'' or ''e'', ''z'', and ''f'' converts to the geographic
stream containing the traces ''x'', ''y'', ''z'', and ''f''
"""
obs = obspy.core.Stream()
# 1) Call get_geo_from_obs using equal h, e streams with a decbas of 0
# the geographic stream values X, Y will be the same.
obs += __create_trace('H', [1])
obs += __create_trace('E', [1])
obs += __create_trace('Z', [1])
obs += __create_trace('F', [1])
geo = StreamConverter.get_geo_from_obs(obs)
X = geo.select(channel='X')[0].data
Y = geo.select(channel='Y')[0].data
assert_almost_equal(X[0], 1, 9,
'Expect X to almost equal 1', True)
assert_almost_equal(Y[0], 1, 9,
'Expect Y to almost equal 1', True)
# 2) Call get_geo_from_obs using a decbas of 15 degrees, and streams
# with H = [cos(15), cos(30)], and E = [sin(15), sin(30)].
# Expect streams of X = [cos(30), cos(45)] and Y = sin(30), sin(45)
obs = obspy.core.Stream()
DECBAS = 15 * D2I
obs += __create_trace('H', [cos(15 * D2R), cos(30 * D2R)], DECBAS)
obs += __create_trace('E', [sin(15 * D2R), sin(30 * D2R)], DECBAS)
obs += __create_trace('Z', [1, 1], DECBAS)
obs += __create_trace('F', [1, 1], DECBAS)
geo = StreamConverter.get_geo_from_obs(obs)
X = geo.select(channel='X')[0].data
Y = geo.select(channel='Y')[0].data
assert_almost_equal(X, [cos(30 * D2R), cos(45 * D2R)], 9,
'Expect X to equal [cos(30), cos(45)]', True)
assert_almost_equal(Y, [sin(30 * D2R), sin(45 * D2R)], 9,
'Expect Y to equal [sin(30), sin(45)]', True)
def test_get_mag_from_geo():
"""StreamConverter_test.test_get_mag_from_geo()
The geographic stream containing the traces ''x'', ''y'', ''z'', and
''f'' converts to the magnetic stream containing the traces
''h'', ''d'', ''z'' and ''f''.
"""
geo = obspy.core.Stream()
# Call get_mag_from_geo using a decbas of 15, a X stream of
# [cos(15), cos(30)], and a Y stream of [sin(15), sin(30)].
# Expect a H stream of [1,1] and a D strem of [15 degrees, 30 degrees]
DECBAS = 15 * D2I
geo += __create_trace('X', [cos(15 * D2R), cos(30 * D2R)], DECBAS)
geo += __create_trace('Y', [sin(15 * D2R), sin(30 * D2R)], DECBAS)
geo += __create_trace('Z', [1, 1], DECBAS)
geo += __create_trace('F', [1, 1], DECBAS)
mag = StreamConverter.get_mag_from_geo(geo)
H = mag.select(channel='H')[0].data
D = mag.select(channel='D')[0].data
assert_almost_equal(H, [1, 1], 9,
'Expect H to equal [1,1]', True)
assert_almost_equal(D, [15 * D2R, 30 * D2R], 9,
'Expect D to equal [15 degrees, 30 degrees]', True)
def test_get_mag_from_obs():
"""StreamConverter_test.test_get_mag_from_obs()
The observatory stream containing the traces ''h'', ''e'' or ''d'',
''z'' and ''f''
"""
obs = obspy.core.Stream()
# Call get_mag_from_obs using a DECBAS of 15 degrees, a H stream of
# [cos(15), cos(30)] and a E stream of [sin(15), sin(30)].
# Expect a H stream of [1, 1] and a D stream of [30 degrees, 45 degrees]
DECBAS = 15 * D2I
obs += __create_trace('H', [cos(15 * D2R), cos(30 * D2R)], DECBAS)
obs += __create_trace('E', [sin(15 * D2R), sin(30 * D2R)], DECBAS)
obs += __create_trace('Z', [1, 1], DECBAS)
obs += __create_trace('F', [1, 1], DECBAS)
mag = StreamConverter.get_mag_from_obs(obs)
H = mag.select(channel='H')[0].data
D = mag.select(channel='D')[0].data
assert_almost_equal(H, [1, 1], 9,
'Expect H to equal [1,1]', True)
assert_almost_equal(D, [30 * D2R, 45 * D2R], 9,
'Expect D to equal [30 degrees, 45 degrees]', True)
def test_get_obs_from_geo():
"""StreamConverter_test.test_get_obs_from_geo()
The geographic stream containing the traces ''x'', ''y'', ''z'', and
''f'' converts to the observatory stream containing the traces
''h'', ''d'' or ''e'', ''z'', and ''f''.
"""
geo = obspy.core.Stream()
# Call get_geo_from_obs using a decbas of 15, a X stream of
# [cos(30), cos(45)], and a Y stream of [sin(30), sin(45)].
# Expect a H stream of [cos(15), cos(30)] and a
# E stream of [sin(15), sin(30)]
DECBAS = 15 * D2I
geo += __create_trace('X', [cos(30 * D2R), cos(45 * D2R)], DECBAS)
geo += __create_trace('Y', [sin(30 * D2R), sin(45 * D2R)], DECBAS)
geo += __create_trace('Z', [1, 1], DECBAS)
geo += __create_trace('F', [1, 1], DECBAS)
obs = StreamConverter.get_obs_from_geo(geo, True)
H = obs.select(channel='H')[0].data
E = obs.select(channel='E')[0].data
D = obs.select(channel='D')[0].data
assert_almost_equal(H, [cos(15 * D2R), cos(30 * D2R)], 9,
'Expect H to equal [cos(15), cos(30)]', True)
assert_almost_equal(E, [sin(15 * D2R), sin(30 * D2R)], 9,
'Expect E to equal [sin(15), sin(30)', True)
assert_almost_equal(D, [15 * D2R, 30 * D2R], 9,
'Expect D to equal [15 degress, 30 degrees]', True)
def test_get_obs_from_mag():
"""StreamConverter_test.test_get_obs_from_mag()
The magnetic stream containing the traces ''h'', ''d'', ''z'', and ''f''
converts to the observatory stream containing the traces
''h'', ''e'' and/or ''d'', ''z'', and ''f''
"""
mag = obspy.core.Stream()
# Call get_obs_from_mag using a decbas of 15, a H stream of [1,1],
# and a D stream of [30 degrees, 45 degrees]. Expect a H stream
# of [cos(15), cos(30)], a D stream of [30 degrees, 45 degrees],
# and a E stream of [sin(15), sin(30)]
DECBAS = 15 * D2I
mag += __create_trace('H', [1, 1], DECBAS)
mag += __create_trace('D', [30 * D2R, 45 * D2R], DECBAS)
mag += __create_trace('Z', [1, 1], DECBAS)
mag += __create_trace('F', [1, 1], DECBAS)
obs = StreamConverter.get_obs_from_mag(mag, True)
H = obs.select(channel='H')[0].data
D = obs.select(channel='D')[0].data
E = obs.select(channel='E')[0].data
assert_almost_equal(H, [cos(15 * D2R), cos(30 * D2R)], 9,
'Expect H to equal [cos(15), cos(30)', True)
assert_almost_equal(D, [15 * D2R, 30 * D2R], 9,
'Expect D to equal [15 degrees, 30 degrees', True)
assert_almost_equal(E, [sin(15 * D2R), sin(30 * D2R)], 9,
'Expect E to equal [sin(15), sin(30)', True)
def test_get_obs_from_obs():
"""StreamConverter_test.test_get_obs_from_obs()
The observatory stream can contain either ''d'' or ''e'' depending
on it's source. get_obs_from_obs will return either or both as part
of the obs Stream.
"""
# 1) Call get_obs_from_obs using a decbas of 15, a H stream of
# [cos(15), cos(30)], and a E stream of [sin(15), sin(30)].
# Expect a D stream of [15 degrees, 30 degrees]
obs_e = obspy.core.Stream()
DECBAS = 15 * D2I
obs_e += __create_trace('H', [cos(15 * D2R), cos(30 * D2R)], DECBAS)
obs_e += __create_trace('E', [sin(15 * D2R), sin(30 * D2R)], DECBAS)
obs_e += __create_trace('Z', [1, 1], DECBAS)
obs_e += __create_trace('F', [1, 1], DECBAS)
obs_D = StreamConverter.get_obs_from_obs(obs_e, False, True)
d = obs_D.select(channel='D')[0].data
assert_almost_equal(d, [15 * D2R, 30 * D2R], 9,
'Expect D to equal [15 degrees, 30 degrees]', True)
# 2) Call get_obs_from_obs using a decbase of 15 degrees, a H stream of
# [cos(15), cos(30)], and a D stream of [15, 30].
# Expect a D stream of [sin(15), sin(30)]
obs_d = obspy.core.Stream()
obs_d += __create_trace('H', [cos(15 * D2R), cos(30 * D2R)], DECBAS)
obs_d += __create_trace('D', [15 * D2R, 30 * D2R], DECBAS)
obs_d += __create_trace('Z', [1, 1], DECBAS)
obs_d += __create_trace('F', [1, 1], DECBAS)
obs_E = StreamConverter.get_obs_from_obs(obs_d, True, False)
e = obs_E.select(channel='E')[0].data
assert_almost_equal(e, [sin(15 * D2R), sin(30 * D2R)], 9,
'Expect E to equal [sin(15), sin(30)', True)
def test_verification_data():
"""StreamConverter_test.test_verification_data()
This is a verification test of data done with different
converters, to see if the same result is returned.
Since the small angle approximation was used in the other
converters, AND round off was done differently, we can't
get the exact results.
Change the precision in assert_almost_equal to larger precision
(ie 2 to 8) to see how off the data is. Most are well within
expectations.
"""
DECBAS = 552.7
obs_v = obspy.core.Stream()
obs_v += __create_trace('H',
[20889.55, 20889.57, 20889.74, 20889.86, 20889.91, 20889.81], DECBAS)
obs_v += __create_trace('E',
[-21.10, -20.89, -20.72, -20.57, -20.39, -20.12], DECBAS)
obs_v += __create_trace('Z',
[47565.29, 47565.34, 47565.39, 47565.45, 47565.51, 47565.54], DECBAS)
obs_v += __create_trace('F',
[52485.77, 52485.84, 52485.94, 52486.06, 52486.11, 52486.10], DECBAS)
obs_V = StreamConverter.get_obs_from_obs(obs_v, True, True)
d = obs_V.select(channel='D')[0].data
d = ChannelConverter.get_minutes_from_radians(d)
# Using d values calculated using small angle approximation.
assert_almost_equal(d,
[-3.47, -3.43, -3.40, -3.38, -3.35, -3.31], 2,
'Expect d to equal [-3.47, -3.43, -3.40, -3.38, -3.35, -3.31]', True)
mag = obspy.core.Stream()
DECBAS = 552.7
mag += __create_trace('H',
[20884.04, 20883.45, 20883.38, 20883.43, 20883.07, 20882.76], DECBAS)
d = ChannelConverter.get_radians_from_minutes(
[556.51, 556.52, 556.56, 556.61, 556.65, 556.64])
mag += __create_trace('D', d, DECBAS)
mag += __create_trace('Z',
[48546.90, 48546.80, 48546.80, 48546.70, 48546.80, 48546.90], DECBAS)
mag += __create_trace('F',
[0.10, 0.00, 0.10, 0.00, 0.00, 0.00, 0.00], DECBAS)
geo = StreamConverter.get_geo_from_mag(mag)
X = geo.select(channel='X')[0].data
Y = geo.select(channel='Y')[0].data
assert_almost_equal(X,
[20611.00, 20610.40, 20610.30, 20610.30, 20609.90, 20609.60], 2)
assert_almost_equal(Y,
[3366.00, 3366.00, 3366.20, 3366.50, 3366.70, 3366.60], 1)
def __create_trace(channel, data, decbase=0):
"""
Utility to create a trace containing the given numpy array.
Parameters
----------
channel: string
The name of the trace being created.
data: array
The array to be inserted into the trace.
Returns
-------
obspy.core.Stream
Stream containing the channel.
"""
stats = obspy.core.Stats()
stats.starttime = STARTTIME
stats.sampling_rate = 0.0166666666667
stats.declination_base = decbase
stats.channel = channel
numpy_data = numpy.array(data, dtype=numpy.float64)
return obspy.core.Trace(numpy_data, stats)