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

Apparent temperature working with scalars #838

Merged
merged 2 commits into from
May 10, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions metpy/calc/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ def apparent_temperature(temperature, rh, speed, face_level_winds=False):
heat_index, windchill

"""
is_not_scalar = isinstance(temperature.m, (list, tuple, np.ndarray))

temperature = atleast_1d(temperature)
rh = atleast_1d(rh)
speed = atleast_1d(speed)

wind_chill_temperature = windchill(temperature, speed, face_level_winds=face_level_winds,
mask_undefined=True).to(temperature.units)

Expand All @@ -274,11 +280,15 @@ def apparent_temperature(temperature, rh, speed, face_level_winds=False):
heat_index_temperature,
wind_chill_temperature)

# Fill in missing areas where neither wind chill or heat index are applicable with the
# ambient temperature.
app_temperature[app_temperature.mask] = temperature[app_temperature.mask]

return np.array(app_temperature) * temperature.units
if is_not_scalar:
# Fill in missing areas where neither wind chill or heat index are applicable with the
# ambient temperature.
app_temperature[app_temperature.mask] = temperature[app_temperature.mask]
return np.array(app_temperature) * temperature.units
else:
if app_temperature.mask:
app_temperature = temperature.m
return app_temperature[0] * temperature.units


@exporter.export
Expand Down
20 changes: 20 additions & 0 deletions metpy/calc/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,23 @@ def test_apparent_temperature():
[8.8140662, 20, 60]]) * units.degF
res = apparent_temperature(temperature, rel_humidity, wind)
assert_array_almost_equal(res, truth, 6)


def test_apparent_temperature_scalar():
"""Test the apparent temperature calculation with a scalar."""
temperature = 90 * units.degF
rel_humidity = 60 * units.percent
wind = 5 * units.mph
truth = 99.6777178 * units.degF
res = apparent_temperature(temperature, rel_humidity, wind)
assert_almost_equal(res, truth, 6)


def test_apparent_temperature_scalar_no_modification():
"""Test the apparent temperature calculation with a scalar that is NOOP."""
temperature = 70 * units.degF
rel_humidity = 60 * units.percent
wind = 5 * units.mph
truth = 70 * units.degF
res = apparent_temperature(temperature, rel_humidity, wind)
assert_almost_equal(res, truth, 6)