Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 13 additions & 2 deletions src/metpy/calc/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1833,6 +1833,9 @@ def angle_to_direction(input_angle, full=False, level=3):
'SW'

"""
if isinstance(input_angle, xr.DataArray):
xr_da = True
coords = input_angle.coords
try: # strip units temporarily
origin_units = input_angle.units
input_angle = input_angle.m
Expand Down Expand Up @@ -1897,12 +1900,20 @@ def angle_to_direction(input_angle, full=False, level=3):
if scalar:
return dir_str_arr[0]
else:
return np.array(dir_str_arr).reshape(origshape)
out = np.array(dir_str_arr).reshape(origshape)
if xr_da:
Comment thread Fixed
return xr.DataArray(out, coords)
else:
return out
else:
if scalar:
return dir_str_arr
else:
return np.array(dir_str_arr).reshape(origshape)
out = np.array(dir_str_arr).reshape(origshape)
if xr_da:
Comment thread Fixed
return xr.DataArray(out, coords)
else:
return out


def _unabbreviate_direction(abb_dir_str):
Expand Down
9 changes: 9 additions & 0 deletions tests/calc/test_calc_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,15 @@ def test_angle_to_direction_ndarray():
assert_array_equal(output_dirs, expected_dirs)


def test_angle_to_direction_ndarray_da():
"""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