Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set summed bin without data to fill_value #364

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
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
14 changes: 12 additions & 2 deletions pyresample/bucket/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,17 @@
target_shape = self.target_area.shape
self.idxs = self.y_idxs * target_shape[1] + self.x_idxs

def get_sum(self, data, skipna=True):
def get_sum(self, data, fill_value=np.nan, skipna=True):
"""Calculate sums for each bin with drop-in-a-bucket resampling.

Parameters
----------
data : Numpy or Dask array
Data to be binned and summed.
fill_value : float
Fill value to mark missing/invalid values in the input data,
as well as in the binned and averaged output data.
Default: np.nan
skipna : boolean (optional)
If True, skips NaN values for the sum calculation
(similarly to Numpy's `nansum`). Buckets containing only NaN are set to zero.
Expand Down Expand Up @@ -241,6 +245,10 @@
# TODO remove following line in favour of weights = data when dask histogram bug (issue #6935) is fixed
sums = self._mask_bins_with_nan_if_not_skipna(skipna, data, out_size, sums)

# set bin without data to fill_value if fill_value exists
if ~np.isnan(fill_value):
sums = da.where(sums == 0, fill_value, sums)

Check warning on line 250 in pyresample/bucket/__init__.py

View check run for this annotation

Codecov / codecov/patch

pyresample/bucket/__init__.py#L250

Added line #L250 was not covered by tests

return sums.reshape(self.target_area.shape)

def _mask_bins_with_nan_if_not_skipna(self, skipna, data, out_size, statistic):
Expand All @@ -251,7 +259,7 @@
statistic = da.where(nan_bins > 0, np.nan, statistic)
return statistic

def _call_bin_statistic(self, statistic_method, data, fill_value=None, skipna=None):
def _call_bin_statistic(self, statistic_method, data, fill_value=np.nan, skipna=None):
"""Calculate statistics (min/max) for each bin with drop-in-a-bucket resampling."""
if isinstance(data, xr.DataArray):
data = data.data
Expand All @@ -268,6 +276,8 @@
shape=out_shape,
dtype=np.float64)

# set bin without data to fill_value
statistics = da.where(np.isin(statistics, (0, fill_value)), fill_value, statistics)
return statistics

def get_min(self, data, fill_value=np.nan, skipna=True):
Expand Down
Loading