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

DayNightCompositor with day_only does not mask out night part if no alpha layer is present, defeating its purpose #3003

Open
gerritholl opened this issue Nov 29, 2024 · 2 comments

Comments

@gerritholl
Copy link
Member

Describe the bug

When using the DayNightCompositor with the parameter day_night: day_only and include_alpha: False, the night part is not set to fill value and the resulting geotiff (mode L or mode RGB) is the same as without using the DayNightCompositor at all.

To Reproduce

import tempfile
import pathlib
import os
import rasterio

config = """sensor/name: visir

composites:
   day_vis_06:
    compositor: !!python/name:satpy.composites.DayNightCompositor
    prerequisites:
      - vis_06
    day_night: day_only
    include_alpha: False
    standard_name: image_ready
"""
with tempfile.TemporaryDirectory() as td:
    p = pathlib.Path(td)
    fn = p / "composites" / "fci.yaml"
    fn.parent.mkdir(exist_ok=True, parents=True)
    with fn.open(mode="wt", encoding="ascii") as fp:
        fp.write(config)
    os.environ["SATPY_CONFIG_PATH"] = td

    import hdf5plugin
    from satpy import Scene
    from glob import glob
    from satpy.utils import debug_on; debug_on()

    fci_files = glob(f"/media/nas/x23352/MTG/FCI/L1c-cases/20241025-nighttime-convection-mediterranean/09/*FDHSI*C_0055_*.nc")
    sc = Scene(filenames={"fci_l1c_nc": fci_files})
    sc.load(["day_vis_06"])
    ls = sc.resample("eurol")
    out = f"{td:s} / out.tif"
    ls.save_dataset("day_vis_06",
                    out,
                    write="geotiff",
                    fill_value=0)

    src = rasterio.open(out)
    arr = src.read(1)
    print(arr[0:5, 0:5])

Expected behavior

I expect that those pixels, which are night-time, are set to zero, in accordance to the fill value, so that supporting image viewers such as QGIS will display those pixels as transparent, like they do with space pixels.

Actual results

[[1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]
 [1 1 1 1 1]]

Environment Info:

  • OS: openSUSE Leap 15.3
  • Satpy Version: satpy main (v0.53.0-35-g096d5d9cc)

Additional context

It seems that the DayNightCompositor does not set the night time pixels to fill value at all, but leaves them at zero:

In [18]: print(sc["day_vis_06"][3000:4000:100, 3000:4000:100].compute())
<xarray.DataArray 'add-b998c5399f861c240d74f380f4bda087' (y: 10, x: 10)> Size: 400B
array([[nan, nan, nan, nan, nan, nan, nan, nan, nan, nan],
       [nan, nan, nan, nan, nan, nan, nan, nan, nan,  0.],
       [nan, nan, nan, nan, nan, nan, nan, nan,  0.,  0.],
       [nan, nan, nan, nan, nan, nan, nan,  0.,  0.,  0.],
       [nan, nan, nan, nan, nan, nan,  0.,  0.,  0.,  0.],
       [nan, nan, nan, nan, nan,  0.,  0.,  0.,  0.,  0.],
       [nan, nan, nan, nan,  0.,  0.,  0.,  0.,  0.,  0.],
       [nan, nan, nan,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [nan, nan,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.],
       [nan,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]], dtype=float32)

Then get_enhanced_image does not know those pixels should be masked, and sets them to one because it cannot know those pixels should be considered masked rather than physical zero values.

@gerritholl
Copy link
Member Author

The documentation contradicts the implementation and the test. The documentation claims that the night is replaced with nans:

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).

The test asserts that the night is replaced with zeroes:

with dask.config.set(scheduler=CustomScheduler(max_computes=1)):
comp = DayNightCompositor(name="dn_test", day_night="night_only", include_alpha=False)
res = comp((self.data_a, self.sza))
res = res.compute()
expected = np.array([[0., 0.11042609], [0.6683502, 1.]], dtype=np.float32)

@gerritholl
Copy link
Member Author

This was added by @yukaribbba in #2358 to solve #2357. Apparently, a black background was desired at the time, but the documentation still contradicts the implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant