Skip to content

Commit

Permalink
Fix deepcode and codebeat issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ch-k committed Apr 30, 2021
1 parent b7b3a31 commit 0f93397
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
37 changes: 21 additions & 16 deletions satpy/readers/generic_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,25 @@ def _mask_image_data(data, info):
data.data = masked_data
data = data.sel(bands=BANDS[data.bands.size - 1])
elif hasattr(data, 'nodatavals') and data.nodatavals:
nodata_handling = info.get('nodata_handling', NODATA_HANDLING_FILLVALUE)
if nodata_handling == NODATA_HANDLING_NANMASK:
# data converted to float and masked with np.nan
data = data.astype(np.float32)
masked_data = da.stack([da.where(data.data[i, :, :] == nodataval, np.nan, data.data[i, :, :])
for i, nodataval in enumerate(data.nodatavals)])
data.data = masked_data
data.attrs['_FillValue'] = np.nan
elif nodata_handling == NODATA_HANDLING_FILLVALUE:
# keep data as it is but set _FillValue attribute to provided
# nodatavalue (first one as it has to be the same for all bands at least
# in GeoTiff, see GDAL gtiff driver documentation)
fill_value = data.nodatavals[0]
if np.issubdtype(data.dtype, np.integer):
fill_value = int(fill_value)
data.attrs['_FillValue'] = fill_value
data = _handle_nodatavals(data, info.get('nodata_handling', NODATA_HANDLING_FILLVALUE))
return data


def _handle_nodatavals(data, nodata_handling):
"""Mask data with np.nan or only set 'attr_FillValue'."""
if nodata_handling == NODATA_HANDLING_NANMASK:
# data converted to float and masked with np.nan
data = data.astype(np.float32)
masked_data = da.stack([da.where(data.data[i, :, :] == nodataval, np.nan, data.data[i, :, :])
for i, nodataval in enumerate(data.nodatavals)])
data.data = masked_data
data.attrs['_FillValue'] = np.nan
elif nodata_handling == NODATA_HANDLING_FILLVALUE:
# keep data as it is but set _FillValue attribute to provided
# nodatavalue (first one as it has to be the same for all bands at least
# in GeoTiff, see GDAL gtiff driver documentation)
fill_value = data.nodatavals[0]
if np.issubdtype(data.dtype, np.integer):
fill_value = int(fill_value)
data.attrs['_FillValue'] = fill_value
return data
26 changes: 13 additions & 13 deletions satpy/tests/reader_tests/test_generic_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ def test_GenericImageFileHandler(self):

dataset = reader.get_dataset(foo, {})
self.assertTrue(isinstance(dataset, xr.DataArray))
self.assertTrue('crs' in dataset.attrs)
self.assertTrue('transform' in dataset.attrs)
self.assertIn('crs', dataset.attrs)
self.assertIn('transform', dataset.attrs)
self.assertTrue(np.all(np.isnan(dataset.data[:, :10, :10].compute())))

def test_GenericImageFileHandler_masking_only_integer(self):
Expand All @@ -229,14 +229,14 @@ def __init__(self, filename, filename_info, filetype_info, file_content, **kwarg
# do nothing if not integer
float_data = data / 255.
reader = FakeGenericImageFileHandler("dummy", {}, {}, {"image": float_data})
self.assertTrue(reader.get_dataset(make_dataid(name='image'), {}) is float_data)
self.assertIs(reader.get_dataset(make_dataid(name='image'), {}), float_data)

# masking if integer
data = data.astype(np.uint32)
self.assertTrue(data.bands.size == 4)
self.assertEqual(data.bands.size, 4)
reader = FakeGenericImageFileHandler("dummy", {}, {}, {"image": data})
ret_data = reader.get_dataset(make_dataid(name='image'), {})
self.assertTrue(ret_data.bands.size == 3)
self.assertEqual(ret_data.bands.size, 3)

def test_GenericImageFileHandler_datasetid(self):
"""Test direct use of the reader."""
Expand All @@ -248,9 +248,9 @@ def test_GenericImageFileHandler_datasetid(self):
reader = GenericImageFileHandler(fname, fname_info, ftype_info)

foo = make_dataid(name='image-custom')
self.assertTrue(reader.file_content)
self.assertTrue(reader.file_content, 'file_content should be set')
dataset = reader.get_dataset(foo, {})
self.assertTrue(isinstance(dataset, xr.DataArray))
self.assertTrue(isinstance(dataset, xr.DataArray), 'dataset should be a xr.DataArray')

def test_GenericImageFileHandler_nodata(self):
"""Test nodata handling with direct use of the reader."""
Expand All @@ -262,21 +262,21 @@ def test_GenericImageFileHandler_nodata(self):
reader = GenericImageFileHandler(fname, fname_info, ftype_info)

foo = make_dataid(name='image-custom')
self.assertTrue(reader.file_content)
self.assertTrue(reader.file_content, 'file_content should be set')
info = {'nodata_handling': 'nan_mask'}
dataset = reader.get_dataset(foo, info)
self.assertTrue(isinstance(dataset, xr.DataArray))
self.assertTrue(np.all(np.isnan(dataset.data[0][:10, :10].compute())))
self.assertTrue(np.isnan(dataset.attrs['_FillValue']))
self.assertTrue(isinstance(dataset, xr.DataArray), 'dataset should be a xr.DataArray')
self.assertTrue(np.all(np.isnan(dataset.data[0][:10, :10].compute())), 'values should be np.nan')
self.assertTrue(np.isnan(dataset.attrs['_FillValue']), '_FillValue should be np.nan')

info = {'nodata_handling': 'fill_value'}
dataset = reader.get_dataset(foo, info)
self.assertTrue(isinstance(dataset, xr.DataArray))
self.assertTrue(isinstance(dataset, xr.DataArray), 'dataset should be a xr.DataArray')
self.assertEqual(np.sum(dataset.data[0][:10, :10].compute()), 0)
self.assertEqual(dataset.attrs['_FillValue'], 0)

# default same as 'nodata_handling': 'fill_value'
dataset = reader.get_dataset(foo, {})
self.assertTrue(isinstance(dataset, xr.DataArray))
self.assertTrue(isinstance(dataset, xr.DataArray), 'dataset should be a xr.DataArray')
self.assertEqual(np.sum(dataset.data[0][:10, :10].compute()), 0)
self.assertEqual(dataset.attrs['_FillValue'], 0)

0 comments on commit 0f93397

Please sign in to comment.