Completed
Push — master ( 3a7cd6...e31be5 )
by Ryan
01:40
created

test_el()   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 `thermo` module."""
5
6
import numpy as np
7
import pytest
8
9
from metpy.calc import (density, dewpoint, dewpoint_rh, dry_lapse, el,
10
                        equivalent_potential_temperature, lcl, lfc, mixing_ratio, moist_lapse,
11
                        parcel_profile, potential_temperature, saturation_mixing_ratio,
12
                        saturation_vapor_pressure, vapor_pressure,
13
                        virtual_potential_temperature, virtual_temperature)
14
15
from metpy.testing import assert_almost_equal, assert_array_almost_equal
16
from metpy.units import units
17
18
19
def test_potential_temperature():
20
    """Test potential_temperature calculation."""
21
    temp = np.array([278., 283., 291., 298.]) * units.kelvin
22
    pres = np.array([900., 500., 300., 100.]) * units.mbar
23
    real_th = np.array([286.493, 344.961, 410.4335, 575.236]) * units.kelvin
24
    assert_array_almost_equal(potential_temperature(pres, temp), real_th, 3)
25
26
27
def test_scalar():
28
    """Test potential_temperature accepts scalar values."""
29
    assert_almost_equal(potential_temperature(1000. * units.mbar, 293. * units.kelvin),
30
                        293. * units.kelvin, 4)
31
    assert_almost_equal(potential_temperature(800. * units.mbar, 293. * units.kelvin),
32
                        312.2828 * units.kelvin, 4)
33
34
35
def test_fahrenheit():
36
    """Test that potential_temperature handles temperature values in Fahrenheit."""
37
    assert_almost_equal(potential_temperature(800. * units.mbar, 68. * units.degF),
38
                        (312.444 * units.kelvin).to(units.degF), 2)
39
40
41
def test_pot_temp_inhg():
42
    """Test that potential_temperature can handle pressure not in mb (issue #165)."""
43
    assert_almost_equal(potential_temperature(29.92 * units.inHg, 29 * units.degC),
44
                        301.019735 * units.kelvin, 4)
45
46
47
def test_dry_lapse():
48
    """Test dry_lapse calculation."""
49
    levels = np.array([1000, 900, 864.89]) * units.mbar
50
    temps = dry_lapse(levels, 303.15 * units.kelvin)
51
    assert_array_almost_equal(temps,
52
                              np.array([303.15, 294.16, 290.83]) * units.kelvin, 2)
53
54
55
def test_dry_lapse_2_levels():
56
    """Test dry_lapse calculation when given only two levels."""
57
    temps = dry_lapse(np.array([1000., 500.]) * units.mbar, 293. * units.kelvin)
58
    assert_array_almost_equal(temps, [293., 240.3723] * units.kelvin, 4)
59
60
61
def test_moist_lapse():
62
    """Test moist_lapse calculation."""
63
    temp = moist_lapse(np.array([1000., 800., 600., 500., 400.]) * units.mbar,
64
                       293. * units.kelvin)
65
    true_temp = np.array([293, 284.64, 272.81, 264.42, 252.91]) * units.kelvin
66
    assert_array_almost_equal(temp, true_temp, 2)
67
68
69
def test_moist_lapse_degc():
70
    """Test moist_lapse with Celsius temperatures."""
71
    temp = moist_lapse(np.array([1000., 800., 600., 500., 400.]) * units.mbar,
72
                       19.85 * units.degC)
73
    true_temp = np.array([293, 284.64, 272.81, 264.42, 252.91]) * units.kelvin
74
    assert_array_almost_equal(temp, true_temp, 2)
75
76
77
def test_parcel_profile():
78
    """Test parcel profile calculation."""
79
    levels = np.array([1000., 900., 800., 700., 600., 500., 400.]) * units.mbar
80
    true_prof = np.array([303.15, 294.16, 288.026, 283.073, 277.058, 269.402,
81
                          258.966]) * units.kelvin
82
83
    prof = parcel_profile(levels, 30. * units.degC, 20. * units.degC)
84
    assert_array_almost_equal(prof, true_prof, 2)
85
86
87
def test_parcel_profile_saturated():
88
    """Test parcel_profile works when LCL in levels (issue #232)."""
89
    levels = np.array([1000., 700., 500.]) * units.mbar
90
    true_prof = np.array([296.95, 284.381, 271.123]) * units.kelvin
91
92
    prof = parcel_profile(levels, 23.8 * units.degC, 23.8 * units.degC)
93
    assert_array_almost_equal(prof, true_prof, 2)
94
95
96
def test_sat_vapor_pressure():
97
    """Test saturation_vapor_pressure calculation."""
98
    temp = np.array([5., 10., 18., 25.]) * units.degC
99
    real_es = np.array([8.72, 12.27, 20.63, 31.67]) * units.mbar
100
    assert_array_almost_equal(saturation_vapor_pressure(temp), real_es, 2)
101
102
103
def test_sat_vapor_pressure_scalar():
104
    """Test saturation_vapor_pressure handles scalar values."""
105
    es = saturation_vapor_pressure(0 * units.degC)
106
    assert_almost_equal(es, 6.112 * units.mbar, 3)
107
108
109
def test_sat_vapor_pressure_fahrenheit():
110
    """Test saturation_vapor_pressure handles temperature in Fahrenheit."""
111
    temp = np.array([50., 68.]) * units.degF
112
    real_es = np.array([12.2717, 23.3695]) * units.mbar
113
    assert_array_almost_equal(saturation_vapor_pressure(temp), real_es, 4)
114
115
116
def test_basic_dewpoint_rh():
117
    """Test dewpoint_rh function."""
118
    temp = np.array([30., 25., 10., 20., 25.]) * units.degC
119
    rh = np.array([30., 45., 55., 80., 85.]) / 100.
120
121
    real_td = np.array([11, 12, 1, 16, 22]) * units.degC
122
    assert_array_almost_equal(real_td, dewpoint_rh(temp, rh), 0)
123
124
125
def test_scalar_dewpoint_rh():
126
    """Test dewpoint_rh with scalar values."""
127
    td = dewpoint_rh(10.6 * units.degC, 0.37)
128
    assert_almost_equal(td, 26. * units.degF, 0)
129
130
131
def test_dewpoint():
132
    """Test dewpoint calculation."""
133
    assert_almost_equal(dewpoint(6.112 * units.mbar), 0. * units.degC, 2)
134
135
136
def test_dewpoint_weird_units():
137
    """Test dewpoint using non-standard units.
138
139
    Revealed from odd dimensionless units and ending up using numpy.ma math
140
    functions instead of numpy ones.
141
    """
142
    assert_almost_equal(dewpoint(15825.6 * units('g * mbar / kg')),
143
                        13.8564 * units.degC, 4)
144
145
146
def test_mixing_ratio():
147
    """Test mixing ratio calculation."""
148
    p = 998. * units.mbar
149
    e = 73.75 * units.mbar
150
    assert_almost_equal(mixing_ratio(e, p), 0.04963, 2)
151
152
153
def test_vapor_pressure():
154
    """Test vapor pressure calculation."""
155
    assert_almost_equal(vapor_pressure(998. * units.mbar, 0.04963),
156
                        73.74925 * units.mbar, 5)
157
158
159
def test_lcl():
160
    """Test LCL calculation."""
161
    lcl_pressure, lcl_temperature = lcl(1000. * units.mbar, 30. * units.degC, 20. * units.degC)
162
    assert_almost_equal(lcl_pressure, 864.761 * units.mbar, 2)
163
    assert_almost_equal(lcl_temperature, 17.676 * units.degC, 2)
164
165
166
def test_lcl_convergence():
167
    """Test LCL calculation convergence failure."""
168
    with pytest.raises(RuntimeError):
169
        lcl(1000. * units.mbar, 30. * units.degC, 20. * units.degC, max_iters=2)
170
171
172
def test_lfc_basic():
173
    """Test LFC calculation."""
174
    levels = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
175
    temperatures = np.array([22.2, 14.6, 12., 9.4, 7., -49.]) * units.celsius
176
    dewpoints = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
177
    l = lfc(levels, temperatures, dewpoints)
178
    assert_almost_equal(l[0], 727.468 * units.mbar, 2)
179
    assert_almost_equal(l[1], 9.705 * units.celsius, 2)
180
181
182
def test_no_lfc():
183
    """Test LFC calculation when there is no LFC in the data."""
184
    levels = np.array([959., 867.9, 779.2, 647.5, 472.5, 321.9, 251.]) * units.mbar
185
    temperatures = np.array([22.2, 17.4, 14.6, 1.4, -17.6, -39.4, -52.5]) * units.celsius
186
    dewpoints = np.array([9., 4.3, -21.2, -26.7, -31., -53.3, -66.7]) * units.celsius
187
    lfc_pressure, lfc_temperature = lfc(levels, temperatures, dewpoints)
188
    assert lfc_pressure is None
189
    assert lfc_temperature is None
190
191
192
def test_saturation_mixing_ratio():
193
    """Test saturation mixing ratio calculation."""
194
    p = 999. * units.mbar
195
    t = 288. * units.kelvin
196
    assert_almost_equal(saturation_mixing_ratio(p, t), .01068, 3)
197
198
199
def test_equivalent_potential_temperature():
200
    """Test equivalent potential temperature calculation."""
201
    p = 999. * units.mbar
202
    t = 288. * units.kelvin
203
    ept = equivalent_potential_temperature(p, t)
204
    assert_almost_equal(ept, 315.9548 * units.kelvin, 3)
205
206
207
def test_virtual_temperature():
208
    """Test virtual temperature calculation."""
209
    t = 288. * units.kelvin
210
    qv = .0016  # kg/kg
211
    tv = virtual_temperature(t, qv)
212
    assert_almost_equal(tv, 288.2796 * units.kelvin, 3)
213
214
215
def test_virtual_potential_temperature():
216
    """Test virtual potential temperature calculation."""
217
    p = 999. * units.mbar
218
    t = 288. * units.kelvin
219
    qv = .0016  # kg/kg
220
    theta_v = virtual_potential_temperature(p, t, qv)
221
    assert_almost_equal(theta_v, 288.3620 * units.kelvin, 3)
222
223
224
def test_density():
225
    """Test density calculation."""
226
    p = 999. * units.mbar
227
    t = 288. * units.kelvin
228
    qv = .0016  # kg/kg
229
    rho = density(p, t, qv).to(units.kilogram / units.meter ** 3)
230
    assert_almost_equal(rho, 1.2072 * (units.kilogram / units.meter ** 3), 3)
231
232
233
def test_el():
234
    """Test equilibrium layer calculation."""
235
    levels = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
236
    temperatures = np.array([22.2, 14.6, 12., 9.4, 7., -38.]) * units.celsius
237
    dewpoints = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
238
    el_pressure, el_temperature = el(levels, temperatures, dewpoints)
239
    assert_almost_equal(el_pressure, 520.8420 * units.mbar, 3)
240
    assert_almost_equal(el_temperature, -11.7055 * units.degC, 3)
241
242
243
def test_no_el():
244
    """Test equilibrium layer calculation when there is no EL in the data."""
245
    levels = np.array([959., 867.9, 779.2, 647.5, 472.5, 321.9, 251.]) * units.mbar
246
    temperatures = np.array([22.2, 17.4, 14.6, 1.4, -17.6, -39.4, -52.5]) * units.celsius
247
    dewpoints = np.array([19., 14.3, -11.2, -16.7, -21., -43.3, -56.7]) * units.celsius
248
    el_pressure, el_temperature = el(levels, temperatures, dewpoints)
249
    assert el_pressure is None
250
    assert el_temperature is None
251