forked from Unidata/MetPy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_thermo.py
1286 lines (1052 loc) · 58.9 KB
/
test_thermo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2008,2015,2016,2017,2018 MetPy Developers.
# Distributed under the terms of the BSD 3-Clause License.
# SPDX-License-Identifier: BSD-3-Clause
"""Test the `thermo` module."""
import numpy as np
import pytest
import xarray as xr
from metpy.calc import (brunt_vaisala_frequency, brunt_vaisala_frequency_squared,
brunt_vaisala_period, cape_cin, density, dewpoint,
dewpoint_from_specific_humidity, dewpoint_rh,
dry_lapse, dry_static_energy, el,
equivalent_potential_temperature,
exner_function, isentropic_interpolation, lcl, lfc, mixed_layer,
mixed_parcel, mixing_ratio, mixing_ratio_from_relative_humidity,
mixing_ratio_from_specific_humidity, moist_lapse,
moist_static_energy, most_unstable_cape_cin, most_unstable_parcel,
parcel_profile, parcel_profile_with_lcl, potential_temperature,
psychrometric_vapor_pressure_wet,
relative_humidity_from_dewpoint,
relative_humidity_from_mixing_ratio,
relative_humidity_from_specific_humidity,
relative_humidity_wet_psychrometric,
saturation_equivalent_potential_temperature,
saturation_mixing_ratio,
saturation_vapor_pressure,
specific_humidity_from_mixing_ratio, static_stability,
surface_based_cape_cin, temperature_from_potential_temperature,
thickness_hydrostatic,
thickness_hydrostatic_from_relative_humidity, vapor_pressure,
vertical_velocity, vertical_velocity_pressure,
virtual_potential_temperature, virtual_temperature,
wet_bulb_temperature)
from metpy.calc.thermo import _find_append_zero_crossings
from metpy.testing import assert_almost_equal, assert_array_almost_equal, assert_nan
from metpy.units import units
def test_relative_humidity_from_dewpoint():
"""Test Relative Humidity calculation."""
assert_almost_equal(relative_humidity_from_dewpoint(25. * units.degC, 15. * units.degC),
53.80 * units.percent, 2)
def test_relative_humidity_from_dewpoint_with_f():
"""Test Relative Humidity accepts temperature in Fahrenheit."""
assert_almost_equal(relative_humidity_from_dewpoint(70. * units.degF, 55. * units.degF),
58.935 * units.percent, 3)
def test_relative_humidity_from_dewpoint_xarray():
"""Test Relative Humidity calculation with xarray data arrays."""
temp = xr.DataArray(25., attrs={'units': 'degC'})
dewp = xr.DataArray(15., attrs={'units': 'degC'})
assert_almost_equal(relative_humidity_from_dewpoint(temp, dewp), 53.80 * units.percent, 2)
def test_exner_function():
"""Test Exner function calculation."""
pres = np.array([900., 500., 300., 100.]) * units.mbar
truth = np.array([0.9703542, 0.8203834, 0.7090065, 0.518048]) * units.dimensionless
assert_array_almost_equal(exner_function(pres), truth, 6)
def test_potential_temperature():
"""Test potential_temperature calculation."""
temp = np.array([278., 283., 291., 298.]) * units.kelvin
pres = np.array([900., 500., 300., 100.]) * units.mbar
real_th = np.array([286.493, 344.961, 410.4335, 575.236]) * units.kelvin
assert_array_almost_equal(potential_temperature(pres, temp), real_th, 3)
def test_temperature_from_potential_temperature():
"""Test temperature_from_potential_temperature calculation."""
theta = np.array([286.12859679, 288.22362587, 290.31865495, 292.41368403]) * units.kelvin
pres = np.array([850] * 4) * units.mbar
real_t = np.array([273.15, 275.15, 277.15, 279.15]) * units.kelvin
assert_array_almost_equal(temperature_from_potential_temperature(pres, theta),
real_t, 2)
def test_scalar():
"""Test potential_temperature accepts scalar values."""
assert_almost_equal(potential_temperature(1000. * units.mbar, 293. * units.kelvin),
293. * units.kelvin, 4)
assert_almost_equal(potential_temperature(800. * units.mbar, 293. * units.kelvin),
312.2828 * units.kelvin, 4)
def test_fahrenheit():
"""Test that potential_temperature handles temperature values in Fahrenheit."""
assert_almost_equal(potential_temperature(800. * units.mbar, 68. * units.degF),
(312.444 * units.kelvin).to(units.degF), 2)
def test_pot_temp_inhg():
"""Test that potential_temperature can handle pressure not in mb (issue #165)."""
assert_almost_equal(potential_temperature(29.92 * units.inHg, 29 * units.degC),
301.019735 * units.kelvin, 4)
def test_dry_lapse():
"""Test dry_lapse calculation."""
levels = np.array([1000, 900, 864.89]) * units.mbar
temps = dry_lapse(levels, 303.15 * units.kelvin)
assert_array_almost_equal(temps,
np.array([303.15, 294.16, 290.83]) * units.kelvin, 2)
def test_dry_lapse_2_levels():
"""Test dry_lapse calculation when given only two levels."""
temps = dry_lapse(np.array([1000., 500.]) * units.mbar, 293. * units.kelvin)
assert_array_almost_equal(temps, [293., 240.3723] * units.kelvin, 4)
def test_moist_lapse():
"""Test moist_lapse calculation."""
temp = moist_lapse(np.array([1000., 800., 600., 500., 400.]) * units.mbar,
293. * units.kelvin)
true_temp = np.array([293, 284.64, 272.81, 264.42, 252.91]) * units.kelvin
assert_array_almost_equal(temp, true_temp, 2)
def test_moist_lapse_degc():
"""Test moist_lapse with Celsius temperatures."""
temp = moist_lapse(np.array([1000., 800., 600., 500., 400.]) * units.mbar,
19.85 * units.degC)
true_temp = np.array([293, 284.64, 272.81, 264.42, 252.91]) * units.kelvin
assert_array_almost_equal(temp, true_temp, 2)
def test_moist_lapse_ref_pres():
"""Test moist_lapse with a reference pressure."""
temp = moist_lapse(np.array([1050., 800., 600., 500., 400.]) * units.mbar,
19.85 * units.degC, 1000. * units.mbar)
true_temp = np.array([294.76, 284.64, 272.81, 264.42, 252.91]) * units.kelvin
assert_array_almost_equal(temp, true_temp, 2)
def test_parcel_profile():
"""Test parcel profile calculation."""
levels = np.array([1000., 900., 800., 700., 600., 500., 400.]) * units.mbar
true_prof = np.array([303.15, 294.16, 288.026, 283.073, 277.058, 269.402,
258.966]) * units.kelvin
prof = parcel_profile(levels, 30. * units.degC, 20. * units.degC)
assert_array_almost_equal(prof, true_prof, 2)
def test_parcel_profile_lcl():
"""Test parcel profile with lcl calculation."""
p = np.array([1004., 1000., 943., 928., 925., 850., 839., 749., 700., 699.]) * units.hPa
t = np.array([24.2, 24., 20.2, 21.6, 21.4, 20.4, 20.2, 14.4, 13.2, 13.]) * units.degC
td = np.array([21.9, 22.1, 19.2, 20.5, 20.4, 18.4, 17.4, 8.4, -2.8, -3.0]) * units.degC
true_prof = np.array([297.35, 297.01, 294.5, 293.48, 292.92, 292.81, 289.79, 289.32,
285.15, 282.59, 282.53]) * units.kelvin
true_p = np.insert(p.m, 2, 970.699) * units.mbar
true_t = np.insert(t.m, 2, 22.047) * units.degC
true_td = np.insert(td.m, 2, 20.609) * units.degC
pressure, temp, dewp, prof = parcel_profile_with_lcl(p, t, td)
assert_almost_equal(pressure, true_p, 3)
assert_almost_equal(temp, true_t, 3)
assert_almost_equal(dewp, true_td, 3)
assert_array_almost_equal(prof, true_prof, 2)
def test_parcel_profile_saturated():
"""Test parcel_profile works when LCL in levels (issue #232)."""
levels = np.array([1000., 700., 500.]) * units.mbar
true_prof = np.array([296.95, 284.381, 271.123]) * units.kelvin
prof = parcel_profile(levels, 23.8 * units.degC, 23.8 * units.degC)
assert_array_almost_equal(prof, true_prof, 2)
def test_sat_vapor_pressure():
"""Test saturation_vapor_pressure calculation."""
temp = np.array([5., 10., 18., 25.]) * units.degC
real_es = np.array([8.72, 12.27, 20.63, 31.67]) * units.mbar
assert_array_almost_equal(saturation_vapor_pressure(temp), real_es, 2)
def test_sat_vapor_pressure_scalar():
"""Test saturation_vapor_pressure handles scalar values."""
es = saturation_vapor_pressure(0 * units.degC)
assert_almost_equal(es, 6.112 * units.mbar, 3)
def test_sat_vapor_pressure_fahrenheit():
"""Test saturation_vapor_pressure handles temperature in Fahrenheit."""
temp = np.array([50., 68.]) * units.degF
real_es = np.array([12.2717, 23.3695]) * units.mbar
assert_array_almost_equal(saturation_vapor_pressure(temp), real_es, 4)
def test_basic_dewpoint_rh():
"""Test dewpoint_rh function."""
temp = np.array([30., 25., 10., 20., 25.]) * units.degC
rh = np.array([30., 45., 55., 80., 85.]) / 100.
real_td = np.array([11, 12, 1, 16, 22]) * units.degC
assert_array_almost_equal(real_td, dewpoint_rh(temp, rh), 0)
def test_scalar_dewpoint_rh():
"""Test dewpoint_rh with scalar values."""
td = dewpoint_rh(10.6 * units.degC, 0.37)
assert_almost_equal(td, 26. * units.degF, 0)
def test_percent_dewpoint_rh():
"""Test dewpoint_rh with rh in percent."""
td = dewpoint_rh(10.6 * units.degC, 37 * units.percent)
assert_almost_equal(td, 26. * units.degF, 0)
def test_warning_dewpoint_rh():
"""Test that warning is raised for >120% RH."""
with pytest.warns(UserWarning):
dewpoint_rh(10.6 * units.degC, 50)
def test_dewpoint():
"""Test dewpoint calculation."""
assert_almost_equal(dewpoint(6.112 * units.mbar), 0. * units.degC, 2)
def test_dewpoint_weird_units():
"""Test dewpoint using non-standard units.
Revealed from odd dimensionless units and ending up using numpy.ma math
functions instead of numpy ones.
"""
assert_almost_equal(dewpoint(15825.6 * units('g * mbar / kg')),
13.8564 * units.degC, 4)
def test_mixing_ratio():
"""Test mixing ratio calculation."""
p = 998. * units.mbar
e = 73.75 * units.mbar
assert_almost_equal(mixing_ratio(e, p), 0.04963, 2)
def test_vapor_pressure():
"""Test vapor pressure calculation."""
assert_almost_equal(vapor_pressure(998. * units.mbar, 0.04963),
73.74925 * units.mbar, 5)
def test_lcl():
"""Test LCL calculation."""
lcl_pressure, lcl_temperature = lcl(1000. * units.mbar, 30. * units.degC, 20. * units.degC)
assert_almost_equal(lcl_pressure, 864.761 * units.mbar, 2)
assert_almost_equal(lcl_temperature, 17.676 * units.degC, 2)
def test_lcl_convergence():
"""Test LCL calculation convergence failure."""
with pytest.raises(RuntimeError):
lcl(1000. * units.mbar, 30. * units.degC, 20. * units.degC, max_iters=2)
def test_lfc_basic():
"""Test LFC calculation."""
levels = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
temperatures = np.array([22.2, 14.6, 12., 9.4, 7., -49.]) * units.celsius
dewpoints = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
lfc_pressure, lfc_temp = lfc(levels, temperatures, dewpoints)
assert_almost_equal(lfc_pressure, 727.468 * units.mbar, 2)
assert_almost_equal(lfc_temp, 9.705 * units.celsius, 2)
def test_lfc_ml():
"""Test Mixed-Layer LFC calculation."""
levels = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
temperatures = np.array([22.2, 14.6, 12., 9.4, 7., -49.]) * units.celsius
dewpoints = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
__, t_mixed, td_mixed = mixed_parcel(levels, temperatures, dewpoints)
mixed_parcel_prof = parcel_profile(levels, t_mixed, td_mixed)
lfc_pressure, lfc_temp = lfc(levels, temperatures, dewpoints, mixed_parcel_prof)
assert_almost_equal(lfc_pressure, 631.794 * units.mbar, 2)
assert_almost_equal(lfc_temp, -1.862 * units.degC, 2)
def test_lfc_ml2():
"""Test a mixed-layer LFC calculation that previously crashed."""
levels = np.array([1024.95703125, 1016.61474609, 1005.33056641, 991.08544922, 973.4163208,
951.3381958, 924.82836914, 898.25482178, 873.46124268, 848.69830322,
823.92553711, 788.49304199, 743.44580078, 700.50970459, 659.62017822,
620.70861816, 583.69421387, 548.49719238, 515.03826904, 483.24401855,
453.0418396, 424.36477661, 397.1505127, 371.33441162, 346.85922241,
323.66995239, 301.70935059, 280.92651367, 261.27053833, 242.69168091,
225.14237976, 208.57781982, 192.95333862, 178.22599792, 164.39630127,
151.54336548, 139.68635559, 128.74923706, 118.6588974, 109.35111237,
100.76405334, 92.84288025, 85.53556824, 78.79430389, 72.57549286,
66.83885193, 61.54678726, 56.66480637, 52.16108322]) * units.mbar
temperatures = np.array([6.00750732, 5.14892578, 4.177948, 3.00268555, 1.55535889,
-0.25527954, -1.93988037, -3.57766724, -4.40600586, -4.19238281,
-3.71185303, -4.47943115, -6.81280518, -8.08685303, -8.41287231,
-10.79302979, -14.13262939, -16.85784912, -19.51675415,
-22.28689575, -24.99938965, -27.79664612, -30.90414429,
-34.49435425, -38.438797, -42.27981567, -45.99230957,
-49.75340271, -53.58230591, -57.30686951, -60.76026917,
-63.92070007, -66.72470093, -68.97846985, -70.4264679,
-71.16407776, -71.53797913, -71.64375305, -71.52735901,
-71.53523254, -71.61097717, -71.92687988, -72.68682861,
-74.129776, -76.02471924, -76.88977051, -76.26008606,
-75.90351868, -76.15809631]) * units.celsius
dewpoints = np.array([4.50012302, 3.42483997, 2.78102994, 2.24474645, 1.593485, -0.9440815,
-3.8044982, -3.55629468, -9.7376976, -10.2950449, -9.67498302,
-10.30486488, -8.70559597, -8.71669006, -12.66509628, -18.6697197,
-23.00351334, -29.46240425, -36.82178497, -41.68824768, -44.50320816,
-48.54426575, -52.50753403, -51.09564209, -48.92690659, -49.97380829,
-51.57516098, -52.62096405, -54.24332809, -57.09109879, -60.5596199,
-63.93486404, -67.07530212, -70.01263428, -72.9258728, -76.12271881,
-79.49847412, -82.2350769, -83.91127014, -84.95665741, -85.61238861,
-86.16391754, -86.7653656, -87.34436035, -87.87495422, -88.34281921,
-88.74453735, -89.04680634, -89.26436615]) * units.celsius
__, t_mixed, td_mixed = mixed_parcel(levels, temperatures, dewpoints)
mixed_parcel_prof = parcel_profile(levels, t_mixed, td_mixed)
lfc_pressure, lfc_temp = lfc(levels, temperatures, dewpoints, mixed_parcel_prof, td_mixed)
assert_almost_equal(lfc_pressure, 962.34 * units.mbar, 2)
assert_almost_equal(lfc_temp, 0.767 * units.degC, 2)
def test_lfc_intersection():
"""Test LFC calculation when LFC is below a tricky intersection."""
p = np.array([1024.957, 930., 924.828, 898.255, 873.461, 848.698, 823.926,
788.493]) * units('hPa')
t = np.array([6.008, -10., -6.94, -8.58, -4.41, -4.19, -3.71, -4.48]) * units('degC')
td = np.array([5., -10., -7., -9., -4.5, -4.2, -3.8, -4.5]) * units('degC')
_, mlt, mltd = mixed_parcel(p, t, td)
ml_profile = parcel_profile(p, mlt, mltd)
mllfc_p, mllfc_t = lfc(p, t, td, ml_profile, mltd)
assert_almost_equal(mllfc_p, 982.762 * units.hPa, 2)
assert_almost_equal(mllfc_t, 272.045 * units.kelvin, 2)
def test_no_lfc():
"""Test LFC calculation when there is no LFC in the data."""
levels = np.array([959., 867.9, 779.2, 647.5, 472.5, 321.9, 251.]) * units.mbar
temperatures = np.array([22.2, 17.4, 14.6, 1.4, -17.6, -39.4, -52.5]) * units.celsius
dewpoints = np.array([9., 4.3, -21.2, -26.7, -31., -53.3, -66.7]) * units.celsius
lfc_pressure, lfc_temperature = lfc(levels, temperatures, dewpoints)
assert assert_nan(lfc_pressure, levels.units)
assert assert_nan(lfc_temperature, temperatures.units)
def test_lfc_inversion():
"""Test LFC when there is an inversion to be sure we don't pick that."""
levels = np.array([963., 789., 782.3, 754.8, 728.1, 727., 700.,
571., 450., 300., 248.]) * units.mbar
temperatures = np.array([25.4, 18.4, 17.8, 15.4, 12.9, 12.8,
10., -3.9, -16.3, -41.1, -51.5]) * units.celsius
dewpoints = np.array([20.4, 0.4, -0.5, -4.3, -8., -8.2, -9.,
-23.9, -33.3, -54.1, -63.5]) * units.celsius
lfc_pressure, lfc_temp = lfc(levels, temperatures, dewpoints)
assert_almost_equal(lfc_pressure, 706.0103 * units.mbar, 2)
assert_almost_equal(lfc_temp, 10.6232 * units.celsius, 2)
def test_lfc_equals_lcl():
"""Test LFC when there is no cap and the lfc is equal to the lcl."""
levels = np.array([912., 905.3, 874.4, 850., 815.1, 786.6, 759.1,
748., 732.2, 700., 654.8]) * units.mbar
temperatures = np.array([29.4, 28.7, 25.2, 22.4, 19.4, 16.8,
14.0, 13.2, 12.6, 11.4, 7.1]) * units.celsius
dewpoints = np.array([18.4, 18.1, 16.6, 15.4, 13.2, 11.4, 9.6,
8.8, 0., -18.6, -22.9]) * units.celsius
lfc_pressure, lfc_temp = lfc(levels, temperatures, dewpoints)
assert_almost_equal(lfc_pressure, 777.0333 * units.mbar, 2)
assert_almost_equal(lfc_temp, 15.8714 * units.celsius, 2)
def test_sensitive_sounding():
"""Test quantities for a sensitive sounding (#902)."""
# This sounding has a very small positive area in the low level. It's only captured
# properly if the parcel profile includes the LCL, otherwise it breaks LFC and CAPE
p = units.Quantity([1004., 1000., 943., 928., 925., 850., 839., 749., 700., 699.,
603., 500., 404., 400., 363., 306., 300., 250., 213., 200.,
176., 150.], 'hectopascal')
t = units.Quantity([24.2, 24., 20.2, 21.6, 21.4, 20.4, 20.2, 14.4, 13.2, 13., 6.8, -3.3,
-13.1, -13.7, -17.9, -25.5, -26.9, -37.9, -46.7, -48.7, -52.1, -58.9],
'degC')
td = units.Quantity([21.9, 22.1, 19.2, 20.5, 20.4, 18.4, 17.4, 8.4, -2.8, -3.0, -15.2,
-20.3, -29.1, -27.7, -24.9, -39.5, -41.9, -51.9, -60.7, -62.7, -65.1,
-71.9], 'degC')
lfc_pressure, lfc_temp = lfc(p, t, td)
assert_almost_equal(lfc_pressure, 947.476 * units.mbar, 2)
assert_almost_equal(lfc_temp, 20.498 * units.degC, 2)
pos, neg = surface_based_cape_cin(p, t, td)
assert_almost_equal(pos, 0.112 * units('J/kg'), 3)
assert_almost_equal(neg, -6.075 * units('J/kg'), 3)
def test_lfc_sfc_precision():
"""Test LFC when there are precision issues with the parcel path."""
levels = np.array([839., 819.4, 816., 807., 790.7, 763., 736.2,
722., 710.1, 700.]) * units.mbar
temperatures = np.array([20.6, 22.3, 22.6, 22.2, 20.9, 18.7, 16.4,
15.2, 13.9, 12.8]) * units.celsius
dewpoints = np.array([10.6, 8., 7.6, 6.2, 5.7, 4.7, 3.7, 3.2, 3., 2.8]) * units.celsius
lfc_pressure, lfc_temp = lfc(levels, temperatures, dewpoints)
assert assert_nan(lfc_pressure, levels.units)
assert assert_nan(lfc_temp, temperatures.units)
def test_lfc_pos_area_below_lcl():
"""Test LFC when there is positive area below the LCL (#1003)."""
p = [902.1554, 897.9034, 893.6506, 889.4047, 883.063, 874.6284, 866.2387, 857.887,
849.5506, 841.2686, 833.0042, 824.7891, 812.5049, 796.2104, 776.0027, 751.9025,
727.9612, 704.1409, 680.4028, 656.7156, 629.077, 597.4286, 565.6315, 533.5961,
501.2452, 468.493, 435.2486, 401.4239, 366.9387, 331.7026, 295.6319, 258.6428,
220.9178, 182.9384, 144.959, 106.9778, 69.00213] * units.hPa
t = [-3.039381, -3.703779, -4.15996, -4.562574, -5.131827, -5.856229, -6.568434,
-7.276881, -7.985013, -8.670911, -8.958063, -7.631381, -6.05927, -5.083627,
-5.11576, -5.687552, -5.453021, -4.981445, -5.236665, -6.324916, -8.434324,
-11.58795, -14.99297, -18.45947, -21.92021, -25.40522, -28.914, -32.78637,
-37.7179, -43.56836, -49.61077, -54.24449, -56.16666, -57.03775, -58.28041,
-60.86264, -64.21677] * units.degC
td = [-22.08774, -22.18181, -22.2508, -22.31323, -22.4024, -22.51582, -22.62526,
-22.72919, -22.82095, -22.86173, -22.49489, -21.66936, -21.67332, -21.94054,
-23.63561, -27.17466, -31.87395, -38.31725, -44.54717, -46.99218, -43.17544,
-37.40019, -34.3351, -36.42896, -42.1396, -46.95909, -49.36232, -48.94634,
-47.90178, -49.97902, -55.02753, -63.06276, -72.53742, -88.81377, -93.54573,
-92.92464, -91.57479] * units.degC
prof = parcel_profile(p, t[0], td[0]).to('degC')
lfc_p, lfc_t = lfc(p, t, td, prof)
assert assert_nan(lfc_p, p.units)
assert assert_nan(lfc_t, t.units)
def test_saturation_mixing_ratio():
"""Test saturation mixing ratio calculation."""
p = 999. * units.mbar
t = 288. * units.kelvin
assert_almost_equal(saturation_mixing_ratio(p, t), .01068, 3)
def test_equivalent_potential_temperature():
"""Test equivalent potential temperature calculation."""
p = 1000 * units.mbar
t = 293. * units.kelvin
td = 280. * units.kelvin
ept = equivalent_potential_temperature(p, t, td)
assert_almost_equal(ept, 311.18586467284007 * units.kelvin, 3)
def test_saturation_equivalent_potential_temperature():
"""Test saturation equivalent potential temperature calculation."""
p = 700 * units.mbar
t = 263.15 * units.kelvin
s_ept = saturation_equivalent_potential_temperature(p, t)
# 299.096584 comes from equivalent_potential_temperature(p,t,t)
# where dewpoint and temperature are equal, which means saturations.
assert_almost_equal(s_ept, 299.096584 * units.kelvin, 3)
def test_virtual_temperature():
"""Test virtual temperature calculation."""
t = 288. * units.kelvin
qv = .0016 * units.dimensionless # kg/kg
tv = virtual_temperature(t, qv)
assert_almost_equal(tv, 288.2796 * units.kelvin, 3)
def test_virtual_potential_temperature():
"""Test virtual potential temperature calculation."""
p = 999. * units.mbar
t = 288. * units.kelvin
qv = .0016 * units.dimensionless # kg/kg
theta_v = virtual_potential_temperature(p, t, qv)
assert_almost_equal(theta_v, 288.3620 * units.kelvin, 3)
def test_density():
"""Test density calculation."""
p = 999. * units.mbar
t = 288. * units.kelvin
qv = .0016 * units.dimensionless # kg/kg
rho = density(p, t, qv).to(units.kilogram / units.meter ** 3)
assert_almost_equal(rho, 1.2072 * (units.kilogram / units.meter ** 3), 3)
def test_el():
"""Test equilibrium layer calculation."""
levels = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
temperatures = np.array([22.2, 14.6, 12., 9.4, 7., -38.]) * units.celsius
dewpoints = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
el_pressure, el_temperature = el(levels, temperatures, dewpoints)
assert_almost_equal(el_pressure, 520.8700 * units.mbar, 3)
assert_almost_equal(el_temperature, -11.7027 * units.degC, 3)
def test_el_ml():
"""Test equilibrium layer calculation for a mixed parcel."""
levels = np.array([959., 779.2, 751.3, 724.3, 700., 400., 269.]) * units.mbar
temperatures = np.array([22.2, 14.6, 12., 9.4, 7., -25., -35.]) * units.celsius
dewpoints = np.array([19., -11.2, -10.8, -10.4, -10., -35., -53.2]) * units.celsius
__, t_mixed, td_mixed = mixed_parcel(levels, temperatures, dewpoints)
mixed_parcel_prof = parcel_profile(levels, t_mixed, td_mixed)
el_pressure, el_temperature = el(levels, temperatures, dewpoints, mixed_parcel_prof)
assert_almost_equal(el_pressure, 355.834 * units.mbar, 3)
assert_almost_equal(el_temperature, -28.371 * units.degC, 3)
def test_no_el():
"""Test equilibrium layer calculation when there is no EL in the data."""
levels = np.array([959., 867.9, 779.2, 647.5, 472.5, 321.9, 251.]) * units.mbar
temperatures = np.array([22.2, 17.4, 14.6, 1.4, -17.6, -39.4, -52.5]) * units.celsius
dewpoints = np.array([19., 14.3, -11.2, -16.7, -21., -43.3, -56.7]) * units.celsius
el_pressure, el_temperature = el(levels, temperatures, dewpoints)
assert assert_nan(el_pressure, levels.units)
assert assert_nan(el_temperature, temperatures.units)
def test_no_el_multi_crossing():
"""Test el calculation with no el and severel parcel path-profile crossings."""
levels = np.array([918., 911., 880., 873.9, 850., 848., 843.5, 818., 813.8, 785.,
773., 763., 757.5, 730.5, 700., 679., 654.4, 645.,
643.9]) * units.mbar
temperatures = np.array([24.2, 22.8, 19.6, 19.1, 17., 16.8, 16.5, 15., 14.9, 14.4, 16.4,
16.2, 15.7, 13.4, 10.6, 8.4, 5.7, 4.6, 4.5]) * units.celsius
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,
3.2, 2.6, -0.6, -4.4, -6.6, -9.3, -10.4, -10.5]) * units.celsius
el_pressure, el_temperature = el(levels, temperatures, dewpoints)
assert assert_nan(el_pressure, levels.units)
assert assert_nan(el_temperature, temperatures.units)
def test_el_lfc_equals_lcl():
"""Test equilibrium layer calculation when the lfc equals the lcl."""
levels = np.array([912., 905.3, 874.4, 850., 815.1, 786.6, 759.1, 748.,
732.3, 700., 654.8, 606.8, 562.4, 501.8, 500., 482.,
400., 393.3, 317.1, 307., 300., 252.7, 250., 200.,
199.3, 197., 190., 172., 156.6, 150., 122.9, 112.,
106.2, 100.]) * units.mbar
temperatures = np.array([29.4, 28.7, 25.2, 22.4, 19.4, 16.8, 14.3,
13.2, 12.6, 11.4, 7.1, 2.2, -2.7, -10.1,
-10.3, -12.4, -23.3, -24.4, -38., -40.1, -41.1,
-49.8, -50.3, -59.1, -59.1, -59.3, -59.7, -56.3,
-56.9, -57.1, -59.1, -60.1, -58.6, -56.9]) * units.celsius
dewpoints = np.array([18.4, 18.1, 16.6, 15.4, 13.2, 11.4, 9.6, 8.8, 0.,
-18.6, -22.9, -27.8, -32.7, -40.1, -40.3, -42.4, -53.3,
-54.4, -68., -70.1, -70., -70., -70., -70., -70., -70.,
-70., -70., -70., -70., -70., -70., -70., -70.]) * units.celsius
el_pressure, el_temperature = el(levels, temperatures, dewpoints)
assert_almost_equal(el_pressure, 175.8684 * units.mbar, 3)
assert_almost_equal(el_temperature, -57.0307 * units.degC, 3)
def test_el_small_surface_instability():
"""Test that no EL is found when there is a small pocket of instability at the sfc."""
levels = np.array([959., 931.3, 925., 899.3, 892., 867.9, 850., 814.,
807.9, 790., 779.2, 751.3, 724.3, 700., 655., 647.5,
599.4, 554.7, 550., 500.]) * units.mbar
temperatures = np.array([22.2, 20.2, 19.8, 18.4, 18., 17.4, 17., 15.4, 15.4,
15.6, 14.6, 12., 9.4, 7., 2.2, 1.4, -4.2, -9.7,
-10.3, -14.9]) * units.degC
dewpoints = np.array([20., 18.5, 18.1, 17.9, 17.8, 15.3, 13.5, 6.4, 2.2,
-10.4, -10.2, -9.8, -9.4, -9., -15.8, -15.7, -14.8, -14.,
-13.9, -17.9]) * units.degC
el_pressure, el_temperature = el(levels, temperatures, dewpoints)
assert assert_nan(el_pressure, levels.units)
assert assert_nan(el_temperature, temperatures.units)
def test_no_el_parcel_colder():
"""Test no EL when parcel stays colder than environment. INL 20170925-12Z."""
levels = np.array([974., 946., 925., 877.2, 866., 850., 814.6, 785.,
756.6, 739., 729.1, 700., 686., 671., 641., 613.,
603., 586., 571., 559.3, 539., 533., 500., 491.,
477.9, 413., 390., 378., 345., 336.]) * units.mbar
temperatures = np.array([10., 8.4, 7.6, 5.9, 7.2, 7.6, 6.8, 7.1, 7.7,
7.8, 7.7, 5.6, 4.6, 3.4, 0.6, -0.9, -1.1, -3.1,
-4.7, -4.7, -6.9, -7.5, -11.1, -10.9, -12.1, -20.5, -23.5,
-24.7, -30.5, -31.7]) * units.celsius
dewpoints = np.array([8.9, 8.4, 7.6, 5.9, 7.2, 7., 5., 3.6, 0.3,
-4.2, -12.8, -12.4, -8.4, -8.6, -6.4, -7.9, -11.1, -14.1,
-8.8, -28.1, -18.9, -14.5, -15.2, -15.1, -21.6, -41.5, -45.5,
-29.6, -30.6, -32.1]) * units.celsius
el_pressure, el_temperature = el(levels, temperatures, dewpoints)
assert assert_nan(el_pressure, levels.units)
assert assert_nan(el_temperature, temperatures.units)
def test_el_below_lcl():
"""Test LFC when there is positive area below the LCL (#1003)."""
p = [902.1554, 897.9034, 893.6506, 889.4047, 883.063, 874.6284, 866.2387, 857.887,
849.5506, 841.2686, 833.0042, 824.7891, 812.5049, 796.2104, 776.0027, 751.9025,
727.9612, 704.1409, 680.4028, 656.7156, 629.077, 597.4286, 565.6315, 533.5961,
501.2452, 468.493, 435.2486, 401.4239, 366.9387, 331.7026, 295.6319, 258.6428,
220.9178, 182.9384, 144.959, 106.9778, 69.00213] * units.hPa
t = [-3.039381, -3.703779, -4.15996, -4.562574, -5.131827, -5.856229, -6.568434,
-7.276881, -7.985013, -8.670911, -8.958063, -7.631381, -6.05927, -5.083627,
-5.11576, -5.687552, -5.453021, -4.981445, -5.236665, -6.324916, -8.434324,
-11.58795, -14.99297, -18.45947, -21.92021, -25.40522, -28.914, -32.78637,
-37.7179, -43.56836, -49.61077, -54.24449, -56.16666, -57.03775, -58.28041,
-60.86264, -64.21677] * units.degC
td = [-22.08774, -22.18181, -22.2508, -22.31323, -22.4024, -22.51582, -22.62526,
-22.72919, -22.82095, -22.86173, -22.49489, -21.66936, -21.67332, -21.94054,
-23.63561, -27.17466, -31.87395, -38.31725, -44.54717, -46.99218, -43.17544,
-37.40019, -34.3351, -36.42896, -42.1396, -46.95909, -49.36232, -48.94634,
-47.90178, -49.97902, -55.02753, -63.06276, -72.53742, -88.81377, -93.54573,
-92.92464, -91.57479] * units.degC
prof = parcel_profile(p, t[0], td[0]).to('degC')
el_p, el_t = el(p, t, td, prof)
assert assert_nan(el_p, p.units)
assert assert_nan(el_t, t.units)
def test_wet_psychrometric_vapor_pressure():
"""Test calculation of vapor pressure from wet and dry bulb temperatures."""
p = 1013.25 * units.mbar
dry_bulb_temperature = 20. * units.degC
wet_bulb_temperature = 18. * units.degC
psychrometric_vapor_pressure = psychrometric_vapor_pressure_wet(dry_bulb_temperature,
wet_bulb_temperature, p)
assert_almost_equal(psychrometric_vapor_pressure, 19.3673 * units.mbar, 3)
def test_wet_psychrometric_rh():
"""Test calculation of relative humidity from wet and dry bulb temperatures."""
p = 1013.25 * units.mbar
dry_bulb_temperature = 20. * units.degC
wet_bulb_temperature = 18. * units.degC
psychrometric_rh = relative_humidity_wet_psychrometric(dry_bulb_temperature,
wet_bulb_temperature, p)
assert_almost_equal(psychrometric_rh, 82.8747 * units.percent, 3)
def test_wet_psychrometric_rh_kwargs():
"""Test calculation of relative humidity from wet and dry bulb temperatures."""
p = 1013.25 * units.mbar
dry_bulb_temperature = 20. * units.degC
wet_bulb_temperature = 18. * units.degC
coeff = 6.1e-4 / units.kelvin
psychrometric_rh = relative_humidity_wet_psychrometric(dry_bulb_temperature,
wet_bulb_temperature, p,
psychrometer_coefficient=coeff)
assert_almost_equal(psychrometric_rh, 82.9701 * units.percent, 3)
def test_mixing_ratio_from_relative_humidity():
"""Test relative humidity from mixing ratio."""
p = 1013.25 * units.mbar
temperature = 20. * units.degC
rh = 81.7219 * units.percent
w = mixing_ratio_from_relative_humidity(rh, temperature, p)
assert_almost_equal(w, 0.012 * units.dimensionless, 3)
def test_rh_mixing_ratio():
"""Test relative humidity from mixing ratio."""
p = 1013.25 * units.mbar
temperature = 20. * units.degC
w = 0.012 * units.dimensionless
rh = relative_humidity_from_mixing_ratio(w, temperature, p)
assert_almost_equal(rh, 81.7219 * units.percent, 3)
def test_mixing_ratio_from_specific_humidity():
"""Test mixing ratio from specific humidity."""
q = 0.012 * units.dimensionless
w = mixing_ratio_from_specific_humidity(q)
assert_almost_equal(w, 0.01215, 3)
def test_specific_humidity_from_mixing_ratio():
"""Test specific humidity from mixing ratio."""
w = 0.01215 * units.dimensionless
q = specific_humidity_from_mixing_ratio(w)
assert_almost_equal(q, 0.01200, 5)
def test_rh_specific_humidity():
"""Test relative humidity from specific humidity."""
p = 1013.25 * units.mbar
temperature = 20. * units.degC
q = 0.012 * units.dimensionless
rh = relative_humidity_from_specific_humidity(q, temperature, p)
assert_almost_equal(rh, 82.7145 * units.percent, 3)
def test_cape_cin():
"""Test the basic CAPE and CIN calculation."""
p = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
temperature = np.array([22.2, 14.6, 12., 9.4, 7., -38.]) * units.celsius
dewpoint = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
parcel_prof = parcel_profile(p, temperature[0], dewpoint[0])
cape, cin = cape_cin(p, temperature, dewpoint, parcel_prof)
assert_almost_equal(cape, 58.0368212 * units('joule / kilogram'), 6)
assert_almost_equal(cin, -89.8073512 * units('joule / kilogram'), 6)
def test_cape_cin_no_el():
"""Test that CAPE works with no EL."""
p = np.array([959., 779.2, 751.3, 724.3]) * units.mbar
temperature = np.array([22.2, 14.6, 12., 9.4]) * units.celsius
dewpoint = np.array([19., -11.2, -10.8, -10.4]) * units.celsius
parcel_prof = parcel_profile(p, temperature[0], dewpoint[0]).to('degC')
cape, cin = cape_cin(p, temperature, dewpoint, parcel_prof)
assert_almost_equal(cape, 0.08750805 * units('joule / kilogram'), 6)
assert_almost_equal(cin, -89.8073512 * units('joule / kilogram'), 6)
def test_cape_cin_no_lfc():
"""Test that CAPE is zero with no LFC."""
p = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
temperature = np.array([22.2, 24.6, 22., 20.4, 18., -10.]) * units.celsius
dewpoint = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
parcel_prof = parcel_profile(p, temperature[0], dewpoint[0]).to('degC')
cape, cin = cape_cin(p, temperature, dewpoint, parcel_prof)
assert_almost_equal(cape, 0.0 * units('joule / kilogram'), 6)
assert_almost_equal(cin, 0.0 * units('joule / kilogram'), 6)
def test_find_append_zero_crossings():
"""Test finding and appending zero crossings of an x, y series."""
x = np.arange(11) * units.hPa
y = np.array([3, 2, 1, -1, 2, 2, 0, 1, 0, -1, 2]) * units.degC
x2, y2 = _find_append_zero_crossings(x, y)
x_truth = np.array([0., 1., 2., 2.5, 3., 3.33333333, 4., 5.,
6., 7., 8., 9., 9.33333333, 10.]) * units.hPa
y_truth = np.array([3, 2, 1, 0, -1, 0, 2, 2, 0, 1, 0, -1, 0, 2]) * units.degC
assert_array_almost_equal(x2, x_truth, 6)
assert_almost_equal(y2, y_truth, 6)
def test_most_unstable_parcel():
"""Test calculating the most unstable parcel."""
levels = np.array([1000., 959., 867.9]) * units.mbar
temperatures = np.array([18.2, 22.2, 17.4]) * units.celsius
dewpoints = np.array([19., 19., 14.3]) * units.celsius
ret = most_unstable_parcel(levels, temperatures, dewpoints, depth=100 * units.hPa)
assert_almost_equal(ret[0], 959.0 * units.hPa, 6)
assert_almost_equal(ret[1], 22.2 * units.degC, 6)
assert_almost_equal(ret[2], 19.0 * units.degC, 6)
def test_isentropic_pressure():
"""Test calculation of isentropic pressure function."""
lev = [100000., 95000., 90000., 85000.] * units.Pa
tmp = np.ones((4, 5, 5))
tmp[0, :] = 296.
tmp[1, :] = 292.
tmp[2, :] = 290
tmp[3, :] = 288.
tmp[:, :, -1] = np.nan
tmpk = tmp * units.kelvin
isentlev = [296.] * units.kelvin
with pytest.warns(RuntimeWarning, match='invalid value'):
isentprs = isentropic_interpolation(isentlev, lev, tmpk)
trueprs = np.ones((1, 5, 5)) * (1000. * units.hPa)
trueprs[:, :, -1] = np.nan
assert isentprs[0].shape == (1, 5, 5)
assert_almost_equal(isentprs[0], trueprs, 3)
def test_isentropic_pressure_masked_column():
"""Test calculation of isentropic pressure function with a masked column (#769)."""
lev = [100000., 95000.] * units.Pa
tmp = np.ma.ones((len(lev), 5, 5))
tmp[0, :] = 296.
tmp[1, :] = 292.
tmp[:, :, -1] = np.ma.masked
isentprs = isentropic_interpolation([296.] * units.kelvin, lev, tmp * units.kelvin)
trueprs = np.ones((1, 5, 5)) * (1000. * units.hPa)
trueprs[:, :, -1] = np.nan
assert isentprs[0].shape == (1, 5, 5)
assert_almost_equal(isentprs[0], trueprs, 3)
def test_isentropic_pressure_p_increase():
"""Test calculation of isentropic pressure function, p increasing order."""
lev = [85000, 90000., 95000., 100000.] * units.Pa
tmp = np.ones((4, 5, 5))
tmp[0, :] = 288.
tmp[1, :] = 290.
tmp[2, :] = 292.
tmp[3, :] = 296.
tmpk = tmp * units.kelvin
isentlev = [296.] * units.kelvin
isentprs = isentropic_interpolation(isentlev, lev, tmpk)
trueprs = 1000. * units.hPa
assert_almost_equal(isentprs[0], trueprs, 3)
def test_isentropic_pressure_adition_args():
"""Test calculation of isentropic pressure function, additional args."""
lev = [100000., 95000., 90000., 85000.] * units.Pa
tmp = np.ones((4, 5, 5))
tmp[0, :] = 296.
tmp[1, :] = 292.
tmp[2, :] = 290.
tmp[3, :] = 288.
rh = np.ones((4, 5, 5))
rh[0, :] = 100.
rh[1, :] = 80.
rh[2, :] = 40.
rh[3, :] = 20.
relh = rh * units.percent
tmpk = tmp * units.kelvin
isentlev = [296.] * units.kelvin
isentprs = isentropic_interpolation(isentlev, lev, tmpk, relh)
truerh = 100. * units.percent
assert_almost_equal(isentprs[1], truerh, 3)
def test_isentropic_pressure_tmp_out():
"""Test calculation of isentropic pressure function, temperature output."""
lev = [100000., 95000., 90000., 85000.] * units.Pa
tmp = np.ones((4, 5, 5))
tmp[0, :] = 296.
tmp[1, :] = 292.
tmp[2, :] = 290.
tmp[3, :] = 288.
tmpk = tmp * units.kelvin
isentlev = [296.] * units.kelvin
isentprs = isentropic_interpolation(isentlev, lev, tmpk, tmpk_out=True)
truetmp = 296. * units.kelvin
assert_almost_equal(isentprs[1], truetmp, 3)
def test_isentropic_pressure_p_increase_rh_out():
"""Test calculation of isentropic pressure function, p increasing order."""
lev = [85000., 90000., 95000., 100000.] * units.Pa
tmp = np.ones((4, 5, 5))
tmp[0, :] = 288.
tmp[1, :] = 290.
tmp[2, :] = 292.
tmp[3, :] = 296.
tmpk = tmp * units.kelvin
rh = np.ones((4, 5, 5))
rh[0, :] = 20.
rh[1, :] = 40.
rh[2, :] = 80.
rh[3, :] = 100.
relh = rh * units.percent
isentlev = 296. * units.kelvin
isentprs = isentropic_interpolation(isentlev, lev, tmpk, relh)
truerh = 100. * units.percent
assert_almost_equal(isentprs[1], truerh, 3)
def test_isentropic_pressure_interp():
"""Test calculation of isentropic pressure function."""
lev = [100000., 95000., 90000., 85000.] * units.Pa
tmp = np.ones((4, 5, 5))
tmp[0, :] = 296.
tmp[1, :] = 292.
tmp[2, :] = 290
tmp[3, :] = 288.
tmpk = tmp * units.kelvin
isentlev = [296., 297] * units.kelvin
isentprs = isentropic_interpolation(isentlev, lev, tmpk)
trueprs = 936.18057 * units.hPa
assert_almost_equal(isentprs[0][1], trueprs, 3)
def test_isentropic_pressure_addition_args_interp():
"""Test calculation of isentropic pressure function, additional args."""
lev = [100000., 95000., 90000., 85000.] * units.Pa
tmp = np.ones((4, 5, 5))
tmp[0, :] = 296.
tmp[1, :] = 292.
tmp[2, :] = 290.
tmp[3, :] = 288.
rh = np.ones((4, 5, 5))
rh[0, :] = 100.
rh[1, :] = 80.
rh[2, :] = 40.
rh[3, :] = 20.
relh = rh * units.percent
tmpk = tmp * units.kelvin
isentlev = [296., 297.] * units.kelvin
isentprs = isentropic_interpolation(isentlev, lev, tmpk, relh)
truerh = 69.171 * units.percent
assert_almost_equal(isentprs[1][1], truerh, 3)
def test_isentropic_pressure_tmp_out_interp():
"""Test calculation of isentropic pressure function, temperature output."""
lev = [100000., 95000., 90000., 85000.] * units.Pa
tmp = np.ones((4, 5, 5))
tmp[0, :] = 296.
tmp[1, :] = 292.
tmp[2, :] = 290.
tmp[3, :] = 288.
tmpk = tmp * units.kelvin
isentlev = [296., 297.] * units.kelvin
isentprs = isentropic_interpolation(isentlev, lev, tmpk, tmpk_out=True)
truetmp = 291.4579 * units.kelvin
assert_almost_equal(isentprs[1][1], truetmp, 3)
def test_isentropic_pressure_data_bounds_error():
"""Test calculation of isentropic pressure function, error for data out of bounds."""
lev = [100000., 95000., 90000., 85000.] * units.Pa
tmp = np.ones((4, 5, 5))
tmp[0, :] = 296.
tmp[1, :] = 292.
tmp[2, :] = 290.
tmp[3, :] = 288.
tmpk = tmp * units.kelvin
isentlev = [296., 350.] * units.kelvin
with pytest.raises(ValueError):
isentropic_interpolation(isentlev, lev, tmpk)
def test_isentropic_pressure_4d():
"""Test calculation of isentropic pressure function."""
lev = [100000., 95000., 90000., 85000.] * units.Pa
tmp = np.ones((3, 4, 5, 5))
tmp[:, 0, :] = 296.
tmp[:, 1, :] = 292.
tmp[:, 2, :] = 290
tmp[:, 3, :] = 288.
tmpk = tmp * units.kelvin
rh = np.ones((3, 4, 5, 5))
rh[:, 0, :] = 100.
rh[:, 1, :] = 80.
rh[:, 2, :] = 40.
rh[:, 3, :] = 20.
relh = rh * units.percent
isentlev = [296., 297., 300.] * units.kelvin
isentprs = isentropic_interpolation(isentlev, lev, tmpk, relh, axis=1)
trueprs = 1000. * units.hPa
trueprs2 = 936.18057 * units.hPa
trueprs3 = 879.446 * units.hPa
truerh = 69.171 * units.percent
assert isentprs[0].shape == (3, 3, 5, 5)
assert_almost_equal(isentprs[0][:, 0, :], trueprs, 3)
assert_almost_equal(isentprs[0][:, 1, :], trueprs2, 3)
assert_almost_equal(isentprs[0][:, 2, :], trueprs3, 3)
assert_almost_equal(isentprs[1][:, 1, ], truerh, 3)
def test_surface_based_cape_cin():
"""Test the surface-based CAPE and CIN calculation."""
p = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
temperature = np.array([22.2, 14.6, 12., 9.4, 7., -38.]) * units.celsius
dewpoint = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
cape, cin = surface_based_cape_cin(p, temperature, dewpoint)
assert_almost_equal(cape, 58.0368212 * units('joule / kilogram'), 6)
assert_almost_equal(cin, -136.597240 * units('joule / kilogram'), 6)
def test_most_unstable_cape_cin_surface():
"""Test the most unstable CAPE/CIN calculation when surface is most unstable."""
pressure = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
temperature = np.array([22.2, 14.6, 12., 9.4, 7., -38.]) * units.celsius
dewpoint = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
mucape, mucin = most_unstable_cape_cin(pressure, temperature, dewpoint)
assert_almost_equal(mucape, 58.0368212 * units('joule / kilogram'), 6)
assert_almost_equal(mucin, -89.8073512 * units('joule / kilogram'), 6)
def test_most_unstable_cape_cin():
"""Test the most unstable CAPE/CIN calculation."""
pressure = np.array([1000., 959., 867.9, 850., 825., 800.]) * units.mbar
temperature = np.array([18.2, 22.2, 17.4, 10., 0., 15]) * units.celsius
dewpoint = np.array([19., 19., 14.3, 0., -10., 0.]) * units.celsius
mucape, mucin = most_unstable_cape_cin(pressure, temperature, dewpoint)
assert_almost_equal(mucape, 157.07111 * units('joule / kilogram'), 4)
assert_almost_equal(mucin, -15.74772 * units('joule / kilogram'), 4)
def test_mixed_parcel():
"""Test the mixed parcel calculation."""
pressure = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.hPa
temperature = np.array([22.2, 14.6, 12., 9.4, 7., -38.]) * units.degC
dewpoint = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.degC
parcel_pressure, parcel_temperature, parcel_dewpoint = mixed_parcel(pressure, temperature,
dewpoint,
depth=250 * units.hPa)
assert_almost_equal(parcel_pressure, 959. * units.hPa, 6)
assert_almost_equal(parcel_temperature, 28.7363771 * units.degC, 6)
assert_almost_equal(parcel_dewpoint, 7.1534658 * units.degC, 6)
def test_mixed_layer():
"""Test the mixed layer calculation."""
pressure = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.hPa
temperature = np.array([22.2, 14.6, 12., 9.4, 7., -38.]) * units.degC
mixed_layer_temperature = mixed_layer(pressure, temperature, depth=250 * units.hPa)[0]
assert_almost_equal(mixed_layer_temperature, 16.4024930 * units.degC, 6)
def test_dry_static_energy():
"""Test the dry static energy calculation."""
dse = dry_static_energy(1000 * units.m, 25 * units.degC)
assert_almost_equal(dse, 309.4474 * units('kJ/kg'), 6)