Skip to main content

pymetkit

Static Badge

[!IMPORTANT] This software is Emerging and subject to ECMWF's guidelines on Software Maturity.

pymetkit is a Python interface to metkit, ECMWF's meteorological toolkit. It exposes the MARS request model in a Pythonic way.

The native libmetkit shared library and its dependencies are located at runtime via findlibs.

Usage

from pymetkit import MarsRequest, parse_mars_request

# Build a request from a verb and a selection
request = MarsRequest(
    "retrieve",
    {
        "class": "od",
        "domain": "g",
        "date": "-1",
        "expver": "0001",
        "step": range(0, 13, 6),
    },
)

# Expand against the MARS language definition
expanded = request.expand()
print(expanded.verb(), dict(expanded))

# Parse requests from a string or a file
requests = parse_mars_request("retrieve,class=od,date=-1,param=129,step=12")

ParamDB — parameter database

ParamDB maps between ECMWF short names, long names and numeric parameter IDs, backed by a bundled parameter_metadata.json or, in mode="online", the ECMWF parameter API.

from pymetkit import ParamDB, AmbiguousParamError

db = ParamDB()                       # mode="offline" by default; data loads lazily

db.shortname_to_param_id("msl")      # 151  — unambiguous
db.param_id_to_shortname(151)        # "msl"
db.shortname_to_longname("2t")       # "2 metre temperature"
db.get_units(167)                    # "K"

Ambiguous short names

Some short names map to more than one parameter ID (e.g. tp228 and 228228). ParamDB never guesses — an ambiguous lookup raises by default:

try:
    db.shortname_to_param_id("tp")
except AmbiguousParamError as exc:
    print(exc.shortname)             # "tp"
    for cand in exc.candidates:      # every ParamIDCandidate, sorted
        print(cand.param_id, cand.table)

You can resolve the ambiguity in three ways:

# 1. Narrow with a MARS context (resolved via the C++ expand engine)
db.shortname_to_param_id("tp", context={"class": "od"})   # 228

# 2. Narrow with hard metadata filters (no MARS request constructed)
db.shortname_to_param_id("tp", table=128)                 # 228

# 3. Accept the canonical (first-sorted, lowest-table/id) candidate
db.shortname_to_param_id("tp", default=True)              # 228

To inspect the options programmatically instead of catching the error, use shortname_to_param_id_candidates, which returns a list of ParamIDCandidate (param_id, table, origin, access, mars_request_context):

for cand in db.shortname_to_param_id_candidates("tp"):
    if cand.hard_filter_selector is not None:
        # A hard-filter selector proven to select exactly this candidate.
        print(cand.param_id, cand.hard_filter_selector)
    else:
        # No hard filter uniquely identifies this candidate (e.g. two ids
        # share the same table, origin and access); use context= instead.
        print(cand.param_id, "no unique hard-filter selector")

Note: hard_filter_selector is either a dict of table/origin/access kwargs proven to select exactly one candidate, or None when no combination of the available hard filters disambiguates it. The API never advertises a selector that would remain ambiguous.

Note: Per-candidate MARS context computation is temporarily deferred, so every returned or raised ParamIDCandidate currently carries mars_request_context=None. Passing context= to narrow a lookup still works; only the advertised selecting context is unavailable for now. The context= path is resolved by the compiled MetKit expand engine, which is always available (pymetkit imports the native pymetkit._internal extension unconditionally).

Command line

python -m pymetkit --print-home        # metkit library home
python -m pymetkit --print-home-deps   # all dependency homes and versions

Technical details

Regenerating bundled parameter metadata

The bundled share/metkit/parameter_metadata.json (and .yaml) are generated by fetching from the ECMWF parameter database API. Run the generator script when the upstream database changes:

python -m pymetkit.paramdb.generate_metadata

This requires network access and the requests and pyyaml packages. It writes the following files relative to the repository root:

File Description
share/metkit/parameter_metadata.json Compact JSON — preferred at runtime (~10-50× faster to load than YAML)
share/metkit/parameter_metadata.yaml Human-readable YAML — fallback if JSON is absent
share/metkit/unit_metadata.yaml Unit definitions
share/metkit/parameter_entry_schema.json JSON Schema for ParameterEntry validation
share/metkit/mars_context_schema.json JSON Schema for MarsRequestContext validation

Commit the updated files to keep the bundled metadata in sync with the upstream database.

Documentation

For implementation details and tooling, see the Metkit project pages.

To build the latest documentation locally, follow the guide at Metkit.

License

License

Release files for pymetkit 1.21.0.30

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for pymetkit 1.21.0.30
File Interpreter ABI Platform
pymetkit-1.21.0.30-cp313-cp313-macosx_15_0_arm64.whl CPython 3.13 CPython 3.13 macOS 15.0+ ARM64 Details
pymetkit-1.21.0.30-cp311-cp311-macosx_15_0_arm64.whl CPython 3.11 CPython 3.11 macOS 15.0+ ARM64 Details

Total release size: 1.2 MB

Release files / pymetkit-1.21.0.30-cp313-cp313-macosx_15_0_arm64.whl

Download URL pymetkit-1.21.0.30-cp313-cp313-macosx_15_0_arm64.whl
Size 620.1 kB
Tags CPython 3.13 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
38a383625bb58918713e7af717f3b50ee912440ebce385303dd7408f0aa5fabe
BLAKE2b-256 checksum
How to use checksums
16b045d1733629f30ac6923345c0c48cda6c9f7934c6a6a6ebb8d1dd29ba3fab
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.15

Release files / pymetkit-1.21.0.30-cp311-cp311-macosx_15_0_arm64.whl

Download URL pymetkit-1.21.0.30-cp311-cp311-macosx_15_0_arm64.whl
Size 611.0 kB
Tags CPython 3.11 macOS 15.0+ ARM64
SHA-256 checksum
How to use checksums
0884dba9ec2ad0906304466e7d0822da0709947013934b08660ad418d580639d
BLAKE2b-256 checksum
How to use checksums
dfecaa70d8a86575477bba09661d8f54e218134b454f50256eab23d75fd8ed16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.16

Release history Release notifications | RSS feed

This release

1.21.0.30 This release

2 release files

1.14.3

2 release files

1.14.2

2 release files

1.14.1

2 release files

1.14.0

2 release files

1.13.3

2 release files

1.13.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page