Completed
Push — master ( 24e483...1c6020 )
by
unknown
25s
created

test_barb_projection()   A

Complexity

Conditions 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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