Completed
Pull Request — master (#443)
by
unknown
02:31 queued 01:01
created

test_add_pressure_to_height()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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