/
test_inventory.py
280 lines (244 loc) · 10.2 KB
/
test_inventory.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Test suite for the inventory class.
:copyright:
Lion Krischer (krischer@geophysik.uni-muenchen.de), 2013
:license:
GNU Lesser General Public License, Version 3
(http://www.gnu.org/copyleft/lesser.html)
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from future.builtins import * # NOQA
import os
import unittest
import warnings
import numpy as np
from obspy import UTCDateTime, read_inventory
from obspy.core.util.base import getBasemapVersion
from obspy.core.util.decorator import skipIf
from obspy.core.util.testing import ImageComparison, getMatplotlibVersion
from obspy.station import Channel, Inventory, Network, Response, Station
# checking for matplotlib/basemap
try:
from matplotlib import rcParams
import mpl_toolkits.basemap
# avoid flake8 complaining about unused import
mpl_toolkits.basemap
HAS_BASEMAP = True
except ImportError:
HAS_BASEMAP = False
MATPLOTLIB_VERSION = getMatplotlibVersion()
BASEMAP_VERSION = getBasemapVersion()
class InventoryTestCase(unittest.TestCase):
"""
Tests the for :class:`~obspy.station.inventory.Inventory` class.
"""
def setUp(self):
self.image_dir = os.path.join(os.path.dirname(__file__), 'images')
self.nperr = np.geterr()
np.seterr(all='ignore')
def tearDown(self):
np.seterr(**self.nperr)
def test_initialization(self):
"""
Some simple sanity tests.
"""
dt = UTCDateTime()
inv = Inventory(source="TEST", networks=[])
# If no time is given, the creation time should be set to the current
# time. Use a large offset for potentially slow computers and test
# runs.
self.assertTrue(inv.created - dt <= 10.0)
def test_get_response(self):
responseN1S1 = Response('RESPN1S1')
responseN1S2 = Response('RESPN1S2')
responseN2S1 = Response('RESPN2S1')
channelsN1S1 = [Channel(code='BHZ',
location_code='',
latitude=0.0,
longitude=0.0,
elevation=0.0,
depth=0.0,
response=responseN1S1)]
channelsN1S2 = [Channel(code='BHZ',
location_code='',
latitude=0.0,
longitude=0.0,
elevation=0.0,
depth=0.0,
response=responseN1S2)]
channelsN2S1 = [Channel(code='BHZ',
location_code='',
latitude=0.0,
longitude=0.0,
elevation=0.0,
depth=0.0,
response=responseN2S1)]
stations1 = [Station(code='N1S1',
latitude=0.0,
longitude=0.0,
elevation=0.0,
channels=channelsN1S1),
Station(code='N1S2',
latitude=0.0,
longitude=0.0,
elevation=0.0,
channels=channelsN1S2)]
stations2 = [Station(code='N2S1',
latitude=0.0,
longitude=0.0,
elevation=0.0,
channels=channelsN2S1)]
networks = [Network('N1', stations=stations1),
Network('N2', stations=stations2)]
inv = Inventory(networks=networks, source='TEST')
response = inv.get_response('N1.N1S1..BHZ',
UTCDateTime('2010-01-01T12:00'))
self.assertEqual(response, responseN1S1)
response = inv.get_response('N1.N1S2..BHZ',
UTCDateTime('2010-01-01T12:00'))
self.assertEqual(response, responseN1S2)
response = inv.get_response('N2.N2S1..BHZ',
UTCDateTime('2010-01-01T12:00'))
self.assertEqual(response, responseN2S1)
def test_get_coordinates(self):
"""
Test extracting coordinates
"""
expected = {u'latitude': 47.737166999999999,
u'longitude': 12.795714,
u'elevation': 860.0,
u'local_depth': 0.0}
channels = [Channel(code='EHZ',
location_code='',
start_date=UTCDateTime('2007-01-01'),
latitude=47.737166999999999,
longitude=12.795714,
elevation=860.0,
depth=0.0)]
stations = [Station(code='RJOB',
latitude=0.0,
longitude=0.0,
elevation=0.0,
channels=channels)]
networks = [Network('BW', stations=stations)]
inv = Inventory(networks=networks, source='TEST')
# 1
coordinates = inv.get_coordinates('BW.RJOB..EHZ',
UTCDateTime('2010-01-01T12:00'))
self.assertEqual(sorted(coordinates.items()), sorted(expected.items()))
# 2 - without datetime
coordinates = inv.get_coordinates('BW.RJOB..EHZ')
self.assertEqual(sorted(coordinates.items()), sorted(expected.items()))
# 3 - unknown SEED ID should raise exception
self.assertRaises(Exception, inv.get_coordinates, 'BW.RJOB..XXX')
@skipIf(not HAS_BASEMAP, 'basemap not installed')
def test_location_plot_global(self):
"""
Tests the inventory location preview plot, default parameters.
"""
inv = read_inventory()
reltol = 1.0
# Coordinate lines might be slightly off, depending on the basemap
# version.
if BASEMAP_VERSION < [1, 0, 7]:
reltol = 3.0
with ImageComparison(self.image_dir, "inventory_location1.png",
reltol=reltol) as ic:
rcParams['savefig.dpi'] = 72
inv.plot(outfile=ic.name)
@skipIf(not HAS_BASEMAP, 'basemap not installed')
def test_location_plot_ortho(self):
"""
Tests the inventory location preview plot, ortho projection, some
non-default parameters.
"""
inv = read_inventory()
with ImageComparison(self.image_dir, "inventory_location2.png") as ic:
rcParams['savefig.dpi'] = 72
inv.plot(projection="ortho", resolution="c",
continent_fill_color="0.3", marker="d",
label=False, outfile=ic.name, colormap="hsv",
color_per_network=True)
@skipIf(not HAS_BASEMAP, 'basemap not installed')
def test_location_plot_local(self):
"""
Tests the inventory location preview plot, local projection, some more
non-default parameters.
"""
inv = read_inventory()
# Coordinate lines might be slightly off, depending on the basemap
# version.
reltol = 2.0
# Basemap smaller 1.0.4 has a serious issue with plotting. Thus the
# tolerance must be much higher.
if BASEMAP_VERSION < [1, 0, 4]:
reltol = 100.0
with ImageComparison(self.image_dir, "inventory_location3.png",
reltol=reltol) as ic:
rcParams['savefig.dpi'] = 72
inv.plot(projection="local", resolution="i", size=20**2,
color_per_network={"GR": "b", "BW": "green"},
outfile=ic.name)
def test_response_plot(self):
"""
Tests the response plot.
"""
# Bug in matplotlib 1.4.0 - 1.4.x:
# See https://github.com/matplotlib/matplotlib/issues/4012
reltol = 1.0
if [1, 4, 0] <= MATPLOTLIB_VERSION <= [1, 5, 0]:
reltol = 2.0
inv = read_inventory()
t = UTCDateTime(2008, 7, 1)
with warnings.catch_warnings(record=True):
warnings.simplefilter("ignore")
with ImageComparison(self.image_dir, "inventory_response.png",
reltol=reltol) as ic:
rcParams['savefig.dpi'] = 72
inv.plot_response(0.01, output="ACC", channel="*N",
station="[WR]*", time=t, outfile=ic.name)
def test_inventory_merging_metadata_update(self):
"""
Tests the metadata update during merging of inventory objects.
"""
inv_1 = read_inventory()
inv_2 = read_inventory()
inv_1 += inv_2
self.assertEqual(inv_1.source, inv_2.source)
self.assertEqual(inv_1.sender, inv_2.sender)
self.assertTrue("ObsPy" in inv_1.module)
self.assertTrue("obspy.org" in inv_1.module_uri)
self.assertTrue((UTCDateTime() - inv_1.created) < 5)
# Now a more advanced case.
inv_1 = read_inventory()
inv_2 = read_inventory()
inv_1.source = "B"
inv_2.source = "A"
inv_1.sender = "Random"
inv_2.sender = "String"
inv_1 += inv_2
self.assertEqual(inv_1.source, "A,B")
self.assertEqual(inv_1.sender, "Random,String")
self.assertTrue("ObsPy" in inv_1.module)
self.assertTrue("obspy.org" in inv_1.module_uri)
self.assertTrue((UTCDateTime() - inv_1.created) < 5)
# One more. Containing a couple of Nones.
inv_1 = read_inventory()
inv_2 = read_inventory()
inv_1.source = None
inv_2.source = "A"
inv_1.sender = "Random"
inv_2.sender = None
inv_1 += inv_2
self.assertEqual(inv_1.source, "A")
self.assertEqual(inv_1.sender, "Random")
self.assertTrue("ObsPy" in inv_1.module)
self.assertTrue("obspy.org" in inv_1.module_uri)
self.assertTrue((UTCDateTime() - inv_1.created) < 5)
def suite():
return unittest.makeSuite(InventoryTestCase, 'test')
if __name__ == '__main__':
unittest.main(defaultTest='suite')