Skip to content

Commit

Permalink
Merge pull request #585 from djhoese/bugfix-geotiff-fill-value
Browse files Browse the repository at this point in the history
Fix geotiff writer not using fill_value from writer YAML config
  • Loading branch information
djhoese authored Jan 23, 2019
2 parents d011adf + af1fe04 commit d83abd8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
16 changes: 16 additions & 0 deletions satpy/tests/writer_tests/test_geotiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
else:
import unittest

try:
from unittest import mock
except ImportError:
import mock


class TestGeoTIFFWriter(unittest.TestCase):
"""Test the GeoTIFF Writer class."""
Expand Down Expand Up @@ -105,6 +110,17 @@ def test_float_write(self):
dtype=np.float32)
w.save_datasets(datasets)

def test_fill_value_from_config(self):
"""Test fill_value coming from the writer config."""
from satpy.writers.geotiff import GeoTIFFWriter
datasets = self._get_test_datasets()
w = GeoTIFFWriter(base_dir=self.base_dir)
w.info['fill_value'] = 128
with mock.patch('satpy.writers.XRImage.save') as save_method:
save_method.return_value = None
w.save_datasets(datasets, compute=False)
self.assertEqual(save_method.call_args[1]['fill_value'], 128)


def suite():
"""The test suite for this writer's tests."""
Expand Down
40 changes: 37 additions & 3 deletions satpy/writers/geotiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,38 @@ def _delayed_create(create_opts, datasets, area, start_time, tags):

def save_image(self, img, filename=None, dtype=None, fill_value=None,
floating_point=None, compute=True, **kwargs):
"""Save the image to the given *filename* in geotiff_ format.
`floating_point` allows the saving of
'L' mode images in floating point format if set to True.
"""Save the image to the given ``filename`` in geotiff_ format.
Note for faster output and reduced memory usage the ``rasterio``
library must be installed. This writer currently falls back to
using ``gdal`` directly, but that will be deprecated in the future.
Args:
img (xarray.DataArray): Data to save to geotiff.
filename (str): Filename to save the image to. Defaults to
``filename`` passed during writer creation. Unlike the
creation ``filename`` keyword argument, this filename does not
get formatted with data attributes.
dtype (numpy.dtype): Numpy data type to save the image as.
Defaults to 8-bit unsigned integer (``np.uint8``). If the
``dtype`` argument is provided during writer creation then
that will be used as the default.
fill_value (int or float): Value to use where data values are
NaN/null. If this is specified in the writer configuration
file that value will be used as the default.
floating_point (bool): Deprecated. Use ``dtype=np.float64``
instead.
compute (bool): Compute dask arrays and save the image
immediately. If ``False`` then the return value can be passed
to :func:`~satpy.writers.compute_writer_results` to do the
computation. This is useful when multiple images may share
input calculations where dask can benefit from not repeating
them multiple times. Defaults to ``True`` in the writer by
itself, but is typically passed as ``False`` by callers where
calculations can be combined.
.. _geotiff: http://trac.osgeo.org/geotiff/
"""
filename = filename or self.get_filename(**img.data.attrs)

Expand All @@ -190,6 +217,9 @@ def save_image(self, img, filename=None, dtype=None, fill_value=None,
for k in kwargs.keys():
if k in self.GDAL_OPTIONS:
gdal_options[k] = kwargs[k]
if fill_value is None:
# fall back to fill_value from configuration file
fill_value = self.info.get('fill_value')

if floating_point is not None:
import warnings
Expand Down Expand Up @@ -222,6 +252,10 @@ def save_image(self, img, filename=None, dtype=None, fill_value=None,
except ImportError:
LOG.warning("Using legacy/slower geotiff save method, install "
"'rasterio' for faster saving.")
warnings.warn("Using legacy/slower geotiff save method with 'gdal'."
"This will be deprecated in the future. Install "
"'rasterio' for faster saving and future "
"compatibility.", PendingDeprecationWarning)
# force to numpy dtype object
dtype = np.dtype(dtype)
gformat = NP2GDAL[dtype.type]
Expand Down

0 comments on commit d83abd8

Please sign in to comment.