Skip to content

Commit

Permalink
Merge pull request #2195 from djhoese/logging-extra-enh
Browse files Browse the repository at this point in the history
Add additional logging information about enhancements being used
  • Loading branch information
mraspaud committed Aug 30, 2022
2 parents ff54a74 + 8a8c7f4 commit 687494c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
13 changes: 13 additions & 0 deletions satpy/tests/test_writers.py
Expand Up @@ -241,6 +241,19 @@ def test_multisensor_exact(self):
# alphabetically first
np.testing.assert_allclose(img.data.values[0], ds.data / 20.0)

def test_enhance_bad_query_value(self):
"""Test Enhancer doesn't fail when query includes bad values."""
from xarray import DataArray

from satpy.writers import Enhancer, get_enhanced_image
ds = DataArray(np.arange(1, 11.).reshape((2, 5)),
attrs=dict(name=["I", "am", "invalid"], sensor='test_sensor2', mode='L'),
dims=['y', 'x'])
e = Enhancer()
assert e.enhancement_tree is not None
with pytest.raises(KeyError, match="No .* found for None"):
get_enhanced_image(ds, enhance=e)


class TestEnhancerUserConfigs(_BaseCustomEnhancementConfigTests):
"""Test `Enhancer` functionality when user's custom configurations are present."""
Expand Down
22 changes: 12 additions & 10 deletions satpy/writers/__init__.py
Expand Up @@ -403,7 +403,7 @@ def get_enhanced_image(dataset, enhance=None, overlay=None, decorate=None,
dataset (xarray.DataArray): Data to be enhanced and converted to an image.
enhance (bool or Enhancer): Whether to automatically enhance
data to be more visually useful and to fit inside the file
format being saved to. By default this will default to using
format being saved to. By default, this will default to using
the enhancement configuration files found using the default
:class:`~satpy.writers.Enhancer` class. This can be set to
`False` so that no enhancments are performed. This can also
Expand Down Expand Up @@ -662,7 +662,7 @@ def save_datasets(self, datasets, compute=True, **kwargs):
Args:
datasets (iterable): Iterable of `xarray.DataArray` objects to
save using this writer.
compute (bool): If `True` (default), compute all of the saves to
compute (bool): If `True` (default), compute all the saves to
disk. If `False` then the return value is either
a :doc:`dask:delayed` object or two lists to
be passed to a :func:`dask.array.store` call.
Expand All @@ -672,7 +672,7 @@ def save_datasets(self, datasets, compute=True, **kwargs):
Returns:
Value returned depends on `compute` keyword argument. If
`compute` is `True` the value is the result of a either a
`compute` is `True` the value is the result of either a
:func:`dask.array.store` operation or a :doc:`dask:delayed`
compute, typically this is `None`. If `compute` is `False` then
the result is either a :doc:`dask:delayed` object that can be
Expand Down Expand Up @@ -725,7 +725,7 @@ def save_dataset(self, dataset, filename=None, fill_value=None,
If `compute` is `False` then the returned value is either a
:doc:`dask:delayed` object that can be computed using
`delayed.compute()` or a tuple of (source, target) that should be
passed to :func:`dask.array.store`. If target is provided the the
passed to :func:`dask.array.store`. If target is provided the
caller is responsible for calling `target.close()` if the target
has this method.
Expand Down Expand Up @@ -760,7 +760,7 @@ def __init__(self, name=None, filename=None, base_dir=None, enhance=None, **kwar
Base destination directories for all created files.
enhance (bool or Enhancer): Whether to automatically enhance
data to be more visually useful and to fit inside the file
format being saved to. By default this will default to using
format being saved to. By default, this will default to using
the enhancement configuration files found using the default
:class:`~satpy.writers.Enhancer` class. This can be set to
`False` so that no enhancments are performed. This can also
Expand Down Expand Up @@ -903,7 +903,7 @@ def __init__(self, decision_dicts, match_keys, multival_keys=None):
decision_dicts (dict): Dictionary of dictionaries. Each
sub-dictionary contains key/value pairs that can be
matched from the `find_match` method. Sub-dictionaries
can include additional keys outside of the ``match_keys``
can include additional keys outside the ``match_keys``
provided to act as the "result" of a query. The keys of
the root dict are arbitrary.
match_keys (list): Keys of the provided dictionary to use for
Expand Down Expand Up @@ -1023,9 +1023,10 @@ def find_match(self, **query_dict):
"""
try:
match = self._find_match(self._tree, self._match_keys, query_dict)
except (KeyError, IndexError, ValueError):
except (KeyError, IndexError, ValueError, TypeError):
LOG.debug("Match exception:", exc_info=True)
LOG.error("Error when finding matching decision section")
match = None

if match is None:
# only possible if no default section was provided
Expand Down Expand Up @@ -1067,6 +1068,7 @@ def add_config_to_tree(self, *decision_dict):
if not enhancement_section:
LOG.debug("Config '{}' has no '{}' section or it is empty".format(config_file, self.prefix))
continue
LOG.debug(f"Adding enhancement configuration from file: {config_file}")
conf = recursive_dict_update(conf, enhancement_section)
elif isinstance(config_file, dict):
conf = recursive_dict_update(conf, config_file)
Expand Down Expand Up @@ -1150,11 +1152,11 @@ def apply(self, img, **info):
"""Apply the enhancements."""
enh_kwargs = self.enhancement_tree.find_match(**info)

LOG.debug("Enhancement configuration options: %s" %
(str(enh_kwargs['operations']), ))
backup_id = f"<name={info.get('name')}, calibration={info.get('calibration')}>"
data_id = info.get("_satpy_id", backup_id)
LOG.debug(f"Data for {data_id} will be enhanced with options:\n\t{enh_kwargs['operations']}")
for operation in enh_kwargs['operations']:
fun = operation['method']
args = operation.get('args', [])
kwargs = operation.get('kwargs', {})
fun(img, *args, **kwargs)
# img.enhance(**enh_kwargs)

0 comments on commit 687494c

Please sign in to comment.