Skip to content

Commit

Permalink
ENH: Directly use numpy to convert deg to mm. [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
Taher Chegini committed Oct 6, 2024
1 parent df041ab commit 1fc61a0
Showing 1 changed file with 8 additions and 22 deletions.
30 changes: 8 additions & 22 deletions src/py3dep/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import functools
import heapq
import warnings
from typing import TYPE_CHECKING, Any, Callable, Literal, Sequence, TypeVar, Union, overload
from typing import TYPE_CHECKING, Any, Callable, Literal, TypeVar, Union, overload

import numpy as np
import xarray as xr
Expand Down Expand Up @@ -258,27 +258,13 @@ def deg2mpm(slope: xr.DataArray) -> xr.DataArray:
is set to ``m/m``.
"""
with xr.set_options(keep_attrs=True):
if hasattr(slope, "_FillValue"):
nodata = slope.attrs["_FillValue"]
elif hasattr(slope, "nodatavals"):
_nodata = slope.attrs["nodatavals"]
nodata = _nodata[0] if isinstance(_nodata, Sequence) else _nodata
else:
nodata = np.nan
slope = slope.where(slope != nodata, drop=False)

def to_mpm(da: xr.DataArray) -> xr.DataArray:
"""Convert slope from degrees to meter/meter."""
return xr.apply_ufunc(
lambda x: np.tan(np.deg2rad(x)),
da,
vectorize=True,
)

slope = to_mpm(slope).compute()
slope.attrs["nodatavals"] = (np.nan,)
if hasattr(slope, "_FillValue"):
slope.attrs["_FillValue"] = np.nan
nodata = slope.rio.nodata
nodata = np.nan if nodata is None else nodata
if not np.isnan(nodata):
slope = slope.where(slope != nodata, drop=False)
slope = xr.where(slope == 90, np.nan, slope)
slope = np.tan(np.deg2rad(slope))
slope = slope.rio.write_nodata(np.nan)
slope.name = "slope"
slope.attrs["units"] = "m/m"
return slope
Expand Down

0 comments on commit 1fc61a0

Please sign in to comment.