Completed
Pull Request — master (#382)
by Ryan
01:26
created

test_skewt_shade_cape_cin()   A

Complexity

Conditions 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
dl 0
loc 11
rs 9.4285
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
from matplotlib.gridspec import GridSpec
7
import matplotlib.pyplot as plt
8
import numpy as np
9
import pytest
10
11
from metpy.plots import Hodograph, SkewT
12
# Fixtures to make sure we have the right backend and consistent round
13
from metpy.testing import patch_round, set_agg_backend  # noqa: F401
14
from metpy.units import units
15
16
17
@pytest.mark.mpl_image_compare(tolerance=0.021, remove_text=True)
18
def test_skewt_api():
19
    """Test the SkewT API."""
20
    fig = plt.figure(figsize=(9, 9))
21
    skew = SkewT(fig)
22
23
    # Plot the data using normal plotting functions, in this case using
24
    # log scaling in Y, as dictated by the typical meteorological plot
25
    p = np.linspace(1000, 100, 10)
26
    t = np.linspace(20, -20, 10)
27
    u = np.linspace(-10, 10, 10)
28
    skew.plot(p, t, 'r')
29
    skew.plot_barbs(p, u, u)
30
31
    # Add the relevant special lines
32
    skew.plot_dry_adiabats()
33
    skew.plot_moist_adiabats()
34
    skew.plot_mixing_lines()
35
36
    return fig
37
38
39
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
40
def test_skewt_subplot():
41
    """Test using SkewT on a sub-plot."""
42
    fig = plt.figure(figsize=(9, 9))
43
    SkewT(fig, subplot=(2, 2, 1))
44
    return fig
45
46
47
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
48
def test_skewt_gridspec():
49
    """Test using SkewT on a sub-plot."""
50
    fig = plt.figure(figsize=(9, 9))
51
    gs = GridSpec(1, 2)
52
    SkewT(fig, subplot=gs[0, 1])
53
    return fig
54
55
56
def test_skewt_with_grid_enabled():
57
    """Test using SkewT when gridlines are already enabled (#271)."""
58
    with plt.rc_context(rc={'axes.grid': True}):
59
        # Also tests when we don't pass in Figure
60
        SkewT()
61
62
63
@pytest.fixture()
64
def test_profile():
65
    """Return data for a test profile."""
66
    return np.linspace(1000, 100, 10), np.linspace(20, -20, 10), np.linspace(25, -30, 10)
67
68
69
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
70
def test_skewt_shade_cape_cin(test_profile):
71
    """Test shading CAPE and CIN on a SkewT plot."""
72
    p, t, tp = test_profile
73
    fig = plt.figure(figsize=(9, 9))
74
    skew = SkewT(fig)
75
    skew.plot(p, t, 'r')
76
    skew.plot(p, tp, 'k')
77
    skew.shade_cape(p, t, tp)
78
    skew.shade_cin(p, t, tp)
79
    return fig
80
81
82
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
83
def test_skewt_shade_area(test_profile):
84
    """Test shading areas on a SkewT plot."""
85
    p, t, tp = test_profile
86
    fig = plt.figure(figsize=(9, 9))
87
    skew = SkewT(fig)
88
    skew.plot(p, t, 'r')
89
    skew.plot(p, tp, 'k')
90
    skew.shade_area(p, t, tp)
91
    return fig
92
93
94
def test_skewt_shade_area_invalid(test_profile):
95
    """Test shading areas on a SkewT plot."""
96
    p, t, tp = test_profile
97
    fig = plt.figure(figsize=(9, 9))
98
    skew = SkewT(fig)
99
    skew.plot(p, t, 'r')
100
    skew.plot(p, tp, 'k')
101
    with pytest.raises(ValueError):
102
        skew.shade_area(p, t, tp, which='positve')
103
104
105
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
106
def test_skewt_shade_area_kwargs(test_profile):
107
    """Test shading areas on a SkewT plot with kwargs."""
108
    p, t, tp = test_profile
109
    fig = plt.figure(figsize=(9, 9))
110
    skew = SkewT(fig)
111
    skew.plot(p, t, 'r')
112
    skew.plot(p, tp, 'k')
113
    skew.shade_area(p, t, tp, facecolor='m')
114
    return fig
115
116
117
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
118
def test_hodograph_api():
119
    """Basic test of Hodograph API."""
120
    fig = plt.figure(figsize=(9, 9))
121
    ax = fig.add_subplot(1, 1, 1)
122
    hodo = Hodograph(ax, component_range=60)
123
    hodo.add_grid(increment=5, color='k')
124
    hodo.plot([1, 10], [1, 10], color='red')
125
    hodo.plot_colormapped(np.array([1, 3, 5, 10]), np.array([2, 4, 6, 11]),
126
                          np.array([0.1, 0.3, 0.5, 0.9]), cmap='Greys')
127
    return fig
128
129
130
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
131
def test_hodograph_units():
132
    """Test passing unit-ed quantities to Hodograph."""
133
    fig = plt.figure(figsize=(9, 9))
134
    ax = fig.add_subplot(1, 1, 1)
135
    hodo = Hodograph(ax)
136
    u = np.arange(10) * units.kt
137
    v = np.arange(10) * units.kt
138
    hodo.plot(u, v)
139
    hodo.plot_colormapped(u, v, np.sqrt(u * u + v * v), cmap='Greys')
140
    return fig
141
142
143
def test_hodograph_alone():
144
    """Test to create Hodograph without specifying axes."""
145
    Hodograph()
146
147
148
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
149
def test_hodograph_plot_colormapped():
150
    """Test hodograph colored line with NaN values."""
151
    u = np.arange(5., 65., 5)
152
    v = np.arange(-5., -65., -5)
153
    u[3] = np.nan
154
    v[6] = np.nan
155
    fig = plt.figure(figsize=(9, 9))
156
    ax = fig.add_subplot(1, 1, 1)
157
    hodo = Hodograph(ax, component_range=80)
158
    hodo.add_grid(increment=20, color='k')
159
    hodo.plot_colormapped(u, v, np.hypot(u, v), cmap='Greys')
160
161
    return fig
162