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

BUG: Fix passing masked arrays to CAPE/CIN calcs #1516

Merged
merged 1 commit into from Sep 30, 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
Expand Up @@ -837,7 +837,7 @@ def _insert_lcl_level(pressure, temperature, lcl_pressure):
# Pressure needs to be increasing for searchsorted, so flip it and then convert
# the index back to the original array
loc = pressure.size - pressure[::-1].searchsorted(lcl_pressure)
return np.insert(temperature.m, loc, interp_temp.m) * temperature.units
return temperature.units * np.insert(temperature.m, loc, interp_temp.m)


@exporter.export
Expand Down Expand Up @@ -1720,7 +1720,7 @@ def _find_append_zero_crossings(x, y):
y values of data

"""
crossings = find_intersections(x[1:], y[1:], np.zeros_like(y[1:]) * y.units, log_x=True)
crossings = find_intersections(x[1:], y[1:], y.units * np.zeros_like(y[1:]), log_x=True)
x = concatenate((x, crossings[0]))
y = concatenate((y, crossings[1]))

Expand Down
11 changes: 6 additions & 5 deletions tests/calc/test_thermo.py
Expand Up @@ -37,7 +37,7 @@
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
from metpy.units import masked_array, units


def test_relative_humidity_from_dewpoint():
Expand Down Expand Up @@ -1159,11 +1159,12 @@ def test_isentropic_interpolation_as_dataset():
assert result['isentropic_level'].attrs == expected['isentropic_level'].attrs


def test_surface_based_cape_cin():
@pytest.mark.parametrize('array_class', (units.Quantity, masked_array))
def test_surface_based_cape_cin(array_class):
"""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
p = array_class([959., 779.2, 751.3, 724.3, 700., 269.], units.mbar)
temperature = array_class([22.2, 14.6, 12., 9.4, 7., -38.], units.celsius)
dewpoint = array_class([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, 75.7340825 * units('joule / kilogram'), 2)
assert_almost_equal(cin, -136.607809 * units('joule / kilogram'), 2)
Expand Down