pytroll / pytroll/satpy

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

Open
#3,003 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug component:compositors
Dominant language
Python
Stars
1.2k
Forks
335
Avg merge
1d 18h
Merged PRs (30d)
10

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the DayNightCompositor entry point and trace how its day_only result is passed to get_enhanced_image when include_alpha is False. Run the provided reproduction to inspect the resulting array and GeoTIFF; done means night pixels retain masking or the fill value instead of being treated as physical zero values.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.