Completed
Pull Request — master (#454)
by
unknown
11:16
created

test_isentropic_pressure_adition_args()   A

Complexity

Conditions 1

Size

Total Lines 19

Duplication

Lines 19
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 19
loc 19
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,
11
                        get_isentropic_pressure,
12
                        lcl, lfc, mixing_ratio,
13
                        mixing_ratio_from_specific_humidity,
14
                        moist_lapse,
15
                        parcel_profile, potential_temperature,
16
                        psychrometric_vapor_pressure_wet,
17
                        relative_humidity_from_mixing_ratio,
18
                        relative_humidity_from_specific_humidity,
19
                        relative_humidity_wet_psychrometric,
20
                        saturation_mixing_ratio,
21
                        saturation_vapor_pressure, vapor_pressure,
22
                        virtual_potential_temperature, virtual_temperature)
23
24
from metpy.testing import assert_almost_equal, assert_array_almost_equal, assert_nan
25
from metpy.units import units
26
27
28
def test_potential_temperature():
29
    """Test potential_temperature calculation."""
30
    temp = np.array([278., 283., 291., 298.]) * units.kelvin
31
    pres = np.array([900., 500., 300., 100.]) * units.mbar
32
    real_th = np.array([286.493, 344.961, 410.4335, 575.236]) * units.kelvin
33
    assert_array_almost_equal(potential_temperature(pres, temp), real_th, 3)
34
35
36
def test_scalar():
37
    """Test potential_temperature accepts scalar values."""
38
    assert_almost_equal(potential_temperature(1000. * units.mbar, 293. * units.kelvin),
39
                        293. * units.kelvin, 4)
40
    assert_almost_equal(potential_temperature(800. * units.mbar, 293. * units.kelvin),
41
                        312.2828 * units.kelvin, 4)
42
43
44
def test_fahrenheit():
45
    """Test that potential_temperature handles temperature values in Fahrenheit."""
46
    assert_almost_equal(potential_temperature(800. * units.mbar, 68. * units.degF),
47
                        (312.444 * units.kelvin).to(units.degF), 2)
48
49
50
def test_pot_temp_inhg():
51
    """Test that potential_temperature can handle pressure not in mb (issue #165)."""
52
    assert_almost_equal(potential_temperature(29.92 * units.inHg, 29 * units.degC),
53
                        301.019735 * units.kelvin, 4)
54
55
56
def test_dry_lapse():
57
    """Test dry_lapse calculation."""
58
    levels = np.array([1000, 900, 864.89]) * units.mbar
59
    temps = dry_lapse(levels, 303.15 * units.kelvin)
60
    assert_array_almost_equal(temps,
61
                              np.array([303.15, 294.16, 290.83]) * units.kelvin, 2)
62
63
64
def test_dry_lapse_2_levels():
65
    """Test dry_lapse calculation when given only two levels."""
66
    temps = dry_lapse(np.array([1000., 500.]) * units.mbar, 293. * units.kelvin)
67
    assert_array_almost_equal(temps, [293., 240.3723] * units.kelvin, 4)
68
69
70
def test_moist_lapse():
71
    """Test moist_lapse calculation."""
72
    temp = moist_lapse(np.array([1000., 800., 600., 500., 400.]) * units.mbar,
73
                       293. * units.kelvin)
74
    true_temp = np.array([293, 284.64, 272.81, 264.42, 252.91]) * units.kelvin
75
    assert_array_almost_equal(temp, true_temp, 2)
76
77
78
def test_moist_lapse_degc():
79
    """Test moist_lapse with Celsius temperatures."""
80
    temp = moist_lapse(np.array([1000., 800., 600., 500., 400.]) * units.mbar,
81
                       19.85 * units.degC)
82
    true_temp = np.array([293, 284.64, 272.81, 264.42, 252.91]) * units.kelvin
83
    assert_array_almost_equal(temp, true_temp, 2)
84
85
86
def test_parcel_profile():
87
    """Test parcel profile calculation."""
88
    levels = np.array([1000., 900., 800., 700., 600., 500., 400.]) * units.mbar
89
    true_prof = np.array([303.15, 294.16, 288.026, 283.073, 277.058, 269.402,
90
                          258.966]) * units.kelvin
91
92
    prof = parcel_profile(levels, 30. * units.degC, 20. * units.degC)
93
    assert_array_almost_equal(prof, true_prof, 2)
94
95
96
def test_parcel_profile_saturated():
97
    """Test parcel_profile works when LCL in levels (issue #232)."""
98
    levels = np.array([1000., 700., 500.]) * units.mbar
99
    true_prof = np.array([296.95, 284.381, 271.123]) * units.kelvin
100
101
    prof = parcel_profile(levels, 23.8 * units.degC, 23.8 * units.degC)
102
    assert_array_almost_equal(prof, true_prof, 2)
103
104
105
def test_sat_vapor_pressure():
106
    """Test saturation_vapor_pressure calculation."""
107
    temp = np.array([5., 10., 18., 25.]) * units.degC
108
    real_es = np.array([8.72, 12.27, 20.63, 31.67]) * units.mbar
109
    assert_array_almost_equal(saturation_vapor_pressure(temp), real_es, 2)
110
111
112
def test_sat_vapor_pressure_scalar():
113
    """Test saturation_vapor_pressure handles scalar values."""
114
    es = saturation_vapor_pressure(0 * units.degC)
115
    assert_almost_equal(es, 6.112 * units.mbar, 3)
116
117
118
def test_sat_vapor_pressure_fahrenheit():
119
    """Test saturation_vapor_pressure handles temperature in Fahrenheit."""
120
    temp = np.array([50., 68.]) * units.degF
121
    real_es = np.array([12.2717, 23.3695]) * units.mbar
122
    assert_array_almost_equal(saturation_vapor_pressure(temp), real_es, 4)
123
124
125
def test_basic_dewpoint_rh():
126
    """Test dewpoint_rh function."""
127
    temp = np.array([30., 25., 10., 20., 25.]) * units.degC
128
    rh = np.array([30., 45., 55., 80., 85.]) / 100.
129
130
    real_td = np.array([11, 12, 1, 16, 22]) * units.degC
131
    assert_array_almost_equal(real_td, dewpoint_rh(temp, rh), 0)
132
133
134
def test_scalar_dewpoint_rh():
135
    """Test dewpoint_rh with scalar values."""
136
    td = dewpoint_rh(10.6 * units.degC, 0.37)
137
    assert_almost_equal(td, 26. * units.degF, 0)
138
139
140
def test_dewpoint():
141
    """Test dewpoint calculation."""
142
    assert_almost_equal(dewpoint(6.112 * units.mbar), 0. * units.degC, 2)
143
144
145
def test_dewpoint_weird_units():
146
    """Test dewpoint using non-standard units.
147
148
    Revealed from odd dimensionless units and ending up using numpy.ma math
149
    functions instead of numpy ones.
150
    """
151
    assert_almost_equal(dewpoint(15825.6 * units('g * mbar / kg')),
152
                        13.8564 * units.degC, 4)
153
154
155
def test_mixing_ratio():
156
    """Test mixing ratio calculation."""
157
    p = 998. * units.mbar
158
    e = 73.75 * units.mbar
159
    assert_almost_equal(mixing_ratio(e, p), 0.04963, 2)
160
161
162
def test_vapor_pressure():
163
    """Test vapor pressure calculation."""
164
    assert_almost_equal(vapor_pressure(998. * units.mbar, 0.04963),
165
                        73.74925 * units.mbar, 5)
166
167
168
def test_lcl():
169
    """Test LCL calculation."""
170
    lcl_pressure, lcl_temperature = lcl(1000. * units.mbar, 30. * units.degC, 20. * units.degC)
171
    assert_almost_equal(lcl_pressure, 864.761 * units.mbar, 2)
172
    assert_almost_equal(lcl_temperature, 17.676 * units.degC, 2)
173
174
175
def test_lcl_convergence():
176
    """Test LCL calculation convergence failure."""
177
    with pytest.raises(RuntimeError):
178
        lcl(1000. * units.mbar, 30. * units.degC, 20. * units.degC, max_iters=2)
179
180
181
def test_lfc_basic():
182
    """Test LFC calculation."""
183
    levels = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
184
    temperatures = np.array([22.2, 14.6, 12., 9.4, 7., -49.]) * units.celsius
185
    dewpoints = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
186
    l = lfc(levels, temperatures, dewpoints)
187
    assert_almost_equal(l[0], 727.468 * units.mbar, 2)
188
    assert_almost_equal(l[1], 9.705 * units.celsius, 2)
189
190
191
def test_no_lfc():
192
    """Test LFC calculation when there is no LFC in the data."""
193
    levels = np.array([959., 867.9, 779.2, 647.5, 472.5, 321.9, 251.]) * units.mbar
194
    temperatures = np.array([22.2, 17.4, 14.6, 1.4, -17.6, -39.4, -52.5]) * units.celsius
195
    dewpoints = np.array([9., 4.3, -21.2, -26.7, -31., -53.3, -66.7]) * units.celsius
196
    lfc_pressure, lfc_temperature = lfc(levels, temperatures, dewpoints)
197
    assert assert_nan(lfc_pressure, levels.units)
198
    assert assert_nan(lfc_temperature, temperatures.units)
199
200
201
def test_lfc_inversion():
202
    """Test LFC when there is an inversion to be sure we don't pick that."""
203
    levels = np.array([963., 789., 782.3, 754.8, 728.1, 727., 700.,
204
                       571., 450., 300., 248.]) * units.mbar
205
    temperatures = np.array([25.4, 18.4, 17.8, 15.4, 12.9, 12.8,
206
                             10., -3.9, -16.3, -41.1, -51.5]) * units.celsius
207
    dewpoints = np.array([20.4, 0.4, -0.5, -4.3, -8., -8.2, -9.,
208
                          -23.9, -33.3, -54.1, -63.5]) * units.celsius
209
    l = lfc(levels, temperatures, dewpoints)
210
    assert_almost_equal(l[0], 706.0103 * units.mbar, 2)
211
    assert_almost_equal(l[1], 10.6232 * units.celsius, 2)
212
213
214
def test_saturation_mixing_ratio():
215
    """Test saturation mixing ratio calculation."""
216
    p = 999. * units.mbar
217
    t = 288. * units.kelvin
218
    assert_almost_equal(saturation_mixing_ratio(p, t), .01068, 3)
219
220
221
def test_equivalent_potential_temperature():
222
    """Test equivalent potential temperature calculation."""
223
    p = 999. * units.mbar
224
    t = 288. * units.kelvin
225
    ept = equivalent_potential_temperature(p, t)
226
    assert_almost_equal(ept, 315.9548 * units.kelvin, 3)
227
228
229
def test_virtual_temperature():
230
    """Test virtual temperature calculation."""
231
    t = 288. * units.kelvin
232
    qv = .0016  # kg/kg
233
    tv = virtual_temperature(t, qv)
234
    assert_almost_equal(tv, 288.2796 * units.kelvin, 3)
235
236
237
def test_virtual_potential_temperature():
238
    """Test virtual potential temperature calculation."""
239
    p = 999. * units.mbar
240
    t = 288. * units.kelvin
241
    qv = .0016  # kg/kg
242
    theta_v = virtual_potential_temperature(p, t, qv)
243
    assert_almost_equal(theta_v, 288.3620 * units.kelvin, 3)
244
245
246
def test_density():
247
    """Test density calculation."""
248
    p = 999. * units.mbar
249
    t = 288. * units.kelvin
250
    qv = .0016  # kg/kg
251
    rho = density(p, t, qv).to(units.kilogram / units.meter ** 3)
252
    assert_almost_equal(rho, 1.2072 * (units.kilogram / units.meter ** 3), 3)
253
254
255
def test_el():
256
    """Test equilibrium layer calculation."""
257
    levels = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
258
    temperatures = np.array([22.2, 14.6, 12., 9.4, 7., -38.]) * units.celsius
259
    dewpoints = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
260
    el_pressure, el_temperature = el(levels, temperatures, dewpoints)
261
    assert_almost_equal(el_pressure, 520.8700 * units.mbar, 3)
262
    assert_almost_equal(el_temperature, -11.7027 * units.degC, 3)
263
264
265
def test_no_el():
266
    """Test equilibrium layer calculation when there is no EL in the data."""
267
    levels = np.array([959., 867.9, 779.2, 647.5, 472.5, 321.9, 251.]) * units.mbar
268
    temperatures = np.array([22.2, 17.4, 14.6, 1.4, -17.6, -39.4, -52.5]) * units.celsius
269
    dewpoints = np.array([19., 14.3, -11.2, -16.7, -21., -43.3, -56.7]) * units.celsius
270
    el_pressure, el_temperature = el(levels, temperatures, dewpoints)
271
    assert assert_nan(el_pressure, levels.units)
272
    assert assert_nan(el_temperature, temperatures.units)
273
274
275
def test_wet_psychrometric_vapor_pressure():
276
    """Test calculation of vapor pressure from wet and dry bulb temperatures."""
277
    p = 1013.25 * units.mbar
278
    dry_bulb_temperature = 20. * units.degC
279
    wet_bulb_temperature = 18. * units.degC
280
    psychrometric_vapor_pressure = psychrometric_vapor_pressure_wet(dry_bulb_temperature,
281
                                                                    wet_bulb_temperature, p)
282
    assert_almost_equal(psychrometric_vapor_pressure, 19.3673 * units.mbar, 3)
283
284
285
def test_wet_psychrometric_rh():
286
    """Test calculation of relative humidity from wet and dry bulb temperatures."""
287
    p = 1013.25 * units.mbar
288
    dry_bulb_temperature = 20. * units.degC
289
    wet_bulb_temperature = 18. * units.degC
290
    psychrometric_rh = relative_humidity_wet_psychrometric(dry_bulb_temperature,
291
                                                           wet_bulb_temperature, p)
292
    assert_almost_equal(psychrometric_rh, 82.8747 * units.percent, 3)
293
294
295
def test_wet_psychrometric_rh_kwargs():
296
    """Test calculation of relative humidity from wet and dry bulb temperatures."""
297
    p = 1013.25 * units.mbar
298
    dry_bulb_temperature = 20. * units.degC
299
    wet_bulb_temperature = 18. * units.degC
300
    coeff = 6.1e-4 / units.kelvin
301
    psychrometric_rh = relative_humidity_wet_psychrometric(dry_bulb_temperature,
302
                                                           wet_bulb_temperature, p,
303
                                                           psychrometer_coefficient=coeff)
304
    assert_almost_equal(psychrometric_rh, 82.9701 * units.percent, 3)
305
306
307 View Code Duplication
def test_isentropic_pressure():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
308
    """Test calculation of isentropic pressure function."""
309
    lev = [100000., 95000., 90000., 85000.] * units.Pa
310
    tmp = np.ones((4, 5, 5))
311
    tmp[0, :] = 296.
312
    tmp[1, :] = 292.
313
    tmp[2, :] = 290
314
    tmp[3, :] = 288.
315
    tmpk = tmp * units.kelvin
316
    isentlev = [296.] * units.kelvin
317
    isentprs = get_isentropic_pressure(isentlev, lev, tmpk)
318
    trueprs = 1000. * units.hPa
319
    assert_almost_equal(isentprs[0], trueprs, 3)
320
321
322 View Code Duplication
def test_isentropic_pressure_p_increase():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
323
    """Test calculation of isentropic pressure function, p increasing order."""
324
    lev = [85000, 90000., 95000., 100000.] * units.Pa
325
    tmp = np.ones((4, 5, 5))
326
    tmp[0, :] = 288.
327
    tmp[1, :] = 290.
328
    tmp[2, :] = 292.
329
    tmp[3, :] = 296.
330
    tmpk = tmp * units.kelvin
331
    isentlev = [296.] * units.kelvin
332
    isentprs = get_isentropic_pressure(isentlev, lev, tmpk)
333
    trueprs = 1000. * units.hPa
334
    assert_almost_equal(isentprs[0], trueprs, 3)
335
336
337 View Code Duplication
def test_isentropic_pressure_adition_args():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
338
    """Test calculation of isentropic pressure function, additional args."""
339
    lev = [100000., 95000., 90000., 85000.] * units.Pa
340
    tmp = np.ones((4, 5, 5))
341
    tmp[0, :] = 296.
342
    tmp[1, :] = 292.
343
    tmp[2, :] = 290.
344
    tmp[3, :] = 288.
345
    rh = np.ones((4, 5, 5))
346
    rh[0, :] = 100.
347
    rh[1, :] = 80.
348
    rh[2, :] = 40.
349
    rh[3, :] = 20.
350
    relh = rh * units.percent
351
    tmpk = tmp * units.kelvin
352
    isentlev = [296.] * units.kelvin
353
    isentprs = get_isentropic_pressure(isentlev, lev, tmpk, relh)
354
    truerh = 100. * units.percent
355
    assert_almost_equal(isentprs[1], truerh, 3)
356
357
358 View Code Duplication
def test_isentropic_pressure_tmp_out():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
359
    """Test calculation of isentropic pressure function, temperature output."""
360
    lev = [100000., 95000., 90000., 85000.] * units.Pa
361
    tmp = np.ones((4, 5, 5))
362
    tmp[0, :] = 296.
363
    tmp[1, :] = 292.
364
    tmp[2, :] = 290.
365
    tmp[3, :] = 288.
366
    tmpk = tmp * units.kelvin
367
    isentlev = [296.] * units.kelvin
368
    isentprs = get_isentropic_pressure(isentlev, lev, tmpk, tmpk_out=True)
369
    truetmp = 296. * units.kelvin
370
    assert_almost_equal(isentprs[1], truetmp, 3)
371
372
373 View Code Duplication
def test_isentropic_pressure_p_increase_rh_out():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
374
    """Test calculation of isentropic pressure function, p increasing order."""
375
    lev = [85000., 90000., 95000., 100000.] * units.Pa
376
    tmp = np.ones((4, 5, 5))
377
    tmp[0, :] = 288.
378
    tmp[1, :] = 290.
379
    tmp[2, :] = 292.
380
    tmp[3, :] = 296.
381
    tmpk = tmp * units.kelvin
382
    rh = np.ones((4, 5, 5))
383
    rh[0, :] = 20.
384
    rh[1, :] = 40.
385
    rh[2, :] = 80.
386
    rh[3, :] = 100.
387
    relh = rh * units.percent
388
    isentlev = [296.] * units.kelvin
389
    isentprs = get_isentropic_pressure(isentlev, lev, tmpk, relh)
390
    truerh = 100. * units.percent
391
    assert_almost_equal(isentprs[1], truerh, 3)
392
393
394
def test_rh_mixing_ratio():
395
    """Tests relative humidity from mixing ratio."""
396
    p = 1013.25 * units.mbar
397
    temperature = 20. * units.degC
398
    w = 0.012
399
    rh = relative_humidity_from_mixing_ratio(w, temperature, p)
400
    assert_almost_equal(rh, 81.7219 * units.percent, 3)
401
402
403
def test_mixing_ratio_from_specific_humidity():
404
    """Tests mixing ratio from specific humidity."""
405
    q = 0.012
406
    w = mixing_ratio_from_specific_humidity(q)
407
    assert_almost_equal(w, 0.01215, 3)
408
409
410
def test_rh_specific_humidity():
411
    """Tests relative humidity from specific humidity."""
412
    p = 1013.25 * units.mbar
413
    temperature = 20. * units.degC
414
    q = 0.012
415
    rh = relative_humidity_from_specific_humidity(q, temperature, p)
416
    assert_almost_equal(rh, 82.7145 * units.percent, 3)
417