From 585db918b2f80264f609858ea7709c194380263a Mon Sep 17 00:00:00 2001 From: Martin Raspaud Date: Wed, 21 Nov 2018 17:36:34 +0100 Subject: [PATCH 1/4] Make sure a discrete data type is preserved through resampling --- satpy/resample.py | 11 ++++++++++- satpy/tests/test_resample.py | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/satpy/resample.py b/satpy/resample.py index 3f4edf733e..4cf266e861 100644 --- a/satpy/resample.py +++ b/satpy/resample.py @@ -851,6 +851,14 @@ def resample(source_area, data, destination_area, return res +def get_fill_value(dataset): + """Get the fill value of the *dataset*, defaulting to np.nan.""" + if np.issubdtype(dataset.dtype, np.integer): + return dataset.attrs.get('_FillValue', np.nan) + else: + return np.nan + + def resample_dataset(dataset, destination_area, **kwargs): """Resample the current projectable and return the resampled one. @@ -874,7 +882,8 @@ def resample_dataset(dataset, destination_area, **kwargs): return dataset - new_data = resample(source_area, dataset, destination_area, **kwargs) + fill_value = kwargs.pop('fill_value', get_fill_value(dataset)) + new_data = resample(source_area, dataset, destination_area, fill_value=fill_value, **kwargs) new_attrs = new_data.attrs new_data.attrs = dataset.attrs.copy() new_data.attrs.update(new_attrs) diff --git a/satpy/tests/test_resample.py b/satpy/tests/test_resample.py index cce99c0e5b..54731bd1f4 100644 --- a/satpy/tests/test_resample.py +++ b/satpy/tests/test_resample.py @@ -33,6 +33,27 @@ import mock +class TestHLResample(unittest.TestCase): + + def test_type_preserve(self): + from satpy.resample import resample_dataset + import xarray as xr + import dask.array as da + import numpy as np + from pyresample.geometry import SwathDefinition + source_area = SwathDefinition(xr.DataArray(da.arange(4, chunks=5).reshape((2, 2)), dims=['y', 'x']), + 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']) + 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)) + + class TestKDTreeResampler(unittest.TestCase): """Test the kd-tree resampler.""" @@ -369,6 +390,7 @@ def suite(): mysuite.addTest(loader.loadTestsFromTestCase(TestNativeResampler)) mysuite.addTest(loader.loadTestsFromTestCase(TestKDTreeResampler)) mysuite.addTest(loader.loadTestsFromTestCase(TestEWAResampler)) + mysuite.addTest(loader.loadTestsFromTestCase(TestHLResample)) return mysuite From 78fbab16525b709ece35a7e98a22c2b4b1c66041 Mon Sep 17 00:00:00 2001 From: Martin Raspaud Date: Wed, 21 Nov 2018 20:27:23 +0100 Subject: [PATCH 2/4] Update the docstrings of previous resampling fixes --- satpy/resample.py | 5 +++-- satpy/tests/test_resample.py | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/satpy/resample.py b/satpy/resample.py index 4cf266e861..99f69ce8a1 100644 --- a/satpy/resample.py +++ b/satpy/resample.py @@ -860,7 +860,7 @@ def get_fill_value(dataset): def resample_dataset(dataset, destination_area, **kwargs): - """Resample the current projectable and return the resampled one. + """Resample *dataset* and return the resampled version. Args: dataset (xarray.DataArray): Data to be resampled. @@ -870,7 +870,8 @@ def resample_dataset(dataset, destination_area, **kwargs): **kwargs: The extra parameters to pass to the resampler objects. Returns: - A resampled DataArray with updated ``.attrs["area"]`` field. + A resampled DataArray with updated ``.attrs["area"]`` field. The dtype + of the array is preserved. """ # call the projection stuff here diff --git a/satpy/tests/test_resample.py b/satpy/tests/test_resample.py index 54731bd1f4..60d5b00936 100644 --- a/satpy/tests/test_resample.py +++ b/satpy/tests/test_resample.py @@ -34,8 +34,10 @@ class TestHLResample(unittest.TestCase): + """Test the higher level resampling functions.""" def test_type_preserve(self): + """Check that the type of resampled datasets is preserved.""" from satpy.resample import resample_dataset import xarray as xr import dask.array as da From a0b54b3982cb6a54c58e13893e82a8b8ece64bb7 Mon Sep 17 00:00:00 2001 From: Martin Raspaud Date: Wed, 21 Nov 2018 20:37:12 +0100 Subject: [PATCH 3/4] Fix style --- satpy/resample.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/satpy/resample.py b/satpy/resample.py index 99f69ce8a1..42b511d39c 100644 --- a/satpy/resample.py +++ b/satpy/resample.py @@ -855,8 +855,7 @@ def get_fill_value(dataset): """Get the fill value of the *dataset*, defaulting to np.nan.""" if np.issubdtype(dataset.dtype, np.integer): return dataset.attrs.get('_FillValue', np.nan) - else: - return np.nan + return np.nan def resample_dataset(dataset, destination_area, **kwargs): From bc6ec5ebb2d2722a5d4ef22a0312df4ecbb97be6 Mon Sep 17 00:00:00 2001 From: David Hoese Date: Fri, 23 Nov 2018 09:56:55 -0600 Subject: [PATCH 4/4] Force pyresample minimum version in setup.py to 1.10.3 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 54429f12f8..83542db4a5 100644 --- a/setup.py +++ b/setup.py @@ -31,7 +31,7 @@ from setuptools import find_packages, setup -requires = ['numpy >=1.12', 'pillow', 'pyresample >=1.10.0', 'trollsift', +requires = ['numpy >=1.12', 'pillow', 'pyresample >=1.10.3', 'trollsift', 'trollimage >=1.5.1', 'pykdtree', 'six', 'pyyaml', 'xarray >=0.10.1', 'dask[array] >=0.17.1']