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 `station_plot` module.""" |
5
|
|
|
|
6
|
|
|
import matplotlib |
7
|
|
|
import matplotlib.pyplot as plt |
8
|
|
|
import numpy as np |
9
|
|
|
import pytest |
10
|
|
|
|
11
|
|
|
from metpy.plots import nws_layout, simple_layout, StationPlot, StationPlotLayout |
12
|
|
|
from metpy.plots.wx_symbols import current_weather, high_clouds, sky_cover |
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
|
|
|
|
18
|
|
|
MPL_VERSION = matplotlib.__version__[:3] |
19
|
|
|
|
20
|
|
|
|
21
|
|
View Code Duplication |
@pytest.mark.mpl_image_compare(tolerance={'1.5': 0.04625, '1.4': 4.1}.get(MPL_VERSION, 0.0033), |
|
|
|
|
22
|
|
|
savefig_kwargs={'dpi': 300}, remove_text=True) |
23
|
|
|
def test_stationplot_api(): |
24
|
|
|
"""Test the StationPlot API.""" |
25
|
|
|
fig = plt.figure(figsize=(9, 9)) |
26
|
|
|
|
27
|
|
|
# testing data |
28
|
|
|
x = np.array([1, 5]) |
29
|
|
|
y = np.array([2, 4]) |
30
|
|
|
|
31
|
|
|
# Make the plot |
32
|
|
|
sp = StationPlot(fig.add_subplot(1, 1, 1), x, y, fontsize=16) |
33
|
|
|
sp.plot_barb([20, 0], [0, -50]) |
34
|
|
|
sp.plot_text('E', ['KOKC', 'ICT'], color='blue') |
35
|
|
|
sp.plot_parameter('NW', [10.5, 15] * units.degC, color='red') |
36
|
|
|
sp.plot_symbol('S', [5, 7], high_clouds, color='green') |
37
|
|
|
|
38
|
|
|
sp.ax.set_xlim(0, 6) |
39
|
|
|
sp.ax.set_ylim(0, 6) |
40
|
|
|
|
41
|
|
|
return fig |
42
|
|
|
|
43
|
|
|
|
44
|
|
View Code Duplication |
@pytest.mark.mpl_image_compare(tolerance=0, savefig_kwargs={'dpi': 300}, remove_text=True) |
|
|
|
|
45
|
|
|
def test_stationplot_clipping(): |
46
|
|
|
"""Test the that clipping can be enabled as a default parameter.""" |
47
|
|
|
fig = plt.figure(figsize=(9, 9)) |
48
|
|
|
|
49
|
|
|
# testing data |
50
|
|
|
x = np.array([1, 5]) |
51
|
|
|
y = np.array([2, 4]) |
52
|
|
|
|
53
|
|
|
# Make the plot |
54
|
|
|
sp = StationPlot(fig.add_subplot(1, 1, 1), x, y, fontsize=16, clip_on=True) |
55
|
|
|
sp.plot_barb([20, 0], [0, -50]) |
56
|
|
|
sp.plot_text('E', ['KOKC', 'ICT'], color='blue') |
57
|
|
|
sp.plot_parameter('NW', [10.5, 15] * units.degC, color='red') |
58
|
|
|
sp.plot_symbol('S', [5, 7], high_clouds, color='green') |
59
|
|
|
|
60
|
|
|
sp.ax.set_xlim(1, 5) |
61
|
|
|
sp.ax.set_ylim(1.75, 4.25) |
62
|
|
|
|
63
|
|
|
return fig |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
@pytest.mark.mpl_image_compare(tolerance={'1.5': 0.05974, '1.4': 3.7}.get(MPL_VERSION, 0.0033), |
67
|
|
|
savefig_kwargs={'dpi': 300}, remove_text=True) |
68
|
|
|
def test_station_plot_replace(): |
69
|
|
|
"""Test that locations are properly replaced.""" |
70
|
|
|
fig = plt.figure(figsize=(3, 3)) |
71
|
|
|
|
72
|
|
|
# testing data |
73
|
|
|
x = np.array([1]) |
74
|
|
|
y = np.array([1]) |
75
|
|
|
|
76
|
|
|
# Make the plot |
77
|
|
|
sp = StationPlot(fig.add_subplot(1, 1, 1), x, y, fontsize=16) |
78
|
|
|
sp.plot_barb([20], [0]) |
79
|
|
|
sp.plot_barb([5], [0]) |
80
|
|
|
sp.plot_parameter('NW', [10.5], color='red') |
81
|
|
|
sp.plot_parameter('NW', [20], color='blue') |
82
|
|
|
|
83
|
|
|
sp.ax.set_xlim(-3, 3) |
84
|
|
|
sp.ax.set_ylim(-3, 3) |
85
|
|
|
|
86
|
|
|
return fig |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
@pytest.mark.mpl_image_compare(tolerance={'1.5': 0.036, '1.4': 2.02}.get(MPL_VERSION, 0.00321), |
90
|
|
|
savefig_kwargs={'dpi': 300}, remove_text=True) |
91
|
|
|
def test_stationlayout_api(): |
92
|
|
|
"""Test the StationPlot API.""" |
93
|
|
|
fig = plt.figure(figsize=(9, 9)) |
94
|
|
|
|
95
|
|
|
# testing data |
96
|
|
|
x = np.array([1, 5]) |
97
|
|
|
y = np.array([2, 4]) |
98
|
|
|
data = {'temp': np.array([32., 212.]) * units.degF, 'u': np.array([2, 0]) * units.knots, |
99
|
|
|
'v': np.array([0, 5]) * units.knots, 'stid': ['KDEN', 'KSHV'], 'cover': [3, 8]} |
100
|
|
|
|
101
|
|
|
# Set up the layout |
102
|
|
|
layout = StationPlotLayout() |
103
|
|
|
layout.add_barb('u', 'v', units='knots') |
104
|
|
|
layout.add_value('NW', 'temp', fmt='0.1f', units=units.degC, color='darkred') |
105
|
|
|
layout.add_symbol('C', 'cover', sky_cover, color='magenta') |
106
|
|
|
layout.add_text((0, 2), 'stid', color='darkgrey') |
107
|
|
|
layout.add_value('NE', 'dewpt', color='green') # This should be ignored |
108
|
|
|
|
109
|
|
|
# Make the plot |
110
|
|
|
sp = StationPlot(fig.add_subplot(1, 1, 1), x, y, fontsize=12) |
111
|
|
|
layout.plot(sp, data) |
112
|
|
|
|
113
|
|
|
sp.ax.set_xlim(0, 6) |
114
|
|
|
sp.ax.set_ylim(0, 6) |
115
|
|
|
|
116
|
|
|
return fig |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
def test_station_layout_odd_data(): |
120
|
|
|
"""Test more corner cases with data passed in.""" |
121
|
|
|
fig = plt.figure(figsize=(9, 9)) |
122
|
|
|
|
123
|
|
|
# Set up test layout |
124
|
|
|
layout = StationPlotLayout() |
125
|
|
|
layout.add_barb('u', 'v') |
126
|
|
|
layout.add_value('W', 'temperature', units='degF') |
127
|
|
|
|
128
|
|
|
# Now only use data without wind and no units |
129
|
|
|
data = {'temperature': [25.]} |
130
|
|
|
|
131
|
|
|
# Make the plot |
132
|
|
|
sp = StationPlot(fig.add_subplot(1, 1, 1), [1], [2], fontsize=12) |
133
|
|
|
layout.plot(sp, data) |
134
|
|
|
assert True |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
def test_station_layout_replace(): |
138
|
|
|
"""Test that layout locations are replaced.""" |
139
|
|
|
layout = StationPlotLayout() |
140
|
|
|
layout.add_text('E', 'temperature') |
141
|
|
|
layout.add_value('E', 'dewpoint') |
142
|
|
|
assert 'E' in layout |
143
|
|
|
assert layout['E'][0] is StationPlotLayout.PlotTypes.value |
144
|
|
|
assert layout['E'][1] == 'dewpoint' |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
def test_station_layout_names(): |
148
|
|
|
"""Test getting station layout names.""" |
149
|
|
|
layout = StationPlotLayout() |
150
|
|
|
layout.add_barb('u', 'v') |
151
|
|
|
layout.add_text('E', 'stid') |
152
|
|
|
layout.add_value('W', 'temp') |
153
|
|
|
layout.add_symbol('C', 'cover', lambda x: x) |
154
|
|
|
assert sorted(layout.names()) == ['cover', 'stid', 'temp', 'u', 'v'] |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
@pytest.mark.mpl_image_compare(tolerance={'1.5': 0.05447, '1.4': 3.0}.get(MPL_VERSION, 0.0039), |
158
|
|
|
savefig_kwargs={'dpi': 300}, remove_text=True) |
159
|
|
|
def test_simple_layout(): |
160
|
|
|
"""Test metpy's simple layout for station plots.""" |
161
|
|
|
fig = plt.figure(figsize=(9, 9)) |
162
|
|
|
|
163
|
|
|
# testing data |
164
|
|
|
x = np.array([1, 5]) |
165
|
|
|
y = np.array([2, 4]) |
166
|
|
|
data = {'air_temperature': np.array([32., 212.]) * units.degF, |
167
|
|
|
'dew_point_temperature': np.array([28., 80.]) * units.degF, |
168
|
|
|
'air_pressure_at_sea_level': np.array([29.92, 28.00]) * units.inHg, |
169
|
|
|
'eastward_wind': np.array([2, 0]) * units.knots, |
170
|
|
|
'northward_wind': np.array([0, 5]) * units.knots, 'cloud_coverage': [3, 8], |
171
|
|
|
'present_weather': [65, 75], 'unused': [1, 2]} |
172
|
|
|
|
173
|
|
|
# Make the plot |
174
|
|
|
sp = StationPlot(fig.add_subplot(1, 1, 1), x, y, fontsize=12) |
175
|
|
|
simple_layout.plot(sp, data) |
176
|
|
|
|
177
|
|
|
sp.ax.set_xlim(0, 6) |
178
|
|
|
sp.ax.set_ylim(0, 6) |
179
|
|
|
|
180
|
|
|
return fig |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
@pytest.mark.mpl_image_compare(tolerance={'1.4': 7.02}.get(MPL_VERSION, 0.1848), |
184
|
|
|
savefig_kwargs={'dpi': 300}, remove_text=True) |
185
|
|
|
def test_nws_layout(): |
186
|
|
|
"""Test metpy's NWS layout for station plots.""" |
187
|
|
|
fig = plt.figure(figsize=(3, 3)) |
188
|
|
|
|
189
|
|
|
# testing data |
190
|
|
|
x = np.array([1]) |
191
|
|
|
y = np.array([2]) |
192
|
|
|
data = {'air_temperature': np.array([77]) * units.degF, |
193
|
|
|
'dew_point_temperature': np.array([71]) * units.degF, |
194
|
|
|
'air_pressure_at_sea_level': np.array([999.8]) * units('mbar'), |
195
|
|
|
'eastward_wind': np.array([15.]) * units.knots, |
196
|
|
|
'northward_wind': np.array([15.]) * units.knots, 'cloud_coverage': [7], |
197
|
|
|
'present_weather': [80], 'high_cloud_type': [1], 'medium_cloud_type': [3], |
198
|
|
|
'low_cloud_type': [2], 'visibility_in_air': np.array([5.]) * units.mile, |
199
|
|
|
'tendency_of_air_pressure': np.array([-0.3]) * units('mbar'), |
200
|
|
|
'tendency_of_air_pressure_symbol': [8]} |
201
|
|
|
|
202
|
|
|
# Make the plot |
203
|
|
|
sp = StationPlot(fig.add_subplot(1, 1, 1), x, y, fontsize=12, spacing=16) |
204
|
|
|
nws_layout.plot(sp, data) |
205
|
|
|
|
206
|
|
|
sp.ax.set_xlim(0, 3) |
207
|
|
|
sp.ax.set_ylim(0, 3) |
208
|
|
|
|
209
|
|
|
return fig |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
@pytest.mark.mpl_image_compare(tolerance={'1.4': 6.68}.get(MPL_VERSION, 1.05), |
213
|
|
|
remove_text=True) |
214
|
|
|
def test_plot_text_fontsize(): |
215
|
|
|
"""Test changing fontsize in plot_text.""" |
216
|
|
|
fig = plt.figure(figsize=(3, 3)) |
217
|
|
|
ax = plt.subplot(1, 1, 1) |
218
|
|
|
|
219
|
|
|
# testing data |
220
|
|
|
x = np.array([1]) |
221
|
|
|
y = np.array([2]) |
222
|
|
|
|
223
|
|
|
# Make the plot |
224
|
|
|
sp = StationPlot(ax, x, y, fontsize=36) |
225
|
|
|
sp.plot_text('NW', ['72'], fontsize=24) |
226
|
|
|
sp.plot_text('SW', ['60'], fontsize=4) |
227
|
|
|
|
228
|
|
|
sp.ax.set_xlim(0, 3) |
229
|
|
|
sp.ax.set_ylim(0, 3) |
230
|
|
|
|
231
|
|
|
return fig |
232
|
|
|
|
233
|
|
|
|
234
|
|
|
@pytest.mark.mpl_image_compare(tolerance={'1.4': 26.8}.get(MPL_VERSION, 1.05), |
235
|
|
|
remove_text=True) |
236
|
|
|
def test_plot_symbol_fontsize(): |
237
|
|
|
"""Test changing fontsize in plotting of symbols.""" |
238
|
|
|
fig = plt.figure(figsize=(3, 3)) |
239
|
|
|
ax = plt.subplot(1, 1, 1) |
240
|
|
|
|
241
|
|
|
sp = StationPlot(ax, [0], [0], fontsize=8, spacing=32) |
242
|
|
|
sp.plot_symbol('E', [92], current_weather) |
243
|
|
|
sp.plot_symbol('W', [96], current_weather, fontsize=100) |
244
|
|
|
|
245
|
|
|
return fig |
246
|
|
|
|
247
|
|
|
|
248
|
|
|
def test_layout_str(): |
249
|
|
|
"""Test layout string representation.""" |
250
|
|
|
layout = StationPlotLayout() |
251
|
|
|
layout.add_barb('u', 'v') |
252
|
|
|
layout.add_text('E', 'stid') |
253
|
|
|
layout.add_value('W', 'temp') |
254
|
|
|
layout.add_symbol('C', 'cover', lambda x: x) |
255
|
|
|
assert str(layout) == ('{C: (symbol, cover, ...), E: (text, stid, ...), ' |
256
|
|
|
'W: (value, temp, ...), barb: (barb, (\'u\', \'v\'), ...)}') |
257
|
|
|
|