Completed
Push — master ( f4be97...862a3e )
by Ryan
23s
created

test_percent_dewpoint_rh()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 4
rs 10
c 1
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 `thermo` module."""
5
6
import numpy as np
7
import pytest
8
9
from metpy.calc import (cape_cin, density, dewpoint, dewpoint_rh, dry_lapse, el,
10
                        equivalent_potential_temperature,
11
                        isentropic_interpolation,
12
                        lcl, lfc, mixing_ratio,
13
                        mixing_ratio_from_specific_humidity, moist_lapse,
14
                        most_unstable_parcel, parcel_profile, potential_temperature,
15
                        psychrometric_vapor_pressure_wet,
16
                        relative_humidity_from_mixing_ratio,
17
                        relative_humidity_from_specific_humidity,
18
                        relative_humidity_wet_psychrometric,
19
                        saturation_mixing_ratio,
20
                        saturation_vapor_pressure, vapor_pressure,
21
                        virtual_potential_temperature, virtual_temperature)
22
23
from metpy.calc.thermo import _find_append_zero_crossings
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_percent_dewpoint_rh():
141
    """Test dewpoint_rh with rh in percent."""
142
    td = dewpoint_rh(10.6 * units.degC, 37 * units.percent)
143
    assert_almost_equal(td, 26. * units.degF, 0)
144
145
146
def test_warning_dewpoint_rh():
147
    """Test that warning is raised for >120% RH."""
148
    with pytest.warns(UserWarning):
149
        dewpoint_rh(10.6 * units.degC, 50)
150
151
152
def test_dewpoint():
153
    """Test dewpoint calculation."""
154
    assert_almost_equal(dewpoint(6.112 * units.mbar), 0. * units.degC, 2)
155
156
157
def test_dewpoint_weird_units():
158
    """Test dewpoint using non-standard units.
159
160
    Revealed from odd dimensionless units and ending up using numpy.ma math
161
    functions instead of numpy ones.
162
    """
163
    assert_almost_equal(dewpoint(15825.6 * units('g * mbar / kg')),
164
                        13.8564 * units.degC, 4)
165
166
167
def test_mixing_ratio():
168
    """Test mixing ratio calculation."""
169
    p = 998. * units.mbar
170
    e = 73.75 * units.mbar
171
    assert_almost_equal(mixing_ratio(e, p), 0.04963, 2)
172
173
174
def test_vapor_pressure():
175
    """Test vapor pressure calculation."""
176
    assert_almost_equal(vapor_pressure(998. * units.mbar, 0.04963),
177
                        73.74925 * units.mbar, 5)
178
179 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
180
def test_lcl():
181
    """Test LCL calculation."""
182
    lcl_pressure, lcl_temperature = lcl(1000. * units.mbar, 30. * units.degC, 20. * units.degC)
183
    assert_almost_equal(lcl_pressure, 864.761 * units.mbar, 2)
184
    assert_almost_equal(lcl_temperature, 17.676 * units.degC, 2)
185
186
187
def test_lcl_convergence():
188
    """Test LCL calculation convergence failure."""
189
    with pytest.raises(RuntimeError):
190
        lcl(1000. * units.mbar, 30. * units.degC, 20. * units.degC, max_iters=2)
191
192
193
def test_lfc_basic():
194
    """Test LFC calculation."""
195
    levels = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
196
    temperatures = np.array([22.2, 14.6, 12., 9.4, 7., -49.]) * units.celsius
197
    dewpoints = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
198
    l = lfc(levels, temperatures, dewpoints)
199
    assert_almost_equal(l[0], 727.468 * units.mbar, 2)
200
    assert_almost_equal(l[1], 9.705 * units.celsius, 2)
201
202
203
def test_no_lfc():
204
    """Test LFC calculation when there is no LFC in the data."""
205
    levels = np.array([959., 867.9, 779.2, 647.5, 472.5, 321.9, 251.]) * units.mbar
206
    temperatures = np.array([22.2, 17.4, 14.6, 1.4, -17.6, -39.4, -52.5]) * units.celsius
207
    dewpoints = np.array([9., 4.3, -21.2, -26.7, -31., -53.3, -66.7]) * units.celsius
208
    lfc_pressure, lfc_temperature = lfc(levels, temperatures, dewpoints)
209
    assert assert_nan(lfc_pressure, levels.units)
210
    assert assert_nan(lfc_temperature, temperatures.units)
211
212
213
def test_lfc_inversion():
214
    """Test LFC when there is an inversion to be sure we don't pick that."""
215
    levels = np.array([963., 789., 782.3, 754.8, 728.1, 727., 700.,
216
                       571., 450., 300., 248.]) * units.mbar
217
    temperatures = np.array([25.4, 18.4, 17.8, 15.4, 12.9, 12.8,
218
                             10., -3.9, -16.3, -41.1, -51.5]) * units.celsius
219
    dewpoints = np.array([20.4, 0.4, -0.5, -4.3, -8., -8.2, -9.,
220
                          -23.9, -33.3, -54.1, -63.5]) * units.celsius
221
    l = lfc(levels, temperatures, dewpoints)
222
    assert_almost_equal(l[0], 706.0103 * units.mbar, 2)
223
    assert_almost_equal(l[1], 10.6232 * units.celsius, 2)
224
225
226
def test_lfc_equals_lcl():
227
    """Test LFC when there is no cap and the lfc is equal to the lcl."""
228
    levels = np.array([912., 905.3, 874.4, 850., 815.1, 786.6, 759.1,
229
                       748., 732.2, 700., 654.8]) * units.mbar
230
    temperatures = np.array([29.4, 28.7, 25.2, 22.4, 19.4, 16.8,
231
                             14.3, 13.2, 12.6, 11.4, 7.1]) * units.celsius
232
    dewpoints = np.array([18.4, 18.1, 16.6, 15.4, 13.2, 11.4, 9.6,
233
                          8.8, 0., -18.6, -22.9]) * units.celsius
234
    l = lfc(levels, temperatures, dewpoints)
235
    assert_almost_equal(l[0], 777.0333 * units.mbar, 2)
236
    assert_almost_equal(l[1], 15.8714 * units.celsius, 2)
237
238
239
def test_lfc_sfc_precision():
240
    """Test LFC when there are precision issues with the parcel path."""
241
    levels = np.array([839., 819.4, 816., 807., 790.7, 763., 736.2,
242
                       722., 710.1, 700.]) * units.mbar
243
    temperatures = np.array([20.6, 22.3, 22.6, 22.2, 20.9, 18.7, 16.4,
244
                             15.2, 13.9, 12.8]) * units.celsius
245
    dewpoints = np.array([10.6, 8., 7.6, 6.2, 5.7, 4.7, 3.7, 3.2, 3., 2.8]) * units.celsius
246
    l = lfc(levels, temperatures, dewpoints)
247
    assert assert_nan(l[0], levels.units)
248
    assert assert_nan(l[1], temperatures.units)
249
250
251
def test_saturation_mixing_ratio():
252
    """Test saturation mixing ratio calculation."""
253
    p = 999. * units.mbar
254
    t = 288. * units.kelvin
255
    assert_almost_equal(saturation_mixing_ratio(p, t), .01068, 3)
256
257
258
def test_equivalent_potential_temperature():
259
    """Test equivalent potential temperature calculation."""
260
    p = 999. * units.mbar
261
    t = 288. * units.kelvin
262
    ept = equivalent_potential_temperature(p, t)
263
    assert_almost_equal(ept, 315.9548 * units.kelvin, 3)
264
265
266
def test_virtual_temperature():
267
    """Test virtual temperature calculation."""
268
    t = 288. * units.kelvin
269
    qv = .0016  # kg/kg
270
    tv = virtual_temperature(t, qv)
271
    assert_almost_equal(tv, 288.2796 * units.kelvin, 3)
272
273
274
def test_virtual_potential_temperature():
275
    """Test virtual potential temperature calculation."""
276
    p = 999. * units.mbar
277
    t = 288. * units.kelvin
278
    qv = .0016  # kg/kg
279
    theta_v = virtual_potential_temperature(p, t, qv)
280
    assert_almost_equal(theta_v, 288.3620 * units.kelvin, 3)
281
282
283
def test_density():
284
    """Test density calculation."""
285
    p = 999. * units.mbar
286
    t = 288. * units.kelvin
287
    qv = .0016  # kg/kg
288
    rho = density(p, t, qv).to(units.kilogram / units.meter ** 3)
289
    assert_almost_equal(rho, 1.2072 * (units.kilogram / units.meter ** 3), 3)
290
291
292
def test_el():
293
    """Test equilibrium layer calculation."""
294
    levels = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
295
    temperatures = np.array([22.2, 14.6, 12., 9.4, 7., -38.]) * units.celsius
296
    dewpoints = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
297
    el_pressure, el_temperature = el(levels, temperatures, dewpoints)
298
    assert_almost_equal(el_pressure, 520.8700 * units.mbar, 3)
299
    assert_almost_equal(el_temperature, -11.7027 * units.degC, 3)
300
301
302
def test_no_el():
303
    """Test equilibrium layer calculation when there is no EL in the data."""
304
    levels = np.array([959., 867.9, 779.2, 647.5, 472.5, 321.9, 251.]) * units.mbar
305
    temperatures = np.array([22.2, 17.4, 14.6, 1.4, -17.6, -39.4, -52.5]) * units.celsius
306
    dewpoints = np.array([19., 14.3, -11.2, -16.7, -21., -43.3, -56.7]) * units.celsius
307
    el_pressure, el_temperature = el(levels, temperatures, dewpoints)
308
    assert assert_nan(el_pressure, levels.units)
309
    assert assert_nan(el_temperature, temperatures.units)
310
311
312
def test_no_el_multi_crossing():
313
    """Test el calculation with no el and severel parcel path-profile crossings."""
314
    levels = np.array([918., 911., 880., 873.9, 850., 848., 843.5, 818., 813.8, 785.,
315
                       773., 763., 757.5, 730.5, 700., 679., 654.4, 645.,
316
                       643.9]) * units.mbar
317
    temperatures = np.array([24.2, 22.8, 19.6, 19.1, 17., 16.8, 16.5, 15., 14.9, 14.4, 16.4,
318
                             16.2, 15.7, 13.4, 10.6, 8.4, 5.7, 4.6, 4.5]) * units.celsius
319
    dewpoints = np.array([19.5, 17.8, 16.7, 16.5, 15.8, 15.7, 15.3, 13.1, 12.9, 11.9, 6.4,
320
                          3.2, 2.6, -0.6, -4.4, -6.6, -9.3, -10.4, -10.5]) * units.celsius
321
    el_pressure, el_temperature = el(levels, temperatures, dewpoints)
322
    assert assert_nan(el_pressure, levels.units)
323
    assert assert_nan(el_temperature, temperatures.units)
324
325
326
def test_el_lfc_equals_lcl():
327
    """Test equilibrium layer calculation when the lfc equals the lcl."""
328
    levels = np.array([912., 905.3, 874.4, 850., 815.1, 786.6, 759.1, 748.,
329
                       732.3, 700., 654.8, 606.8, 562.4, 501.8, 500., 482.,
330
                       400., 393.3, 317.1, 307., 300., 252.7, 250., 200.,
331
                       199.3, 197., 190., 172., 156.6, 150., 122.9, 112.,
332
                       106.2, 100.]) * units.mbar
333
    temperatures = np.array([29.4, 28.7, 25.2, 22.4, 19.4, 16.8, 14.3,
334
                             13.2, 12.6, 11.4, 7.1, 2.2, -2.7, -10.1,
335
                             -10.3, -12.4, -23.3, -24.4, -38., -40.1, -41.1,
336
                             -49.8, -50.3, -59.1, -59.1, -59.3, -59.7, -56.3,
337
                             -56.9, -57.1, -59.1, -60.1, -58.6, -56.9]) * units.celsius
338
    dewpoints = np.array([18.4, 18.1, 16.6, 15.4, 13.2, 11.4, 9.6, 8.8, 0.,
339
                          -18.6, -22.9, -27.8, -32.7, -40.1, -40.3, -42.4, -53.3,
340
                          -54.4, -68., -70.1, -70., -70., -70., -70., -70., -70.,
341
                          -70., -70., -70., -70., -70., -70., -70., -70.]) * units.celsius
342
    el_pressure, el_temperature = el(levels, temperatures, dewpoints)
343
    assert_almost_equal(el_pressure, 175.8684 * units.mbar, 3)
344
    assert_almost_equal(el_temperature, -57.0307 * units.degC, 3)
345
346
347
def test_wet_psychrometric_vapor_pressure():
348
    """Test calculation of vapor pressure from wet and dry bulb temperatures."""
349
    p = 1013.25 * units.mbar
350
    dry_bulb_temperature = 20. * units.degC
351
    wet_bulb_temperature = 18. * units.degC
352
    psychrometric_vapor_pressure = psychrometric_vapor_pressure_wet(dry_bulb_temperature,
353
                                                                    wet_bulb_temperature, p)
354
    assert_almost_equal(psychrometric_vapor_pressure, 19.3673 * units.mbar, 3)
355
356
357
def test_wet_psychrometric_rh():
358
    """Test calculation of relative humidity from wet and dry bulb temperatures."""
359
    p = 1013.25 * units.mbar
360
    dry_bulb_temperature = 20. * units.degC
361
    wet_bulb_temperature = 18. * units.degC
362
    psychrometric_rh = relative_humidity_wet_psychrometric(dry_bulb_temperature,
363
                                                           wet_bulb_temperature, p)
364 View Code Duplication
    assert_almost_equal(psychrometric_rh, 82.8747 * units.percent, 3)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
365
366
367
def test_wet_psychrometric_rh_kwargs():
368
    """Test calculation of relative humidity from wet and dry bulb temperatures."""
369
    p = 1013.25 * units.mbar
370
    dry_bulb_temperature = 20. * units.degC
371
    wet_bulb_temperature = 18. * units.degC
372
    coeff = 6.1e-4 / units.kelvin
373
    psychrometric_rh = relative_humidity_wet_psychrometric(dry_bulb_temperature,
374
                                                           wet_bulb_temperature, p,
375
                                                           psychrometer_coefficient=coeff)
376
    assert_almost_equal(psychrometric_rh, 82.9701 * units.percent, 3)
377
378
379
def test_rh_mixing_ratio():
380
    """Tests relative humidity from mixing ratio."""
381
    p = 1013.25 * units.mbar
382
    temperature = 20. * units.degC
383
    w = 0.012
384
    rh = relative_humidity_from_mixing_ratio(w, temperature, p)
385
    assert_almost_equal(rh, 81.7219 * units.percent, 3)
386 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
387
388
def test_mixing_ratio_from_specific_humidity():
389
    """Tests mixing ratio from specific humidity."""
390
    q = 0.012
391
    w = mixing_ratio_from_specific_humidity(q)
392
    assert_almost_equal(w, 0.01215, 3)
393
394
395
def test_rh_specific_humidity():
396
    """Tests relative humidity from specific humidity."""
397
    p = 1013.25 * units.mbar
398
    temperature = 20. * units.degC
399
    q = 0.012
400
    rh = relative_humidity_from_specific_humidity(q, temperature, p)
401
    assert_almost_equal(rh, 82.7145 * units.percent, 3)
402
403
404
def test_cape_cin():
405
    """Tests the basic CAPE and CIN calculation."""
406
    p = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
407
    temperature = np.array([22.2, 14.6, 12., 9.4, 7., -38.]) * units.celsius
408
    dewpoint = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
409
    parcel_prof = parcel_profile(p, temperature[0], dewpoint[0])
410 View Code Duplication
    cape, cin = cape_cin(p, temperature, dewpoint, parcel_prof)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
411
    assert_almost_equal(cape, 58.0368212 * units('joule / kilogram'), 6)
412
    assert_almost_equal(cin, -89.8073512 * units('joule / kilogram'), 6)
413
414
415
def test_cape_cin_no_el():
416
    """Tests that CAPE works with no EL."""
417
    p = np.array([959., 779.2, 751.3, 724.3]) * units.mbar
418
    temperature = np.array([22.2, 14.6, 12., 9.4]) * units.celsius
419
    dewpoint = np.array([19., -11.2, -10.8, -10.4]) * units.celsius
420
    parcel_prof = parcel_profile(p, temperature[0], dewpoint[0]).to('degC')
421
    cape, cin = cape_cin(p, temperature, dewpoint, parcel_prof)
422
    assert_almost_equal(cape, 0.08750805 * units('joule / kilogram'), 6)
423
    assert_almost_equal(cin, -89.8073512 * units('joule / kilogram'), 6)
424
425
426
def test_cape_cin_no_lfc():
427
    """Tests that CAPE is zero with no LFC."""
428
    p = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
429
    temperature = np.array([22.2, 24.6, 22., 20.4, 18., -10.]) * units.celsius
430
    dewpoint = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
431
    parcel_prof = parcel_profile(p, temperature[0], dewpoint[0]).to('degC')
432
    cape, cin = cape_cin(p, temperature, dewpoint, parcel_prof)
433
    assert_almost_equal(cape, 0.0 * units('joule / kilogram'), 6)
434
    assert_almost_equal(cin, 0.0 * units('joule / kilogram'), 6)
435
436
437 View Code Duplication
def test_find_append_zero_crossings():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
438
    """Tests finding and appending zero crossings of an x, y series."""
439
    x = np.arange(11) * units.hPa
440
    y = np.array([3, 2, 1, -1, 2, 2, 0, 1, 0, -1, 2]) * units.degC
441
    x2, y2 = _find_append_zero_crossings(x, y)
442
443
    x_truth = np.array([0., 1., 2., 2.5, 3., 3.33333333, 4., 5.,
444
                        6., 7., 8., 9., 9.33333333, 10.]) * units.hPa
445
    y_truth = np.array([3, 2, 1, 0, -1, 0, 2, 2, 0, 1, 0, -1, 0, 2]) * units.degC
446
    assert_array_almost_equal(x2, x_truth, 6)
447
    assert_almost_equal(y2, y_truth, 6)
448
449
450
def test_most_unstable_parcel():
451
    """Tests calculating the most unstable parcel."""
452
    levels = np.array([1000., 959., 867.9]) * units.mbar
453 View Code Duplication
    temperatures = np.array([18.2, 22.2, 17.4]) * units.celsius
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
454
    dewpoints = np.array([19., 19., 14.3]) * units.celsius
455
    ret = most_unstable_parcel(levels, temperatures, dewpoints, depth=100 * units.hPa)
456
    assert_almost_equal(ret[0], 959.0 * units.hPa, 6)
457
    assert_almost_equal(ret[1], 22.2 * units.degC, 6)
458
    assert_almost_equal(ret[2], 19.0 * units.degC, 6)
459
460
461
def test_isentropic_pressure():
462
    """Test calculation of isentropic pressure function."""
463
    lev = [100000., 95000., 90000., 85000.] * units.Pa
464
    tmp = np.ones((4, 5, 5))
465
    tmp[0, :] = 296.
466
    tmp[1, :] = 292.
467
    tmp[2, :] = 290
468 View Code Duplication
    tmp[3, :] = 288.
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
469
    tmpk = tmp * units.kelvin
470
    isentlev = [296.] * units.kelvin
471
    isentprs = isentropic_interpolation(isentlev, lev, tmpk)
472
    trueprs = 1000. * units.hPa
473
    assert_almost_equal(isentprs[0].shape, (1, 5, 5), 3)
474
    assert_almost_equal(isentprs[0], trueprs, 3)
475
476
477
def test_isentropic_pressure_p_increase():
478
    """Test calculation of isentropic pressure function, p increasing order."""
479
    lev = [85000, 90000., 95000., 100000.] * units.Pa
480
    tmp = np.ones((4, 5, 5))
481
    tmp[0, :] = 288.
482
    tmp[1, :] = 290.
483
    tmp[2, :] = 292.
484
    tmp[3, :] = 296.
485
    tmpk = tmp * units.kelvin
486
    isentlev = [296.] * units.kelvin
487
    isentprs = isentropic_interpolation(isentlev, lev, tmpk)
488
    trueprs = 1000. * units.hPa
489 View Code Duplication
    assert_almost_equal(isentprs[0], trueprs, 3)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
490
491
492
def test_isentropic_pressure_adition_args():
493
    """Test calculation of isentropic pressure function, additional args."""
494
    lev = [100000., 95000., 90000., 85000.] * units.Pa
495
    tmp = np.ones((4, 5, 5))
496
    tmp[0, :] = 296.
497
    tmp[1, :] = 292.
498
    tmp[2, :] = 290.
499
    tmp[3, :] = 288.
500
    rh = np.ones((4, 5, 5))
501
    rh[0, :] = 100.
502
    rh[1, :] = 80.
503
    rh[2, :] = 40.
504 View Code Duplication
    rh[3, :] = 20.
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
505
    relh = rh * units.percent
506
    tmpk = tmp * units.kelvin
507
    isentlev = [296.] * units.kelvin
508
    isentprs = isentropic_interpolation(isentlev, lev, tmpk, relh)
509
    truerh = 100. * units.percent
510
    assert_almost_equal(isentprs[1], truerh, 3)
511
512
513
def test_isentropic_pressure_tmp_out():
514
    """Test calculation of isentropic pressure function, temperature output."""
515
    lev = [100000., 95000., 90000., 85000.] * units.Pa
516
    tmp = np.ones((4, 5, 5))
517
    tmp[0, :] = 296.
518
    tmp[1, :] = 292.
519
    tmp[2, :] = 290.
520
    tmp[3, :] = 288.
521
    tmpk = tmp * units.kelvin
522
    isentlev = [296.] * units.kelvin
523
    isentprs = isentropic_interpolation(isentlev, lev, tmpk, tmpk_out=True)
524
    truetmp = 296. * units.kelvin
525 View Code Duplication
    assert_almost_equal(isentprs[1], truetmp, 3)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
526
527
528
def test_isentropic_pressure_p_increase_rh_out():
529
    """Test calculation of isentropic pressure function, p increasing order."""
530
    lev = [85000., 90000., 95000., 100000.] * units.Pa
531
    tmp = np.ones((4, 5, 5))
532
    tmp[0, :] = 288.
533
    tmp[1, :] = 290.
534
    tmp[2, :] = 292.
535
    tmp[3, :] = 296.
536
    tmpk = tmp * units.kelvin
537
    rh = np.ones((4, 5, 5))
538
    rh[0, :] = 20.
539
    rh[1, :] = 40.
540 View Code Duplication
    rh[2, :] = 80.
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
541
    rh[3, :] = 100.
542
    relh = rh * units.percent
543
    isentlev = 296. * units.kelvin
544
    isentprs = isentropic_interpolation(isentlev, lev, tmpk, relh)
545
    truerh = 100. * units.percent
546
    assert_almost_equal(isentprs[1], truerh, 3)
547
548
549
def test_isentropic_pressure_interp():
550
    """Test calculation of isentropic pressure function."""
551
    lev = [100000., 95000., 90000., 85000.] * units.Pa
552
    tmp = np.ones((4, 5, 5))
553
    tmp[0, :] = 296.
554
    tmp[1, :] = 292.
555
    tmp[2, :] = 290
556
    tmp[3, :] = 288.
557
    tmpk = tmp * units.kelvin
558
    isentlev = [296., 297] * units.kelvin
559
    isentprs = isentropic_interpolation(isentlev, lev, tmpk)
560
    trueprs = 936.18057 * units.hPa
561 View Code Duplication
    assert_almost_equal(isentprs[0][1], trueprs, 3)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
562
563
564
def test_isentropic_pressure_adition_args_interp():
565
    """Test calculation of isentropic pressure function, additional args."""
566
    lev = [100000., 95000., 90000., 85000.] * units.Pa
567
    tmp = np.ones((4, 5, 5))
568
    tmp[0, :] = 296.
569
    tmp[1, :] = 292.
570
    tmp[2, :] = 290.
571
    tmp[3, :] = 288.
572
    rh = np.ones((4, 5, 5))
573
    rh[0, :] = 100.
574
    rh[1, :] = 80.
575
    rh[2, :] = 40.
576
    rh[3, :] = 20.
577
    relh = rh * units.percent
578
    tmpk = tmp * units.kelvin
579
    isentlev = [296., 297.] * units.kelvin
580
    isentprs = isentropic_interpolation(isentlev, lev, tmpk, relh)
581
    truerh = 69.171 * units.percent
582
    assert_almost_equal(isentprs[1][1], truerh, 3)
583
584
585
def test_isentropic_pressure_tmp_out_interp():
586
    """Test calculation of isentropic pressure function, temperature output."""
587
    lev = [100000., 95000., 90000., 85000.] * units.Pa
588
    tmp = np.ones((4, 5, 5))
589
    tmp[0, :] = 296.
590
    tmp[1, :] = 292.
591
    tmp[2, :] = 290.
592
    tmp[3, :] = 288.
593
    tmpk = tmp * units.kelvin
594
    isentlev = [296., 297.] * units.kelvin
595
    isentprs = isentropic_interpolation(isentlev, lev, tmpk, tmpk_out=True)
596
    truetmp = 291.4579 * units.kelvin
597
    assert_almost_equal(isentprs[1][1], truetmp, 3)
598
599
600
def test_isentropic_pressure_data_bounds_error():
601
    """Test calculation of isentropic pressure function, error for data out of bounds."""
602
    lev = [100000., 95000., 90000., 85000.] * units.Pa
603
    tmp = np.ones((4, 5, 5))
604
    tmp[0, :] = 296.
605
    tmp[1, :] = 292.
606
    tmp[2, :] = 290.
607
    tmp[3, :] = 288.
608
    tmpk = tmp * units.kelvin
609
    isentlev = [296., 350.] * units.kelvin
610
    with pytest.raises(ValueError):
611
        isentropic_interpolation(isentlev, lev, tmpk)
612
613
614
def test_isentropic_pressure_4d():
615
    """Test calculation of isentropic pressure function."""
616
    lev = [100000., 95000., 90000., 85000.] * units.Pa
617
    tmp = np.ones((3, 4, 5, 5))
618
    tmp[:, 0, :] = 296.
619
    tmp[:, 1, :] = 292.
620
    tmp[:, 2, :] = 290
621
    tmp[:, 3, :] = 288.
622
    tmpk = tmp * units.kelvin
623
    rh = np.ones((3, 4, 5, 5))
624
    rh[:, 0, :] = 100.
625
    rh[:, 1, :] = 80.
626
    rh[:, 2, :] = 40.
627
    rh[:, 3, :] = 20.
628
    relh = rh * units.percent
629
    isentlev = [296., 297., 300.] * units.kelvin
630
    isentprs = isentropic_interpolation(isentlev, lev, tmpk, relh, axis=1)
631
    trueprs = 1000. * units.hPa
632
    trueprs2 = 936.18057 * units.hPa
633
    trueprs3 = 879.446 * units.hPa
634
    truerh = 69.171 * units.percent
635
    assert_almost_equal(isentprs[0].shape, (3, 3, 5, 5), 3)
636
    assert_almost_equal(isentprs[0][:, 0, :], trueprs, 3)
637
    assert_almost_equal(isentprs[0][:, 1, :], trueprs2, 3)
638
    assert_almost_equal(isentprs[0][:, 2, :], trueprs3, 3)
639
    assert_almost_equal(isentprs[1][:, 1, ], truerh, 3)
640