Completed
Push — master ( 10e33f...5d2a6a )
by Ryan
17s
created

test_sigma_to_pressure()   A

Complexity

Conditions 1

Size

Total Lines 8

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