ECMWF#
ECMWF Climate Data Store data source via cdsapi.
earthlens.ecmwf.ECMWF
#
Bases: AbstractDataSource
ECMWF / Copernicus Climate Data Store backend.
Downloads ERA5 reanalysis (and ERA5-Land where the catalog
indicates) via :class:cdsapi.Client. The user-friendly variable
short codes (e.g. "2m-temperature", "total-precipitation") are resolved through
:class:Catalog, which loads the per-variable metadata from
cds_data_catalog.yaml.
The download pipeline (per variable) is a single step:
- :meth:
_api— build the cdsapi request dict (daily / monthly branch ontemporal_resolution) and submit it viaclient.retrieve(dataset, request, target). Returns the absolute path to the NetCDF that CDS wrote.
Per-date GeoTIFF post-processing (time-window mean, flux
scaling, raster output) is intentionally not part of the
package — see examples/post_process_ecmwf_netcdf.py for a
runnable script that consumes the NetCDF this method writes.
The valid temporal_resolution values are "daily" and
"monthly". _check_input_dates raises ValueError for
anything else; that is the authoritative gate. Spatial cell
size lives on :attr:SpatialExtent.resolution (populated by
:meth:_create_grid) and is sourced from
:data:ERA5_GRID_DEGREES.
Source code in src/earthlens/ecmwf/backend.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 | |
__init__(start, end, variables, lat_lim, lon_lim, temporal_resolution='daily', path='', fmt='%Y-%m-%d', skip_constraints=False)
#
Initialize an ECMWF backend instance.
Forwards every argument to :class:AbstractDataSource,
which captures the cdsapi client into self.client and
the bbox/date dict into self.space/self.time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
str
|
Inclusive start date as a string (parsed with
|
required |
end
|
str
|
Inclusive end date as a string. Required. |
required |
variables
|
dict[str, list[str]]
|
Mapping from CDS dataset short name to a list
of variable codes drawn from that dataset, e.g.
|
required |
lat_lim
|
list[float]
|
|
required |
lon_lim
|
list[float]
|
|
required |
temporal_resolution
|
str
|
Either |
'daily'
|
path
|
Path | str
|
Output directory. Created by the parent if it does
not exist. Defaults to |
''
|
fmt
|
str
|
|
'%Y-%m-%d'
|
skip_constraints
|
bool
|
When |
False
|
Source code in src/earthlens/ecmwf/backend.py
download(progress_bar=True, aggregate=None)
#
Download every (dataset, variable) pair in self.vars from CDS.
Iterates the user-supplied variables mapping (CDS dataset
short name → list of variable codes) and, for each pair,
looks the variable up in the CDS :class:Catalog and
delegates to :meth:_download_dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
progress_bar
|
bool
|
Reserved; currently unused since the
slicing pipeline that previously consumed it has
been moved out of the package. Defaults to |
True
|
aggregate
|
AggregationConfig | None
|
Optional :class:
Worked example — daily Pass an explicit |
None
|
Returns:
None. Per-variable NetCDFs land at
<self.root_dir>/<cds_variable>_<cds_dataset>.nc. When
aggregate is set, per-window GeoTIFFs land at
<aggregate.out_dir or self.root_dir/aggregated>/<cds_variable>_<freq>_<window>.tif.
Raises:
| Type | Description |
|---|---|
KeyError
|
If any dataset key in |
Exception
|
Any error :meth: |
Examples:
-
End-to-end download via the user-facing :class:
EarthLensfacade. Marked# doctest: +SKIPbecause it requires a configured~/.cdsapircand several minutes of CDS queue time:>>> from earthlens.earthlens import EarthLens >>> earthlens = EarthLens( # doctest: +SKIP ... data_source="ecmwf", ... temporal_resolution="daily", ... start="2022-01-01", ... end="2022-01-01", ... variables={ ... "reanalysis-era5-single-levels": [ ... "2m-temperature", "total-precipitation" ... ], ... }, ... lat_lim=[4.0, 5.0], ... lon_lim=[-75.0, -74.0], ... path="examples/data/era5", ... ) >>> earthlens.download() # doctest: +SKIP
See Also
:meth:_download_dataset: Per-variable download +
post-processing.
:meth:_api: Builds and submits the cdsapi request.
:class:Catalog: Resolves (dataset, code) pairs to
per-variable metadata.
Source code in src/earthlens/ecmwf/backend.py
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 | |
earthlens.ecmwf.Catalog
#
Bases: AbstractCatalog
Variable catalog for the CDS-backed ECMWF data source.
Reads cds_data_catalog.yaml (shipped as package data) and
exposes its consumed top-level sections as typed pydantic fields.
Instantiate with no arguments (Catalog()) — :func:model_post_init
parses the YAML and populates every field in one pass.
Variables are addressed by the (dataset_name, variable_name)
pair via :meth:get_variable; there is no flat per-code lookup.
The same short code can legitimately appear under more than one
dataset (e.g. "2m-temperature" lives in both
reanalysis-era5-single-levels and reanalysis-era5-land), so
the dataset name is part of the identity.
Attributes:
| Name | Type | Description |
|---|---|---|
available_datasets |
list[str]
|
Informational list of every CDS dataset
short name. Mirrors the |
datasets |
dict[str, Dataset]
|
Structural map keyed by CDS dataset short name. Each
value is a :class: |
Examples:
-
Look up a variable by
(dataset_name, variable_name):- The same short code under a different dataset is a different :class:>>> from earthlens.ecmwf import Catalog >>> spec = Catalog().get_variable( ... "reanalysis-era5-single-levels", "2m-temperature" ... ) >>> spec.cds_dataset 'reanalysis-era5-single-levels' >>> spec.nc_variable 't2m'Variable:- Iterate variables grouped by dataset (structural):>>> from earthlens.ecmwf import Catalog >>> Catalog().get_variable( ... "reanalysis-era5-land", "2m-temperature" ... ).cds_dataset 'reanalysis-era5-land'- Inspect what CDS hosts overall:>>> from earthlens.ecmwf import Catalog >>> cat = Catalog() >>> cat.get_dataset("reanalysis-era5-pressure-levels").monthly 'reanalysis-era5-pressure-levels-monthly-means' >>> sorted(cat.get_dataset("reanalysis-era5-pressure-levels").variables)[:3] ['divergence', 'fraction-of-cloud-cover', 'geopotential']
Source code in src/earthlens/ecmwf/catalog.py
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 | |
describe(dataset_name)
#
Return a structured introspection record for a CDS dataset.
Useful for "what variables and extras does dataset X expose?" questions at runtime — the CLI / notebook caller can dump the result without needing to walk the YAML themselves.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
str
|
CDS dataset short name as it appears as a
key in :attr: |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict with keys |
dict[str, Any]
|
(the monthly-aggregate dataset name or |
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
|
dict[str, Any]
|
available under this dataset). |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Examples:
-
Describe ERA5-Land at a glance:
Source code in src/earthlens/ecmwf/catalog.py
download_job(job_id, target, chunk_size=1 << 20)
#
Download the result asset of a successful CDS job.
Thin wrapper that delegates to
:func:earthlens.ecmwf.jobs.download_job (N3); see that for
the full docstring.
Source code in src/earthlens/ecmwf/catalog.py
get_catalog()
#
Return the structural per-dataset map.
Satisfies the abstract base's contract; the actual parsing
is done in :func:model_post_init.
Returns:
| Type | Description |
|---|---|
dict[str, Dataset]
|
dict[str, Dataset]: One entry per CDS dataset. Same |
dict[str, Dataset]
|
object as :attr: |
Examples:
-
Inspect the dataset count and a sample:
Source code in src/earthlens/ecmwf/catalog.py
get_variable(dataset_name, variable_name)
#
Return the :class:Variable for a (dataset, code) pair.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
str
|
CDS dataset short name as it appears as a
key in :attr: |
required |
variable_name
|
str
|
Short variable code as it appears as a
YAML key under that dataset (e.g.
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
Variable |
Variable
|
Per-variable metadata loaded from |
Variable
|
|
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Examples:
-
Look up a single-level ERA5 variable and read its CDS dataset and NetCDF short name:
- Pressure-level variables expose>>> from earthlens.ecmwf import Catalog >>> spec = Catalog().get_variable( ... "reanalysis-era5-single-levels", "2m-temperature" ... ) >>> spec.cds_dataset 'reanalysis-era5-single-levels' >>> spec.nc_variable, spec.units ('t2m', 'K')cds_pressure_level:- The same short code under a different dataset is a different Variable:>>> from earthlens.ecmwf import Catalog >>> spec = Catalog().get_variable( ... "reanalysis-era5-pressure-levels", "temperature" ... ) >>> spec.cds_pressure_level ['1000']- Unknown dataset or variable raises>>> from earthlens.ecmwf import Catalog >>> Catalog().get_variable( ... "reanalysis-era5-land", "2m-temperature" ... ).cds_dataset 'reanalysis-era5-land'KeyError:
Source code in src/earthlens/ecmwf/catalog.py
health()
#
Report structural hygiene issues across the loaded catalog (L1).
Returns a mapping check_name -> list of "<dataset>/<variable>"
offenders. An empty list means the check is currently passing;
an empty dict means the catalog is clean. Most schema-level
invariants (duplicate keys, unknown fields, missing required
fields, legacy MARS keys in extras) are already enforced at
load time — this method covers the residual data-quality checks
that can't be expressed in the pydantic schema.
Checks reported:
variable_missing_nc_variable— variables whosenc_variableis empty or whitespace-only (would break downstream NetCDF reads).dataset_without_variables— datasets carrying zero curated variables. Should always be[]since the loader rejects these; included for defence in depth.
Source code in src/earthlens/ecmwf/catalog.py
list_recent_jobs(status=None, max_age_min=60, limit=50)
#
Return the user's recent CDS retrieval jobs.
Thin wrapper that delegates to
:func:earthlens.ecmwf.jobs.list_recent_jobs (N3); see that
for the full docstring. Kept on Catalog as a convenience so
Catalog().list_recent_jobs(...) keeps working.
Source code in src/earthlens/ecmwf/catalog.py
load(catalog_path=None, providers_path=None)
classmethod
#
Read the CDS catalog + providers registry from disk (cached).
Mirrors :meth:earthlens.gee.Catalog.load so the two backends
feel identical. Validates that every Dataset.provider slug
is in the registry; an unregistered slug is a load-time error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
catalog_path
|
Path | None
|
Path to a |
None
|
providers_path
|
Path | None
|
Path to |
None
|
Returns:
| Type | Description |
|---|---|
Catalog
|
A fully-populated :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
Propagated from :func: |
Source code in src/earthlens/ecmwf/catalog.py
minimal_valid_request(dataset_name)
#
Return a known-valid minimal request for dataset_name.
Walks the dataset's published constraints.json (cached
per-process) and returns the first entry expanded into a
request dict with one value per selector. Useful for:
- verifying a CDS account is set up correctly (submit the
returned dict via :meth:
cdsapi.Client.retrieveand watch for a NetCDF rather than a 400), - seeing what a valid extras schema looks like for a new dataset before authoring catalog rows,
- starting points for tests.
The returned request always carries data_format: netcdf;
the rest is whatever the first constraint entry enumerates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset_name
|
str
|
CDS dataset short name. Does not need to be
in :attr: |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: A request dict ready to pass to |
dict[str, Any]
|
meth: |
dict[str, Any]
|
|
dict[str, Any]
|
empty / unreachable. |
Examples:
-
Inspect ECMWF's published shape for a new dataset before authoring rows. Marked
# doctest: +SKIPbecause it requires network access:
Source code in src/earthlens/ecmwf/catalog.py
model_post_init(__context)
#
Auto-load cds_data_catalog.yaml when the user didn't supply one.
Catalog() with no args is sugar for Catalog.load() — it
reads the bundled YAML through the (path, mtime_ns)-keyed
cache so repeated construction is ~1 ms. If the caller passed
datasets=..., the disk read is skipped (test path; see
:meth:load for the heavy-lifting classmethod).
Raises:
| Type | Description |
|---|---|
ValueError
|
When auto-loading, propagates the same errors
as :meth: |