-
Notifications
You must be signed in to change notification settings - Fork 11
Add prototype paramdb api #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
5f38216
d64ad19
c2b7ee8
a8b8e31
dabc222
192e642
9c374d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| from .pymetkit import * | ||
| from .pymetkit import ParamDB |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,156 @@ | ||||||||||
| """ | ||||||||||
| Standalone script to generate: | ||||||||||
| - parameter_metadata.yaml — one entry per ECMWF parameter | ||||||||||
| - unit_metadata.yaml — one entry per ECMWF unit | ||||||||||
|
|
||||||||||
| Usage | ||||||||||
| ----- | ||||||||||
| python -m pymetkit.generate_parameter_metadata | ||||||||||
| # or directly: | ||||||||||
| python generate_parameter_metadata.py | ||||||||||
| """ | ||||||||||
|
|
||||||||||
| import requests | ||||||||||
| import yaml | ||||||||||
| from pathlib import Path | ||||||||||
|
|
||||||||||
| PARAM_URL = "https://codes.ecmwf.int/parameter-database/api/v1/param/" | ||||||||||
| UNIT_URL = "https://codes.ecmwf.int/parameter-database/api/v1/unit/" | ||||||||||
|
|
||||||||||
| # Output paths: canonical location is share/metkit/ at the repo root, which is | ||||||||||
| # four parent directories above this module file: | ||||||||||
| # python/pymetkit/src/pymetkit/ -> python/pymetkit/src/ -> python/pymetkit/ | ||||||||||
| # -> python/ -> <repo_root> | ||||||||||
| _REPO_ROOT = Path(__file__).parents[4] | ||||||||||
| PARAM_OUTPUT = _REPO_ROOT / "share" / "metkit" / "parameter_metadata.yaml" | ||||||||||
| UNIT_OUTPUT = _REPO_ROOT / "share" / "metkit" / "unit_metadata.yaml" | ||||||||||
|
|
||||||||||
| #: Timeout in seconds for HTTP requests to the ECMWF parameter database API. | ||||||||||
| REQUEST_TIMEOUT = 30 | ||||||||||
|
|
||||||||||
|
|
||||||||||
| # --------------------------------------------------------------------------- | ||||||||||
| # Units | ||||||||||
| # --------------------------------------------------------------------------- | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def fetch_units(url: str = UNIT_URL) -> tuple[list[dict], dict[int, str]]: | ||||||||||
| """ | ||||||||||
| Fetch all units from the ECMWF parameter database API. | ||||||||||
|
|
||||||||||
| Returns | ||||||||||
| ------- | ||||||||||
| units : list[dict] | ||||||||||
| Normalised unit records ready to be written to unit_metadata.yaml. | ||||||||||
| unit_map : dict[int, str] | ||||||||||
| Mapping of unit id -> unit name string for use in parameter enrichment. | ||||||||||
| """ | ||||||||||
| print(f"Fetching units from {url} ...") | ||||||||||
| response = requests.get(url, timeout=REQUEST_TIMEOUT) | ||||||||||
| response.raise_for_status() | ||||||||||
| raw_units = response.json() | ||||||||||
|
Comment on lines
+48
to
+51
|
||||||||||
| print(f" Received {len(raw_units)} units.") | ||||||||||
|
|
||||||||||
| units = [] | ||||||||||
| unit_map: dict[int, str] = {} | ||||||||||
|
|
||||||||||
| for raw in raw_units: | ||||||||||
| uid = int(raw["id"]) | ||||||||||
| # The API may use 'name', 'symbol', or 'label' for the unit string | ||||||||||
| name = raw.get("name") or raw.get("symbol") or raw.get("label") or "" | ||||||||||
|
|
||||||||||
| entry = {"id": uid} | ||||||||||
| # Preserve all fields the API returns, but ensure id comes first | ||||||||||
| for key, value in raw.items(): | ||||||||||
| if key == "id": | ||||||||||
| continue | ||||||||||
| entry[key] = value | ||||||||||
| # Always emit a canonical 'name' field so unit_metadata.yaml has a | ||||||||||
| # stable schema regardless of which key the API uses (name/symbol/label) | ||||||||||
| entry["name"] = name | ||||||||||
|
|
||||||||||
|
||||||||||
| # Always emit a canonical name field so unit_metadata.yaml has a stable schema | |
| entry["name"] = name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The generator writes
parameter_metadata.yamlandunit_metadata.yamlnext to the Python module (Path(__file__).parent), but the PR adds the YAML undershare/metkit/andParamDB’s fallback search also expectsshare/metkit/parameter_metadata.yaml. Regenerating with this script will therefore write to a different location than the committed data. Align the output paths with the repository’s canonical YAML location (or update the rest of the codebase to consume the module-adjacent files).