-
Notifications
You must be signed in to change notification settings - Fork 14
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 6 commits
5f38216
d64ad19
c2b7ee8
a8b8e31
dabc222
192e642
9c374d6
63f51f7
0276e49
5bfdf4e
8998c5f
a5370ef
abbc236
53d5ccc
81f5c18
9885271
e622f7f
917d33b
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,144 @@ | ||||||||||
| """ | ||||||||||
| 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/" | ||||||||||
| PARAM_OUTPUT = Path(__file__).parent / "parameter_metadata.yaml" | ||||||||||
| UNIT_OUTPUT = Path(__file__).parent / "unit_metadata.yaml" | ||||||||||
|
|
||||||||||
|
Comment on lines
+18
to
+34
|
||||||||||
|
|
||||||||||
| # --------------------------------------------------------------------------- | ||||||||||
| # 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) | ||||||||||
| response.raise_for_status() | ||||||||||
| raw_units = response.json() | ||||||||||
|
Comment on lines
+52
to
+55
|
||||||||||
| 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 | |
| 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.
parameter_metadata.yamlis declared as package data for thepymetkitpackage, but the repository’s YAML lives undershare/metkit/parameter_metadata.yaml(there is nopython/pymetkit/src/pymetkit/parameter_metadata.yaml). As a result, installed wheels/sdists will likely not contain the bundled YAML and offlineParamDB()will fail. Either move/copy the YAML into the package directory at build time, or adjust packaging and the loader to useimportlib.resources/pkgutilto read the bundled file reliably.