Skip to content

Filling the store

glofas_ewds.local

Fill the store: find the river of a place, download its thresholds, history and forecast.

This is where the I/O of the store is. Reading the threshold files and the EWDS files needs the netcdf extra; the EWDS downloads need the ewds extra and an account; Open-Meteo needs neither.

  • :func:data_folder: where the library keeps its data.
  • :func:download_thresholds: the three official threshold files (about 520 MB, once), from ECMWF, with no account.
  • :func:locate: a place, its river cell and the thresholds of that cell.
  • :func:fill_from_open_meteo: history and forecast of every place, with no key.
  • :func:fill_from_ewds_files: the same from EWDS files you downloaded (:class:~glofas_ewds.client.EwdsClient): the source of record.

Two sources, one store. Open-Meteo is a third party that serves the same GloFAS 4 numbers (checked against the EWDS every week, live.yml), under its own terms and attribution; its reanalysis starts on 1 January 1997, and after 2024 its history is its archive of forecasts. The EWDS is Copernicus itself, from 1979, and is what to use for anything you publish.

data_folder

data_folder(folder: Path | str | None = None) -> Path

Where the data of this library are: folder, else $DATA_ROOT/glofas-ewds, else ~/.cache/glofas-ewds.

threshold_files

threshold_files(folder: Path, periods: Sequence[int] = RETURN_PERIODS) -> dict[int, Path]

Where the threshold file of each return period is kept under folder.

download_thresholds

download_thresholds(folder: Path, periods: Sequence[int] = RETURN_PERIODS, *, opener: Callable[[str], Any] | None = None) -> dict[int, Path]

The official threshold files under folder; one already there is not asked again.

About 173 MB each. opener opens a URL as a readable stream (default: urllib); a download that fails leaves no file behind.

locate

locate(name: str, longitude: float, latitude: float, files: Mapping[int, Path], *, radius_km: float = RIVER_RADIUS_KM, place_id: str | None = None) -> Place

A place with its river cell and the official thresholds of that cell.

The river is looked for on the file of the shortest return period (the 2-year flow). A place with no river within radius_km is returned without one: GloFAS models nothing there.

add_history

add_history(places: Sequence[Place], by_node: Mapping[Node, Mapping[date, float]], *, source: str, now: datetime | None = None) -> int

Merge daily values into the history of the places that read each node.

Returns how many places got at least one day.

add_runs

add_runs(places: Sequence[Place], by_node: Mapping[Node, tuple[date, Sequence[Sequence[float]]]], *, source: str, now: datetime | None = None) -> int

Add a forecast run, (run, members), to the places that read each node.

NaN in members is stored as null. Returns how many places got one.

fill_from_open_meteo

fill_from_open_meteo(places: Sequence[Place], *, today: date | None = None, years: float = HISTORY_YEARS, history: bool = True, forecast_days: int = FORECAST_DAYS, fetch: Fetch = openmeteo.fetch, now: datetime | None = None) -> dict[str, int]

History and forecast of every place with a river, from Open-Meteo. No key.

The history goes from years ago (never before 1 January 1997) to yesterday; a place that already has days is asked only for what is after its last one. Returns {"history": places, "forecast": places} filled.

fill_from_ewds_files

fill_from_ewds_files(places: Sequence[Place], *, history: Iterable[Path] = (), start: date | None = None, end: date | None = None, run: date | None = None, forecast: Sequence[Path] = (), now: datetime | None = None) -> dict[str, int]

History and forecast of every place with a river, from EWDS files.

history are the files of :func:~glofas_ewds.download.download_history for start to end. forecast are the files of one run: the control run first, then (optionally) its ensemble. Needs the netcdf extra.

save_all

save_all(store: Store, places: Sequence[Place]) -> list[Path]

Write every place to the store.