-
Notifications
You must be signed in to change notification settings - Fork 528
/
test_inventory.py
729 lines (648 loc) · 27.3 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
#!/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
(https://www.gnu.org/copyleft/lesser.html)
"""
import copy
import io
import os
import re
import warnings
from pathlib import Path
from unittest import mock
import pytest
import obspy
from obspy import UTCDateTime, read_inventory, read_events
from obspy.core.util import CARTOPY_VERSION
from obspy.core.util.base import _get_entry_points
from obspy.core.util.testing import WarningsCapture
from obspy.core.inventory import (Channel, Inventory, Network, Response,
Station)
from obspy.core.inventory.util import _unified_content_strings
def sum_stations(inv):
"""
Count the number of stations in inventory.
"""
return sum(len(sta) for net in inv for sta in net)
@pytest.mark.usefixtures('ignore_numpy_errors')
class TestInventory:
"""
Tests the for :class:`~obspy.core.inventory.inventory.Inventory` class.
"""
# TODO put these into fixtures
path = os.path.join(os.path.dirname(__file__), 'data')
station_xml1 = os.path.join(path, 'IU_ANMO_00_BHZ.xml')
station_xml2 = os.path.join(path, 'IU_ULN_00_LH1.xml')
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.
assert inv.created - dt <= 10.0
def test_get_response(self):
response_n1_s1 = Response('RESPN1S1')
response_n1_s2 = Response('RESPN1S2')
response_n2_s1 = Response('RESPN2S1')
channels_n1_s1 = [Channel(code='BHZ',
location_code='',
latitude=0.0,
longitude=0.0,
elevation=0.0,
depth=0.0,
response=response_n1_s1)]
channels_n1_s2 = [Channel(code='BHZ',
location_code='',
latitude=0.0,
longitude=0.0,
elevation=0.0,
depth=0.0,
response=response_n1_s2)]
channels_n2_s1 = [Channel(code='BHZ',
location_code='',
latitude=0.0,
longitude=0.0,
elevation=0.0,
depth=0.0,
response=response_n2_s1)]
stations_1 = [Station(code='N1S1',
latitude=0.0,
longitude=0.0,
elevation=0.0,
channels=channels_n1_s1),
Station(code='N1S2',
latitude=0.0,
longitude=0.0,
elevation=0.0,
channels=channels_n1_s2)]
stations_2 = [Station(code='N2S1',
latitude=0.0,
longitude=0.0,
elevation=0.0,
channels=channels_n2_s1)]
networks = [Network('N1', stations=stations_1),
Network('N2', stations=stations_2)]
inv = Inventory(networks=networks, source='TEST')
response = inv.get_response('N1.N1S1..BHZ',
UTCDateTime('2010-01-01T12:00'))
assert response == response_n1_s1
response = inv.get_response('N1.N1S2..BHZ',
UTCDateTime('2010-01-01T12:00'))
assert response == response_n1_s2
response = inv.get_response('N2.N2S1..BHZ',
UTCDateTime('2010-01-01T12:00'))
assert response == response_n2_s1
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'))
assert sorted(coordinates.items()) == sorted(expected.items())
# 2 - without datetime
coordinates = inv.get_coordinates('BW.RJOB..EHZ')
assert sorted(coordinates.items()) == sorted(expected.items())
# 3 - unknown SEED ID should raise exception
with pytest.raises(Exception):
inv.get_coordinates('BW.RJOB..XXX')
def test_get_orientation(self):
"""
Test extracting orientation
"""
expected = {u'azimuth': 90.0,
u'dip': 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,
azimuth=90.0,
dip=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
orientation = inv.get_orientation('BW.RJOB..EHZ',
UTCDateTime('2010-01-01T12:00'))
assert sorted(orientation.items()) == sorted(expected.items())
# 2 - without datetime
orientation = inv.get_orientation('BW.RJOB..EHZ')
assert sorted(orientation.items()) == sorted(expected.items())
# 3 - unknown SEED ID should raise exception
with pytest.raises(Exception):
inv.get_orientation('BW.RJOB..XXX')
def test_response_plot(self, image_path):
"""
Tests the response plot.
"""
inv = read_inventory()
t = UTCDateTime(2008, 7, 1)
with WarningsCapture():
inv.plot_response(0.01, output="ACC", channel="*N",
station="[WR]*", time=t, outfile=image_path)
def test_response_plot_epoch_times_in_label(self):
"""
Tests response plot with epoch times in labels switched on.
"""
import matplotlib.pyplot as plt
inv = read_inventory().select(station='RJOB', channel='EHZ')
with warnings.catch_warnings(record=True):
warnings.simplefilter("ignore")
fig = inv.plot_response(0.01, label_epoch_dates=True, show=False)
try:
legend = fig.axes[0].get_legend()
texts = legend.get_texts()
expecteds = ['BW.RJOB..EHZ\n2001-05-15 -- 2006-12-12',
'BW.RJOB..EHZ\n2006-12-13 -- 2007-12-17',
'BW.RJOB..EHZ\n2007-12-17 -- open']
assert len(texts) == 3
for text, expected in zip(texts, expecteds):
assert text.get_text() == expected
finally:
plt.close(fig)
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
assert inv_1.source == inv_2.source
assert inv_1.sender == inv_2.sender
assert "ObsPy" in inv_1.module
assert "obspy.org" in inv_1.module_uri
assert (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
assert inv_1.source == "A,B"
assert inv_1.sender == "Random,String"
assert "ObsPy" in inv_1.module
assert "obspy.org" in inv_1.module_uri
assert (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
assert inv_1.source == "A"
assert inv_1.sender == "Random"
assert "ObsPy" in inv_1.module
assert "obspy.org" in inv_1.module_uri
assert (UTCDateTime() - inv_1.created) < 5
def test_len(self):
"""
Tests the __len__ property.
"""
inv = read_inventory()
assert len(inv) == len(inv.networks)
assert len(inv) == 2
def test_inventory_remove(self):
"""
Test for the Inventory.remove() method.
"""
inv = read_inventory()
# Currently contains 30 channels.
assert sum(len(sta) for net in inv for sta in net) == 30
# No arguments, everything should be removed, as `None` values left in
# network/station/location/channel are interpreted as wildcards.
inv_ = inv.remove()
assert len(inv_) == 0
# remove one entire network code
for network in ['GR', 'G?', 'G*', '?R']:
inv_ = inv.remove(network=network)
assert len(inv_) == 1
assert inv_[0].code == 'BW'
assert len(inv_[0]) == 3
for sta in inv_[0]:
assert len(sta) == 3
# remove one specific network/station
for network in ['GR', 'G?', 'G*', '?R']:
for station in ['FUR', 'F*', 'F??', '*R']:
inv_ = inv.remove(network=network, station=station)
assert len(inv_) == 2
assert inv_[0].code == 'GR'
assert len(inv_[0]) == 1
for sta in inv_[0]:
assert len(sta) == 9
assert sta.code == 'WET'
assert inv_[1].code == 'BW'
assert len(inv_[1]) == 3
for sta in inv_[1]:
assert len(sta) == 3
assert sta.code == 'RJOB'
# remove one specific channel
inv_ = inv.remove(channel='*Z')
assert len(inv_) == 2
assert inv_[0].code == 'GR'
assert len(inv_[0]) == 2
assert len(inv_[0][0]) == 8
assert len(inv_[0][1]) == 6
assert inv_[0][0].code == 'FUR'
assert inv_[0][1].code == 'WET'
assert inv_[1].code == 'BW'
assert len(inv_[1]) == 3
for sta in inv_[1]:
assert len(sta) == 2
assert sta.code == 'RJOB'
for net in inv_:
for sta in net:
for cha in sta:
assert cha.code[2] != 'Z'
# check keep_empty kwarg
inv_ = inv.remove(station='R*')
assert len(inv_) == 1
assert inv_[0].code == 'GR'
inv_ = inv.remove(station='R*', keep_empty=True)
assert len(inv_) == 2
assert inv_[0].code == 'GR'
assert inv_[1].code == 'BW'
assert len(inv_[1]) == 0
inv_ = inv.remove(channel='EH*')
assert len(inv_) == 1
assert inv_[0].code == 'GR'
inv_ = inv.remove(channel='EH*', keep_empty=True)
assert len(inv_) == 2
assert inv_[0].code == 'GR'
assert inv_[1].code == 'BW'
assert len(inv_[1]) == 3
for sta in inv_[1]:
assert sta.code == 'RJOB'
assert len(sta) == 0
# some remove calls that don't match anything and should not do
# anything
for kwargs in [dict(network='AA'),
dict(network='AA', station='FUR'),
dict(network='GR', station='ABCD'),
dict(network='GR', channel='EHZ')]:
inv_ = inv.remove(**kwargs)
assert inv_ == inv
def test_issue_2266(self):
"""
Ensure the remove method works for more than just channel level
inventories. See #2266.
"""
# get inventory and remove all channel level info
inv = obspy.read_inventory()
for net in inv:
for sta in net:
sta.channels = []
# filter by one of the networks
inv_net = copy.deepcopy(inv).remove(network='BW')
assert len(inv_net.networks) == 1
# filter by the stations, this should also remove network BW
inv_sta = copy.deepcopy(inv).remove(station='RJOB')
assert len(inv_sta.networks) == 1
assert len(inv_sta.networks[0].stations) == 2
# but is keep empty is selected network BW should remain
inv_sta = copy.deepcopy(inv).remove(station='RJOB', keep_empty=True)
assert len(inv_sta.networks) == 2
def test_inventory_select(self):
"""
Test for the Inventory.select() method.
"""
inv = read_inventory()
# Currently contains 30 channels.
assert sum_stations(inv) == 30
# No arguments, everything should be selected.
assert sum_stations(inv.select()) == 30
# All networks.
assert sum_stations(inv.select(network="*")) == 30
# All stations.
assert sum_stations(inv.select(station="*")) == 30
# All locations.
assert sum_stations(inv.select(location="*")) == 30
# All channels.
assert sum_stations(inv.select(channel="*")) == 30
# Only BW network.
assert sum_stations(inv.select(network="BW")) == 9
assert sum_stations(inv.select(network="B?")) == 9
# Only RJOB Station.
assert sum_stations(inv.select(station="RJOB")) == 9
assert sum_stations(inv.select(station="R?O*")) == 9
out = inv.select(
minlatitude=47.5, maxlatitude=47.9,
minlongitude=11.9, maxlongitude=13.3
)
assert sum_stations(out) == 9
assert sum(len(sta) for net in inv.select(
latitude=48, longitude=13,
maxradius=0.5) for sta in net) == \
9
# Only WET Station.
assert sum(len(sta) for net in inv.select(
latitude=48, longitude=13,
minradius=0.5, maxradius=1.15) for sta in net) == \
9
# Most parameters are just passed to the Network.select() method.
select_kwargs = {
"station": "BW",
"location": "00",
"channel": "EHE",
"keep_empty": True,
"time": UTCDateTime(2001, 1, 1),
"sampling_rate": 123.0,
"starttime": UTCDateTime(2002, 1, 1),
"endtime": UTCDateTime(2003, 1, 1),
"minlatitude": None,
"maxlatitude": None,
"minlongitude": None,
"maxlongitude": None,
"latitude": None,
"longitude": None,
"minradius": None,
"maxradius": None}
with mock.patch("obspy.core.inventory.network.Network.select") as p:
p.return_value = obspy.core.inventory.network.Network("BW")
inv.select(**select_kwargs)
assert p.call_args[1] == select_kwargs
# Artificially set start-and end dates for the first network.
inv[0].start_date = UTCDateTime(2000, 1, 1)
inv[0].end_date = UTCDateTime(2015, 1, 1)
# Nothing will stick around if keep_empty it False.
assert len(inv.select(time=UTCDateTime(2001, 1, 1))) == 0
# If given, both will stick around.
assert len(inv.select(time=UTCDateTime(2001, 1, 1),
keep_empty=True)) == 2
# Or only one.
assert len(inv.select(time=UTCDateTime(1999, 1, 1),
keep_empty=True)) == 1
# Also test the starttime and endtime parameters.
assert len(inv.select(starttime=UTCDateTime(1999, 1, 1),
keep_empty=True)) == 2
assert len(inv.select(starttime=UTCDateTime(2016, 1, 1),
keep_empty=True)) == 1
assert len(inv.select(endtime=UTCDateTime(1999, 1, 1),
keep_empty=True)) == 1
assert len(inv.select(endtime=UTCDateTime(2016, 1, 1),
keep_empty=True)) == 2
def test_inventory_select_with_empty_networks(self):
"""
Tests the behaviour of the Inventory.select() method with empty
Network objects.
"""
inv = read_inventory()
# Empty all networks.
for net in inv:
net.stations = []
assert len(inv) == 2
assert sum(len(net) for net in inv) == 0
# No arguments, everything should be selected.
assert len(inv) == 2
# Same if everything is selected.
assert len(inv.select(network="*")) == 2
# Select only one.
assert len(inv.select(network="BW")) == 1
assert len(inv.select(network="G?")) == 1
# Should only be empty if trying to select something that does not
# exist.
assert len(inv.select(network="RR")) == 0
def test_util_unified_content_string(self):
"""
Tests helper routine that compresses inventory content lists.
"""
contents = (
[u'IU.ULN (Ulaanbaatar, Mongolia)',
u'IU.ULN (Ulaanbaatar, Mongolia)',
u'IU.ULN (Ulaanbaatar, Mongolia)'],
[u'IU.ULN.00.BH1', u'IU.ULN.00.BH2', u'IU.ULN.00.BHE',
u'IU.ULN.00.BHE', u'IU.ULN.00.BHE', u'IU.ULN.00.BHE',
u'IU.ULN.00.BHN', u'IU.ULN.00.BHN', u'IU.ULN.00.BHN',
u'IU.ULN.00.BHN', u'IU.ULN.00.BHZ', u'IU.ULN.00.BHZ',
u'IU.ULN.00.BHZ', u'IU.ULN.00.BHZ', u'IU.ULN.00.BHZ',
u'IU.ULN.00.LH1', u'IU.ULN.00.LH2', u'IU.ULN.00.LHE',
u'IU.ULN.00.LHE', u'IU.ULN.00.LHE', u'IU.ULN.00.LHE',
u'IU.ULN.00.LHN', u'IU.ULN.00.LHN', u'IU.ULN.00.LHN',
u'IU.ULN.00.LHN', u'IU.ULN.00.LHZ', u'IU.ULN.00.LHZ',
u'IU.ULN.00.LHZ', u'IU.ULN.00.LHZ', u'IU.ULN.00.LHZ',
u'IU.ULN.00.UHE', u'IU.ULN.00.UHE', u'IU.ULN.00.UHN',
u'IU.ULN.00.UHN', u'IU.ULN.00.UHZ', u'IU.ULN.00.UHZ',
u'IU.ULN.00.VE1', u'IU.ULN.00.VE1', u'IU.ULN.00.VH1',
u'IU.ULN.00.VH2', u'IU.ULN.00.VHE', u'IU.ULN.00.VHE',
u'IU.ULN.00.VHE', u'IU.ULN.00.VHE', u'IU.ULN.00.VHN',
u'IU.ULN.00.VHN', u'IU.ULN.00.VHN', u'IU.ULN.00.VHN',
u'IU.ULN.00.VHZ', u'IU.ULN.00.VHZ', u'IU.ULN.00.VHZ',
u'IU.ULN.00.VHZ', u'IU.ULN.00.VHZ', u'IU.ULN.00.VK1',
u'IU.ULN.00.VK1', u'IU.ULN.00.VM1', u'IU.ULN.00.VM2',
u'IU.ULN.00.VME', u'IU.ULN.00.VME', u'IU.ULN.00.VMN',
u'IU.ULN.00.VMN', u'IU.ULN.00.VMZ', u'IU.ULN.00.VMZ',
u'IU.ULN.00.VMZ'])
expected = (
[u'IU.ULN (Ulaanbaatar, Mongolia) (3x)'],
[u'IU.ULN.00.BHZ (5x)', u'IU.ULN.00.BHN (4x)',
u'IU.ULN.00.BHE (4x)', u'IU.ULN.00.BH1', u'IU.ULN.00.BH2',
u'IU.ULN.00.LHZ (5x)', u'IU.ULN.00.LHN (4x)',
u'IU.ULN.00.LHE (4x)', u'IU.ULN.00.LH1', u'IU.ULN.00.LH2',
u'IU.ULN.00.UHZ (2x)', u'IU.ULN.00.UHN (2x)',
u'IU.ULN.00.UHE (2x)', u'IU.ULN.00.VE1 (2x)',
u'IU.ULN.00.VHZ (5x)', u'IU.ULN.00.VHN (4x)',
u'IU.ULN.00.VHE (4x)', u'IU.ULN.00.VH1', u'IU.ULN.00.VH2',
u'IU.ULN.00.VK1 (2x)', u'IU.ULN.00.VMZ (3x)',
u'IU.ULN.00.VMN (2x)', u'IU.ULN.00.VME (2x)', u'IU.ULN.00.VM1',
u'IU.ULN.00.VM2'])
for contents_, expected_ in zip(contents, expected):
assert expected_ == _unified_content_strings(contents_)
def test_util_unified_content_string_with_dots_in_description(self):
"""
The unified content string might have dots in the station description.
Make sure it still works.
"""
contents = (
['II.ABKT (Alibek, Turkmenistan)',
'II.ALE (Alert, N.W.T., Canada)'],
[u'IU.ULN (Ulaanbaatar, A.B.C., Mongolia)',
u'IU.ULN (Ulaanbaatar, A.B.C., Mongolia)',
u'IU.ULN (Ulaanbaatar, A.B.C., Mongolia)'],
)
expected = (
['II.ABKT (Alibek, Turkmenistan)',
'II.ALE (Alert, N.W.T., Canada)'],
[u'IU.ULN (Ulaanbaatar, A.B.C., Mongolia) (3x)'],
)
for contents_, expected_ in zip(contents, expected):
assert expected_ == _unified_content_strings(contents_)
def test_read_invalid_filename(self):
"""
Tests that we get a sane error message when calling read_inventory()
with a filename that doesn't exist
"""
doesnt_exist = 'dsfhjkfs'
for i in range(10):
if os.path.exists(doesnt_exist):
doesnt_exist += doesnt_exist
continue
break
else:
self.fail('unable to get invalid file path')
exception_msg = "[Errno 2] No such file or directory: '{}'"
formats = _get_entry_points(
'obspy.plugin.inventory', 'readFormat').keys()
# try read_inventory() with invalid filename for all registered read
# plugins and also for filetype autodiscovery
formats = [None] + list(formats)
expected_error_message = re.escape(exception_msg.format(doesnt_exist))
for format in formats[:1]:
with pytest.raises(IOError, match=expected_error_message):
read_inventory(doesnt_exist, format=format)
def test_inventory_can_be_initialized_with_no_arguments(self):
"""
Source and networks need not be specified.
"""
inv = Inventory()
assert inv.networks == []
assert inv.source == "ObsPy %s" % obspy.__version__
# Should also be serializable.
with io.BytesIO() as buf:
# This actually would not be a valid StationXML file but there
# might be uses for this.
inv.write(buf, format="stationxml")
buf.seek(0, 0)
inv2 = read_inventory(buf)
assert inv == inv2
def test_copy(self):
"""
Test for copying inventory.
"""
inv = read_inventory()
inv2 = inv.copy()
assert inv is not inv2
assert inv == inv2
# make sure changing inv2 doesnt affect inv
original_latitude = inv2[0][0][0].latitude
inv2[0][0][0].latitude = original_latitude + 1
assert inv[0][0][0].latitude == original_latitude
assert inv2[0][0][0].latitude == original_latitude + 1
assert inv[0][0][0].latitude != inv2[0][0][0].latitude
def test_add(self):
"""
Test shallow copies for inventory addition
"""
inv1 = read_inventory()
inv2 = read_inventory()
# __add__ creates two shallow copies
inv_sum = inv1 + inv2
assert {id(net) for net in inv_sum} == \
{id(net) for net in inv1} | {id(net) for net in inv2}
# __iadd__ creates a shallow copy of other and keeps self
ids1 = {id(net) for net in inv1}
inv1 += inv2
assert {id(net) for net in inv1} == ids1 | {id(net) for net in inv2}
# __add__ with a network appends the network to a shallow copy of
# the inventory
net1 = Network('N1')
inv_sum = inv1 + net1
assert {id(net) for net in inv_sum} == \
{id(net) for net in inv1} | {id(net1)}
# __iadd__ with a network appends the network to the inventory
net1 = Network('N1')
ids1 = {id(net) for net in inv1}
inv1 += net1
assert {id(net) for net in inv1} == ids1 | {id(net1)}
def test_extend_metadata(self):
"""
Test that extend merges the metadata of the Inventories
"""
inv1 = Inventory([], source='S1', sender='T1')
inv2 = Inventory([], source='S2', sender='T2')
inv1.extend(inv2)
assert inv1.source == 'S1,S2'
assert inv1.sender == 'T1,T2'
def test_read_inventory_with_wildcard(self):
"""
Tests the read_inventory() function with a filename wild card.
"""
# without wildcard..
expected = read_inventory(self.station_xml1)
expected += read_inventory(self.station_xml2)
# with wildcard
got = read_inventory(os.path.join(self.path, "IU_*_00*.xml"))
assert expected == got
def test_read_inventory_with_path(self):
"""
Tests that pathlib.Path objects works for input to read_inventory().
"""
path1 = Path(self.station_xml1)
inv1 = read_inventory(path1)
assert inv1 == read_inventory(self.station_xml1)
@pytest.mark.usefixtures('ignore_numpy_errors')
@pytest.mark.skipif(not CARTOPY_VERSION, reason='cartopy not installed')
class TestInventoryCartopy:
"""
Tests the for :meth:`~obspy.station.inventory.Inventory.plot` with Cartopy.
"""
image_dir = os.path.join(os.path.dirname(__file__), 'images')
def test_location_plot_global(self, image_path):
"""
Tests the inventory location preview plot, default parameters, using
Cartopy.
"""
inv = read_inventory()
inv.plot(method='cartopy', outfile=image_path)
def test_location_plot_ortho(self, image_path):
"""
Tests the inventory location preview plot, ortho projection, some
non-default parameters, using Cartopy.
"""
inv = read_inventory()
inv.plot(method='cartopy', projection='ortho', resolution='c',
continent_fill_color='0.3', marker='d', label=False,
colormap='Set3', color_per_network=True, outfile=image_path)
def test_location_plot_local(self, image_path):
"""
Tests the inventory location preview plot, local projection, some more
non-default parameters, using Cartopy.
"""
inv = read_inventory()
inv.plot(method='cartopy', projection='local', resolution='50m',
size=20**2, color_per_network={'GR': 'b', 'BW': 'green'},
outfile=image_path)
def test_combined_station_event_plot(self, image_path):
"""
Tests the combined plotting of inventory/event data in one plot,
reusing the cartopy instance.
"""
inv = read_inventory()
cat = read_events()
fig = inv.plot(show=False)
cat.plot(outfile=image_path, fig=fig)