Completed
Pull Request — master (#322)
by
unknown
02:02 queued 36s
created

test_virtual_temperature()   A

Complexity

Conditions 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
# Copyright (c) 2008-2015 MetPy Developers.
2
# Distributed under the terms of the BSD 3-Clause License.
3
# SPDX-License-Identifier: BSD-3-Clause
4
"""Test the `thermo` module."""
5
6
import numpy as np
7
8
from metpy.calc import (dewpoint, dewpoint_rh, dry_lapse, equivalent_potential_temperature,
9
                        lcl, lfc, mixing_ratio, moist_lapse, parcel_profile,
10
                        potential_temperature, saturation_mixing_ratio, el,
11
                        saturation_vapor_pressure, vapor_pressure, virtual_temperature)
12
from metpy.testing import assert_almost_equal, assert_array_almost_equal
13
from metpy.units import units
14
15
16
def test_potential_temperature():
17
    """Test potential_temperature calculation."""
18
    temp = np.array([278., 283., 291., 298.]) * units.kelvin
19
    pres = np.array([900., 500., 300., 100.]) * units.mbar
20
    real_th = np.array([286.493, 344.961, 410.4335, 575.236]) * units.kelvin
21
    assert_array_almost_equal(potential_temperature(pres, temp), real_th, 3)
22
23
24
def test_scalar():
25
    """Test potential_temperature accepts scalar values."""
26
    assert_almost_equal(potential_temperature(1000. * units.mbar, 293. * units.kelvin),
27
                        293. * units.kelvin, 4)
28
    assert_almost_equal(potential_temperature(800. * units.mbar, 293. * units.kelvin),
29
                        312.2828 * units.kelvin, 4)
30
31
32
def test_fahrenheit():
33
    """Test that potential_temperature handles temperature values in Fahrenheit."""
34
    assert_almost_equal(potential_temperature(800. * units.mbar, 68. * units.degF),
35
                        (312.444 * units.kelvin).to(units.degF), 2)
36
37
38
def test_pot_temp_inhg():
39
    """Test that potential_temperature can handle pressure not in mb (issue #165)."""
40
    assert_almost_equal(potential_temperature(29.92 * units.inHg, 29 * units.degC),
41
                        301.019735 * units.kelvin, 4)
42
43
44
def test_dry_lapse():
45
    """Test dry_lapse calculation."""
46
    levels = np.array([1000, 900, 864.89]) * units.mbar
47
    temps = dry_lapse(levels, 303.15 * units.kelvin)
48
    assert_array_almost_equal(temps,
49
                              np.array([303.15, 294.16, 290.83]) * units.kelvin, 2)
50
51
52
def test_dry_lapse_2_levels():
53
    """Test dry_lapse calculation when given only two levels."""
54
    temps = dry_lapse(np.array([1000., 500.]) * units.mbar, 293. * units.kelvin)
55
    assert_array_almost_equal(temps, [293., 240.3723] * units.kelvin, 4)
56
57
58
def test_moist_lapse():
59
    """Test moist_lapse calculation."""
60
    temp = moist_lapse(np.array([1000., 800., 600., 500., 400.]) * units.mbar,
61
                       293. * units.kelvin)
62
    true_temp = np.array([293, 284.64, 272.81, 264.42, 252.91]) * units.kelvin
63
    assert_array_almost_equal(temp, true_temp, 2)
64
65
66
def test_moist_lapse_degc():
67
    """Test moist_lapse with Celsius temperatures."""
68
    temp = moist_lapse(np.array([1000., 800., 600., 500., 400.]) * units.mbar,
69
                       19.85 * units.degC)
70
    true_temp = np.array([293, 284.64, 272.81, 264.42, 252.91]) * units.kelvin
71
    assert_array_almost_equal(temp, true_temp, 2)
72
73
74
def test_parcel_profile():
75
    """Test parcel profile calculation."""
76
    levels = np.array([1000., 900., 800., 700., 600., 500., 400.]) * units.mbar
77
    true_prof = np.array([303.15, 294.16, 288.026, 283.073, 277.058, 269.402,
78
                          258.966]) * units.kelvin
79
80
    prof = parcel_profile(levels, 30. * units.degC, 20. * units.degC)
81
    assert_array_almost_equal(prof, true_prof, 2)
82
83
84
def test_parcel_profile_saturated():
85
    """Test parcel_profile works when LCL in levels (issue #232)."""
86
    levels = np.array([1000., 700., 500.]) * units.mbar
87
    true_prof = np.array([296.95, 284.381, 271.123]) * units.kelvin
88
89
    prof = parcel_profile(levels, 23.8 * units.degC, 23.8 * units.degC)
90
    assert_array_almost_equal(prof, true_prof, 2)
91
92
93
def test_sat_vapor_pressure():
94
    """Test saturation_vapor_pressure calculation."""
95
    temp = np.array([5., 10., 18., 25.]) * units.degC
96
    real_es = np.array([8.72, 12.27, 20.63, 31.67]) * units.mbar
97
    assert_array_almost_equal(saturation_vapor_pressure(temp), real_es, 2)
98
99
100
def test_sat_vapor_pressure_scalar():
101
    """Test saturation_vapor_pressure handles scalar values."""
102
    es = saturation_vapor_pressure(0 * units.degC)
103
    assert_almost_equal(es, 6.112 * units.mbar, 3)
104
105
106
def test_sat_vapor_pressure_fahrenheit():
107
    """Test saturation_vapor_pressure handles temperature in Fahrenheit."""
108
    temp = np.array([50., 68.]) * units.degF
109
    real_es = np.array([12.2717, 23.3695]) * units.mbar
110
    assert_array_almost_equal(saturation_vapor_pressure(temp), real_es, 4)
111
112
113
def test_basic_dewpoint_rh():
114
    """Test dewpoint_rh function."""
115
    temp = np.array([30., 25., 10., 20., 25.]) * units.degC
116
    rh = np.array([30., 45., 55., 80., 85.]) / 100.
117
118
    real_td = np.array([11, 12, 1, 16, 22]) * units.degC
119
    assert_array_almost_equal(real_td, dewpoint_rh(temp, rh), 0)
120
121
122
def test_scalar_dewpoint_rh():
123
    """Test dewpoint_rh with scalar values."""
124
    td = dewpoint_rh(10.6 * units.degC, 0.37)
125
    assert_almost_equal(td, 26. * units.degF, 0)
126
127
128
def test_dewpoint():
129
    """Test dewpoint calculation."""
130
    assert_almost_equal(dewpoint(6.112 * units.mbar), 0. * units.degC, 2)
131
132
133
def test_dewpoint_weird_units():
134
    """Test dewpoint using non-standard units.
135
136
    Revealed from odd dimensionless units and ending up using numpy.ma math
137
    functions instead of numpy ones.
138
    """
139
    assert_almost_equal(dewpoint(15825.6 * units('g * mbar / kg')),
140
                        13.8564 * units.degC, 4)
141
142
143
def test_mixing_ratio():
144
    """Test mixing ratio calculation."""
145
    p = 998. * units.mbar
146
    e = 73.75 * units.mbar
147
    assert_almost_equal(mixing_ratio(e, p), 0.04963, 2)
148
149
150
def test_vapor_pressure():
151
    """Test vapor pressure calculation."""
152
    assert_almost_equal(vapor_pressure(998. * units.mbar, 0.04963),
153
                        73.74925 * units.mbar, 5)
154
155
156
def test_lcl():
157
    """Test LCL calculation."""
158
    lcl_pressure, lcl_temperature = lcl(1000. * units.mbar, 30. * units.degC, 20. * units.degC)
159
    assert_almost_equal(lcl_pressure, 864.761 * units.mbar, 2)
160
    assert_almost_equal(lcl_temperature, 17.676 * units.degC, 2)
161
162
163
def test_lfc_basic():
164
    """Test LFC calculation."""
165
    levels = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
166
    temperatures = np.array([22.2, 14.6, 12., 9.4, 7., -49.]) * units.celsius
167
    dewpoints = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
168
    l = lfc(levels, temperatures, dewpoints)
169
    assert_almost_equal(l[0], 727.468 * units.mbar, 2)
170
    assert_almost_equal(l[1], 9.705 * units.celsius, 2)
171
172
173
def test_saturation_mixing_ratio():
174
    """Test saturation mixing ratio calculation."""
175
    p = 999. * units.mbar
176
    t = 288. * units.kelvin
177
    assert_almost_equal(saturation_mixing_ratio(p, t), .01068, 3)
178
179
180
def test_equivalent_potential_temperature():
181
    """Test equivalent potential temperature calculation."""
182
    p = 999. * units.mbar
183
    t = 288. * units.kelvin
184
    ept = equivalent_potential_temperature(p, t)
185
    assert_almost_equal(ept, 315.9548 * units.kelvin, 3)
186
187
188
def test_virtual_temperature():
189
    """Test virtual temperature calculation."""
190
    pressure = 1013. * units.mbar
191
    temperature = 293. * units.kelvin
192
    dewpt = 288 * units.kelvin
193
    assert_almost_equal(virtual_temperature(pressure, temperature, dewpt),
194
                        294.8569 * units.kelvin, 3)
195
196
197
def test_el():
198
    """Test equilibrium layer calculation."""
199
    levels = np.array([959., 779.2, 751.3, 724.3, 700., 269.]) * units.mbar
200
    temperatures = np.array([22.2, 14.6, 12., 9.4, 7., -38.]) * units.celsius
201
    dewpoints = np.array([19., -11.2, -10.8, -10.4, -10., -53.2]) * units.celsius
202
    el_pressure, el_temperature = el(levels, temperatures, dewpoints)
203
    assert_almost_equal(el_pressure, 520.8420 * units.mbar, 3)
204
    assert_almost_equal(el_temperature, -11.7055 * units.degC, 3)
205