Skip to content

Commit

Permalink
Merge pull request #1426 from jthielen/fix-1364-2
Browse files Browse the repository at this point in the history
 Fix masked array issue in equivalent potential temperature
  • Loading branch information
dopplershift committed Jul 28, 2020
2 parents 321b969 + 895a5f7 commit e420468
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/metpy/calc/thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ def equivalent_potential_temperature(pressure, temperature, dewpoint):
th_l = t * (1000 / (p - e)) ** mpconsts.kappa * (t / t_l) ** (0.28 * r)
th_e = th_l * np.exp((3036. / t_l - 1.78) * r * (1 + 0.448 * r))

return th_e * units.kelvin
return units.Quantity(th_e, units.kelvin)


@exporter.export
Expand Down Expand Up @@ -1055,7 +1055,7 @@ def saturation_equivalent_potential_temperature(pressure, temperature):
th_l = t * (1000 / (p - e)) ** mpconsts.kappa
th_es = th_l * np.exp((3036. / t - 1.78) * r * (1 + 0.448 * r))

return th_es * units.kelvin
return units.Quantity(th_es, units.kelvin)


@exporter.export
Expand Down
32 changes: 32 additions & 0 deletions tests/calc/test_thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,24 @@ def test_equivalent_potential_temperature():
assert_almost_equal(ept, 311.18586467284007 * units.kelvin, 3)


def test_equivalent_potential_temperature_masked():
"""Test equivalent potential temperature calculation with masked arrays."""
p = 1000 * units.mbar
t = units.Quantity(np.ma.array([293., 294., 295.]), units.kelvin)
td = units.Quantity(
np.ma.array([280., 281., 282.], mask=[False, True, False]),
units.kelvin
)
ept = equivalent_potential_temperature(p, t, td)
expected = units.Quantity(
np.ma.array([311.18586, 313.51781, 315.93971], mask=[False, True, False]),
units.kelvin
)
assert isinstance(ept, units.Quantity)
assert isinstance(ept.m, np.ma.MaskedArray)
assert_array_almost_equal(ept, expected, 3)


def test_saturation_equivalent_potential_temperature():
"""Test saturation equivalent potential temperature calculation."""
p = 700 * units.mbar
Expand All @@ -486,6 +504,20 @@ def test_saturation_equivalent_potential_temperature():
assert_almost_equal(s_ept, 299.096584 * units.kelvin, 3)


def test_saturation_equivalent_potential_temperature_masked():
"""Test saturation equivalent potential temperature calculation with masked arrays."""
p = 1000 * units.mbar
t = units.Quantity(np.ma.array([293., 294., 295.]), units.kelvin)
s_ept = saturation_equivalent_potential_temperature(p, t)
expected = units.Quantity(
np.ma.array([335.02750, 338.95813, 343.08740]),
units.kelvin
)
assert isinstance(s_ept, units.Quantity)
assert isinstance(s_ept.m, np.ma.MaskedArray)
assert_array_almost_equal(s_ept, expected, 3)


def test_virtual_temperature():
"""Test virtual temperature calculation."""
t = 288. * units.kelvin
Expand Down

0 comments on commit e420468

Please sign in to comment.