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

return xr.DataArray for angle_to_direction if input is xr.DataArray #3463

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion src/metpy/calc/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1804,7 +1804,7 @@ def _abbreviate_direction(ext_dir_str):


@exporter.export
@preprocess_and_wrap()
@preprocess_and_wrap(wrap_like='input_angle')
def angle_to_direction(input_angle, full=False, level=3):
"""Convert the meteorological angle to directional text.

Expand Down Expand Up @@ -1838,6 +1838,8 @@ def angle_to_direction(input_angle, full=False, level=3):
input_angle = input_angle.m
except AttributeError: # no units associated
origin_units = units.degree
if origin_units == units.dimensionless: # assume if dimensionless is degree
origin_units = units.degree

if not hasattr(input_angle, '__len__') or isinstance(input_angle, str):
input_angle = [input_angle]
Expand Down
11 changes: 10 additions & 1 deletion tests/calc/test_calc_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,14 +933,23 @@ def test_angle_to_direction_level_1():
assert_array_equal(output_dirs, expected_dirs)


def test_angle_to_direction_ndarray():
def test_angle_to_direction_ndarray_np():
"""Test array of angles in degree with a 2d numpy array."""
expected_dirs = np.array([['E', 'W'], ['E', 'W']])
input_angle = np.array([[90, 270], [90, 270]])
output_dirs = angle_to_direction(input_angle, level=1)
assert_array_equal(output_dirs, expected_dirs)


def test_angle_to_direction_ndarray_xr():
"""Test array of angles in degree with a 2d xarray.DataArray."""
expected_dirs = xr.DataArray(np.array([['E', 'W'], ['E', 'W']]))
input_angle = xr.DataArray(np.array([[90, 270], [90, 270]]))
output_dirs = angle_to_direction(input_angle, level=1)
assert_array_equal(output_dirs, expected_dirs)
assert isinstance(output_dirs, xr.DataArray)


def test_azimuth_range_to_lat_lon():
"""Test conversion of azimuth and range to lat/lon grid."""
az = [332.2403, 334.6765, 337.2528, 339.73846, 342.26257]
Expand Down
Loading