/
test_channel.py
180 lines (159 loc) · 6.44 KB
/
test_channel.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Test suite for the channel handling.
:copyright:
Lion Krischer (krischer@geophysik.uni-muenchen.de), 2013
:license:
GNU Lesser General Public License, Version 3
(https://www.gnu.org/copyleft/lesser.html)
"""
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from future.builtins import * # NOQA
import inspect
import os
import unittest
import warnings
import numpy as np
from matplotlib import rcParams
from obspy.core.util import MATPLOTLIB_VERSION
from obspy.core.util.testing import ImageComparison
from obspy import read_inventory
from obspy.core.inventory import Channel, Equipment
class ChannelTestCase(unittest.TestCase):
"""
Tests the for :class:`~obspy.core.inventory.channel.Channel` class.
"""
def setUp(self):
# Most generic way to get the actual data directory.
self.data_dir = os.path.join(os.path.dirname(os.path.abspath(
inspect.getfile(inspect.currentframe()))), "data")
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_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
cha = read_inventory()[0][0][0]
with warnings.catch_warnings(record=True):
warnings.simplefilter("ignore")
with ImageComparison(self.image_dir, "channel_response.png",
reltol=reltol) as ic:
rcParams['savefig.dpi'] = 72
cha.plot(0.005, outfile=ic.name)
def test_channel_str(self):
"""
Tests the __str__ method of the channel object.
"""
c = Channel(code="BHE", location_code="10", latitude=1, longitude=2,
elevation=3, depth=4, azimuth=5, dip=6)
assert str(c) == (
"Channel 'BHE', Location '10' \n"
"\tTime range: -- - --\n"
"\tLatitude: 1.00, Longitude: 2.00, Elevation: 3.0 m, "
"Local Depth: 4.0 m\n"
"\tAzimuth: 5.00 degrees from north, clockwise\n"
"\tDip: 6.00 degrees down from horizontal\n")
# Adding channel types.
c.types = ["A", "B"]
assert str(c) == (
"Channel 'BHE', Location '10' \n"
"\tTime range: -- - --\n"
"\tLatitude: 1.00, Longitude: 2.00, Elevation: 3.0 m, "
"Local Depth: 4.0 m\n"
"\tAzimuth: 5.00 degrees from north, clockwise\n"
"\tDip: 6.00 degrees down from horizontal\n"
"\tChannel types: A, B\n")
# Adding channel types.
c.sample_rate = 10.0
assert str(c) == (
"Channel 'BHE', Location '10' \n"
"\tTime range: -- - --\n"
"\tLatitude: 1.00, Longitude: 2.00, Elevation: 3.0 m, "
"Local Depth: 4.0 m\n"
"\tAzimuth: 5.00 degrees from north, clockwise\n"
"\tDip: 6.00 degrees down from horizontal\n"
"\tChannel types: A, B\n"
"\tSampling Rate: 10.00 Hz\n")
# "Adding" response
c.response = True
assert str(c) == (
"Channel 'BHE', Location '10' \n"
"\tTime range: -- - --\n"
"\tLatitude: 1.00, Longitude: 2.00, Elevation: 3.0 m, "
"Local Depth: 4.0 m\n"
"\tAzimuth: 5.00 degrees from north, clockwise\n"
"\tDip: 6.00 degrees down from horizontal\n"
"\tChannel types: A, B\n"
"\tSampling Rate: 10.00 Hz\n"
"\tResponse information available"
)
# Adding an empty sensor.
c.sensor = Equipment(type=None)
assert str(c) == (
"Channel 'BHE', Location '10' \n"
"\tTime range: -- - --\n"
"\tLatitude: 1.00, Longitude: 2.00, Elevation: 3.0 m, "
"Local Depth: 4.0 m\n"
"\tAzimuth: 5.00 degrees from north, clockwise\n"
"\tDip: 6.00 degrees down from horizontal\n"
"\tChannel types: A, B\n"
"\tSampling Rate: 10.00 Hz\n"
"\tSensor (Description): None (None)\n"
"\tResponse information available"
)
# Adding a sensor with only a type.
c.sensor = Equipment(type="random")
assert str(c) == (
"Channel 'BHE', Location '10' \n"
"\tTime range: -- - --\n"
"\tLatitude: 1.00, Longitude: 2.00, Elevation: 3.0 m, "
"Local Depth: 4.0 m\n"
"\tAzimuth: 5.00 degrees from north, clockwise\n"
"\tDip: 6.00 degrees down from horizontal\n"
"\tChannel types: A, B\n"
"\tSampling Rate: 10.00 Hz\n"
"\tSensor (Description): random (None)\n"
"\tResponse information available"
)
# Adding a sensor with only a description
c.sensor = Equipment(description="some description")
assert str(c) == (
"Channel 'BHE', Location '10' \n"
"\tTime range: -- - --\n"
"\tLatitude: 1.00, Longitude: 2.00, Elevation: 3.0 m, "
"Local Depth: 4.0 m\n"
"\tAzimuth: 5.00 degrees from north, clockwise\n"
"\tDip: 6.00 degrees down from horizontal\n"
"\tChannel types: A, B\n"
"\tSampling Rate: 10.00 Hz\n"
"\tSensor (Description): None (some description)\n"
"\tResponse information available"
)
# Adding a sensor with type and description
c.sensor = Equipment(type="random", description="some description")
assert str(c) == (
"Channel 'BHE', Location '10' \n"
"\tTime range: -- - --\n"
"\tLatitude: 1.00, Longitude: 2.00, Elevation: 3.0 m, "
"Local Depth: 4.0 m\n"
"\tAzimuth: 5.00 degrees from north, clockwise\n"
"\tDip: 6.00 degrees down from horizontal\n"
"\tChannel types: A, B\n"
"\tSampling Rate: 10.00 Hz\n"
"\tSensor (Description): random (some description)\n"
"\tResponse information available"
)
def suite():
return unittest.makeSuite(ChannelTestCase, 'test')
if __name__ == '__main__':
unittest.main(defaultTest='suite')