/
test_taup_plotting.py
215 lines (195 loc) · 8.35 KB
/
test_taup_plotting.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Tests the high level obspy.taup.tau interface.
"""
import warnings
import matplotlib as mpl
import matplotlib.pyplot as plt
import pytest
from obspy.core.util.testing import WarningsCapture
from obspy.core.util.deprecation_helpers import ObsPyDeprecationWarning
from obspy.taup import TauPyModel, plot_travel_times
class TestTauPyPlotting:
"""
TauPy plotting tests.
"""
@pytest.fixture(scope='class')
def model(self):
"""return a model for testing."""
return TauPyModel(model="iasp91")
@staticmethod
def _test_plot_all(plot_all):
"""Helper function for plotting all phases."""
mpl.rcParams['lines.markersize'] = 200
plot_travel_times(10, phase_list=("SSS",), min_degrees=150,
max_degrees=200, npoints=4, legend=False,
plot_all=plot_all)
def test_spherical_many_phases(self, model, image_path):
"""
Spherical plot of the ray paths for many phases for a single
epicentral distance, but both ways around the globe.
"""
rays = model.get_ray_paths(500, 140)
rays.plot_rays(plot_type="spherical", plot_all=True, show=False)
plt.savefig(image_path)
def test_spherical_many_phases_buried_station(self, model, image_path):
"""
Same as test_spherical_many_phases, but this time the receiver is
buried.
"""
arrs = model.get_ray_paths(500, 140, receiver_depth_in_km=1000)
arrs.plot_rays(plot_type="spherical", plot_all=True, show=False)
plt.savefig(image_path)
def test_spherical_many_phases_one_way(self, model, image_path):
"""
Same as test_spherical_many_phases, but this time no phases
travelling the other way around the globe are plotted.
"""
rays = model.get_ray_paths(500, 140)
rays.plot_rays(plot_type="spherical", plot_all=False, show=False)
plt.savefig(image_path)
def test_spherical_more_then_360_degrees(self, model, image_path):
"""
Spherical plot with rays traveling more than 360.0 degrees.
"""
model.get_ray_paths(0, 10, phase_list=["PPPPPP"]).plot_rays(
plot_type="spherical", plot_all=True, show=False,
phase_list=['PPPPPP'])
plt.savefig(image_path)
def test_spherical_diff_phases(self, model, image_path):
"""
Spherical plot of ``diff`` phases.
"""
model.get_ray_paths(
700, 140, phase_list=["Pdiff", "Sdiff", "pSdiff", "sSdiff",
"pPdiff", "sPdiff"]).plot_rays(
phase_list=["Pdiff", "Sdiff",
"pSdiff", "sSdiff",
"pPdiff", "sPdiff"],
plot_type="spherical", legend=True,
plot_all=True, show=False)
plt.savefig(image_path)
def test_cartesian_many_phases(self, model, image_path):
"""
Cartesian plot of the ray paths for many phases for a single
epicentral distance, but both ways around the globe.
"""
model.get_ray_paths(500, 140).plot_rays(plot_type="cartesian",
plot_all=True,
show=False)
plt.savefig(image_path)
def test_cartesian_many_phases_buried_station(self, model, image_path):
"""
Same as test_cartesian_many_phases but this time the receiver is
buried.
"""
arrivals = model.get_ray_paths(500, 140, receiver_depth_in_km=1000)
arrivals.plot_rays(plot_type="cartesian", plot_all=True,
show=False)
plt.savefig(image_path)
def test_cartesian_many_phases_one_way(self, model, image_path):
"""
Same as test_cartesian_many_phases but this time no phases
travelling the other way around the globe are plotted.
"""
arrivals = model.get_ray_paths(500, 140)
arrivals.plot_rays(plot_type="cartesian", plot_all=False,
show=False)
plt.savefig(image_path)
def test_cartesian_many_phases_one_way_depr(self, model, image_path):
"""
Same as above but checking deprecation warning.
"""
arrivals = model.get_ray_paths(500, 140)
# check warning message on deprecated routine
with WarningsCapture() as w:
arrivals.plot(plot_type="cartesian", plot_all=False,
show=False, legend=False)
plt.savefig(image_path)
assert len(w) >= 1
for w_ in w:
try:
assert str(w_.message) == 'The plot() function is ' \
'deprecated. Please use arrivals.plot_rays()'
assert w_.category == ObsPyDeprecationWarning
except AssertionError:
continue
break
else:
raise
def test_plot_travel_times(self, model, image_path):
"""
Travel time plot for many phases at a single epicentral distance.
"""
ray_paths = model.get_ray_paths(10, 100, phase_list=['ttbasic', ])
ray_paths.plot_times(show=False, legend=True)
plt.savefig(image_path)
def test_plot_travel_times_convenience_1(self, model, image_path):
"""
Travel time plot for many phases at multiple epicentral distances,
convenience function
"""
plot_travel_times(10, phase_list=("P", "S", "SKS", "PP"),
min_degrees=40, max_degrees=60, show=False,
legend=True, npoints=4)
plt.savefig(image_path)
def test_plot_travel_times_convenience_2(self, model, image_path):
"""
Same as above but with plot_all == False
"""
self._test_plot_all(plot_all=False)
plt.savefig(image_path)
def test_plot_travel_times_convenience_3(self, model, image_path):
"""
Now with plot_all == True
"""
self._test_plot_all(plot_all=True)
plt.savefig(image_path)
def test_ray_plot_mismatching_axes_type_warnings(self, model):
"""
Test warnings when attempting ray path plots in spherical/cartesian
with bad axes type (polar/not polar).
"""
arrivals = model.get_ray_paths(500, 20, phase_list=['P'])
# polar pot attempted in cartesian axes
fig, ax = plt.subplots()
expected_message = ("Axes instance provided for plotting with "
"`plot_type='spherical'` but it seems the axes is "
"not a polar axes.")
try:
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
# this raises an exception as well:
# "AttributeError: 'AxesSubplot' object has no attribute "
# "'set_theta_zero_location'"
with pytest.raises(AttributeError):
arrivals.plot_rays(plot_type="spherical", ax=ax,
show=False)
assert len(w) == 1
assert str(w[0].message) == expected_message
assert w[0].category == UserWarning
finally:
plt.close(fig)
# cartesian pot attempted in polar axes
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
expected_message = ("Axes instance provided for plotting with "
"`plot_type='cartesian'` but it seems the axes is "
"a polar axes.")
try:
with WarningsCapture() as w:
arrivals.plot_rays(plot_type="cartesian", ax=ax, show=False)
assert len(w) == 1
assert str(w[0].message) == expected_message
assert issubclass(w[0].category, Warning)
finally:
plt.close(fig)
def test_invalid_plot_option(self, model):
"""
Test error message when attempting ray path plots with invalid plot
type
"""
arrivals = model.get_ray_paths(500, 20, phase_list=['P'])
# polar plot attempted in cartesian axes
with pytest.raises(ValueError):
arrivals.plot_rays(plot_type="spam")