Completed
Push — master ( 31695a...86c841 )
by Ryan
19s
created

test_bulk_shear_no_depth()   A

Complexity

Conditions 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
1
# Copyright (c) 2008-2017 MetPy Developers.
2
# Distributed under the terms of the BSD 3-Clause License.
3
# SPDX-License-Identifier: BSD-3-Clause
4
"""Test the `indices` module."""
5
6
from datetime import datetime
7
import warnings
8
9
from metpy.calc import (bulk_shear, bunkers_storm_motion, mean_pressure_weighted,
10
                        precipitable_water, significant_tornado, supercell_composite)
11
from metpy.deprecation import MetpyDeprecationWarning
12
from metpy.io import get_upper_air_data
13
from metpy.io.upperair import UseSampleData
14
from metpy.testing import assert_almost_equal, assert_array_equal
15
from metpy.units import concatenate, units
16
17
warnings.simplefilter('ignore', MetpyDeprecationWarning)
18
19
20
def test_precipitable_water():
21
    """Test precipitable water with observed sounding."""
22
    with UseSampleData():
23
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
24
    pw = precipitable_water(data.variables['dewpoint'][:], data.variables['pressure'][:])
25
    truth = (0.8899441949243486 * units('inches')).to('millimeters')
26
    assert_array_equal(pw, truth)
27
28
29
def test_mean_pressure_weighted():
30
    """Test pressure-weighted mean wind function with vertical interpolation."""
31
    with UseSampleData():
32
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
33
    u, v = mean_pressure_weighted(data.variables['pressure'][:],
34
                                  data.variables['u_wind'][:],
35
                                  data.variables['v_wind'][:],
36
                                  heights=data.variables['height'][:],
37
                                  depth=6000 * units('meter'))
38 View Code Duplication
    assert_almost_equal(u, 6.0208700094534775 * units('m/s'), 7)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
39
    assert_almost_equal(v, 7.966031839967931 * units('m/s'), 7)
40
41
42
def test_mean_pressure_weighted_elevated():
43
    """Test pressure-weighted mean wind function with a base above the surface."""
44
    with UseSampleData():
45
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
46
    u, v = mean_pressure_weighted(data.variables['pressure'][:],
47
                                  data.variables['u_wind'][:],
48
                                  data.variables['v_wind'][:],
49
                                  heights=data.variables['height'][:],
50
                                  depth=3000 * units('meter'),
51
                                  bottom=data.variables['height'][0] + 3000 * units('meter'))
52
    assert_almost_equal(u, 8.270829843626476 * units('m/s'), 7)
53
    assert_almost_equal(v, 1.7392601775853547 * units('m/s'), 7)
54
55
56
def test_bunkers_motion():
57
    """Test Bunkers storm motion with observed sounding."""
58
    with UseSampleData():
59
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
60
    motion = concatenate(bunkers_storm_motion(data.variables['pressure'][:],
61
                         data.variables['u_wind'][:], data.variables['v_wind'][:],
62
                         data.variables['height'][:]))
63
    truth = [1.4537892577864744, 2.0169333025630616, 10.587950761120482, 13.915130377372801,
64 View Code Duplication
             6.0208700094534775, 7.9660318399679308] * units('m/s')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
65
    assert_almost_equal(motion.flatten(), truth, 8)
66
67
68
def test_bulk_shear():
69
    """Test bulk shear with observed sounding."""
70
    with UseSampleData():
71
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
72
    u, v = bulk_shear(data.variables['pressure'][:], data.variables['u_wind'][:],
73
                      data.variables['v_wind'][:], heights=data.variables['height'][:],
74
                      depth=6000 * units('meter'))
75
    truth = [29.899581266946115, -14.389225800205509] * units('knots')
76
    assert_almost_equal(u.to('knots'), truth[0], 8)
77
    assert_almost_equal(v.to('knots'), truth[1], 8)
78
79
80
def test_bulk_shear_no_depth():
81
    """Test bulk shear with observed sounding and no depth given. Issue #568."""
82
    with UseSampleData():
83
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
84
    u, v = bulk_shear(data.variables['pressure'][:], data.variables['u_wind'][:],
85
                      data.variables['v_wind'][:], heights=data.variables['height'][:])
86
    truth = [20.225018939, 22.602359692] * units('knots')
87
    assert_almost_equal(u.to('knots'), truth[0], 8)
88
    assert_almost_equal(v.to('knots'), truth[1], 8)
89
90
91
def test_bulk_shear_elevated():
92
    """Test bulk shear with observed sounding and a base above the surface."""
93
    with UseSampleData():
94
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
95
    u, v = bulk_shear(data.variables['pressure'][:], data.variables['u_wind'][:],
96
                      data.variables['v_wind'][:], heights=data.variables['height'][:],
97
                      bottom=data.variables['height'][0] + 3000 * units('meter'),
98
                      depth=3000 * units('meter'))
99
    truth = [0.9655943923302139, -3.8405428777944466] * units('m/s')
100
    assert_almost_equal(u, truth[0], 8)
101
    assert_almost_equal(v, truth[1], 8)
102
103
104
def test_supercell_composite():
105
    """Test supercell composite function."""
106
    mucape = [2000., 1000., 500., 2000.] * units('J/kg')
107
    esrh = [400., 150., 45., 45.] * units('m^2/s^2')
108
    ebwd = [30., 15., 5., 5.] * units('m/s')
109
    truth = [16., 2.25, 0., 0.]
110
    supercell_comp = supercell_composite(mucape, esrh, ebwd)
111
    assert_array_equal(supercell_comp, truth)
112
113
114
def test_sigtor():
115
    """Test significant tornado parameter function."""
116
    sbcape = [2000., 2000., 2000., 2000., 3000, 4000] * units('J/kg')
117
    sblcl = [3000., 1500., 500., 1500., 1500, 800] * units('meter')
118
    srh1 = [200., 200., 200., 200., 300, 400] * units('m^2/s^2')
119
    shr6 = [20., 5., 20., 35., 20., 35] * units('m/s')
120
    truth = [0., 0, 1.777778, 1.333333, 2., 10.666667]
121
    sigtor = significant_tornado(sbcape, sblcl, srh1, shr6)
122
    assert_almost_equal(sigtor, truth, 6)
123