Skip to content

Commit

Permalink
return heat_index with temperature units
Browse files Browse the repository at this point in the history
  • Loading branch information
renee.walton authored and rwalton91 committed Dec 7, 2023
1 parent f09de38 commit d9b68b9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/metpy/calc/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def heat_index(temperature, relative_humidity, mask_undefined=True):
>>> from metpy.calc import heat_index
>>> from metpy.units import units
>>> heat_index(30 * units.degC, 90 * units.percent)
<Quantity([105.3943646], 'degree_Fahrenheit')>
<Quantity([40.774647], 'degree_Celsius')>
>>> heat_index(90 * units.degF, 90 * units.percent)
<Quantity([121.901204], 'degree_Fahrenheit')>
>>> heat_index(60 * units.degF, 90 * units.percent)
Expand Down Expand Up @@ -332,6 +332,9 @@ def heat_index(temperature, relative_humidity, mask_undefined=True):
* (units.Quantity(87., 'delta_degF') - delta[sel]))
hi[sel] = hi[sel] + rh85adj

# Convert heat index to temperature units
hi = hi.to(temperature.units)

# See if we need to mask any undefined values
if mask_undefined:
mask = np.array(temperature < units.Quantity(80., 'degF'))
Expand Down
9 changes: 5 additions & 4 deletions tests/calc/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,20 @@ def test_heat_index_undefined_flag():


def test_heat_index_units():
"""Test units coming out of heat index."""
"""Test units coming out of heat index are unchanged."""
temp = units.Quantity([35., 20.], units.degC)
rh = 70 * units.percent
hi = heat_index(temp, rh)
assert_almost_equal(hi.to('degC'), units.Quantity([50.3405, np.nan], units.degC), 4)
assert hi.units == temp.units
assert_almost_equal(hi, units.Quantity([50.3405, np.nan], units.degC), 4)


def test_heat_index_ratio():
"""Test giving humidity as number [0, 1] to heat index."""
temp = units.Quantity([35., 20.], units.degC)
rh = 0.7
hi = heat_index(temp, rh)
assert_almost_equal(hi.to('degC'), units.Quantity([50.3405, np.nan], units.degC), 4)
assert_almost_equal(hi, units.Quantity([50.3405, np.nan], units.degC), 4)


def test_heat_index_vs_nws():
Expand All @@ -278,7 +279,7 @@ def test_heat_index_kelvin():
rh = 0.7
hi = heat_index(temp, rh)
# NB rounded up test value here vs the above two tests
assert_almost_equal(hi.to('degC'), 50.3406 * units.degC, 4)
assert_almost_equal(hi, 50.3406 * units.degC, 4)


def test_height_to_geopotential(array_type):
Expand Down

0 comments on commit d9b68b9

Please sign in to comment.