Completed
Pull Request — master (#602)
by
unknown
50s
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
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
import numpy as np
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_precipitable_water_bound_error():
43
    """Test with no top bound given and data that produced floating point issue #596."""
44
    p = np.array([993., 978., 960.5, 927.6, 925., 895.8, 892., 876., 45.9, 39.9, 36., 36.,
45
                  34.3]) * units.hPa
46
    Td = np.array([25.5, 24.1, 23.1, 21.2, 21.1, 19.4, 19.2, 19.2, -87.1, -86.5, -86.5, -86.5,
47
                   -88.1]) * units.degC
48
    pw = precipitable_water(Td, p)
49
    truth = 89.86955998646951 * units('millimeters')
50
    assert_array_equal(pw, truth)
51
52
53
def test_mean_pressure_weighted():
54
    """Test pressure-weighted mean wind function with vertical interpolation."""
55 View Code Duplication
    with UseSampleData():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
56
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
57
    u, v = mean_pressure_weighted(data.variables['pressure'][:],
58
                                  data.variables['u_wind'][:],
59
                                  data.variables['v_wind'][:],
60
                                  heights=data.variables['height'][:],
61
                                  depth=6000 * units('meter'))
62
    assert_almost_equal(u, 6.0208700094534775 * units('m/s'), 7)
63
    assert_almost_equal(v, 7.966031839967931 * units('m/s'), 7)
64
65
66
def test_mean_pressure_weighted_elevated():
67
    """Test pressure-weighted mean wind function with a base above the surface."""
68
    with UseSampleData():
69
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
70
    u, v = mean_pressure_weighted(data.variables['pressure'][:],
71
                                  data.variables['u_wind'][:],
72
                                  data.variables['v_wind'][:],
73
                                  heights=data.variables['height'][:],
74
                                  depth=3000 * units('meter'),
75
                                  bottom=data.variables['height'][0] + 3000 * units('meter'))
76
    assert_almost_equal(u, 8.270829843626476 * units('m/s'), 7)
77
    assert_almost_equal(v, 1.7392601775853547 * units('m/s'), 7)
78
79
80
def test_bunkers_motion():
81 View Code Duplication
    """Test Bunkers storm motion with observed sounding."""
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
82
    with UseSampleData():
83
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
84
    motion = concatenate(bunkers_storm_motion(data.variables['pressure'][:],
85
                         data.variables['u_wind'][:], data.variables['v_wind'][:],
86
                         data.variables['height'][:]))
87
    truth = [1.4537892577864744, 2.0169333025630616, 10.587950761120482, 13.915130377372801,
88
             6.0208700094534775, 7.9660318399679308] * units('m/s')
89
    assert_almost_equal(motion.flatten(), truth, 8)
90
91
92
def test_bulk_shear():
93 View Code Duplication
    """Test bulk shear with observed sounding."""
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
94
    with UseSampleData():
95
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
96
    u, v = bulk_shear(data.variables['pressure'][:], data.variables['u_wind'][:],
97
                      data.variables['v_wind'][:], heights=data.variables['height'][:],
98
                      depth=6000 * units('meter'))
99
    truth = [29.899581266946115, -14.389225800205509] * 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_no_depth():
105
    """Test bulk shear with observed sounding and no depth given. Issue #568."""
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
    truth = [20.225018939, 22.602359692] * units('knots')
111
    assert_almost_equal(u.to('knots'), truth[0], 8)
112
    assert_almost_equal(v.to('knots'), truth[1], 8)
113
114
115
def test_bulk_shear_elevated():
116
    """Test bulk shear with observed sounding and a base above the surface."""
117
    with UseSampleData():
118
        data = get_upper_air_data(datetime(2016, 5, 22, 0), 'DDC', source='wyoming')
119
    u, v = bulk_shear(data.variables['pressure'][:], data.variables['u_wind'][:],
120
                      data.variables['v_wind'][:], heights=data.variables['height'][:],
121
                      bottom=data.variables['height'][0] + 3000 * units('meter'),
122
                      depth=3000 * units('meter'))
123
    truth = [0.9655943923302139, -3.8405428777944466] * units('m/s')
124
    assert_almost_equal(u, truth[0], 8)
125
    assert_almost_equal(v, truth[1], 8)
126
127
128
def test_supercell_composite():
129
    """Test supercell composite function."""
130
    mucape = [2000., 1000., 500., 2000.] * units('J/kg')
131
    esrh = [400., 150., 45., 45.] * units('m^2/s^2')
132
    ebwd = [30., 15., 5., 5.] * units('m/s')
133
    truth = [16., 2.25, 0., 0.]
134
    supercell_comp = supercell_composite(mucape, esrh, ebwd)
135
    assert_array_equal(supercell_comp, truth)
136
137
138
def test_sigtor():
139
    """Test significant tornado parameter function."""
140
    sbcape = [2000., 2000., 2000., 2000., 3000, 4000] * units('J/kg')
141
    sblcl = [3000., 1500., 500., 1500., 1500, 800] * units('meter')
142
    srh1 = [200., 200., 200., 200., 300, 400] * units('m^2/s^2')
143
    shr6 = [20., 5., 20., 35., 20., 35] * units('m/s')
144
    truth = [0., 0, 1.777778, 1.333333, 2., 10.666667]
145
    sigtor = significant_tornado(sbcape, sblcl, srh1, shr6)
146
    assert_almost_equal(sigtor, truth, 6)
147