IMERG: retry NASA 5xx/network errors, skip a failed day instead of stopping
Browse files- pardig_eval/run.py +10 -2
- pardig_eval/sources.py +23 -1
pardig_eval/run.py
CHANGED
|
@@ -163,7 +163,7 @@ def imerg_pass(store: Store, runs: list[IMERG], start, end, upload_every: int, m
|
|
| 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 |
half-hour missing for good does not block the days after it."""
|
| 166 |
-
processed = 0
|
| 167 |
for day in pd.date_range(start, end, freq="D"):
|
| 168 |
if store.has_fields(day, "imerg"):
|
| 169 |
continue
|
|
@@ -172,7 +172,15 @@ def imerg_pass(store: Store, runs: list[IMERG], start, end, upload_every: int, m
|
|
| 172 |
logger.info("%s: IMERG incomplete, skipping", day.date())
|
| 173 |
continue
|
| 174 |
t0 = time.perf_counter()
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
store.save_fields(fields.imerg_dataset(day, fine, run.name), day, "imerg")
|
| 177 |
logger.info("%s (%s): IMERG %.0fs", day.date(), run.name, time.perf_counter() - t0)
|
| 178 |
processed += 1
|
|
|
|
| 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 |
half-hour missing for good does not block the days after it."""
|
| 166 |
+
processed = failures = 0
|
| 167 |
for day in pd.date_range(start, end, freq="D"):
|
| 168 |
if store.has_fields(day, "imerg"):
|
| 169 |
continue
|
|
|
|
| 172 |
logger.info("%s: IMERG incomplete, skipping", day.date())
|
| 173 |
continue
|
| 174 |
t0 = time.perf_counter()
|
| 175 |
+
try:
|
| 176 |
+
fine = run.hourly_fine(day)
|
| 177 |
+
except Exception as exc: # e.g. NASA down for a while: the next run fills the day
|
| 178 |
+
logger.warning("%s: IMERG download failed, skipping: %s", day.date(), exc)
|
| 179 |
+
failures += 1
|
| 180 |
+
if failures >= 5:
|
| 181 |
+
logger.error("IMERG: %d failed days, giving up for this run", failures)
|
| 182 |
+
break
|
| 183 |
+
continue
|
| 184 |
store.save_fields(fields.imerg_dataset(day, fine, run.name), day, "imerg")
|
| 185 |
logger.info("%s (%s): IMERG %.0fs", day.date(), run.name, time.perf_counter() - t0)
|
| 186 |
processed += 1
|
pardig_eval/sources.py
CHANGED
|
@@ -299,7 +299,7 @@ class IMERG:
|
|
| 299 |
raise RuntimeError("set EARTHDATA_TOKEN (urs.earthdata.nasa.gov -> Generate Token) to download IMERG")
|
| 300 |
headers = {"Authorization": f"Bearer {token}"}
|
| 301 |
for _ in range(6):
|
| 302 |
-
r =
|
| 303 |
if r.is_redirect:
|
| 304 |
url = str(r.next_request.url)
|
| 305 |
host = r.next_request.url.host
|
|
@@ -314,6 +314,28 @@ class IMERG:
|
|
| 314 |
return r.content
|
| 315 |
raise RuntimeError(f"too many redirects for {url}")
|
| 316 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 317 |
@staticmethod
|
| 318 |
def read_rate(data: bytes) -> np.ndarray:
|
| 319 |
"""Half-hourly rate (1800, 3600) in mm/h on ascending latitudes, longitudes 0..360, NaN where missing."""
|
|
|
|
| 299 |
raise RuntimeError("set EARTHDATA_TOKEN (urs.earthdata.nasa.gov -> Generate Token) to download IMERG")
|
| 300 |
headers = {"Authorization": f"Bearer {token}"}
|
| 301 |
for _ in range(6):
|
| 302 |
+
r = self._get_retrying(client, url, headers)
|
| 303 |
if r.is_redirect:
|
| 304 |
url = str(r.next_request.url)
|
| 305 |
host = r.next_request.url.host
|
|
|
|
| 314 |
return r.content
|
| 315 |
raise RuntimeError(f"too many redirects for {url}")
|
| 316 |
|
| 317 |
+
@staticmethod
|
| 318 |
+
def _get_retrying(client, url: str, headers: dict, attempts: int = 6):
|
| 319 |
+
"""GET, retrying network errors and 429/5xx answers (GES DISC returns the odd 502) with backoff."""
|
| 320 |
+
import time
|
| 321 |
+
|
| 322 |
+
import httpx
|
| 323 |
+
|
| 324 |
+
for attempt in range(attempts):
|
| 325 |
+
try:
|
| 326 |
+
r = client.get(url, headers=headers)
|
| 327 |
+
if r.status_code != 429 and r.status_code < 500:
|
| 328 |
+
return r
|
| 329 |
+
problem = f"HTTP {r.status_code}"
|
| 330 |
+
except httpx.TransportError as exc:
|
| 331 |
+
problem = repr(exc)
|
| 332 |
+
if attempt == attempts - 1:
|
| 333 |
+
break
|
| 334 |
+
wait = min(5 * 2 ** attempt, 120)
|
| 335 |
+
logger.info("%s on %s, retrying in %ds", problem, url.rsplit("/", 1)[-1], wait)
|
| 336 |
+
time.sleep(wait)
|
| 337 |
+
raise RuntimeError(f"giving up on {url} after {attempts} attempts: {problem}")
|
| 338 |
+
|
| 339 |
@staticmethod
|
| 340 |
def read_rate(data: bytes) -> np.ndarray:
|
| 341 |
"""Half-hourly rate (1800, 3600) in mm/h on ascending latitudes, longitudes 0..360, NaN where missing."""
|