Fix two scene input.

This commit is contained in:
Felix Delattre 2026-06-11 11:35:35 +02:00
parent aa4535a600
commit e33256d43d
2 changed files with 232 additions and 0 deletions

View file

@ -597,6 +597,51 @@ def _import_distance_to_clouds():
) from exc
def _normalize_s2_grid(s2_dir: Path) -> None:
"""Remove REFL (and companion DIST_CLOUD/GCC) files that don't match the dominant shape.
Sites at MGRS tile boundaries can have S2 downloads from two tiles with different
spatial extents. For example, a site at the top edge of tile 16SDA will produce
narrow (e.g. 30×180) REFL files from 16SDA and full-extent (180×180) files from
16SDB. EFAST requires all S2 files to share the same grid, so we keep only the
shape with the most pixels (largest tile coverage) and discard the rest together
with any already-computed companions.
"""
refl_files = sorted(s2_dir.glob("*_REFL.tif"))
if not refl_files:
return
shape_to_files: dict[tuple[int, int], list[Path]] = {}
for f in refl_files:
with rasterio.open(f) as src:
shape: tuple[int, int] = src.shape # type: ignore[assignment]
shape_to_files.setdefault(shape, []).append(f)
if len(shape_to_files) <= 1:
return
ref_shape = max(shape_to_files.keys(), key=lambda s: s[0] * s[1])
shapes_summary = ", ".join(
f"{s[0]}×{s[1]}({len(fs)})" for s, fs in shape_to_files.items()
)
n_removed = 0
for shape, files in shape_to_files.items():
if shape == ref_shape:
continue
for refl_path in files:
stem = refl_path.stem[: -len("_REFL")] # e.g. "S2A_16SDA_20250106_0_L2A"
for companion in s2_dir.glob(f"{stem}_*.tif"):
companion.unlink()
refl_path.unlink(missing_ok=True)
n_removed += 1
print(
f"[S2-NORM] Tile boundary — shapes: {shapes_summary}; "
f"removed {n_removed} file-sets, kept {ref_shape[0]}×{ref_shape[1]}"
)
def _rescale_dist_cloud(s2_dir: Path) -> None:
"""Ensure DIST_CLOUD values are in pixel units (not normalised to [0,1])."""
for dc_path in s2_dir.glob("*DIST_CLOUD.tif"):
@ -796,6 +841,7 @@ def process_site(
items = stac_search_s2(bbox, datetime(year, 1, 1), datetime(year, 12, 31))
print(f"[{sitename}] {len(items)} S2 items found — downloading windows...")
download_s2_window(items, bbox, s2_out, S2_BANDS, RESOLUTION_RATIO)
_normalize_s2_grid(s2_out)
# S2 distance-to-clouds
print(f"[{sitename}] Computing distance-to-clouds...")