Completed
Pull Request — master (#454)
by
unknown
01:58
created

test_isentropic_pressure_data_warning()   A

Complexity

Conditions 2

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 12
loc 12
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
                        isentropic_interpolation,
12
                        lcl, lfc, mixing_ratio,
13
                        mixing_ratio_from_specific_humidity, moist_lapse,
14
                        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.testing import assert_almost_equal, assert_array_almost_equal, assert_nan
24
from metpy.units import units
25
26
27
def test_potential_temperature():
28
    """Test potential_temperature calculation."""
29
    temp = np.array([278., 283., 291., 298.]) * units.kelvin
30
    pres = np.array([900., 500., 300., 100.]) * units.mbar
31
    real_th = np.array([286.493, 344.961, 410.4335, 575.236]) * units.kelvin
32
    assert_array_almost_equal(potential_temperature(pres, temp), real_th, 3)
33
34
35
def test_scalar():
36
    """Test potential_temperature accepts scalar values."""
37
    assert_almost_equal(potential_temperature(1000. * units.mbar, 293. * units.kelvin),
38
                        293. * units.kelvin, 4)
39
    assert_almost_equal(potential_temperature(800. * units.mbar, 293. * units.kelvin),
40
                        312.2828 * units.kelvin, 4)
41
42
43
def test_fahrenheit():
44
    """Test that potential_temperature handles temperature values in Fahrenheit."""
45
    assert_almost_equal(potential_temperature(800. * units.mbar, 68. * units.degF),
46
                        (312.444 * units.kelvin).to(units.degF), 2)
47
48
49
def test_pot_temp_inhg():
50
    """Test that potential_temperature can handle pressure not in mb (issue #165)."""
51
    assert_almost_equal(potential_temperature(29.92 * units.inHg, 29 * units.degC),
52
                        301.019735 * units.kelvin, 4)
53
54
55
def test_dry_lapse():
56
    """Test dry_lapse calculation."""
57
    levels = np.array([1000, 900, 864.89]) * units.mbar
58
    temps = dry_lapse(levels, 303.15 * units.kelvin)
59
    assert_array_almost_equal(temps,
60
                              np.array([303.15, 294.16, 290.83]) * units.kelvin, 2)
61
62
63
def test_dry_lapse_2_levels():
64
    """Test dry_lapse calculation when given only two levels."""
65
    temps = dry_lapse(np.array([1000., 500.]) * units.mbar, 293. * units.kelvin)
66
    assert_array_almost_equal(temps, [293., 240.3723] * units.kelvin, 4)
67
68
69
def test_moist_lapse():
70
    """Test moist_lapse calculation."""
71
    temp = moist_lapse(np.array([1000., 800., 600., 500., 400.]) * units.mbar,
72
                       293. * units.kelvin)
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_moist_lapse_degc():
78
    """Test moist_lapse with Celsius temperatures."""
79
    temp = moist_lapse(np.array([1000., 800., 600., 500., 400.]) * units.mbar,
80
                       19.85 * units.degC)
81
    true_temp = np.array([293, 284.64, 272.81, 264.42, 252.91]) * units.kelvin
82
    assert_array_almost_equal(temp, true_temp, 2)
83
84
85
def test_parcel_profile():
86
    """Test parcel profile calculation."""
87
    levels = np.array([1000., 900., 800., 700., 600., 500., 400.]) * units.mbar
88
    true_prof = np.array([303.15, 294.16, 288.026, 283.073, 277.058, 269.402,
89
                          258.966]) * units.kelvin
90
91
    prof = parcel_profile(levels, 30. * units.degC, 20. * units.degC)
92
    assert_array_almost_equal(prof, true_prof, 2)
93
94
95
def test_parcel_profile_saturated():
96
    """Test parcel_profile works when LCL in levels (issue #232)."""
97
    levels = np.array([1000., 700., 500.]) * units.mbar
98
    true_prof = np.array([296.95, 284.381, 271.123]) * units.kelvin
99
100
    prof = parcel_profile(levels, 23.8 * units.degC, 23.8 * units.degC)
101
    assert_array_almost_equal(prof, true_prof, 2)
102
103
104
def test_sat_vapor_pressure():
105
    """Test saturation_vapor_pressure calculation."""
106
    temp = np.array([5., 10., 18., 25.]) * units.degC
107
    real_es = np.array([8.72, 12.27, 20.63, 31.67]) * units.mbar
108
    assert_array_almost_equal(saturation_vapor_pressure(temp), real_es, 2)
109
110
111
def test_sat_vapor_pressure_scalar():
112
    """Test saturation_vapor_pressure handles scalar values."""
113
    es = saturation_vapor_pressure(0 * units.degC)
114
    assert_almost_equal(es, 6.112 * units.mbar, 3)
115
116
117
def test_sat_vapor_pressure_fahrenheit():
118
    """Test saturation_vapor_pressure handles temperature in Fahrenheit."""
119
    temp = np.array([50., 68.]) * units.degF
120
    real_es = np.array([12.2717, 23.3695]) * units.mbar
121
    assert_array_almost_equal(saturation_vapor_pressure(temp), real_es, 4)
122
123
124
def test_basic_dewpoint_rh():
125
    """Test dewpoint_rh function."""
126
    temp = np.array([30., 25., 10., 20., 25.]) * units.degC
127
    rh = np.array([30., 45., 55., 80., 85.]) / 100.
128
129
    real_td = np.array([11, 12, 1, 16, 22]) * units.degC
130
    assert_array_almost_equal(real_td, dewpoint_rh(temp, rh), 0)
131
132
133
def test_scalar_dewpoint_rh():
134
    """Test dewpoint_rh with scalar values."""
135
    td = dewpoint_rh(10.6 * units.degC, 0.37)
136
    assert_almost_equal(td, 26. * units.degF, 0)
137
138
139
def test_dewpoint():
140
    """Test dewpoint calculation."""
141
    assert_almost_equal(dewpoint(6.112 * units.mbar), 0. * units.degC, 2)
142
143
144
def test_dewpoint_weird_units():
145
    """Test dewpoint using non-standard units.
146
147
    Revealed from odd dimensionless units and ending up using numpy.ma math
148
    functions instead of numpy ones.
149
    """
150
    assert_almost_equal(dewpoint(15825.6 * units('g * mbar / kg')),
151
                        13.8564 * units.degC, 4)
152
153
154
def test_mixing_ratio():
155
    """Test mixing ratio calculation."""
156
    p = 998. * units.mbar
157
    e = 73.75 * units.mbar
158
    assert_almost_equal(mixing_ratio(e, p), 0.04963, 2)
159
160
161
def test_vapor_pressure():
162
    """Test vapor pressure calculation."""
163
    assert_almost_equal(vapor_pressure(998. * units.mbar, 0.04963),
164
                        73.74925 * units.mbar, 5)
165
166
167
def test_lcl():
168
    """Test LCL calculation."""
169
    lcl_pressure, lcl_temperature = lcl(1000. * units.mbar, 30. * units.degC, 20. * units.degC)
170
    assert_almost_equal(lcl_pressure, 864.761 * units.mbar, 2)
171
    assert_almost_equal(lcl_temperature, 17.676 * units.degC, 2)
172
173
174
def test_lcl_convergence():
175
    """Test LCL calculation convergence failure."""
176
    with pytest.raises(RuntimeError):
177
        lcl(1000. * units.mbar, 30. * units.degC, 20. * units.degC, max_iters=2)
178
179
180
def test_lfc_basic():
181
    """Test LFC calculation."""
182
    levels = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
183
    temperatures = np.array([22.2, 14.6, 12., 9.4, 7., -49.]) * units.celsius
184
    dewpoints = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
185
    l = lfc(levels, temperatures, dewpoints)
186
    assert_almost_equal(l[0], 727.468 * units.mbar, 2)
187
    assert_almost_equal(l[1], 9.705 * units.celsius, 2)
188
189
190
def test_no_lfc():
191
    """Test LFC calculation when there is no LFC in the data."""
192
    levels = np.array([959., 867.9, 779.2, 647.5, 472.5, 321.9, 251.]) * units.mbar
193
    temperatures = np.array([22.2, 17.4, 14.6, 1.4, -17.6, -39.4, -52.5]) * units.celsius
194
    dewpoints = np.array([9., 4.3, -21.2, -26.7, -31., -53.3, -66.7]) * units.celsius
195
    lfc_pressure, lfc_temperature = lfc(levels, temperatures, dewpoints)
196
    assert assert_nan(lfc_pressure, levels.units)
197
    assert assert_nan(lfc_temperature, temperatures.units)
198
199
200
def test_lfc_inversion():
201
    """Test LFC when there is an inversion to be sure we don't pick that."""
202
    levels = np.array([963., 789., 782.3, 754.8, 728.1, 727., 700.,
203
                       571., 450., 300., 248.]) * units.mbar
204
    temperatures = np.array([25.4, 18.4, 17.8, 15.4, 12.9, 12.8,
205
                             10., -3.9, -16.3, -41.1, -51.5]) * units.celsius
206
    dewpoints = np.array([20.4, 0.4, -0.5, -4.3, -8., -8.2, -9.,
207
                          -23.9, -33.3, -54.1, -63.5]) * units.celsius
208
    l = lfc(levels, temperatures, dewpoints)
209
    assert_almost_equal(l[0], 706.0103 * units.mbar, 2)
210
    assert_almost_equal(l[1], 10.6232 * units.celsius, 2)
211
212
213
def test_lfc_equals_lcl():
214
    """Test LFC when there is no cap and the lfc is equal to the lcl."""
215
    levels = np.array([912., 905.3, 874.4, 850., 815.1, 786.6, 759.1,
216
                       748., 732.2, 700., 654.8]) * units.mbar
217
    temperatures = np.array([29.4, 28.7, 25.2, 22.4, 19.4, 16.8,
218
                             14.3, 13.2, 12.6, 11.4, 7.1]) * units.celsius
219
    dewpoints = np.array([18.4, 18.1, 16.6, 15.4, 13.2, 11.4, 9.6,
220
                          8.8, 0., -18.6, -22.9]) * units.celsius
221
    l = lfc(levels, temperatures, dewpoints)
222
    assert_almost_equal(l[0], 777.0333 * units.mbar, 2)
223
    assert_almost_equal(l[1], 15.8714 * units.celsius, 2)
224
225
226
def test_saturation_mixing_ratio():
227
    """Test saturation mixing ratio calculation."""
228
    p = 999. * units.mbar
229
    t = 288. * units.kelvin
230
    assert_almost_equal(saturation_mixing_ratio(p, t), .01068, 3)
231
232
233
def test_equivalent_potential_temperature():
234
    """Test equivalent potential temperature calculation."""
235
    p = 999. * units.mbar
236
    t = 288. * units.kelvin
237
    ept = equivalent_potential_temperature(p, t)
238
    assert_almost_equal(ept, 315.9548 * units.kelvin, 3)
239
240
241
def test_virtual_temperature():
242
    """Test virtual temperature calculation."""
243
    t = 288. * units.kelvin
244
    qv = .0016  # kg/kg
245
    tv = virtual_temperature(t, qv)
246
    assert_almost_equal(tv, 288.2796 * units.kelvin, 3)
247
248
249
def test_virtual_potential_temperature():
250
    """Test virtual potential temperature calculation."""
251
    p = 999. * units.mbar
252
    t = 288. * units.kelvin
253
    qv = .0016  # kg/kg
254
    theta_v = virtual_potential_temperature(p, t, qv)
255
    assert_almost_equal(theta_v, 288.3620 * units.kelvin, 3)
256
257
258
def test_density():
259
    """Test density calculation."""
260
    p = 999. * units.mbar
261
    t = 288. * units.kelvin
262
    qv = .0016  # kg/kg
263
    rho = density(p, t, qv).to(units.kilogram / units.meter ** 3)
264
    assert_almost_equal(rho, 1.2072 * (units.kilogram / units.meter ** 3), 3)
265
266
267
def test_el():
268
    """Test equilibrium layer calculation."""
269
    levels = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
270
    temperatures = np.array([22.2, 14.6, 12., 9.4, 7., -38.]) * units.celsius
271
    dewpoints = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
272
    el_pressure, el_temperature = el(levels, temperatures, dewpoints)
273
    assert_almost_equal(el_pressure, 520.8700 * units.mbar, 3)
274
    assert_almost_equal(el_temperature, -11.7027 * units.degC, 3)
275
276
277
def test_no_el():
278
    """Test equilibrium layer calculation when there is no EL in the data."""
279
    levels = np.array([959., 867.9, 779.2, 647.5, 472.5, 321.9, 251.]) * units.mbar
280
    temperatures = np.array([22.2, 17.4, 14.6, 1.4, -17.6, -39.4, -52.5]) * units.celsius
281
    dewpoints = np.array([19., 14.3, -11.2, -16.7, -21., -43.3, -56.7]) * units.celsius
282
    el_pressure, el_temperature = el(levels, temperatures, dewpoints)
283
    assert assert_nan(el_pressure, levels.units)
284
    assert assert_nan(el_temperature, temperatures.units)
285
286
287
def test_el_lfc_equals_lcl():
288
    """Test equilibrium layer calculation when the lfc equals the lcl."""
289
    levels = np.array([912., 905.3, 874.4, 850., 815.1, 786.6, 759.1, 748.,
290
                       732.3, 700., 654.8, 606.8, 562.4, 501.8, 500., 482.,
291
                       400., 393.3, 317.1, 307., 300., 252.7, 250., 200.,
292
                       199.3, 197., 190., 172., 156.6, 150., 122.9, 112.,
293
                       106.2, 100.]) * units.mbar
294
    temperatures = np.array([29.4, 28.7, 25.2, 22.4, 19.4, 16.8, 14.3,
295
                             13.2, 12.6, 11.4, 7.1, 2.2, -2.7, -10.1,
296
                             -10.3, -12.4, -23.3, -24.4, -38., -40.1, -41.1,
297
                             -49.8, -50.3, -59.1, -59.1, -59.3, -59.7, -56.3,
298
                             -56.9, -57.1, -59.1, -60.1, -58.6, -56.9]) * units.celsius
299
    dewpoints = np.array([18.4, 18.1, 16.6, 15.4, 13.2, 11.4, 9.6, 8.8, 0.,
300
                          -18.6, -22.9, -27.8, -32.7, -40.1, -40.3, -42.4, -53.3,
301
                          -54.4, -68., -70.1, -70., -70., -70., -70., -70., -70.,
302
                          -70., -70., -70., -70., -70., -70., -70., -70.]) * units.celsius
303
    el_pressure, el_temperature = el(levels, temperatures, dewpoints)
304
    assert_almost_equal(el_pressure, 175.8684 * units.mbar, 3)
305
    assert_almost_equal(el_temperature, -57.0307 * units.degC, 3)
306
307
308
def test_wet_psychrometric_vapor_pressure():
309
    """Test calculation of vapor pressure from wet and dry bulb temperatures."""
310
    p = 1013.25 * units.mbar
311
    dry_bulb_temperature = 20. * units.degC
312
    wet_bulb_temperature = 18. * units.degC
313
    psychrometric_vapor_pressure = psychrometric_vapor_pressure_wet(dry_bulb_temperature,
314
                                                                    wet_bulb_temperature, p)
315
    assert_almost_equal(psychrometric_vapor_pressure, 19.3673 * units.mbar, 3)
316
317
318
def test_wet_psychrometric_rh():
319
    """Test calculation of relative humidity from wet and dry bulb temperatures."""
320
    p = 1013.25 * units.mbar
321
    dry_bulb_temperature = 20. * units.degC
322
    wet_bulb_temperature = 18. * units.degC
323
    psychrometric_rh = relative_humidity_wet_psychrometric(dry_bulb_temperature,
324
                                                           wet_bulb_temperature, p)
325
    assert_almost_equal(psychrometric_rh, 82.8747 * units.percent, 3)
326
327
328
def test_wet_psychrometric_rh_kwargs():
329
    """Test calculation of relative humidity from wet and dry bulb temperatures."""
330
    p = 1013.25 * units.mbar
331
    dry_bulb_temperature = 20. * units.degC
332
    wet_bulb_temperature = 18. * units.degC
333
    coeff = 6.1e-4 / units.kelvin
334
    psychrometric_rh = relative_humidity_wet_psychrometric(dry_bulb_temperature,
335
                                                           wet_bulb_temperature, p,
336
                                                           psychrometer_coefficient=coeff)
337
    assert_almost_equal(psychrometric_rh, 82.9701 * units.percent, 3)
338
339
340
def test_rh_mixing_ratio():
341
    """Tests relative humidity from mixing ratio."""
342
    p = 1013.25 * units.mbar
343
    temperature = 20. * units.degC
344
    w = 0.012
345
    rh = relative_humidity_from_mixing_ratio(w, temperature, p)
346
    assert_almost_equal(rh, 81.7219 * units.percent, 3)
347
348
349
def test_mixing_ratio_from_specific_humidity():
350
    """Tests mixing ratio from specific humidity."""
351
    q = 0.012
352
    w = mixing_ratio_from_specific_humidity(q)
353
    assert_almost_equal(w, 0.01215, 3)
354
355
356
def test_rh_specific_humidity():
357
    """Tests relative humidity from specific humidity."""
358
    p = 1013.25 * units.mbar
359
    temperature = 20. * units.degC
360
    q = 0.012
361
    rh = relative_humidity_from_specific_humidity(q, temperature, p)
362
    assert_almost_equal(rh, 82.7145 * units.percent, 3)
363
364
365 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...
366
    """Test calculation of isentropic pressure function."""
367
    lev = [100000., 95000., 90000., 85000.] * units.Pa
368
    tmp = np.ones((4, 5, 5))
369
    tmp[0, :] = 296.
370
    tmp[1, :] = 292.
371
    tmp[2, :] = 290
372
    tmp[3, :] = 288.
373
    tmpk = tmp * units.kelvin
374
    isentlev = [296.] * units.kelvin
375
    isentprs = isentropic_interpolation(isentlev, lev, tmpk)
376
    trueprs = 1000. * units.hPa
377
    assert_almost_equal(isentprs[0], trueprs, 3)
378
379
380 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...
381
    """Test calculation of isentropic pressure function, p increasing order."""
382
    lev = [85000, 90000., 95000., 100000.] * units.Pa
383
    tmp = np.ones((4, 5, 5))
384
    tmp[0, :] = 288.
385
    tmp[1, :] = 290.
386
    tmp[2, :] = 292.
387
    tmp[3, :] = 296.
388
    tmpk = tmp * units.kelvin
389
    isentlev = [296.] * units.kelvin
390
    isentprs = isentropic_interpolation(isentlev, lev, tmpk)
391
    trueprs = 1000. * units.hPa
392
    assert_almost_equal(isentprs[0], trueprs, 3)
393
394
395 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...
396
    """Test calculation of isentropic pressure function, additional args."""
397
    lev = [100000., 95000., 90000., 85000.] * units.Pa
398
    tmp = np.ones((4, 5, 5))
399
    tmp[0, :] = 296.
400
    tmp[1, :] = 292.
401
    tmp[2, :] = 290.
402
    tmp[3, :] = 288.
403
    rh = np.ones((4, 5, 5))
404
    rh[0, :] = 100.
405
    rh[1, :] = 80.
406
    rh[2, :] = 40.
407
    rh[3, :] = 20.
408
    relh = rh * units.percent
409
    tmpk = tmp * units.kelvin
410
    isentlev = [296.] * units.kelvin
411
    isentprs = isentropic_interpolation(isentlev, lev, tmpk, relh)
412
    truerh = 100. * units.percent
413
    assert_almost_equal(isentprs[1], truerh, 3)
414
415
416 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...
417
    """Test calculation of isentropic pressure function, temperature output."""
418
    lev = [100000., 95000., 90000., 85000.] * units.Pa
419
    tmp = np.ones((4, 5, 5))
420
    tmp[0, :] = 296.
421
    tmp[1, :] = 292.
422
    tmp[2, :] = 290.
423
    tmp[3, :] = 288.
424
    tmpk = tmp * units.kelvin
425
    isentlev = [296.] * units.kelvin
426
    isentprs = isentropic_interpolation(isentlev, lev, tmpk, tmpk_out=True)
427
    truetmp = 296. * units.kelvin
428
    assert_almost_equal(isentprs[1], truetmp, 3)
429
430
431 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...
432
    """Test calculation of isentropic pressure function, p increasing order."""
433
    lev = [85000., 90000., 95000., 100000.] * units.Pa
434
    tmp = np.ones((4, 5, 5))
435
    tmp[0, :] = 288.
436
    tmp[1, :] = 290.
437
    tmp[2, :] = 292.
438
    tmp[3, :] = 296.
439
    tmpk = tmp * units.kelvin
440
    rh = np.ones((4, 5, 5))
441
    rh[0, :] = 20.
442
    rh[1, :] = 40.
443
    rh[2, :] = 80.
444
    rh[3, :] = 100.
445
    relh = rh * units.percent
446
    isentlev = [296.] * units.kelvin
447
    isentprs = isentropic_interpolation(isentlev, lev, tmpk, relh)
448
    truerh = 100. * units.percent
449
    assert_almost_equal(isentprs[1], truerh, 3)
450
451
452 View Code Duplication
def test_isentropic_pressure_interp():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
453
    """Test calculation of isentropic pressure function."""
454
    lev = [100000., 95000., 90000., 85000.] * units.Pa
455
    tmp = np.ones((4, 5, 5))
456
    tmp[0, :] = 296.
457
    tmp[1, :] = 292.
458
    tmp[2, :] = 290
459
    tmp[3, :] = 288.
460
    tmpk = tmp * units.kelvin
461
    isentlev = [296., 297] * units.kelvin
462
    isentprs = isentropic_interpolation(isentlev, lev, tmpk)
463
    trueprs = 936.18057 * units.hPa
464
    assert_almost_equal(isentprs[0][1], trueprs, 3)
465
466
467 View Code Duplication
def test_isentropic_pressure_adition_args_interp():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
468
    """Test calculation of isentropic pressure function, additional args."""
469
    lev = [100000., 95000., 90000., 85000.] * units.Pa
470
    tmp = np.ones((4, 5, 5))
471
    tmp[0, :] = 296.
472
    tmp[1, :] = 292.
473
    tmp[2, :] = 290.
474
    tmp[3, :] = 288.
475
    rh = np.ones((4, 5, 5))
476
    rh[0, :] = 100.
477
    rh[1, :] = 80.
478
    rh[2, :] = 40.
479
    rh[3, :] = 20.
480
    relh = rh * units.percent
481
    tmpk = tmp * units.kelvin
482
    isentlev = [296., 297.] * units.kelvin
483
    isentprs = isentropic_interpolation(isentlev, lev, tmpk, relh)
484
    truerh = 69.15897 * units.percent
485
    assert_almost_equal(isentprs[1][1], truerh, 3)
486
487
488 View Code Duplication
def test_isentropic_pressure_tmp_out_interp():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
489
    """Test calculation of isentropic pressure function, temperature output."""
490
    lev = [100000., 95000., 90000., 85000.] * units.Pa
491
    tmp = np.ones((4, 5, 5))
492
    tmp[0, :] = 296.
493
    tmp[1, :] = 292.
494
    tmp[2, :] = 290.
495
    tmp[3, :] = 288.
496
    tmpk = tmp * units.kelvin
497
    isentlev = [296., 297.] * units.kelvin
498
    isentprs = isentropic_interpolation(isentlev, lev, tmpk, tmpk_out=True)
499
    truetmp = 291.4579 * units.kelvin
500
    assert_almost_equal(isentprs[1][1], truetmp, 3)
501
502
503 View Code Duplication
def test_isentropic_pressure_3d_error():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
504
    """Test calculation of isentropic pressure function, error for input data not 3-D."""
505
    lev = [100000., 95000., 90000., 85000.] * units.Pa
506
    tmp = np.ones((4, 5,))
507
    tmp[0, :] = 296.
508
    tmp[1, :] = 292.
509
    tmp[2, :] = 290.
510
    tmp[3, :] = 288.
511
    tmpk = tmp * units.kelvin
512
    isentlev = [296., 297.] * units.kelvin
513
    with pytest.raises(ValueError):
514
        isentropic_interpolation(isentlev, lev, tmpk)
515
516
517 View Code Duplication
def test_isentropic_pressure_data_bounds_error():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
518
    """Test calculation of isentropic pressure function, error for data out of bounds."""
519
    lev = [100000., 95000., 90000., 85000.] * units.Pa
520
    tmp = np.ones((4, 5, 5))
521
    tmp[0, :] = 296.
522
    tmp[1, :] = 292.
523
    tmp[2, :] = 290.
524
    tmp[3, :] = 288.
525
    tmpk = tmp * units.kelvin
526
    isentlev = [296., 350.] * units.kelvin
527
    with pytest.raises(ValueError):
528
        isentropic_interpolation(isentlev, lev, tmpk)
529
530
531 View Code Duplication
def test_isentropic_pressure_data_warning():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
532
    """Test of isentropic pressure function, warning for non-meteorological data."""
533
    lev = [1000., 950., 900., 850.] * units.Pa
534
    tmp = np.ones((4, 5, 5))
535
    tmp[0, :] = 296.
536
    tmp[1, :] = 292.
537
    tmp[2, :] = 290.
538
    tmp[3, :] = 288.
539
    tmpk = tmp * units.kelvin
540
    isentlev = [296., 297.] * units.kelvin
541
    with pytest.warns(Warning):
542
        isentropic_interpolation(isentlev, lev, tmpk)
543