Completed
Pull Request — master (#492)
by
unknown
59s
created

test_sbcape_profile()   A

Complexity

Conditions 1

Size

Total Lines 10

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