-
Notifications
You must be signed in to change notification settings - Fork 529
/
test_client.py
183 lines (167 loc) · 6.44 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
# -*- coding: utf-8 -*-
"""
The obspy.clients.earthworm.client test suite.
"""
import pytest
from obspy import read
from obspy.core.utcdatetime import UTCDateTime
from obspy.core.util import NamedTemporaryFile
from obspy.core.util.decorator import skip_on_network_error
from obspy.clients.earthworm import Client
pytestmark = pytest.mark.network
class TestEWClient:
"""
Test cases for obspy.clients.earthworm.client.Client.
"""
start = UTCDateTime() - 24 * 3600 * 4
end = start + 1.0
def try_get_stream(self, client, kwargs):
"""Try to get the stream with either '' or '--' location codes. """
# I am not sure why, but sometimes the location code needs to be --
# and other times '' in order to get a stream of nonzero length.
# Just try both.
st = client.get_waveforms(
location='', starttime=self.start, endtime=self.end, **kwargs
)
if len(st) > 0:
return st
st = client.get_waveforms(
location='--', starttime=self.start, endtime=self.end, **kwargs
)
return st
@pytest.fixture(scope='class', autouse=True)
def set_utc_precision(self):
"""Set UTC precision to 4 for this tests class."""
current_precision = UTCDateTime.DEFAULT_PRECISION
UTCDateTime.DEFAULT_PRECISION = 4
yield
UTCDateTime.DEFAULT_PRECISION = current_precision
@pytest.fixture(scope='class')
def ew_client(self):
"""Return the earthworn client."""
ew_client = Client("pubavo1.wr.usgs.gov", 16022, timeout=30.0)
return ew_client
@pytest.fixture(scope='class')
def ew_stream(self, ew_client):
"""Return a stream fetched from the test ew client."""
# example 1 -- 1 channel, cleanup
kwargs = dict(
network='AV',
station='AKV',
channel='BHE',
)
return self.try_get_stream(ew_client, kwargs)
@pytest.fixture(scope='class')
def ew_stream_no_cleanup(self, ew_client):
"""Return a stream fetched from the test ew client with no cleanup."""
kwargs = dict(
network='AV',
station='AKV',
channel='BHE',
cleanup=False,
)
return self.try_get_stream(ew_client, kwargs)
@pytest.fixture(scope='class')
def ew_stream_wildcard(self, ew_client):
"""Return a stream fetched from the test ew client with wildcard."""
kwargs = dict(
network='AV',
station='AKV',
channel='BH?',
)
return self.try_get_stream(ew_client, kwargs)
@skip_on_network_error
def test_get_waveform(self, ew_client, ew_stream):
"""
Tests get_waveforms method.
"""
assert len(ew_stream) == 1
delta = ew_stream[0].stats.delta
trace = ew_stream[0]
assert len(trace) in (50, 51)
assert trace.stats.starttime >= self.start - delta
assert trace.stats.starttime <= self.start + delta
assert trace.stats.endtime >= self.end - delta
assert trace.stats.endtime <= self.end + delta
assert trace.stats.network == 'AV'
assert trace.stats.station == 'AKV'
assert trace.stats.location in ('--', '')
assert trace.stats.channel == 'BHE'
@skip_on_network_error
def test_get_waveform_no_cleanup(self, ew_client, ew_stream_no_cleanup):
"""
Tests get_waveforms method again, 1 channel no cleanup.
"""
# example 2 -- 1 channel, no cleanup
ew_stream = ew_stream_no_cleanup
delta = ew_stream[0].stats.delta
assert len(ew_stream) >= 2
summed_length = sum(len(tr) for tr in ew_stream)
assert summed_length in (50, 51)
assert ew_stream[0].stats.starttime >= self.start - delta
assert ew_stream[0].stats.starttime <= self.start + delta
assert ew_stream[-1].stats.endtime >= self.end - delta
assert ew_stream[-1].stats.endtime <= self.end + delta
for trace in ew_stream:
assert trace.stats.network == 'AV'
assert trace.stats.station == 'AKV'
assert trace.stats.location in ('--', '')
assert trace.stats.channel == 'BHE'
@skip_on_network_error
def test_get_waveform_widlcard(self, ew_client, ew_stream_wildcard):
"""
Test example 3 -- component wildcarded with '?'
"""
# example 3 -- component wildcarded with '?'
stream = ew_stream_wildcard
delta = stream[0].stats.delta
assert len(stream) == 3
for trace in stream:
assert len(trace) in (50, 51)
assert trace.stats.starttime >= self.start - delta
assert trace.stats.starttime <= self.start + delta
assert trace.stats.endtime >= self.end - delta
assert trace.stats.endtime <= self.end + delta
assert trace.stats.network == 'AV'
assert trace.stats.station == 'AKV'
assert trace.stats.location in ('--', '')
assert stream[0].stats.channel == 'BHZ'
assert stream[1].stats.channel == 'BHN'
assert stream[2].stats.channel == 'BHE'
@skip_on_network_error
def test_save_waveform(self, ew_client):
"""
Tests save_waveforms method.
"""
# initialize client
with NamedTemporaryFile() as tf:
testfile = tf.name
# 1 channel, cleanup (using SLIST to avoid dependencies)
ew_client.save_waveforms(
testfile,
'AV',
'AKV',
'--',
'BHE',
self.start,
self.end,
format="SLIST",
)
stream = read(testfile)
assert len(stream) == 1
delta = stream[0].stats.delta
trace = stream[0]
assert len(trace) == 51
assert trace.stats.starttime >= self.start - delta
assert trace.stats.starttime <= self.start + delta
assert trace.stats.endtime >= self.end - delta
assert trace.stats.endtime <= self.end + delta
assert trace.stats.network == 'AV'
assert trace.stats.station == 'AKV'
assert trace.stats.location in ('--', '')
assert trace.stats.channel == 'BHE'
@skip_on_network_error
def test_availability(self, ew_client):
data = ew_client.get_availability()
seeds = ["%s.%s.%s.%s" % (d[0], d[1], d[2], d[3]) for d in data]
assert 'AV.AKV.--.BHZ' in seeds