pardig_eval: PARDIG maps on their own (pardig/), one map pass per source
Browse files- pardig_eval/fields.py +13 -1
- pardig_eval/run.py +37 -22
pardig_eval/fields.py
CHANGED
|
@@ -5,7 +5,8 @@
|
|
| 5 |
* ``pardig`` (time, lat_pardig, lon_pardig): PARDIG-NRT at 0.1° (1801 x 3600), and
|
| 6 |
* ``era5`` (time, lat_era5, lon_era5): ERA5 at 0.25° (721 x 1440);
|
| 7 |
|
| 8 |
-
``
|
|
|
|
| 9 |
cell centres at .05°), its run and version in the ``imerg_run`` attribute. Both files store
|
| 10 |
hourly depths in mm over ``(time-1h, time]``, ascending latitudes and longitudes 0..360. Values are
|
| 11 |
stored as uint16 hundredths of a mm (up to 655.34 mm; 65535 = missing), compressed with zstd, in
|
|
@@ -47,6 +48,17 @@ def imerg_dataset(day, imerg: np.ndarray, name: str = "IMERG") -> xr.Dataset:
|
|
| 47 |
)
|
| 48 |
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
def dataset(day, pardig: np.ndarray, era5: np.ndarray, era5_source: str = "") -> xr.Dataset:
|
| 51 |
"""Both sources' hourly fields for a day as one dataset."""
|
| 52 |
if pardig.shape != (24, 1801, 3600) or era5.shape != (24, 721, 1440):
|
|
|
|
| 5 |
* ``pardig`` (time, lat_pardig, lon_pardig): PARDIG-NRT at 0.1° (1801 x 3600), and
|
| 6 |
* ``era5`` (time, lat_era5, lon_era5): ERA5 at 0.25° (721 x 1440);
|
| 7 |
|
| 8 |
+
``pardig/YYYY/MM/YYYY-MM-DD.zarr.zip`` holds ``pardig`` alone, written as soon as PARDIG has the day (the Space
|
| 9 |
+
reads PARDIG from here, so it does not wait for ERA5). ``imerg/YYYY/MM/YYYY-MM-DD.zarr.zip`` holds ``imerg`` (time, lat_imerg, lon_imerg): IMERG at 0.1° (1800 x 3600,
|
| 10 |
cell centres at .05°), its run and version in the ``imerg_run`` attribute. Both files store
|
| 11 |
hourly depths in mm over ``(time-1h, time]``, ascending latitudes and longitudes 0..360. Values are
|
| 12 |
stored as uint16 hundredths of a mm (up to 655.34 mm; 65535 = missing), compressed with zstd, in
|
|
|
|
| 48 |
)
|
| 49 |
|
| 50 |
|
| 51 |
+
def pardig_dataset(day, pardig: np.ndarray, name: str = "PARDIG-NRT") -> xr.Dataset:
|
| 52 |
+
"""PARDIG's hourly fields for a day on its own 0.1° grid, on their own (``pardig/`` folder)."""
|
| 53 |
+
if pardig.shape != (24, 1801, 3600):
|
| 54 |
+
raise ValueError("expected PARDIG (24, 1801, 3600) hourly fields")
|
| 55 |
+
attrs = {"units": "mm", "interval": "(time-1h, time]", "source": f"{name}, gs://pardig_1"}
|
| 56 |
+
return xr.Dataset(
|
| 57 |
+
{"pardig": (("time", "lat_pardig", "lon_pardig"), pardig, attrs)},
|
| 58 |
+
coords={"time": hours(day), "lat_pardig": regrid.PARDIG_LAT, "lon_pardig": regrid.PARDIG_LON},
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
def dataset(day, pardig: np.ndarray, era5: np.ndarray, era5_source: str = "") -> xr.Dataset:
|
| 63 |
"""Both sources' hourly fields for a day as one dataset."""
|
| 64 |
if pardig.shape != (24, 1801, 3600) or era5.shape != (24, 721, 1440):
|
pardig_eval/run.py
CHANGED
|
@@ -8,9 +8,11 @@ Two products per day, each skipped when already stored, so the command can be re
|
|
| 8 |
|
| 9 |
* ``scores``: per-tile sums added to ``months/YYYY-MM.nc`` (:mod:`pardig_eval.sums`);
|
| 10 |
* ``fields``: hourly maps in ``fields/YYYY/MM/YYYY-MM-DD.zarr.zip`` (:mod:`pardig_eval.fields`);
|
|
|
|
| 11 |
* ``imerg``: IMERG's hourly maps in ``imerg/YYYY/MM/YYYY-MM-DD.zarr.zip``, from the first ``--imerg`` run that
|
| 12 |
has the day complete (needs ``EARTHDATA_TOKEN``). IMERG runs on its own pass, from 1 January 2026 (or
|
| 13 |
``--start``), filling every missing day, so its latency never holds back the scores, nor theirs IMERG.
|
|
|
|
| 14 |
|
| 15 |
Without ``--start``, resumes after the last day stored; without ``--end``, stops at the last day
|
| 16 |
both PARDIG and ERA5 have complete.
|
|
@@ -31,7 +33,7 @@ from pardig_eval import fields, regrid, sums
|
|
| 31 |
from pardig_eval.sources import ERA5_SOURCES, IMERG, IMERG_RUNS, PARDIG, PARDIG_LIVE_START
|
| 32 |
|
| 33 |
logger = logging.getLogger("pardig_eval")
|
| 34 |
-
PRODUCTS = ("scores", "fields", "imerg")
|
| 35 |
|
| 36 |
|
| 37 |
def month_name(day) -> str:
|
|
@@ -113,15 +115,17 @@ class Store:
|
|
| 113 |
operations = [CommitOperationAdd(rel, self.root / rel) for rel in pending]
|
| 114 |
days = sorted(Path(rel).name[:10] for rel in pending if rel.startswith("fields/"))
|
| 115 |
imerg = sorted(Path(rel).name[:10] for rel in pending if rel.startswith("imerg/"))
|
|
|
|
| 116 |
months = [Path(rel).stem for rel in pending if rel.startswith("months/")]
|
| 117 |
what = ", ".join(filter(None, [f"scores {', '.join(months)}" if months else "",
|
| 118 |
f"maps {days[0]}..{days[-1]}" if days else "",
|
|
|
|
| 119 |
f"IMERG {imerg[0]}..{imerg[-1]}" if imerg else ""]))
|
| 120 |
self.api.create_commit(self.repo, operations, repo_type="dataset", commit_message=f"Add {what}")
|
| 121 |
logger.info("pushed %s", what)
|
| 122 |
self.remote.update(pending)
|
| 123 |
for rel in pending:
|
| 124 |
-
if rel.startswith(("fields/", "imerg/")):
|
| 125 |
(self.root / rel).unlink(missing_ok=True)
|
| 126 |
self.pending = set()
|
| 127 |
|
|
@@ -158,31 +162,40 @@ def process_day(day, era5, pardig, store: Store, month: xr.Dataset | None, what:
|
|
| 158 |
return month
|
| 159 |
|
| 160 |
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
Incomplete days are skipped, not waited for: a recent day is filled by a later run, and an old day with a
|
| 165 |
-
|
|
|
|
| 166 |
processed = failures = 0
|
| 167 |
for day in pd.date_range(start, end, freq="D"):
|
| 168 |
-
if store.has_fields(day,
|
| 169 |
continue
|
| 170 |
-
|
| 171 |
-
if
|
| 172 |
-
logger.info("%s:
|
| 173 |
continue
|
| 174 |
t0 = time.perf_counter()
|
| 175 |
try:
|
| 176 |
-
fine =
|
| 177 |
-
except Exception as exc: # e.g.
|
| 178 |
-
logger.warning("%s:
|
| 179 |
failures += 1
|
| 180 |
if failures >= 5:
|
| 181 |
-
logger.error("
|
| 182 |
break
|
| 183 |
continue
|
| 184 |
-
store.save_fields(
|
| 185 |
-
logger.info("%s (%s):
|
| 186 |
processed += 1
|
| 187 |
if processed % upload_every == 0:
|
| 188 |
store.push()
|
|
@@ -200,7 +213,7 @@ def main(argv=None):
|
|
| 200 |
" e.g. 'earthmover,arco' falls back to ARCO for days Earthmover does not have yet")
|
| 201 |
parser.add_argument("--imerg", default="final,late",
|
| 202 |
help=f"IMERG runs to try in order for each day, from {list(IMERG_RUNS)}")
|
| 203 |
-
parser.add_argument("--what", default="scores,fields,imerg", help=f"products to write, from {list(PRODUCTS)}")
|
| 204 |
parser.add_argument("--out", default="data", help="local working folder")
|
| 205 |
parser.add_argument("--repo", help="Hub dataset to read and push files, e.g. user/era5-pardig-scores")
|
| 206 |
parser.add_argument("--max-days", type=int, default=None, help="stop after this many processed days")
|
|
@@ -232,7 +245,7 @@ def main(argv=None):
|
|
| 232 |
|
| 233 |
processed = 0
|
| 234 |
month, month_key = None, None
|
| 235 |
-
main_what = what -
|
| 236 |
try:
|
| 237 |
for day in pd.date_range(start, end, freq="D") if main_what else []:
|
| 238 |
if "scores" in what and month_name(day) != month_key:
|
|
@@ -259,11 +272,13 @@ def main(argv=None):
|
|
| 259 |
store.push()
|
| 260 |
if args.max_days and processed >= args.max_days:
|
| 261 |
break
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
|
|
|
|
|
|
| 267 |
finally:
|
| 268 |
store.push()
|
| 269 |
logger.info("processed %d day(s)", processed)
|
|
|
|
| 8 |
|
| 9 |
* ``scores``: per-tile sums added to ``months/YYYY-MM.nc`` (:mod:`pardig_eval.sums`);
|
| 10 |
* ``fields``: hourly maps in ``fields/YYYY/MM/YYYY-MM-DD.zarr.zip`` (:mod:`pardig_eval.fields`);
|
| 11 |
+
* ``pardig``: PARDIG's hourly maps alone in ``pardig/YYYY/MM/YYYY-MM-DD.zarr.zip``, as soon as PARDIG has the day;
|
| 12 |
* ``imerg``: IMERG's hourly maps in ``imerg/YYYY/MM/YYYY-MM-DD.zarr.zip``, from the first ``--imerg`` run that
|
| 13 |
has the day complete (needs ``EARTHDATA_TOKEN``). IMERG runs on its own pass, from 1 January 2026 (or
|
| 14 |
``--start``), filling every missing day, so its latency never holds back the scores, nor theirs IMERG.
|
| 15 |
+
``pardig`` runs the same way, so PARDIG's maps never wait for ERA5.
|
| 16 |
|
| 17 |
Without ``--start``, resumes after the last day stored; without ``--end``, stops at the last day
|
| 18 |
both PARDIG and ERA5 have complete.
|
|
|
|
| 33 |
from pardig_eval.sources import ERA5_SOURCES, IMERG, IMERG_RUNS, PARDIG, PARDIG_LIVE_START
|
| 34 |
|
| 35 |
logger = logging.getLogger("pardig_eval")
|
| 36 |
+
PRODUCTS = ("scores", "fields", "pardig", "imerg")
|
| 37 |
|
| 38 |
|
| 39 |
def month_name(day) -> str:
|
|
|
|
| 115 |
operations = [CommitOperationAdd(rel, self.root / rel) for rel in pending]
|
| 116 |
days = sorted(Path(rel).name[:10] for rel in pending if rel.startswith("fields/"))
|
| 117 |
imerg = sorted(Path(rel).name[:10] for rel in pending if rel.startswith("imerg/"))
|
| 118 |
+
pardig_only = sorted(Path(rel).name[:10] for rel in pending if rel.startswith("pardig/"))
|
| 119 |
months = [Path(rel).stem for rel in pending if rel.startswith("months/")]
|
| 120 |
what = ", ".join(filter(None, [f"scores {', '.join(months)}" if months else "",
|
| 121 |
f"maps {days[0]}..{days[-1]}" if days else "",
|
| 122 |
+
f"PARDIG {pardig_only[0]}..{pardig_only[-1]}" if pardig_only else "",
|
| 123 |
f"IMERG {imerg[0]}..{imerg[-1]}" if imerg else ""]))
|
| 124 |
self.api.create_commit(self.repo, operations, repo_type="dataset", commit_message=f"Add {what}")
|
| 125 |
logger.info("pushed %s", what)
|
| 126 |
self.remote.update(pending)
|
| 127 |
for rel in pending:
|
| 128 |
+
if rel.startswith(("fields/", "pardig/", "imerg/")):
|
| 129 |
(self.root / rel).unlink(missing_ok=True)
|
| 130 |
self.pending = set()
|
| 131 |
|
|
|
|
| 162 |
return month
|
| 163 |
|
| 164 |
|
| 165 |
+
#: Map-only products, one folder each, written on their own pass: label and dataset builder.
|
| 166 |
+
MAP_PASSES = {
|
| 167 |
+
"pardig": ("PARDIG", lambda day, fine, reader: fields.pardig_dataset(day, fine, reader.name)),
|
| 168 |
+
"imerg": ("IMERG", lambda day, fine, reader: fields.imerg_dataset(day, fine, reader.name)),
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
def map_pass(store: Store, folder: str, readers: list, start, end, upload_every: int, max_days: int | None) -> int:
|
| 173 |
+
"""Write one source's maps (``folder/YYYY/MM/DD.zarr.zip``) for every day from start to end that has none yet,
|
| 174 |
+
from the first reader with the day complete; returns the number written.
|
| 175 |
|
| 176 |
Incomplete days are skipped, not waited for: a recent day is filled by a later run, and an old day with a
|
| 177 |
+
file missing for good does not block the days after it."""
|
| 178 |
+
label, build = MAP_PASSES[folder]
|
| 179 |
processed = failures = 0
|
| 180 |
for day in pd.date_range(start, end, freq="D"):
|
| 181 |
+
if store.has_fields(day, folder):
|
| 182 |
continue
|
| 183 |
+
reader = next((r for r in readers if _available(r, day)), None)
|
| 184 |
+
if reader is None:
|
| 185 |
+
logger.info("%s: %s incomplete, skipping", day.date(), label)
|
| 186 |
continue
|
| 187 |
t0 = time.perf_counter()
|
| 188 |
try:
|
| 189 |
+
fine = reader.hourly_fine(day)
|
| 190 |
+
except Exception as exc: # e.g. the source's server down for a while: the next run fills the day
|
| 191 |
+
logger.warning("%s: %s read failed, skipping: %s", day.date(), label, exc)
|
| 192 |
failures += 1
|
| 193 |
if failures >= 5:
|
| 194 |
+
logger.error("%s: %d failed days, giving up for this run", label, failures)
|
| 195 |
break
|
| 196 |
continue
|
| 197 |
+
store.save_fields(build(day, fine, reader), day, folder)
|
| 198 |
+
logger.info("%s (%s): %s %.0fs", day.date(), reader.name, label, time.perf_counter() - t0)
|
| 199 |
processed += 1
|
| 200 |
if processed % upload_every == 0:
|
| 201 |
store.push()
|
|
|
|
| 213 |
" e.g. 'earthmover,arco' falls back to ARCO for days Earthmover does not have yet")
|
| 214 |
parser.add_argument("--imerg", default="final,late",
|
| 215 |
help=f"IMERG runs to try in order for each day, from {list(IMERG_RUNS)}")
|
| 216 |
+
parser.add_argument("--what", default="scores,fields,pardig,imerg", help=f"products to write, from {list(PRODUCTS)}")
|
| 217 |
parser.add_argument("--out", default="data", help="local working folder")
|
| 218 |
parser.add_argument("--repo", help="Hub dataset to read and push files, e.g. user/era5-pardig-scores")
|
| 219 |
parser.add_argument("--max-days", type=int, default=None, help="stop after this many processed days")
|
|
|
|
| 245 |
|
| 246 |
processed = 0
|
| 247 |
month, month_key = None, None
|
| 248 |
+
main_what = what - set(MAP_PASSES)
|
| 249 |
try:
|
| 250 |
for day in pd.date_range(start, end, freq="D") if main_what else []:
|
| 251 |
if "scores" in what and month_name(day) != month_key:
|
|
|
|
| 272 |
store.push()
|
| 273 |
if args.max_days and processed >= args.max_days:
|
| 274 |
break
|
| 275 |
+
readers = {"pardig": lambda: [pardig], "imerg": lambda: [IMERG(r) for r in imerg_names]}
|
| 276 |
+
for folder in MAP_PASSES:
|
| 277 |
+
if folder in what:
|
| 278 |
+
store.push()
|
| 279 |
+
first = pd.Timestamp(args.start) if args.start else PARDIG_LIVE_START
|
| 280 |
+
n = map_pass(store, folder, readers[folder](), first, end, args.upload_every, args.max_days)
|
| 281 |
+
logger.info("%s: %d day(s)", MAP_PASSES[folder][0], n)
|
| 282 |
finally:
|
| 283 |
store.push()
|
| 284 |
logger.info("processed %d day(s)", processed)
|