Completed
Pull Request — master (#377)
by Ryan
01:32
created

test_pressure_to_heights_units()   A

Complexity

Conditions 1

Size

Total Lines 3

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 3
rs 10
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
"""Test the `basic` module."""
5
6
import numpy as np
7
8
from metpy.calc import (coriolis_parameter, get_wind_components, get_wind_dir, get_wind_speed,
9
                        heat_index, pressure_to_height_std, windchill)
10
from metpy.testing import assert_almost_equal, assert_array_almost_equal, assert_array_equal
11
from metpy.units import units
12
13
14
def test_wind_comps_basic():
15
    """Test the basic wind component calculation."""
16
    speed = np.array([4, 4, 4, 4, 25, 25, 25, 25, 10.]) * units.mph
17
    dirs = np.array([0, 45, 90, 135, 180, 225, 270, 315, 360]) * units.deg
18
    s2 = np.sqrt(2.)
19
20
    u, v = get_wind_components(speed, dirs)
21
22
    true_u = np.array([0, -4 / s2, -4, -4 / s2, 0, 25 / s2, 25, 25 / s2, 0]) * units.mph
23
    true_v = np.array([-4, -4 / s2, 0, 4 / s2, 25, 25 / s2, 0, -25 / s2, -10]) * units.mph
24
25
    assert_array_almost_equal(true_u, u, 4)
26
    assert_array_almost_equal(true_v, v, 4)
27
28
29
def test_wind_comps_scalar():
30
    """Test wind components calculation with scalars."""
31
    u, v = get_wind_components(8 * units('m/s'), 150 * units.deg)
32
    assert_almost_equal(u, -4 * units('m/s'), 3)
33
    assert_almost_equal(v, 6.9282 * units('m/s'), 3)
34
35
36
def test_speed():
37
    """Test calculating wind speed."""
38
    u = np.array([4., 2., 0., 0.]) * units('m/s')
39
    v = np.array([0., 2., 4., 0.]) * units('m/s')
40
41
    speed = get_wind_speed(u, v)
42
43
    s2 = np.sqrt(2.)
44
    true_speed = np.array([4., 2 * s2, 4., 0.]) * units('m/s')
45
46
    assert_array_almost_equal(true_speed, speed, 4)
47
48
49
def test_dir():
50
    """Test calculating wind direction."""
51
    u = np.array([4., 2., 0., 0.]) * units('m/s')
52
    v = np.array([0., 2., 4., 0.]) * units('m/s')
53
54
    direc = get_wind_dir(u, v)
55
56
    true_dir = np.array([270., 225., 180., 270.]) * units.deg
57
58
    assert_array_almost_equal(true_dir, direc, 4)
59
60
61
def test_speed_dir_roundtrip():
62
    """Test round-tripping between speed/direction and components."""
63
    # Test each quadrant of the whole circle
64
    wspd = np.array([15., 5., 2., 10.]) * units.meters / units.seconds
65
    wdir = np.array([160., 30., 225., 350.]) * units.degrees
66
67
    u, v = get_wind_components(wspd, wdir)
68
69
    wdir_out = get_wind_dir(u, v)
70
    wspd_out = get_wind_speed(u, v)
71
72
    assert_array_almost_equal(wspd, wspd_out, 4)
73
    assert_array_almost_equal(wdir, wdir_out, 4)
74
75
76
def test_scalar_speed():
77
    """Test wind speed with scalars."""
78
    s = get_wind_speed(-3. * units('m/s'), -4. * units('m/s'))
79
    assert_almost_equal(s, 5. * units('m/s'), 3)
80
81
82
def test_scalar_dir():
83
    """Test wind direction with scalars."""
84
    d = get_wind_dir(3. * units('m/s'), 4. * units('m/s'))
85
    assert_almost_equal(d, 216.870 * units.deg, 3)
86
87
88
def test_windchill_scalar():
89
    """Test wind chill with scalars."""
90
    wc = windchill(-5 * units.degC, 35 * units('m/s'))
91
    assert_almost_equal(wc, -18.9357 * units.degC, 0)
92
93
94
def test_windchill_basic():
95
    """Test the basic wind chill calculation."""
96
    temp = np.array([40, -10, -45, 20]) * units.degF
97
    speed = np.array([5, 55, 25, 15]) * units.mph
98
99
    wc = windchill(temp, speed)
100
    values = np.array([36, -46, -84, 6]) * units.degF
101
    assert_array_almost_equal(wc, values, 0)
102
103
104
def test_windchill_invalid():
105
    """Test windchill for values that should be masked."""
106
    temp = np.array([10, 51, 49, 60, 80, 81]) * units.degF
107
    speed = np.array([4, 4, 3, 1, 10, 39]) * units.mph
108
109
    wc = windchill(temp, speed)
110
    # We don't care about the masked values
111
    truth = np.array([2.6230789, np.nan, np.nan, np.nan, np.nan, np.nan]) * units.degF
112
    mask = np.array([False, True, True, True, True, True])
113
    assert_array_almost_equal(truth, wc)
114
    assert_array_equal(wc.mask, mask)
115
116
117
def test_windchill_undefined_flag():
118
    """Test whether masking values for windchill can be disabled."""
119
    temp = units.Quantity(np.ma.array([49, 50, 49, 60, 80, 81]), units.degF)
120
    speed = units.Quantity(([4, 4, 3, 1, 10, 39]), units.mph)
121
122
    wc = windchill(temp, speed, mask_undefined=False)
123
    mask = np.array([False] * 6)
124
    assert_array_equal(wc.mask, mask)
125
126
127
def test_windchill_face_level():
128
    """Test windchill using the face_level flag."""
129
    temp = np.array([20, 0, -20, -40]) * units.degF
130
    speed = np.array([15, 30, 45, 60]) * units.mph
131
132
    wc = windchill(temp, speed, face_level_winds=True)
133
    values = np.array([3, -30, -64, -98]) * units.degF
134
    assert_array_almost_equal(wc, values, 0)
135
136
137
def test_heat_index_basic():
138
    """Test the basic heat index calculation."""
139
    temp = np.array([80, 88, 92, 110]) * units.degF
140
    rh = np.array([40, 100, 70, 40]) * units.percent
141
142
    hi = heat_index(temp, rh)
143
    values = np.array([80, 121, 112, 136]) * units.degF
144
    assert_array_almost_equal(hi, values, 0)
145
146
147
def test_heat_index_scalar():
148
    """Test heat index using scalars."""
149
    hi = heat_index(96 * units.degF, 65 * units.percent)
150
    assert_almost_equal(hi, 121 * units.degF, 0)
151
152
153
def test_heat_index_invalid():
154
    """Test heat index for values that should be masked."""
155
    temp = np.array([80, 88, 92, 79, 30, 81]) * units.degF
156
    rh = np.array([40, 39, 2, 70, 50, 39]) * units.percent
157
158
    hi = heat_index(temp, rh)
159
    mask = np.array([False, True, True, True, True, True])
160
    assert_array_equal(hi.mask, mask)
161
162
163
def test_heat_index_undefined_flag():
164
    """Test whether masking values can be disabled for heat index."""
165
    temp = units.Quantity(np.ma.array([80, 88, 92, 79, 30, 81]), units.degF)
166
    rh = np.ma.array([40, 39, 2, 70, 50, 39]) * units.percent
167
168
    hi = heat_index(temp, rh, mask_undefined=False)
169
    mask = np.array([False] * 6)
170
    assert_array_equal(hi.mask, mask)
171
172
173
def test_heat_index_units():
174
    """Test units coming out of heat index."""
175
    temp = units.Quantity([35., 20.], units.degC)
176
    rh = 70 * units.percent
177
    hi = heat_index(temp, rh)
178
    assert_almost_equal(hi.to('degC'), units.Quantity([50.3405, np.nan], units.degC), 4)
179
180
181
def test_heat_index_ratio():
182
    """Test giving humidity as number [0, 1] to heat index."""
183
    temp = units.Quantity([35., 20.], units.degC)
184
    rh = 0.7
185
    hi = heat_index(temp, rh)
186
    assert_almost_equal(hi.to('degC'), units.Quantity([50.3405, np.nan], units.degC), 4)
187
188
# class TestIrrad(object):
189
#    def test_basic(self):
190
#        'Test the basic solar irradiance calculation.'
191
#        from datetime import date
192
193
#        d = date(2008, 9, 28)
194
#        lat = 35.25
195
#        hours = np.linspace(6,18,10)
196
197
#        s = solar_irradiance(lat, d, hours)
198
#        values = np.array([0., 344.1, 682.6, 933.9, 1067.6, 1067.6, 933.9,
199
#            682.6, 344.1, 0.])
200
#        assert_array_almost_equal(s, values, 1)
201
202
#    def test_scalar(self):
203
#        from datetime import date
204
#        d = date(2008, 9, 28)
205
#        lat = 35.25
206
#        hour = 9.5
207
#        s = solar_irradiance(lat, d, hour)
208
#        assert_almost_equal(s, 852.1, 1)
209
210
#    def test_invalid(self):
211
#        'Test for values that should be masked.'
212
#        from datetime import date
213
#        d = date(2008, 9, 28)
214
#        lat = 35.25
215
#        hours = np.linspace(0,22,12)
216
#        s = solar_irradiance(lat, d, hours)
217
218
#        mask = np.array([ True,  True,  True,  True, False, False, False,
219
#            False, False, True,  True,  True])
220
#        assert_array_equal(s.mask, mask)
221
222
223
def test_pressure_to_heights_basic():
224
    """Test basic pressure to height calculation for standard atmosphere."""
225
    pressures = np.array([975.2, 987.5, 956., 943.]) * units.mbar
226
    heights = pressure_to_height_std(pressures)
227
    values = np.array([321.5, 216.5, 487.6, 601.7]) * units.meter
228
    assert_almost_equal(heights, values, 1)
229
230
231
def test_pressure_to_heights_units():
232
    """Test that passing non-mbar units works."""
233
    assert_almost_equal(pressure_to_height_std(29 * units.inHg), 262.859 * units.meter, 3)
234
235
236
def test_coriolis_force():
237
    """Test basic coriolis force calculation."""
238
    lat = np.array([-90., -30., 0., 30., 90.]) * units.degrees
239
    cor = coriolis_parameter(lat)
240
    values = np.array([-1.4584232E-4, -.72921159E-4, 0, .72921159E-4,
241
                       1.4584232E-4]) * units('s^-1')
242
    assert_almost_equal(cor, values, 7)
243