Skip to content

Commit

Permalink
Merge pull request #662 from mraspaud/fix-resample-int-mask
Browse files Browse the repository at this point in the history
Fix masked resampling when dataset dtype is integer
  • Loading branch information
mraspaud committed Mar 15, 2019
2 parents be6e5a0 + c383dbd commit 757a052
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 5 additions & 1 deletion satpy/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,11 @@ def resample(self, data, cache_dir=None, mask_area=None, **kwargs):
geo_dims = ('y', 'x')
flat_dims = [dim for dim in data.dims if dim not in geo_dims]
# xarray <= 0.10.1 computes dask arrays during isnull
kwargs['mask'] = data.isnull().all(dim=flat_dims)
if np.issubdtype(data.dtype, np.integer):
kwargs['mask'] = data == data.attrs.get('_FillValue', np.iinfo(data.dtype.type).max)
else:
kwargs['mask'] = data.isnull()
kwargs['mask'] = kwargs['mask'].all(dim=flat_dims)
cache_id = self.precompute(cache_dir=cache_dir, **kwargs)
return self.compute(data, cache_id=cache_id, **kwargs)

Expand Down
11 changes: 8 additions & 3 deletions satpy/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ def test_type_preserve(self):
xr.DataArray(da.arange(4, chunks=5).reshape((2, 2)), dims=['y', 'x']))
dest_area = SwathDefinition(xr.DataArray(da.arange(4, chunks=5).reshape((2, 2)) + .0001, dims=['y', 'x']),
xr.DataArray(da.arange(4, chunks=5).reshape((2, 2)) + .0001, dims=['y', 'x']))
expected = np.array([[1, 2], [3, 255]])
data = xr.DataArray(da.from_array(expected, chunks=5), dims=['y', 'x'])
expected_gap = np.array([[1, 2], [3, 255]])
data = xr.DataArray(da.from_array(expected_gap, chunks=5), dims=['y', 'x'])
data.attrs['_FillValue'] = 255
data.attrs['area'] = source_area
res = resample_dataset(data, dest_area)
self.assertEqual(res.dtype, data.dtype)
self.assertTrue(np.all(res.values == expected))
self.assertTrue(np.all(res.values == expected_gap))

expected_filled = np.array([[1, 2], [3, 3]])
res = resample_dataset(data, dest_area, radius_of_influence=1000000)
self.assertEqual(res.dtype, data.dtype)
self.assertTrue(np.all(res.values == expected_filled))


class TestKDTreeResampler(unittest.TestCase):
Expand Down

0 comments on commit 757a052

Please sign in to comment.