Skip to content

Commit

Permalink
(temporarily) Pin dependencies (#11)
Browse files Browse the repository at this point in the history
* 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
benbovy authored Mar 17, 2021
1 parent 7c29bc0 commit 82e7ef3
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
7 changes: 4 additions & 3 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@ dependencies:
- dask
- dask-labextension
- xarray
- xarray-simlab=0.4.1
- xarray-simlab=0.5.0
- netcdf4
- fastscapelib-f2py=2.8.2
- ipyfastscape
- jupyter
- jupyterlab
- notebook
- ipykernel
- ipython=7.10
- matplotlib-base
- graphviz
- python-graphviz
- numba
- ipywidgets
- ipygany
- hvplot
- datashader
- hvplot=0.7.0
- datashader=0.11.1
- zarr
- tqdm
- nodejs
Expand Down
60 changes: 44 additions & 16 deletions examples/xshade.py
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)
4 changes: 2 additions & 2 deletions tutorial/01_run_basic_model.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,8 @@
"metadata": {},
"outputs": [],
"source": [
"# TODO: Fix border fillvalue issue\n",
"out_ds.border.attrs.pop(\"_FillValue\")\n",
"# TODO: fix border encoding\n",
"out_ds['border'] = out_ds.border.astype('S6')\n",
"\n",
"in_ds.to_netcdf('basic_input.nc')\n",
"\n",
Expand Down

0 comments on commit 82e7ef3

Please sign in to comment.