Completed
Push — master ( cd87fe...c19390 )
by Ryan
01:05
created

test_hodograph_plot_arbitrary_layer()   A

Complexity

Conditions 1

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
dl 15
loc 15
rs 9.4285
c 2
b 0
f 0
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
@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
    return fig
83
84
85
@pytest.mark.mpl_image_compare(tolerance={'1.4': 1.70}.get(MPL_VERSION, 0.), remove_text=True)
86
def test_skewt_shade_area(test_profile):
87
    """Test shading areas on a SkewT plot."""
88
    p, t, tp = test_profile
89
    fig = plt.figure(figsize=(9, 9))
90
    skew = SkewT(fig)
91
    skew.plot(p, t, 'r')
92
    skew.plot(p, tp, 'k')
93
    skew.shade_area(p, t, tp)
94
    return fig
95
96
97
def test_skewt_shade_area_invalid(test_profile):
98
    """Test shading areas on a SkewT plot."""
99
    p, t, tp = test_profile
100
    fig = plt.figure(figsize=(9, 9))
101
    skew = SkewT(fig)
102
    skew.plot(p, t, 'r')
103
    skew.plot(p, tp, 'k')
104
    with pytest.raises(ValueError):
105
        skew.shade_area(p, t, tp, which='positve')
106
107
108
@pytest.mark.mpl_image_compare(tolerance={'1.4': 1.75}.get(MPL_VERSION, 0.), remove_text=True)
109
def test_skewt_shade_area_kwargs(test_profile):
110
    """Test shading areas on a SkewT plot with kwargs."""
111
    p, t, tp = test_profile
112
    fig = plt.figure(figsize=(9, 9))
113
    skew = SkewT(fig)
114
    skew.plot(p, t, 'r')
115
    skew.plot(p, tp, 'k')
116
    skew.shade_area(p, t, tp, facecolor='m')
117
    return fig
118
119
120
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
121
def test_hodograph_api():
122
    """Basic test of Hodograph API."""
123
    fig = plt.figure(figsize=(9, 9))
124
    ax = fig.add_subplot(1, 1, 1)
125
    hodo = Hodograph(ax, component_range=60)
126
    hodo.add_grid(increment=5, color='k')
127
    hodo.plot([1, 10], [1, 10], color='red')
128
    hodo.plot_colormapped(np.array([1, 3, 5, 10]), np.array([2, 4, 6, 11]),
129
                          np.array([0.1, 0.3, 0.5, 0.9]), cmap='Greys')
130
    return fig
131
132
133
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
134
def test_hodograph_units():
135
    """Test passing unit-ed quantities to Hodograph."""
136
    fig = plt.figure(figsize=(9, 9))
137
    ax = fig.add_subplot(1, 1, 1)
138
    hodo = Hodograph(ax)
139
    u = np.arange(10) * units.kt
140
    v = np.arange(10) * units.kt
141
    hodo.plot(u, v)
142
    hodo.plot_colormapped(u, v, np.sqrt(u * u + v * v), cmap='Greys')
143
    return fig
144
145
146
def test_hodograph_alone():
147
    """Test to create Hodograph without specifying axes."""
148
    Hodograph()
149
150
151
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
152
def test_hodograph_plot_colormapped():
153
    """Test hodograph colored line with NaN values."""
154
    u = np.arange(5., 65., 5)
155
    v = np.arange(-5., -65., -5)
156
    u[3] = np.nan
157
    v[6] = np.nan
158
    fig = plt.figure(figsize=(9, 9))
159
    ax = fig.add_subplot(1, 1, 1)
160
    hodo = Hodograph(ax, component_range=80)
161
    hodo.add_grid(increment=20, color='k')
162
    hodo.plot_colormapped(u, v, np.hypot(u, v), cmap='Greys')
163
164
    return fig
165
166
167
@pytest.mark.mpl_image_compare(tolerance=0.021, remove_text=True)
168
def test_skewt_barb_color():
169
    """Test plotting colored wind barbs on the Skew-T."""
170
    fig = plt.figure(figsize=(9, 9))
171
    skew = SkewT(fig)
172
173
    p = np.linspace(1000, 100, 10)
174
    u = np.linspace(-10, 10, 10)
175
    skew.plot_barbs(p, u, u, c=u)
176
177
    return fig
178
179
180 View Code Duplication
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
181
def test_hodograph_plot_layers():
182
    """Test hodograph colored height layers with interpolation."""
183
    u = np.arange(5, 65, 5) * units('knot')
184
    v = np.arange(-5, -65, -5) * units('knot')
185
    h = [178, 213, 610, 656, 721, 914, 1060,
186
         1219, 1372, 1412, 1512, 1524] * units('meter')
187
    colors = ['red', 'green']
188
    levels = [0, 500, 1000] * units('meter')
189
    fig = plt.figure(figsize=(9, 9))
190
    ax = fig.add_subplot(1, 1, 1)
191
    hodo = Hodograph(ax, component_range=80)
192
    hodo.add_grid(increment=20, color='k')
193
    hodo.plot_colormapped(u, v, h, bounds=levels, colors=colors)
194
195
    return fig
196
197
198 View Code Duplication
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
199
def test_hodograph_plot_arbitrary_layer():
200
    """Test hodograph colored layers for arbitrary variables without interpolation."""
201
    u = np.arange(5, 65, 5) * units('knot')
202
    v = np.arange(-5, -65, -5) * units('knot')
203
    speed = np.sqrt(u ** 2 + v ** 2)
204
    colors = ['red', 'green', 'blue']
205
    levels = [0, 10, 20, 30] * units('knot')
206
    fig = plt.figure(figsize=(9, 9))
207
    ax = fig.add_subplot(1, 1, 1)
208
    hodo = Hodograph(ax, component_range=80)
209
    hodo.add_grid(increment=20, color='k')
210
    hodo.plot_colormapped(u, v, speed, bounds=levels, colors=colors)
211
212
    return fig
213