Skip to content

Commit

Permalink
Merge pull request Unidata#1516 from dopplershift/fix-1496
Browse files Browse the repository at this point in the history
BUG: Fix passing masked arrays to CAPE/CIN calcs
  • Loading branch information
dopplershift authored Sep 30, 2020
2 parents 0546a76 + 5904440 commit bf77d24
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/metpy/calc/thermo.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit bf77d24

Please sign in to comment.