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

Add an argument to the compositor to switch alpha band on/off in DayNightCompositor #2358

Merged
merged 19 commits into from
Jan 23, 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
18 changes: 17 additions & 1 deletion doc/source/composites.rst
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ first composite will be placed on the day-side of the scene, and the
second one on the night side. The transition from day to night is
done by calculating solar zenith angle (SZA) weighed average of the
two composites. The SZA can optionally be given as third dataset, and
if not given, the angles will be calculated. Three arguments are used
if not given, the angles will be calculated. Four arguments are used
to generate the image (default values shown in the example below).
They can be defined when initializing the compositor::

Expand All @@ -143,6 +143,10 @@ They can be defined when initializing the compositor::
- day_night (string): "day_night" means both day and night portions will be kept
"day_only" means only day portion will be kept
"night_only" means only night portion will be kept
- include_alpha (bool): This only affects the "day only" or "night only" result.
True means an alpha band will be added to the output image for transparency.
False means the output is a single-band image with undesired pixels being masked out
(replaced with NaNs).

Usage (with default values)::

Expand All @@ -160,6 +164,18 @@ provides only a day product with night portion masked-out::
>>> compositor = DayNightCompositor("dnc", lim_low=85., lim_high=88., day_night="day_only")
>>> composite = compositor([local_scene['true_color'])

By default, the image under `day_only` or `night_only` flag will come out
with an alpha band to display its transparency. It could be changed by
setting `include_alpha` to False if there's no need for that alpha band.
In such cases, it is recommended to use it together with `fill_value=0`
when saving to geotiff to get a single-band image with black background.
In the case below, the image shows its day portion and day/night
transition with night portion blacked-out instead of transparent::

>>> from satpy.composites import DayNightCompositor
>>> compositor = DayNightCompositor("dnc", lim_low=85., lim_high=88., day_night="day_only", need_alpha=False)
>>> composite = compositor([local_scene['true_color'])

RealisticColors
---------------

Expand Down
19 changes: 16 additions & 3 deletions satpy/composites/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ class DayNightCompositor(GenericCompositor):
of the image (night or day). See the documentation below for more details.
"""

def __init__(self, name, lim_low=85., lim_high=88., day_night="day_night", **kwargs):
def __init__(self, name, lim_low=85., lim_high=88., day_night="day_night", include_alpha=True, **kwargs):
"""Collect custom configuration values.

Args:
Expand All @@ -668,11 +668,16 @@ def __init__(self, name, lim_low=85., lim_high=88., day_night="day_night", **kwa
day_night (string): "day_night" means both day and night portions will be kept
"day_only" means only day portion will be kept
"night_only" means only night portion will be kept
include_alpha (bool): This only affects the "day only" or "night only" result.
True means an alpha band will be added to the output image for transparency.
False means the output is a single-band image with undesired pixels being masked out
(replaced with NaNs).

"""
self.lim_low = lim_low
self.lim_high = lim_high
self.day_night = day_night
self.include_alpha = include_alpha
super(DayNightCompositor, self).__init__(name, **kwargs)

def __call__(self, projectables, **kwargs):
Expand Down Expand Up @@ -700,10 +705,18 @@ def __call__(self, projectables, **kwargs):

if "only" in self.day_night:
# Only one portion (day or night) is selected. One composite is requested.
# Add alpha band to single L/RGB composite to make the masked-out portion transparent
# Add alpha band to single L/RGB composite to make the masked-out portion transparent when needed
# L -> LA
# RGB -> RGBA
foreground_data = add_alpha_bands(foreground_data)
if self.include_alpha:
foreground_data = add_alpha_bands(foreground_data)

# Use coszen to determine the undesired pixels and replace the coszen of these pixels with NaN.
# They will be passed to subsequent calculation and therefore make sure those pixels being masked-out.
if "day" in self.day_night:
coszen = da.where(coszen != 0, coszen, np.nan).compute()
else:
coszen = da.where(coszen != 1, coszen, np.nan).compute()

# No need to replace missing channel data with zeros
# Get metadata
Expand Down
68 changes: 52 additions & 16 deletions satpy/tests/test_composites.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,42 +399,78 @@ def test_daynight_area(self):
expected = np.array([[0., 0.33164983], [0.66835017, 1.]])
np.testing.assert_allclose(res.values[0], expected)

def test_night_only_sza(self):
"""Test compositor with night portion when SZA data is included."""
def test_night_only_sza_with_alpha(self):
"""Test compositor with night portion with alpha band when SZA data is included."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="night_only")
comp = DayNightCompositor(name='dn_test', day_night="night_only", include_alpha=True)
res = comp((self.data_b, self.sza))
res = res.compute()
expected = np.array([[np.nan, 0.], [0.5, 1.]])
np.testing.assert_allclose(res.values[0], expected)

def test_night_only_area(self):
"""Test compositor with night portion when SZA data is not provided."""
def test_night_only_sza_without_alpha(self):
"""Test compositor with night portion without alpha band when SZA data is included."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="night_only")
res = comp((self.data_b))
comp = DayNightCompositor(name='dn_test', day_night="night_only", include_alpha=False)
res = comp((self.data_b, self.sza))
res = res.compute()
expected = np.array([[np.nan, 0.], [0.5, 1.]])
np.testing.assert_allclose(res.values[0], expected)

def test_night_only_area_with_alpha(self):
"""Test compositor with night portion with alpha band when SZA data is not provided."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="night_only", include_alpha=True)
res = comp(self.data_b)
res = res.compute()
expected = np.array([[np.nan, np.nan], [np.nan, np.nan]])
np.testing.assert_allclose(res.values[0], expected)

def test_night_only_area_without_alpha(self):
"""Test compositor with night portion without alpha band when SZA data is not provided."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="night_only", include_alpha=False)
res = comp(self.data_b)
res = res.compute()
expected = np.array([[np.nan, 0.], [0., 0.]])
expected = np.array([np.nan, np.nan])
np.testing.assert_allclose(res.values[0], expected)

def test_day_only_sza(self):
"""Test compositor with day portion when SZA data is included."""
def test_day_only_sza_with_alpha(self):
"""Test compositor with day portion with alpha band when SZA data is included."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="day_only")
comp = DayNightCompositor(name='dn_test', day_night="day_only", include_alpha=True)
res = comp((self.data_a, self.sza))
res = res.compute()
expected = np.array([[0., 0.22122352], [0., 0.]])
expected = np.array([[0., 0.22122352], [np.nan, np.nan]])
np.testing.assert_allclose(res.values[0], expected)

def test_day_only_area(self):
"""Test compositor with day portion when SZA data is not provided."""
def test_day_only_sza_without_alpha(self):
"""Test compositor with day portion without alpha band when SZA data is included."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="day_only")
res = comp((self.data_a))
comp = DayNightCompositor(name='dn_test', day_night="day_only", include_alpha=False)
res = comp((self.data_a, self.sza))
res = res.compute()
expected = np.array([[0., 0.22122352], [np.nan, np.nan]])
np.testing.assert_allclose(res.values[0], expected)

def test_day_only_area_with_alpha(self):
"""Test compositor with day portion with alpha_band when SZA data is not provided."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="day_only", include_alpha=True)
res = comp(self.data_a)
res = res.compute()
expected = np.array([[0., 0.33164983], [0.66835017, 1.]])
np.testing.assert_allclose(res.values[0], expected)

def test_day_only_area_without_alpha(self):
"""Test compositor with day portion without alpha_band when SZA data is not provided."""
from satpy.composites import DayNightCompositor
comp = DayNightCompositor(name='dn_test', day_night="day_only", include_alpha=False)
res = comp(self.data_a)
res = res.compute()
expected = np.array([0., 0.33164983])
np.testing.assert_allclose(res.values[0], expected)


class TestFillingCompositor(unittest.TestCase):
"""Test case for the filling compositor."""
Expand Down