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

Make sure a discrete data type is preserved through resampling #510

Merged
merged 4 commits into from
Nov 23, 2018
Merged
Show file tree
Hide file tree
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
15 changes: 12 additions & 3 deletions satpy/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,8 +851,15 @@ 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)
return np.nan


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.
Expand All @@ -862,7 +869,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
Expand All @@ -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)
djhoese marked this conversation as resolved.
Show resolved Hide resolved
new_attrs = new_data.attrs
new_data.attrs = dataset.attrs.copy()
new_data.attrs.update(new_attrs)
Expand Down
24 changes: 24 additions & 0 deletions satpy/tests/test_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@
import mock


class TestHLResample(unittest.TestCase):
mraspaud marked this conversation as resolved.
Show resolved Hide resolved
"""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
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."""

Expand Down Expand Up @@ -369,6 +392,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

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

Expand Down