/
test_client.py
269 lines (251 loc) · 11.3 KB
/
test_client.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
# -*- coding: utf-8 -*-
"""
The obspy.clients.iris.client test suite.
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from future.builtins import * # NOQA @UnusedWildImport
import os
import unittest
import numpy as np
from obspy.core.utcdatetime import UTCDateTime
from obspy.core.util import NamedTemporaryFile
from obspy.clients.iris import Client
class ClientTestCase(unittest.TestCase):
"""
Test cases for obspy.clients.iris.client.Client.
"""
def setUp(self):
# directory where the test files are located
self.path = os.path.dirname(__file__)
def test_sacpz(self):
"""
Fetches SAC poles and zeros information.
"""
client = Client()
# 1
t1 = UTCDateTime("2005-01-01")
t2 = UTCDateTime("2008-01-01")
result = client.sacpz("IU", "ANMO", "00", "BHZ", t1, t2)
# drop lines with creation date (current time during request)
result = result.splitlines()
sacpz_file = os.path.join(self.path, 'data', 'IU.ANMO.00.BHZ.sacpz')
with open(sacpz_file, 'rb') as fp:
expected = fp.read().splitlines()
result.pop(5)
expected.pop(5)
self.assertEqual(result, expected)
# 2 - empty location code
dt = UTCDateTime("2002-11-01")
result = client.sacpz('UW', 'LON', '', 'BHZ', dt)
self.assertIn(b"* STATION (KSTNM): LON", result)
self.assertIn(b"* LOCATION (KHOLE): ", result)
# 3 - empty location code via '--'
result = client.sacpz('UW', 'LON', '--', 'BHZ', dt)
self.assertIn(b"* STATION (KSTNM): LON", result)
self.assertIn(b"* LOCATION (KHOLE): ", result)
def test_distaz(self):
"""
Tests distance and azimuth calculation between two points on a sphere.
"""
client = Client()
# normal request
result = client.distaz(stalat=1.1, stalon=1.2, evtlat=3.2, evtlon=1.4)
self.assertAlmostEqual(result['distance'], 2.10256)
self.assertAlmostEqual(result['distancemeters'], 233272.79028)
self.assertAlmostEqual(result['backazimuth'], 5.46944)
self.assertAlmostEqual(result['azimuth'], 185.47695)
self.assertEqual(result['ellipsoidname'], 'WGS84')
# w/o kwargs
result = client.distaz(1.1, 1.2, 3.2, 1.4)
self.assertAlmostEqual(result['distance'], 2.10256)
self.assertAlmostEqual(result['distancemeters'], 233272.79028)
self.assertAlmostEqual(result['backazimuth'], 5.46944)
self.assertAlmostEqual(result['azimuth'], 185.47695)
self.assertEqual(result['ellipsoidname'], 'WGS84')
# missing parameters
self.assertRaises(Exception, client.distaz, stalat=1.1)
self.assertRaises(Exception, client.distaz, 1.1)
self.assertRaises(Exception, client.distaz, stalat=1.1, stalon=1.2)
self.assertRaises(Exception, client.distaz, 1.1, 1.2)
def test_flinnengdahl(self):
"""
Tests calculation of Flinn-Engdahl region code or name.
"""
client = Client()
# code
result = client.flinnengdahl(lat=-20.5, lon=-100.6, rtype="code")
self.assertEqual(result, 683)
# w/o kwargs
result = client.flinnengdahl(-20.5, -100.6, "code")
self.assertEqual(result, 683)
# region
result = client.flinnengdahl(lat=42, lon=-122.24, rtype="region")
self.assertEqual(result, 'OREGON')
# w/o kwargs
result = client.flinnengdahl(42, -122.24, "region")
self.assertEqual(result, 'OREGON')
# both
result = client.flinnengdahl(lat=-20.5, lon=-100.6, rtype="both")
self.assertEqual(result, (683, 'SOUTHEAST CENTRAL PACIFIC OCEAN'))
# w/o kwargs
result = client.flinnengdahl(-20.5, -100.6, "both")
self.assertEqual(result, (683, 'SOUTHEAST CENTRAL PACIFIC OCEAN'))
# default rtype
result = client.flinnengdahl(lat=42, lon=-122.24)
self.assertEqual(result, (32, 'OREGON'))
# w/o kwargs
# outside boundaries
self.assertRaises(Exception, client.flinnengdahl, lat=-90.1, lon=0)
self.assertRaises(Exception, client.flinnengdahl, lat=90.1, lon=0)
self.assertRaises(Exception, client.flinnengdahl, lat=0, lon=-180.1)
self.assertRaises(Exception, client.flinnengdahl, lat=0, lon=180.1)
def test_traveltime(self):
"""
Tests calculation of travel-times for seismic phases.
"""
client = Client()
result = client.traveltime(
evloc=(-36.122, -72.898), evdepth=22.9,
staloc=[(-33.45, -70.67), (47.61, -122.33), (35.69, 139.69)])
self.assertTrue(result.startswith(b'Model: iasp91'))
def test_evalresp(self):
"""
Tests evaluating instrument response information.
"""
client = Client()
dt = UTCDateTime("2005-01-01")
# plot as PNG file
with NamedTemporaryFile() as tf:
tempfile = tf.name
client.evalresp(network="IU", station="ANMO", location="00",
channel="BHZ", time=dt, output='plot',
filename=tempfile)
with open(tempfile, 'rb') as fp:
self.assertEqual(fp.read(4)[1:4], b'PNG')
# plot-amp as PNG file
with NamedTemporaryFile() as tf:
tempfile = tf.name
client.evalresp(network="IU", station="ANMO", location="00",
channel="BHZ", time=dt, output='plot-amp',
filename=tempfile)
with open(tempfile, 'rb') as fp:
self.assertEqual(fp.read(4)[1:4], b'PNG')
# plot-phase as PNG file
with NamedTemporaryFile() as tf:
tempfile = tf.name
client.evalresp(network="IU", station="ANMO", location="00",
channel="BHZ", time=dt, output='plot-phase',
filename=tempfile)
with open(tempfile, 'rb') as fp:
self.assertEqual(fp.read(4)[1:4], b'PNG')
# fap as ASCII file
with NamedTemporaryFile() as tf:
tempfile = tf.name
client.evalresp(network="IU", station="ANMO", location="00",
channel="BHZ", time=dt, output='fap',
filename=tempfile)
with open(tempfile, 'rt') as fp:
self.assertEqual(fp.readline(),
'1.000000E-05 1.055999E+04 1.792007E+02\n')
# cs as ASCII file
with NamedTemporaryFile() as tf:
tempfile = tf.name
client.evalresp(network="IU", station="ANMO", location="00",
channel="BHZ", time=dt, output='cs',
filename=tempfile)
with open(tempfile, 'rt') as fp:
self.assertEqual(fp.readline(),
'1.000000E-05 -1.055896E+04 1.473054E+02\n')
# fap & def as ASCII file
with NamedTemporaryFile() as tf:
tempfile = tf.name
client.evalresp(network="IU", station="ANMO", location="00",
channel="BHZ", time=dt, output='fap', units='def',
filename=tempfile)
with open(tempfile, 'rt') as fp:
self.assertEqual(fp.readline(),
'1.000000E-05 1.055999E+04 1.792007E+02\n')
# fap & dis as ASCII file
with NamedTemporaryFile() as tf:
tempfile = tf.name
client.evalresp(network="IU", station="ANMO", location="00",
channel="BHZ", time=dt, output='fap', units='dis',
filename=tempfile)
with open(tempfile, 'rt') as fp:
self.assertEqual(fp.readline(),
'1.000000E-05 6.635035E-01 2.692007E+02\n')
# fap & vel as ASCII file
with NamedTemporaryFile() as tf:
tempfile = tf.name
client.evalresp(network="IU", station="ANMO", location="00",
channel="BHZ", time=dt, output='fap', units='vel',
filename=tempfile)
with open(tempfile, 'rt') as fp:
self.assertEqual(fp.readline(),
'1.000000E-05 1.055999E+04 1.792007E+02\n')
# fap & acc as ASCII file
with NamedTemporaryFile() as tf:
tempfile = tf.name
client.evalresp(network="IU", station="ANMO", location="00",
channel="BHZ", time=dt, output='fap', units='acc',
filename=tempfile)
with open(tempfile, 'rt') as fp:
self.assertEqual(fp.readline(),
'1.000000E-05 1.680674E+08 8.920073E+01\n')
# fap as NumPy ndarray
data = client.evalresp(network="IU", station="ANMO", location="00",
channel="BHZ", time=dt, output='fap')
np.testing.assert_array_equal(
data[0], [1.00000000e-05, 1.05599900e+04, 1.79200700e+02])
# cs as NumPy ndarray
data = client.evalresp(network="IU", station="ANMO", location="00",
channel="BHZ", time=dt, output='cs')
np.testing.assert_array_equal(
data[0], [1.00000000e-05, -1.05589600e+04, 1.47305400e+02])
def test_resp(self):
"""
Tests resp Web service interface.
Examples are inspired by https://service.iris.edu/irisws/resp/1/.
"""
client = Client()
# 1
t1 = UTCDateTime("2005-001T00:00:00")
t2 = UTCDateTime("2008-001T00:00:00")
result = client.resp("IU", "ANMO", "00", "BHZ", t1, t2)
self.assertIn(b'B050F03 Station: ANMO', result)
# 2 - empty location code
result = client.resp("UW", "LON", "", "EHZ")
self.assertIn(b'B050F03 Station: LON', result)
self.assertIn(b'B052F03 Location: ??', result)
# 3 - empty location code via '--'
result = client.resp("UW", "LON", "--", "EHZ")
self.assertIn(b'B050F03 Station: LON', result)
self.assertIn(b'B052F03 Location: ??', result)
# 4
dt = UTCDateTime("2010-02-27T06:30:00.000")
result = client.resp("IU", "ANMO", "*", "*", dt)
self.assertIn(b'B050F03 Station: ANMO', result)
def test_timeseries(self):
"""
Tests timeseries Web service interface.
Examples are inspired by https://service.iris.edu/irisws/timeseries/1/.
"""
client = Client()
# 1
t1 = UTCDateTime("2005-001T00:00:00")
t2 = UTCDateTime("2005-001T00:01:00")
# no filter
st1 = client.timeseries("IU", "ANMO", "00", "BHZ", t1, t2)
# instrument corrected
st2 = client.timeseries("IU", "ANMO", "00", "BHZ", t1, t2,
filter=["correct"])
# compare results
self.assertEqual(st1[0].stats.starttime, st2[0].stats.starttime)
self.assertEqual(st1[0].stats.endtime, st2[0].stats.endtime)
self.assertEqual(st1[0].data[0], 24)
self.assertAlmostEqual(st2[0].data[0], -2.8373747e-06)
def suite():
return unittest.makeSuite(ClientTestCase, 'test')
if __name__ == '__main__':
unittest.main(defaultTest='suite')