Completed
Push — master ( 2c3643...6ffd26 )
by Ryan
23s
created

test_precipitable_water_bound_error()   A

Complexity

Conditions 1

Size

Total Lines 9

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 9
rs 9.6666
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
import numpy as np
10
from metpy.calc import (bulk_shear, bunkers_storm_motion, mean_pressure_weighted,
11
                        precipitable_water, significant_tornado, supercell_composite)
12
from metpy.deprecation import MetpyDeprecationWarning
13
from metpy.io import get_upper_air_data
14
from metpy.io.upperair import UseSampleData
15
from metpy.testing import assert_almost_equal, assert_array_equal
16
from metpy.units import concatenate, units
17
18
warnings.simplefilter('ignore', MetpyDeprecationWarning)
19
20
21
def test_precipitable_water():
22
    """Test precipitable water with observed sounding."""
23
    with UseSampleData():
24
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
25
    pw = precipitable_water(data.variables['dewpoint'][:], data.variables['pressure'][:],
26
                            top=400 * units.hPa)
27
    truth = (0.8899441949243486 * units('inches')).to('millimeters')
28
    assert_array_equal(pw, truth)
29
30
31
def test_precipitable_water_no_bounds():
32
    """Test precipitable water with observed sounding and no bounds given."""
33
    with UseSampleData():
34
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
35
    dewpoint = data.variables['dewpoint'][:]
36
    pressure = data.variables['pressure'][:]
37
    inds = pressure >= 400 * units.hPa
38
    pw = precipitable_water(dewpoint[inds], pressure[inds])
39
    truth = (0.8899441949243486 * units('inches')).to('millimeters')
40
    assert_array_equal(pw, truth)
41
42
43
def test_precipitable_water_bound_error():
44
    """Test with no top bound given and data that produced floating point issue #596."""
45
    pressure = np.array([993., 978., 960.5, 927.6, 925., 895.8, 892., 876., 45.9, 39.9, 36.,
46
                         36., 34.3]) * units.hPa
47
    dewpoint = np.array([25.5, 24.1, 23.1, 21.2, 21.1, 19.4, 19.2, 19.2, -87.1, -86.5, -86.5,
48
                         -86.5, -88.1]) * units.degC
49
    pw = precipitable_water(dewpoint, pressure)
50
    truth = 89.86955998646951 * units('millimeters')
51
    assert_array_equal(pw, truth)
52
53
54
def test_mean_pressure_weighted():
55 View Code Duplication
    """Test pressure-weighted mean wind function with vertical interpolation."""
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
56
    with UseSampleData():
57
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
58
    u, v = mean_pressure_weighted(data.variables['pressure'][:],
59
                                  data.variables['u_wind'][:],
60
                                  data.variables['v_wind'][:],
61
                                  heights=data.variables['height'][:],
62
                                  depth=6000 * units('meter'))
63
    assert_almost_equal(u, 6.0208700094534775 * units('m/s'), 7)
64
    assert_almost_equal(v, 7.966031839967931 * units('m/s'), 7)
65
66
67
def test_mean_pressure_weighted_elevated():
68
    """Test pressure-weighted mean wind function with a base above the surface."""
69
    with UseSampleData():
70
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
71
    u, v = mean_pressure_weighted(data.variables['pressure'][:],
72
                                  data.variables['u_wind'][:],
73
                                  data.variables['v_wind'][:],
74
                                  heights=data.variables['height'][:],
75
                                  depth=3000 * units('meter'),
76
                                  bottom=data.variables['height'][0] + 3000 * units('meter'))
77
    assert_almost_equal(u, 8.270829843626476 * units('m/s'), 7)
78
    assert_almost_equal(v, 1.7392601775853547 * units('m/s'), 7)
79
80
81 View Code Duplication
def test_bunkers_motion():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
82
    """Test Bunkers storm motion with observed sounding."""
83
    with UseSampleData():
84
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
85
    motion = concatenate(bunkers_storm_motion(data.variables['pressure'][:],
86
                         data.variables['u_wind'][:], data.variables['v_wind'][:],
87
                         data.variables['height'][:]))
88
    truth = [1.4537892577864744, 2.0169333025630616, 10.587950761120482, 13.915130377372801,
89
             6.0208700094534775, 7.9660318399679308] * units('m/s')
90
    assert_almost_equal(motion.flatten(), truth, 8)
91
92
93 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...
94
    """Test bulk shear with observed sounding."""
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
                      depth=6000 * units('meter'))
100
    truth = [29.899581266946115, -14.389225800205509] * units('knots')
101
    assert_almost_equal(u.to('knots'), truth[0], 8)
102
    assert_almost_equal(v.to('knots'), truth[1], 8)
103
104
105
def test_bulk_shear_no_depth():
106
    """Test bulk shear with observed sounding and no depth given. Issue #568."""
107
    with UseSampleData():
108
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
109
    u, v = bulk_shear(data.variables['pressure'][:], data.variables['u_wind'][:],
110
                      data.variables['v_wind'][:], heights=data.variables['height'][:])
111
    truth = [20.225018939, 22.602359692] * units('knots')
112
    assert_almost_equal(u.to('knots'), truth[0], 8)
113
    assert_almost_equal(v.to('knots'), truth[1], 8)
114
115
116
def test_bulk_shear_elevated():
117
    """Test bulk shear with observed sounding and a base above the surface."""
118
    with UseSampleData():
119
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
120
    u, v = bulk_shear(data.variables['pressure'][:], data.variables['u_wind'][:],
121
                      data.variables['v_wind'][:], heights=data.variables['height'][:],
122
                      bottom=data.variables['height'][0] + 3000 * units('meter'),
123
                      depth=3000 * units('meter'))
124
    truth = [0.9655943923302139, -3.8405428777944466] * units('m/s')
125
    assert_almost_equal(u, truth[0], 8)
126
    assert_almost_equal(v, truth[1], 8)
127
128
129
def test_supercell_composite():
130
    """Test supercell composite function."""
131
    mucape = [2000., 1000., 500., 2000.] * units('J/kg')
132
    esrh = [400., 150., 45., 45.] * units('m^2/s^2')
133
    ebwd = [30., 15., 5., 5.] * units('m/s')
134
    truth = [16., 2.25, 0., 0.]
135
    supercell_comp = supercell_composite(mucape, esrh, ebwd)
136
    assert_array_equal(supercell_comp, truth)
137
138
139
def test_sigtor():
140
    """Test significant tornado parameter function."""
141
    sbcape = [2000., 2000., 2000., 2000., 3000, 4000] * units('J/kg')
142
    sblcl = [3000., 1500., 500., 1500., 1500, 800] * units('meter')
143
    srh1 = [200., 200., 200., 200., 300, 400] * units('m^2/s^2')
144
    shr6 = [20., 5., 20., 35., 20., 35] * units('m/s')
145
    truth = [0., 0, 1.777778, 1.333333, 2., 10.666667]
146
    sigtor = significant_tornado(sbcape, sblcl, srh1, shr6)
147
    assert_almost_equal(sigtor, truth, 6)
148