-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(temporarily) Pin dependencies (#11)
* bump xarray-simlab version * pin ipython, hvplot and datashader * update xshade module (more flexible) * remove old workaround for saving to netcdf * fix hillshade kwargs pass through * border encoding workaround
- Loading branch information
Showing
3 changed files
with
50 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,48 @@ | ||
import datashader.geo as dgeo | ||
import xarray as xr | ||
from datashader import geo | ||
|
||
|
||
def map_xr_func(func, da, time_dim=None, batch_dim=None, **kwargs): | ||
"""Map a DataArray compatible function, maybe brodcasting it over | ||
the time and/or batch dimension(s). | ||
Parameters | ||
---------- | ||
func : callable | ||
Any function accepting a :class:`xarray.DataArray` as first and | ||
only positional argument and returning another DataArray. | ||
da : :class:`xarray.DataArray` | ||
The DataArray object used to map ``func``. | ||
time_dim : str, optional | ||
Time dimension label. | ||
batch_dim : str | ||
Batch dimension label. | ||
**kwargs | ||
Keyword arguments passed to ``func``. | ||
Returns | ||
------- | ||
result : :class:`xarray.DataArray` | ||
The resulting DataArray. | ||
""" | ||
if batch_dim is not None: | ||
res_t = [] | ||
for _, da_t in da.groupby(batch_dim): | ||
res_t.append(map_xr_func(func, da, time_dim=time_dim, **kwargs)) | ||
|
||
result = xr.concat(res_t, batch_dim) | ||
|
||
elif time_dim is not None: | ||
result = da.groupby(time_dim).map(func, shortcut=False, **kwargs) | ||
|
||
else: | ||
result = func(da, **kwargs) | ||
|
||
return result | ||
|
||
|
||
def hillshade(ds, groupby=None, elev_var='topography__elevation', **kwargs): | ||
elev = ds[elev_var] | ||
|
||
if groupby is not None: | ||
# TODO: use shortcut=True | ||
# https://github.com/holoviz/datashader/issues/871 | ||
hshade = elev.groupby(groupby).apply(dgeo.hillshade, shortcut=False, | ||
**kwargs) | ||
else: | ||
hshade = dgeo.hillshade(elev, **kwargs) | ||
|
||
return hshade | ||
|
||
# TODO: related to todo above | ||
#return (hshade | ||
# .rename(dim_0='y', dim_1='x') | ||
# .assign_coords(x=ds.x, y=ds.y)) | ||
|
||
return map_xr_func(geo.hillshade, elev, time_dim=groupby, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters