Completed
Push — master ( f1beb3...24e483 )
by Ryan
01:05
created

test_precipitable_water_no_bounds()   A

Complexity

Conditions 2

Size

Total Lines 10

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 10
rs 9.4285
1
# Copyright (c) 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
                            top=400 * units.hPa)
26
    truth = (0.8899441949243486 * units('inches')).to('millimeters')
27
    assert_array_equal(pw, truth)
28
29
30
def test_precipitable_water_no_bounds():
31
    """Test precipitable water with observed sounding and no bounds given."""
32
    with UseSampleData():
33
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
34
    dewpoint = data.variables['dewpoint'][:]
35
    pressure = data.variables['pressure'][:]
36
    inds = pressure >= 400 * units.hPa
37
    pw = precipitable_water(dewpoint[inds], pressure[inds])
38
    truth = (0.8899441949243486 * units('inches')).to('millimeters')
39
    assert_array_equal(pw, truth)
40
41
42
def test_mean_pressure_weighted():
43
    """Test pressure-weighted mean wind function with vertical interpolation."""
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=6000 * units('meter'))
51
    assert_almost_equal(u, 6.0208700094534775 * units('m/s'), 7)
52
    assert_almost_equal(v, 7.966031839967931 * units('m/s'), 7)
53
54
55 View Code Duplication
def test_mean_pressure_weighted_elevated():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
56
    """Test pressure-weighted mean wind function with a base above the surface."""
57
    with UseSampleData():
58
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
59
    u, v = mean_pressure_weighted(data.variables['pressure'][:],
60
                                  data.variables['u_wind'][:],
61
                                  data.variables['v_wind'][:],
62
                                  heights=data.variables['height'][:],
63
                                  depth=3000 * units('meter'),
64
                                  bottom=data.variables['height'][0] + 3000 * units('meter'))
65
    assert_almost_equal(u, 8.270829843626476 * units('m/s'), 7)
66
    assert_almost_equal(v, 1.7392601775853547 * units('m/s'), 7)
67
68
69
def test_bunkers_motion():
70
    """Test Bunkers storm motion with observed sounding."""
71
    with UseSampleData():
72
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
73
    motion = concatenate(bunkers_storm_motion(data.variables['pressure'][:],
74
                         data.variables['u_wind'][:], data.variables['v_wind'][:],
75
                         data.variables['height'][:]))
76
    truth = [1.4537892577864744, 2.0169333025630616, 10.587950761120482, 13.915130377372801,
77
             6.0208700094534775, 7.9660318399679308] * units('m/s')
78
    assert_almost_equal(motion.flatten(), truth, 8)
79
80
81 View Code Duplication
def test_bulk_shear():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
82
    """Test bulk shear with observed sounding."""
83
    with UseSampleData():
84
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
85
    u, v = bulk_shear(data.variables['pressure'][:], data.variables['u_wind'][:],
86
                      data.variables['v_wind'][:], heights=data.variables['height'][:],
87
                      depth=6000 * units('meter'))
88
    truth = [29.899581266946115, -14.389225800205509] * units('knots')
89
    assert_almost_equal(u.to('knots'), truth[0], 8)
90
    assert_almost_equal(v.to('knots'), truth[1], 8)
91
92
93 View Code Duplication
def test_bulk_shear_no_depth():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
94
    """Test bulk shear with observed sounding and no depth given. Issue #568."""
95
    with UseSampleData():
96
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
97
    u, v = bulk_shear(data.variables['pressure'][:], data.variables['u_wind'][:],
98
                      data.variables['v_wind'][:], heights=data.variables['height'][:])
99
    truth = [20.225018939, 22.602359692] * units('knots')
100
    assert_almost_equal(u.to('knots'), truth[0], 8)
101
    assert_almost_equal(v.to('knots'), truth[1], 8)
102
103
104
def test_bulk_shear_elevated():
105
    """Test bulk shear with observed sounding and a base above the surface."""
106
    with UseSampleData():
107
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
108
    u, v = bulk_shear(data.variables['pressure'][:], data.variables['u_wind'][:],
109
                      data.variables['v_wind'][:], heights=data.variables['height'][:],
110
                      bottom=data.variables['height'][0] + 3000 * units('meter'),
111
                      depth=3000 * units('meter'))
112
    truth = [0.9655943923302139, -3.8405428777944466] * units('m/s')
113
    assert_almost_equal(u, truth[0], 8)
114
    assert_almost_equal(v, truth[1], 8)
115
116
117
def test_supercell_composite():
118
    """Test supercell composite function."""
119
    mucape = [2000., 1000., 500., 2000.] * units('J/kg')
120
    esrh = [400., 150., 45., 45.] * units('m^2/s^2')
121
    ebwd = [30., 15., 5., 5.] * units('m/s')
122
    truth = [16., 2.25, 0., 0.]
123
    supercell_comp = supercell_composite(mucape, esrh, ebwd)
124
    assert_array_equal(supercell_comp, truth)
125
126
127
def test_sigtor():
128
    """Test significant tornado parameter function."""
129
    sbcape = [2000., 2000., 2000., 2000., 3000, 4000] * units('J/kg')
130
    sblcl = [3000., 1500., 500., 1500., 1500, 800] * units('meter')
131
    srh1 = [200., 200., 200., 200., 300, 400] * units('m^2/s^2')
132
    shr6 = [20., 5., 20., 35., 20., 35] * units('m/s')
133
    truth = [0., 0, 1.777778, 1.333333, 2., 10.666667]
134
    sigtor = significant_tornado(sbcape, sblcl, srh1, shr6)
135
    assert_almost_equal(sigtor, truth, 6)
136