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

Fix RealisticColors compositor upcasting data to float64 #2669

Merged
merged 1 commit into from
Dec 14, 2023
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
8 changes: 4 additions & 4 deletions satpy/composites/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,17 +992,17 @@ def __call__(self, projectables, *args, **kwargs):
hrv = projectables[2]

try:
ch3 = 3 * hrv - vis06 - vis08
ch3 = 3.0 * hrv - vis06 - vis08
ch3.attrs = hrv.attrs
except ValueError:
raise IncompatibleAreas

ndvi = (vis08 - vis06) / (vis08 + vis06)
ndvi = np.where(ndvi < 0, 0, ndvi)
ndvi = ndvi.where(ndvi >= 0.0, 0.0)

ch1 = ndvi * vis06 + (1 - ndvi) * vis08
ch1 = ndvi * vis06 + (1.0 - ndvi) * vis08
ch1.attrs = vis06.attrs
ch2 = ndvi * vis08 + (1 - ndvi) * vis06
ch2 = ndvi * vis08 + (1.0 - ndvi) * vis06
ch2.attrs = vis08.attrs

res = super(RealisticColors, self).__call__((ch1, ch2, ch3),
Expand Down
34 changes: 34 additions & 0 deletions satpy/tests/test_composites.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python

Check notice on line 1 in satpy/tests/test_composites.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Lines of Code in a Single File

The lines of code increases from 1346 to 1370, improve code health by reducing it to 600. The number of Lines of Code in a single file. More Lines of Code lowers the code health.

Check notice on line 1 in satpy/tests/test_composites.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Number of Functions in a Single Module

The number of functions increases from 111 to 112, threshold = 75. This file contains too many functions. Beyond a certain threshold, more functions lower the code health.

Check notice on line 1 in satpy/tests/test_composites.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Low Cohesion

The number of different responsibilities increases from 67 to 68, threshold = 4. Cohesion is calculated using the LCOM4 metric. Low cohesion means that the module/class has multiple unrelated responsibilities, doing too many things and breaking the Single Responsibility Principle.
# -*- coding: utf-8 -*-
# Copyright (c) 2018-2020 Satpy developers
#
Expand Down Expand Up @@ -1867,3 +1867,37 @@
},
comp_file,
)


class TestRealisticColors:
"""Test the SEVIRI Realistic Colors compositor."""

def test_realistic_colors(self):
"""Test the compositor."""
from satpy.composites import RealisticColors

vis06 = xr.DataArray(da.arange(0, 15, dtype=np.float32).reshape(3, 5), dims=("y", "x"),
attrs={"foo": "foo"})
vis08 = xr.DataArray(da.arange(15, 0, -1, dtype=np.float32).reshape(3, 5), dims=("y", "x"),
attrs={"bar": "bar"})
hrv = xr.DataArray(6 * da.ones((3, 5), dtype=np.float32), dims=("y", "x"),
attrs={"baz": "baz"})

expected_red = np.array([[0.0, 2.733333, 4.9333334, 6.6, 7.733333],
[8.333333, 8.400001, 7.9333334, 7.0, 6.0],
[5.0, 4.0, 3.0, 2.0, 1.0]], dtype=np.float32)
expected_green = np.array([
[15.0, 12.266666, 10.066668, 8.400001, 7.2666664],
[6.6666665, 6.6000004, 7.0666666, 8.0, 9.0],
[10.0, 11.0, 12.0, 13.0, 14.0]], dtype=np.float32)

with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = RealisticColors("Ni!")
res = comp((vis06, vis08, hrv))

arr = res.values

assert res.dtype == np.float32
np.testing.assert_allclose(arr[0, :, :], expected_red)
np.testing.assert_allclose(arr[1, :, :], expected_green)
np.testing.assert_allclose(arr[2, :, :], 3.0)
Loading