Skip to content

Commit

Permalink
Option to specify fallback value instead of default NaN for Masking (#96
Browse files Browse the repository at this point in the history
)

* Update sio.py

Fall back value instead NaN

* Update sio.py

Added option for specifying fallback value instead of NaN for masking.

* Changed docstring for roi
  • Loading branch information
Schlump authored and fmaussion committed Apr 5, 2018
1 parent 7ed7d3b commit 1c15b0c
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions salem/sio.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def subset(self, margin=0, ds=None, **kwargs):

def roi(self, ds=None, **kwargs):
"""roi(self, shape=None, geometry=None, grid=None, corners=None,
crs=wgs84, roi=None, all_touched=False)
crs=wgs84, roi=None, all_touched=False, other=None)
Make a region of interest (ROI) for the dataset.
Expand All @@ -489,8 +489,11 @@ def roi(self, ds=None, **kwargs):
pass-through argument for rasterio.features.rasterize, indicating
that all grid cells which are clipped by the shapefile defining
the region of interest should be included (default=False)
other : scalar, DataArray or Dataset, optional
Value to use for locations in this object where cond is False. By default, these locations filled with NA.
As in http://xarray.pydata.org/en/stable/generated/xarray.DataArray.where.html
"""

other = kwargs.pop('other', None)
if ds is not None:
grid = ds.salem.grid
kwargs.setdefault('grid', grid)
Expand All @@ -499,8 +502,9 @@ def roi(self, ds=None, **kwargs):
coords = {self.y_dim: self._obj[self.y_dim].values,
self.x_dim: self._obj[self.x_dim].values}
mask = xr.DataArray(mask, coords=coords,
dims=(self.y_dim, self.x_dim))
out = self._obj.where(mask)
dims=(self.y_dim, self.x_dim))

out = self._obj.where(mask, other=other)

# keep attrs
out.attrs = self._obj.attrs
Expand Down

0 comments on commit 1c15b0c

Please sign in to comment.