fatmur Claude Opus 5.5 commited on
Commit
b2b341a
·
1 Parent(s): 79388d7

ingest: drop superseded processings; name packing by its source, the baseline

Browse files
Files changed (1) hide show
  1. scripts/ingest_dynamic_world.py +31 -7
scripts/ingest_dynamic_world.py CHANGED
@@ -27,7 +27,7 @@ from geosave.geodata.stac import StacClient, StacQuery
27
 
28
  from modules.segmentation.supervised.datasets import DATA, SOURCES, find_labels
29
 
30
- # Planetary Computer's L2A; its STAC states no scale or offset (see read_packing).
31
  COLLECTION = "sentinel-2-l2a"
32
 
33
  # The 10 m and 20 m bands the Dynamic World model reads (Brown et al., 2022).
@@ -63,11 +63,35 @@ def search_items(
63
  return client.search_items(query)
64
 
65
 
66
- def read_packing(items: list[pystac.Item]) -> attrs.CFPacking:
67
- """Read how the items' digital numbers decode to reflectance.
68
 
69
- Processing baseline 04.00 added a -1000 DN offset. Tiles of different
70
- baselines decode differently, so one image must not mosaic them.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  """
72
  baselines = set()
73
  for item in items:
@@ -85,8 +109,8 @@ def load_image(client: StacClient, label_path: Path, date: pd.Timestamp) -> xr.D
85
  Neighbouring tiles of one pass are mosaicked into one image.
86
  """
87
  label = io.open_geotiff(label_path, chunks=None)
88
- items = search_items(client, label, date)
89
- packing = read_packing(items)
90
 
91
  image = stac.load_dataset(
92
  items,
 
27
 
28
  from modules.segmentation.supervised.datasets import DATA, SOURCES, find_labels
29
 
30
+ # Planetary Computer's L2A; its STAC states no scale or offset (see read_baseline_packing).
31
  COLLECTION = "sentinel-2-l2a"
32
 
33
  # The 10 m and 20 m bands the Dynamic World model reads (Brown et al., 2022).
 
63
  return client.search_items(query)
64
 
65
 
66
+ def drop_superseded(items: list[pystac.Item]) -> list[pystac.Item]:
67
+ """Keep only the newest processing of each tile acquisition.
68
 
69
+ Planetary Computer lists an acquisition once per ESA processing, e.g. the
70
+ 2020 original (baseline 02.12) and the 2023 reprocessing (05.00). Their
71
+ ids differ only in the last field, the processing time. ODC would fuse
72
+ both as one observation, so the first listed would silently win.
73
+ """
74
+ newest = {}
75
+ for item in items:
76
+ acquisition, processed = item.id.rsplit("_", 1)
77
+ if acquisition not in newest or processed > newest[acquisition][0]:
78
+ newest[acquisition] = (processed, item)
79
+
80
+ kept = []
81
+ for _, item in newest.values():
82
+ kept.append(item)
83
+ return kept
84
+
85
+
86
+ def read_baseline_packing(items: list[pystac.Item]) -> attrs.CFPacking:
87
+ """Derive how the items' digital numbers decode to reflectance from their baseline.
88
+
89
+ Planetary Computer's STAC states no scale or offset, only the ESA
90
+ processing baseline (`s2:processing_baseline`, the `N0500` of a product
91
+ name). ESA: reflectance = (DN + BOA_ADD_OFFSET) / 10000, where
92
+ BOA_ADD_OFFSET is -1000 from baseline 04.00 and 0 before
93
+ (https://forum.step.esa.int/t/35431). Tiles of different baselines
94
+ decode differently, so one image must not mosaic them.
95
  """
96
  baselines = set()
97
  for item in items:
 
109
  Neighbouring tiles of one pass are mosaicked into one image.
110
  """
111
  label = io.open_geotiff(label_path, chunks=None)
112
+ items = drop_superseded(search_items(client, label, date))
113
+ packing = read_baseline_packing(items)
114
 
115
  image = stac.load_dataset(
116
  items,