Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix masked array issue in equivalent potential temperature #1426

Merged
merged 1 commit into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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