Skip to content

Commit

Permalink
Merge pull request #2190 from djhoese/bugfix-btthreshold-enh
Browse files Browse the repository at this point in the history
Fix some enhancements producing dask arrays wrapped in dask arrays
  • Loading branch information
mraspaud committed Aug 23, 2022
2 parents 7a23508 + ff5ff96 commit a5573c1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
13 changes: 2 additions & 11 deletions satpy/enhancements/__init__.py
Expand Up @@ -277,11 +277,6 @@ def _srgb_gamma(arr):
return da.where(arr < 0.0031308, arr * 12.92, 1.055 * arr ** 0.41666 - 0.055)


def _lookup_delayed(luts, band_data):
# can't use luts.__getitem__ for some reason
return luts[band_data]


def lookup(img, **kwargs):
"""Assign values to channels based on a table."""
luts = np.array(kwargs['luts'], dtype=np.float32) / 255.0
Expand All @@ -295,11 +290,7 @@ def _lookup_table(band_data, luts=None, index=-1):
# NaN/null values will become 0
lut = luts[:, index] if len(luts.shape) == 2 else luts
band_data = band_data.clip(0, lut.size - 1).astype(np.uint8)

new_delay = dask.delayed(_lookup_delayed)(lut, band_data)
new_data = da.from_delayed(new_delay, shape=band_data.shape,
dtype=luts.dtype)
return new_data
return lut[band_data]


def colorize(img, **kwargs):
Expand Down Expand Up @@ -596,6 +587,6 @@ def btemp_threshold(img, min_in, max_in, threshold, threshold_out=None, **kwargs
@using_map_blocks
def _bt_threshold(band_data, threshold, high_coeffs, low_coeffs):
# expects dask array to be passed
return da.where(band_data >= threshold,
return np.where(band_data >= threshold,
high_coeffs.offset - high_coeffs.factor * band_data,
low_coeffs.offset - low_coeffs.factor * band_data)
7 changes: 6 additions & 1 deletion satpy/tests/enhancement_tests/test_enhancements.py
Expand Up @@ -44,7 +44,12 @@ def run_and_check_enhancement(func, data, expected, **kwargs):
new_keys = set(img.data.attrs.keys()) - {"enhancement_history"}
assert old_keys == new_keys

np.testing.assert_allclose(img.data.values, expected, atol=1.e-6, rtol=0)
res_data_arr = img.data
assert isinstance(res_data_arr, xr.DataArray)
assert isinstance(res_data_arr.data, da.Array)
res_data = res_data_arr.data.compute() # mimics what xrimage geotiff writing does
assert not isinstance(res_data, da.Array)
np.testing.assert_allclose(res_data, expected, atol=1.e-6, rtol=0)


def identical_decorator(func):
Expand Down

0 comments on commit a5573c1

Please sign in to comment.