Completed
Pull Request — master (#382)
by
unknown
01:32
created

test_skewt_shade_cape_cin()   A

Complexity

Conditions 1

Size

Total Lines 10

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 10
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
p = np.linspace(1000, 100, 10)
63
T = np.linspace(20, -20, 10)
64
Tp = np.linspace(25, -30, 10)
65
66
67
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
68
def test_skewt_shade_cape_cin():
69
    """Test shading CAPE and CIN on a SkewT plot."""
70
    fig = plt.figure(figsize=(9, 9))
71
    skew = SkewT(fig)
72
    skew.plot(p, T, 'r')
73
    skew.plot(p, Tp, 'k')
74
    skew.shade_cape(p, T, Tp)
75
    skew.shade_cin(p, T, Tp)
76
    return fig
77
78
79
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
80
def test_skewt_shade_area():
81
    """Test shading areas on a SkewT plot."""
82
    fig = plt.figure(figsize=(9, 9))
83
    skew = SkewT(fig)
84
    skew.plot(p, T, 'r')
85
    skew.plot(p, Tp, 'k')
86
    skew.shade_area(p, T, Tp)
87
    return fig
88
89
90
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
91
def test_skewt_shade_area_kwargs():
92
    """Test shading areas on a SkewT plot with kwargs."""
93
    fig = plt.figure(figsize=(9, 9))
94
    skew = SkewT(fig)
95
    skew.plot(p, T, 'r')
96
    skew.plot(p, Tp, 'k')
97
    skew.shade_area(p, T, Tp, color='magenta')
98
    return fig
99
100
101
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
102
def test_hodograph_api():
103
    """Basic test of Hodograph API."""
104
    fig = plt.figure(figsize=(9, 9))
105
    ax = fig.add_subplot(1, 1, 1)
106
    hodo = Hodograph(ax, component_range=60)
107
    hodo.add_grid(increment=5, color='k')
108
    hodo.plot([1, 10], [1, 10], color='red')
109
    hodo.plot_colormapped(np.array([1, 3, 5, 10]), np.array([2, 4, 6, 11]),
110
                          np.array([0.1, 0.3, 0.5, 0.9]), cmap='Greys')
111
    return fig
112
113
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
114
def test_hodograph_api():
115
    """Basic test of Hodograph API."""
116
    fig = plt.figure(figsize=(9, 9))
117
    ax = fig.add_subplot(1, 1, 1)
118
    hodo = Hodograph(ax, component_range=60)
119
    hodo.add_grid(increment=5, color='k')
120
    hodo.plot([1, 10], [1, 10], color='red')
121
    hodo.plot_colormapped(np.array([1, 3, 5, 10]), np.array([2, 4, 6, 11]),
122
                          np.array([0.1, 0.3, 0.5, 0.9]), cmap='Greys')
123
    return fig
124
125
126
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
127
def test_hodograph_units():
128
    """Test passing unit-ed quantities to Hodograph."""
129
    fig = plt.figure(figsize=(9, 9))
130
    ax = fig.add_subplot(1, 1, 1)
131
    hodo = Hodograph(ax)
132
    u = np.arange(10) * units.kt
133
    v = np.arange(10) * units.kt
134
    hodo.plot(u, v)
135
    hodo.plot_colormapped(u, v, np.sqrt(u * u + v * v), cmap='Greys')
136
    return fig
137
138
139
def test_hodograph_alone():
140
    """Test to create Hodograph without specifying axes."""
141
    Hodograph()
142
143
144
@pytest.mark.mpl_image_compare(tolerance=0, remove_text=True)
145
def test_hodograph_plot_colormapped():
146
    """Test hodograph colored line with NaN values."""
147
    u = np.arange(5., 65., 5)
148
    v = np.arange(-5., -65., -5)
149
    u[3] = np.nan
150
    v[6] = np.nan
151
    fig = plt.figure(figsize=(9, 9))
152
    ax = fig.add_subplot(1, 1, 1)
153
    hodo = Hodograph(ax, component_range=80)
154
    hodo.add_grid(increment=20, color='k')
155
    hodo.plot_colormapped(u, v, np.hypot(u, v), cmap='Greys')
156
157
    return fig
158