Completed
Push — master ( f02dbf...1f8181 )
by Ryan
16s
created

test_heights_to_pressure_basic()   A

Complexity

Conditions 1

Size

Total Lines 6

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 6
rs 9.4285
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, height_to_pressure_std, 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_kelvin():
105
    """Test wind chill when given Kelvin temperatures."""
106
    wc = windchill(268.15 * units.kelvin, 35 * units('m/s'))
107
    assert_almost_equal(wc, -18.9357 * units.degC, 0)
108
109
110
def test_windchill_invalid():
111
    """Test windchill for values that should be masked."""
112
    temp = np.array([10, 51, 49, 60, 80, 81]) * units.degF
113
    speed = np.array([4, 4, 3, 1, 10, 39]) * units.mph
114
115
    wc = windchill(temp, speed)
116
    # We don't care about the masked values
117
    truth = np.array([2.6230789, np.nan, np.nan, np.nan, np.nan, np.nan]) * units.degF
118
    mask = np.array([False, True, True, True, True, True])
119
    assert_array_almost_equal(truth, wc)
120
    assert_array_equal(wc.mask, mask)
121
122
123
def test_windchill_undefined_flag():
124
    """Test whether masking values for windchill can be disabled."""
125
    temp = units.Quantity(np.ma.array([49, 50, 49, 60, 80, 81]), units.degF)
126
    speed = units.Quantity(([4, 4, 3, 1, 10, 39]), units.mph)
127
128
    wc = windchill(temp, speed, mask_undefined=False)
129
    mask = np.array([False] * 6)
130
    assert_array_equal(wc.mask, mask)
131
132
133
def test_windchill_face_level():
134
    """Test windchill using the face_level flag."""
135
    temp = np.array([20, 0, -20, -40]) * units.degF
136
    speed = np.array([15, 30, 45, 60]) * units.mph
137
138
    wc = windchill(temp, speed, face_level_winds=True)
139
    values = np.array([3, -30, -64, -98]) * units.degF
140
    assert_array_almost_equal(wc, values, 0)
141
142
143
def test_heat_index_basic():
144
    """Test the basic heat index calculation."""
145
    temp = np.array([80, 88, 92, 110]) * units.degF
146
    rh = np.array([40, 100, 70, 40]) * units.percent
147
148
    hi = heat_index(temp, rh)
149
    values = np.array([80, 121, 112, 136]) * units.degF
150
    assert_array_almost_equal(hi, values, 0)
151
152
153
def test_heat_index_scalar():
154
    """Test heat index using scalars."""
155
    hi = heat_index(96 * units.degF, 65 * units.percent)
156
    assert_almost_equal(hi, 121 * units.degF, 0)
157
158
159
def test_heat_index_invalid():
160
    """Test heat index for values that should be masked."""
161
    temp = np.array([80, 88, 92, 79, 30, 81]) * units.degF
162
    rh = np.array([40, 39, 2, 70, 50, 39]) * units.percent
163
164
    hi = heat_index(temp, rh)
165
    mask = np.array([False, True, True, True, True, True])
166
    assert_array_equal(hi.mask, mask)
167
168
169
def test_heat_index_undefined_flag():
170
    """Test whether masking values can be disabled for heat index."""
171
    temp = units.Quantity(np.ma.array([80, 88, 92, 79, 30, 81]), units.degF)
172
    rh = np.ma.array([40, 39, 2, 70, 50, 39]) * units.percent
173
174
    hi = heat_index(temp, rh, mask_undefined=False)
175
    mask = np.array([False] * 6)
176
    assert_array_equal(hi.mask, mask)
177
178
179
def test_heat_index_units():
180
    """Test units coming out of heat index."""
181
    temp = units.Quantity([35., 20.], units.degC)
182
    rh = 70 * units.percent
183
    hi = heat_index(temp, rh)
184
    assert_almost_equal(hi.to('degC'), units.Quantity([50.3405, np.nan], units.degC), 4)
185
186
187
def test_heat_index_ratio():
188
    """Test giving humidity as number [0, 1] to heat index."""
189
    temp = units.Quantity([35., 20.], units.degC)
190
    rh = 0.7
191
    hi = heat_index(temp, rh)
192
    assert_almost_equal(hi.to('degC'), units.Quantity([50.3405, np.nan], units.degC), 4)
193
194
# class TestIrrad(object):
195
#    def test_basic(self):
196
#        'Test the basic solar irradiance calculation.'
197
#        from datetime import date
198
199
#        d = date(2008, 9, 28)
200
#        lat = 35.25
201
#        hours = np.linspace(6,18,10)
202
203
#        s = solar_irradiance(lat, d, hours)
204
#        values = np.array([0., 344.1, 682.6, 933.9, 1067.6, 1067.6, 933.9,
205
#            682.6, 344.1, 0.])
206
#        assert_array_almost_equal(s, values, 1)
207
208
#    def test_scalar(self):
209
#        from datetime import date
210
#        d = date(2008, 9, 28)
211
#        lat = 35.25
212
#        hour = 9.5
213
#        s = solar_irradiance(lat, d, hour)
214
#        assert_almost_equal(s, 852.1, 1)
215
216
#    def test_invalid(self):
217
#        'Test for values that should be masked.'
218
#        from datetime import date
219
#        d = date(2008, 9, 28)
220
#        lat = 35.25
221
#        hours = np.linspace(0,22,12)
222
#        s = solar_irradiance(lat, d, hours)
223
224
#        mask = np.array([ True,  True,  True,  True, False, False, False,
225
#            False, False, True,  True,  True])
226
#        assert_array_equal(s.mask, mask)
227
228
229
def test_pressure_to_heights_basic():
230
    """Test basic pressure to height calculation for standard atmosphere."""
231
    pressures = np.array([975.2, 987.5, 956., 943.]) * units.mbar
232
    heights = pressure_to_height_std(pressures)
233
    values = np.array([321.5, 216.5, 487.6, 601.7]) * units.meter
234
    assert_almost_equal(heights, values, 1)
235
236
237
def test_heights_to_pressure_basic():
238
    """Test basic height to pressure calculation for standard atmosphere."""
239
    heights = np.array([321.5, 216.5, 487.6, 601.7]) * units.meter
240
    pressures = height_to_pressure_std(heights)
241
    values = np.array([975.2, 987.5, 956., 943.]) * units.mbar
242
    assert_almost_equal(pressures, values, 1)
243
244
245
def test_pressure_to_heights_units():
246
    """Test that passing non-mbar units works."""
247
    assert_almost_equal(pressure_to_height_std(29 * units.inHg), 262.859 * units.meter, 3)
248
249
250
def test_coriolis_force():
251
    """Test basic coriolis force calculation."""
252
    lat = np.array([-90., -30., 0., 30., 90.]) * units.degrees
253
    cor = coriolis_parameter(lat)
254
    values = np.array([-1.4584232E-4, -.72921159E-4, 0, .72921159E-4,
255
                       1.4584232E-4]) * units('s^-1')
256
    assert_almost_equal(cor, values, 7)
257