diff --git a/pyresample/resampler.py b/pyresample/resampler.py index 2d6dbcf9..16ac3393 100644 --- a/pyresample/resampler.py +++ b/pyresample/resampler.py @@ -95,7 +95,7 @@ def compute(self, data, **kwargs): """ raise NotImplementedError - def resample(self, data, cache_dir=None, mask_area=None, **kwargs): + def resample(self, data, cache_dir=None, mask_area=None, force=False, **kwargs): """Resample `data` by calling `precompute` and `compute` methods. Only certain resampling classes may use `cache_dir` and the `mask` @@ -112,13 +112,16 @@ def resample(self, data, cache_dir=None, mask_area=None, **kwargs): mask_area (bool): Mask geolocation data where data values are invalid. This should be used when data values may affect what neighbors are considered valid. + force (bool): Force resampling by skipping the check for source + and target geometries equality. Can be useful e.g. for + gapfilling data. kwargs: Keyword arguments to pass to both the ``precompute`` and ``compute`` stages of the resampler. Returns (xarray.DataArray): Data resampled to the target area """ - if self._geometries_are_the_same(): + if not force and self._geometries_are_the_same(): return data # default is to mask areas for SwathDefinitions if mask_area is None and isinstance( diff --git a/pyresample/test/test_resamplers/test_resampler.py b/pyresample/test/test_resamplers/test_resampler.py index d7065058..4bbeb07c 100644 --- a/pyresample/test/test_resamplers/test_resampler.py +++ b/pyresample/test/test_resamplers/test_resampler.py @@ -86,8 +86,9 @@ def test_resampler(src, dst): (True, "dask"), # same dask tasks are equal (True, "swath_def"), # same underlying arrays are equal ]) -def test_base_resampler_does_nothing_when_src_and_dst_areas_are_equal(_geos_area, use_swaths, copy_dst_swath): - """Test that the BaseResampler does nothing when the source and target areas are the same.""" +@pytest.mark.parametrize("force", [False, True]) +def test_base_resampler_does_nothing_when_src_and_dst_areas_are_equal(_geos_area, use_swaths, copy_dst_swath, force): + """Test that the BaseResampler does nothing when the source and target areas are the same, unless forced.""" src_geom = _geos_area if not use_swaths else _xarray_swath_def_from_area(_geos_area) dst_geom = src_geom if copy_dst_swath == "dask": @@ -97,7 +98,12 @@ def test_base_resampler_does_nothing_when_src_and_dst_areas_are_equal(_geos_area resampler = BaseResampler(src_geom, dst_geom) some_data = xr.DataArray(da.zeros(src_geom.shape, dtype=np.float64), dims=('y', 'x')) - assert resampler.resample(some_data) is some_data + + if not force: + assert resampler.resample(some_data, force=force) is some_data + else: + with pytest.raises(NotImplementedError): + resampler.resample(some_data, force=force) @pytest.mark.parametrize(