|
1
|
|
|
# Copyright (c) 2008-2015 MetPy Developers. |
|
2
|
|
|
# Distributed under the terms of the BSD 3-Clause License. |
|
3
|
|
|
# SPDX-License-Identifier: BSD-3-Clause |
|
4
|
|
|
"""Tests for the `skewt` module.""" |
|
5
|
|
|
|
|
6
|
|
|
import matplotlib |
|
7
|
|
|
from matplotlib.gridspec import GridSpec |
|
8
|
|
|
import matplotlib.pyplot as plt |
|
9
|
|
|
import numpy as np |
|
10
|
|
|
import pytest |
|
11
|
|
|
|
|
12
|
|
|
from metpy.plots import Hodograph, SkewT |
|
13
|
|
|
# Fixtures to make sure we have the right backend and consistent round |
|
14
|
|
|
from metpy.testing import patch_round, set_agg_backend # noqa: F401 |
|
15
|
|
|
from metpy.units import units |
|
16
|
|
|
|
|
17
|
|
|
MPL_VERSION = matplotlib.__version__[:3] |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
@pytest.mark.mpl_image_compare(tolerance=0.021, remove_text=True) |
|
21
|
|
|
def test_skewt_api(): |
|
22
|
|
|
"""Test the SkewT API.""" |
|
23
|
|
|
fig = plt.figure(figsize=(9, 9)) |
|
24
|
|
|
skew = SkewT(fig) |
|
25
|
|
|
|
|
26
|
|
|
# Plot the data using normal plotting functions, in this case using |
|
27
|
|
|
# log scaling in Y, as dictated by the typical meteorological plot |
|
28
|
|
|
p = np.linspace(1000, 100, 10) |
|
29
|
|
|
t = np.linspace(20, -20, 10) |
|
30
|
|
|
u = np.linspace(-10, 10, 10) |
|
31
|
|
|
skew.plot(p, t, 'r') |
|
32
|
|
|
skew.plot_barbs(p, u, u) |
|
33
|
|
|
|
|
34
|
|
|
# Add the relevant special lines |
|
35
|
|
|
skew.plot_dry_adiabats() |
|
36
|
|
|
skew.plot_moist_adiabats() |
|
37
|
|
|
skew.plot_mixing_lines() |
|
38
|
|
|
|
|
39
|
|
|
return fig |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True) |
|
43
|
|
|
def test_skewt_subplot(): |
|
44
|
|
|
"""Test using SkewT on a sub-plot.""" |
|
45
|
|
|
fig = plt.figure(figsize=(9, 9)) |
|
46
|
|
|
SkewT(fig, subplot=(2, 2, 1)) |
|
47
|
|
|
return fig |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True) |
|
51
|
|
|
def test_skewt_gridspec(): |
|
52
|
|
|
"""Test using SkewT on a sub-plot.""" |
|
53
|
|
|
fig = plt.figure(figsize=(9, 9)) |
|
54
|
|
|
gs = GridSpec(1, 2) |
|
55
|
|
|
SkewT(fig, subplot=gs[0, 1]) |
|
56
|
|
|
return fig |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
def test_skewt_with_grid_enabled(): |
|
60
|
|
|
"""Test using SkewT when gridlines are already enabled (#271).""" |
|
61
|
|
|
with plt.rc_context(rc={'axes.grid': True}): |
|
62
|
|
|
# Also tests when we don't pass in Figure |
|
63
|
|
|
SkewT() |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
@pytest.fixture() |
|
67
|
|
|
def test_profile(): |
|
68
|
|
|
"""Return data for a test profile.""" |
|
69
|
|
|
return np.linspace(1000, 100, 10), np.linspace(20, -20, 10), np.linspace(25, -30, 10) |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
View Code Duplication |
@pytest.mark.mpl_image_compare(tolerance={'1.4': 1.71}.get(MPL_VERSION, 0.), remove_text=True) |
|
|
|
|
|
|
73
|
|
|
def test_skewt_shade_cape_cin(test_profile): |
|
74
|
|
|
"""Test shading CAPE and CIN on a SkewT plot.""" |
|
75
|
|
|
p, t, tp = test_profile |
|
76
|
|
|
fig = plt.figure(figsize=(9, 9)) |
|
77
|
|
|
skew = SkewT(fig) |
|
78
|
|
|
skew.plot(p, t, 'r') |
|
79
|
|
|
skew.plot(p, tp, 'k') |
|
80
|
|
|
skew.shade_cape(p, t, tp) |
|
81
|
|
|
skew.shade_cin(p, t, tp) |
|
82
|
|
|
skew.ax.set_xlim(-50, 50) |
|
83
|
|
|
return fig |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
View Code Duplication |
@pytest.mark.mpl_image_compare(tolerance={'1.4': 1.70}.get(MPL_VERSION, 0.), remove_text=True) |
|
|
|
|
|
|
87
|
|
|
def test_skewt_shade_area(test_profile): |
|
88
|
|
|
"""Test shading areas on a SkewT plot.""" |
|
89
|
|
|
p, t, tp = test_profile |
|
90
|
|
|
fig = plt.figure(figsize=(9, 9)) |
|
91
|
|
|
skew = SkewT(fig) |
|
92
|
|
|
skew.plot(p, t, 'r') |
|
93
|
|
|
skew.plot(p, tp, 'k') |
|
94
|
|
|
skew.shade_area(p, t, tp) |
|
95
|
|
|
skew.ax.set_xlim(-50, 50) |
|
96
|
|
|
return fig |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
def test_skewt_shade_area_invalid(test_profile): |
|
100
|
|
|
"""Test shading areas on a SkewT plot.""" |
|
101
|
|
|
p, t, tp = test_profile |
|
102
|
|
|
fig = plt.figure(figsize=(9, 9)) |
|
103
|
|
|
skew = SkewT(fig) |
|
104
|
|
|
skew.plot(p, t, 'r') |
|
105
|
|
|
skew.plot(p, tp, 'k') |
|
106
|
|
|
with pytest.raises(ValueError): |
|
107
|
|
|
skew.shade_area(p, t, tp, which='positve') |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
View Code Duplication |
@pytest.mark.mpl_image_compare(tolerance={'1.4': 1.75}.get(MPL_VERSION, 0.), remove_text=True) |
|
|
|
|
|
|
111
|
|
|
def test_skewt_shade_area_kwargs(test_profile): |
|
112
|
|
|
"""Test shading areas on a SkewT plot with kwargs.""" |
|
113
|
|
|
p, t, tp = test_profile |
|
114
|
|
|
fig = plt.figure(figsize=(9, 9)) |
|
115
|
|
|
skew = SkewT(fig) |
|
116
|
|
|
skew.plot(p, t, 'r') |
|
117
|
|
|
skew.plot(p, tp, 'k') |
|
118
|
|
|
skew.shade_area(p, t, tp, facecolor='m') |
|
119
|
|
|
skew.ax.set_xlim(-50, 50) |
|
120
|
|
|
return fig |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True) |
|
124
|
|
|
def test_hodograph_api(): |
|
125
|
|
|
"""Basic test of Hodograph API.""" |
|
126
|
|
|
fig = plt.figure(figsize=(9, 9)) |
|
127
|
|
|
ax = fig.add_subplot(1, 1, 1) |
|
128
|
|
|
hodo = Hodograph(ax, component_range=60) |
|
129
|
|
|
hodo.add_grid(increment=5, color='k') |
|
130
|
|
|
hodo.plot([1, 10], [1, 10], color='red') |
|
131
|
|
|
hodo.plot_colormapped(np.array([1, 3, 5, 10]), np.array([2, 4, 6, 11]), |
|
132
|
|
|
np.array([0.1, 0.3, 0.5, 0.9]), cmap='Greys') |
|
133
|
|
|
return fig |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True) |
|
137
|
|
|
def test_hodograph_units(): |
|
138
|
|
|
"""Test passing unit-ed quantities to Hodograph.""" |
|
139
|
|
|
fig = plt.figure(figsize=(9, 9)) |
|
140
|
|
|
ax = fig.add_subplot(1, 1, 1) |
|
141
|
|
|
hodo = Hodograph(ax) |
|
142
|
|
|
u = np.arange(10) * units.kt |
|
143
|
|
|
v = np.arange(10) * units.kt |
|
144
|
|
|
hodo.plot(u, v) |
|
145
|
|
|
hodo.plot_colormapped(u, v, np.sqrt(u * u + v * v), cmap='Greys') |
|
146
|
|
|
ax.set_xlabel('') |
|
147
|
|
|
ax.set_ylabel('') |
|
148
|
|
|
return fig |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
def test_hodograph_alone(): |
|
152
|
|
|
"""Test to create Hodograph without specifying axes.""" |
|
153
|
|
|
Hodograph() |
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True) |
|
157
|
|
|
def test_hodograph_plot_colormapped(): |
|
158
|
|
|
"""Test hodograph colored line with NaN values.""" |
|
159
|
|
|
u = np.arange(5., 65., 5) |
|
160
|
|
|
v = np.arange(-5., -65., -5) |
|
161
|
|
|
u[3] = np.nan |
|
162
|
|
|
v[6] = np.nan |
|
163
|
|
|
fig = plt.figure(figsize=(9, 9)) |
|
164
|
|
|
ax = fig.add_subplot(1, 1, 1) |
|
165
|
|
|
hodo = Hodograph(ax, component_range=80) |
|
166
|
|
|
hodo.add_grid(increment=20, color='k') |
|
167
|
|
|
hodo.plot_colormapped(u, v, np.hypot(u, v), cmap='Greys') |
|
168
|
|
|
|
|
169
|
|
|
return fig |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
@pytest.mark.mpl_image_compare(tolerance=0.021, remove_text=True) |
|
173
|
|
|
def test_skewt_barb_color(): |
|
174
|
|
|
"""Test plotting colored wind barbs on the Skew-T.""" |
|
175
|
|
|
fig = plt.figure(figsize=(9, 9)) |
|
176
|
|
|
skew = SkewT(fig) |
|
177
|
|
|
|
|
178
|
|
|
p = np.linspace(1000, 100, 10) |
|
179
|
|
|
u = np.linspace(-10, 10, 10) |
|
180
|
|
View Code Duplication |
skew.plot_barbs(p, u, u, c=u) |
|
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
return fig |
|
183
|
|
|
|
|
184
|
|
|
|
|
185
|
|
|
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True) |
|
186
|
|
|
def test_hodograph_plot_layers(): |
|
187
|
|
|
"""Test hodograph colored height layers with interpolation.""" |
|
188
|
|
|
u = np.arange(5, 65, 5) * units('knot') |
|
189
|
|
|
v = np.arange(-5, -65, -5) * units('knot') |
|
190
|
|
|
h = [178, 213, 610, 656, 721, 914, 1060, |
|
191
|
|
|
1219, 1372, 1412, 1512, 1524] * units('meter') |
|
192
|
|
|
colors = ['red', 'green'] |
|
193
|
|
|
levels = [0, 500, 1000] * units('meter') |
|
194
|
|
|
fig = plt.figure(figsize=(9, 9)) |
|
195
|
|
|
ax = fig.add_subplot(1, 1, 1) |
|
196
|
|
|
hodo = Hodograph(ax, component_range=80) |
|
197
|
|
|
hodo.add_grid(increment=20, color='k') |
|
198
|
|
View Code Duplication |
hodo.plot_colormapped(u, v, h, bounds=levels, colors=colors) |
|
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
return fig |
|
201
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True) |
|
204
|
|
|
def test_hodograph_plot_layers_different_units(): |
|
205
|
|
|
"""Test hodograph colored height layers with interpolation and different units.""" |
|
206
|
|
|
u = np.arange(5, 65, 5) * units('knot') |
|
207
|
|
|
v = np.arange(-5, -65, -5) * units('knot') |
|
208
|
|
|
h = [178, 213, 610, 656, 721, 914, 1060, |
|
209
|
|
|
1219, 1372, 1412, 1512, 1524] * units('meter') |
|
210
|
|
|
colors = ['red', 'green'] |
|
211
|
|
|
levels = [0, 0.5, 1] * units.km |
|
212
|
|
|
fig = plt.figure(figsize=(9, 9)) |
|
213
|
|
|
ax = fig.add_subplot(1, 1, 1) |
|
214
|
|
|
hodo = Hodograph(ax, component_range=80) |
|
215
|
|
|
hodo.add_grid(increment=20, color='k') |
|
216
|
|
|
hodo.plot_colormapped(u, v, h, bounds=levels, colors=colors) |
|
217
|
|
|
|
|
218
|
|
|
return fig |
|
219
|
|
|
|
|
220
|
|
|
|
|
221
|
|
|
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True) |
|
222
|
|
|
def test_hodograph_plot_arbitrary_layer(): |
|
223
|
|
|
"""Test hodograph colored layers for arbitrary variables without interpolation.""" |
|
224
|
|
|
u = np.arange(5, 65, 5) * units('knot') |
|
225
|
|
|
v = np.arange(-5, -65, -5) * units('knot') |
|
226
|
|
|
speed = np.sqrt(u ** 2 + v ** 2) |
|
227
|
|
|
colors = ['red', 'green', 'blue'] |
|
228
|
|
|
levels = [0, 10, 20, 30] * units('knot') |
|
229
|
|
|
fig = plt.figure(figsize=(9, 9)) |
|
230
|
|
|
ax = fig.add_subplot(1, 1, 1) |
|
231
|
|
|
hodo = Hodograph(ax, component_range=80) |
|
232
|
|
|
hodo.add_grid(increment=20, color='k') |
|
233
|
|
|
hodo.plot_colormapped(u, v, speed, bounds=levels, colors=colors) |
|
234
|
|
|
|
|
235
|
|
|
return fig |
|
236
|
|
|
|