Dimension Parsing#
Dimension metadata extraction, time coordinate parsing, and variable-dimension relationship handling for NetCDF files.
pyramids.netcdf.dimensions
#
DimMetaData
dataclass
#
Unified information for a single netCDF dimension.
This immutable dataclass captures both the structural information the GDAL
netCDF driver exposes via NETCDF_DIM_* keys and, optionally, the
per-dimension attribute mapping collected from keys of the form
"<name>#<attr>" (e.g., time#units).
It subsumes the previous "Dimension" helper by adding an attrs field
while still preserving the original raw bucket that stores the exact
strings parsed from metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Dimension name (e.g., "time", "level0"). |
required |
size
|
int | None
|
Dimension length (if known). Often derived from the first integer
in |
None
|
values
|
list[str | Number] | None
|
Parsed scalar values from the |
None
|
def_fields
|
tuple[int, ...] | None
|
Parsed integers from the |
None
|
raw
|
dict[str, str]
|
Raw strings captured from metadata for this dimension (e.g., the
original |
dict()
|
attrs
|
dict[str, str]
|
Optional attribute dictionary associated with the same dimension
name (e.g., |
dict()
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
- Construct manually for testing
- With attributes merged
See Also
- :class:
DimensionsIndex: Factory that populates structural entries. - :class:
MetaData: Provides convenient construction with merged attrs.
Source code in src/pyramids/netcdf/dimensions.py
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 | |
__post_init__()
#
Validate dimension fields for consistency.
Source code in src/pyramids/netcdf/dimensions.py
DimensionsIndex
dataclass
#
Index of netCDF dimensions parsed from GDAL metadata.
A thin mapping-like container that stores :class:DimMetaData objects
keyed by dimension name. Use :meth:from_metadata to construct an index
from a GDAL metadata mapping (e.g., gdal.Dataset.GetMetadata()).
Behavior
- Accepts dimensions listed under
<prefix>EXTRA. - Also recognizes any
<prefix><name>_DEFand<prefix><name>_VALUESkeys, even when<name>is not listed inEXTRA. - Coerces numeric tokens to
int/floatwhere possible.
Notes
The default prefix is NETCDF_DIM_ but any prefix can be supplied to
:meth:from_metadata and :meth:to_metadata.
See Also
- :class:
DimMetaData - :class:
MetaDatafor a higher-level view that merges attributes liketime#unitswith dimension structure.
Examples:
- Build from typical NETCDF_DIM_* keys
>>> from pyramids.netcdf.dimensions import DimensionsIndex >>> md = { ... 'NETCDF_DIM_EXTRA': '{time,level0}', ... 'NETCDF_DIM_level0_DEF': '{3,6}', ... 'NETCDF_DIM_level0_VALUES': '{1,2,3}', ... 'NETCDF_DIM_time_DEF': '{2,6}', ... 'NETCDF_DIM_time_VALUES': '{0,31}', ... } >>> idx = DimensionsIndex.from_metadata(md) >>> sorted(idx.names) ['level0', 'time'] >>> idx['time'].size 2 >>> idx['level0'].values [1, 2, 3] - Using a custom prefix
Source code in src/pyramids/netcdf/dimensions.py
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 | |
names
property
#
Return the list of dimension names.
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: Names in insertion order (or sorted order, depending on |
list[str]
|
construction) matching the keys of the index. |
Examples:
- Simple index
from_metadata(metadata, *, prefix='NETCDF_DIM_')
classmethod
#
Parse dimensions from a GDAL metadata dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
Mapping[str, str]
|
GDAL metadata mapping (e.g., from |
required |
prefix
|
str
|
Key prefix to filter on (default: |
'NETCDF_DIM_'
|
Returns:
| Name | Type | Description |
|---|---|---|
DimensionsIndex |
DimensionsIndex
|
Parsed index of dimensions. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Examples:
- Basic usage with standard prefix
>>> from pyramids.netcdf.dimensions import DimensionsIndex >>> md = { ... 'NETCDF_DIM_EXTRA': '{time,level0}', ... 'NETCDF_DIM_level0_DEF': '{3,6}', ... 'NETCDF_DIM_level0_VALUES': '{1,2,3}', ... 'NETCDF_DIM_time_DEF': '{2,6}', ... 'NETCDF_DIM_time_VALUES': '{0,31}', ... } >>> idx = DimensionsIndex.from_metadata(md) >>> idx['time'].size 2 - Using a custom prefix
Source code in src/pyramids/netcdf/dimensions.py
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 | |
__len__()
#
Number of dimensions in the index.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Count of stored dimensions. |
Examples:
- Length of a simple index
Source code in src/pyramids/netcdf/dimensions.py
__iter__()
#
Iterate over stored dimensions.
Yields:
| Name | Type | Description |
|---|---|---|
DimMetaData |
Iterable[DimMetaData]
|
Each stored dimension in unspecified order. |
Examples:
- Iterate names
Source code in src/pyramids/netcdf/dimensions.py
__contains__(name)
#
Check if a dimension name exists in the index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Dimension name to check. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Examples:
- Membership test
Source code in src/pyramids/netcdf/dimensions.py
__getitem__(name)
#
Get a dimension by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Dimension name. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
DimMetaData |
DimMetaData
|
The matching dimension. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the name is not present in the index. |
Examples:
- Access an existing dimension
Source code in src/pyramids/netcdf/dimensions.py
__str__()
#
Return a compact, human-readable summary of the index.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A multi-line string listing each dimension with size, values |
str
|
and DEF fields when available. |
Examples:
- Pretty-print a small index
Source code in src/pyramids/netcdf/dimensions.py
to_dict()
#
Serialize the index to a plain dictionary.
Useful for logging, debugging, or JSON/YAML output.
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, object]]
|
dict[str, dict[str, object]]: Mapping from dimension name to a |
dict[str, dict[str, object]]
|
structure with |
Examples:
- Convert to a simple dict
Source code in src/pyramids/netcdf/dimensions.py
to_metadata(*, prefix='NETCDF_DIM_', include_extra=True, sort_names=True)
#
Serialize the index back to GDAL netCDF metadata keys.
This produces keys compatible with the netCDF GDAL driver such as
<prefix>EXTRA, <prefix><name>_DEF and <prefix><name>_VALUES.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
Metadata key prefix (defaults to |
'NETCDF_DIM_'
|
include_extra
|
bool
|
Whether to include the |
True
|
sort_names
|
bool
|
Whether to sort names deterministically in outputs. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
dict[str, str]: A dictionary suitable for use with GDAL's metadata API. |
Examples:
- Round-trip a simple index
>>> from pyramids.netcdf.dimensions import DimensionsIndex >>> md = {'NETCDF_DIM_time_DEF': '{2,6}', 'NETCDF_DIM_time_VALUES': '{0,31}'} >>> idx = DimensionsIndex.from_metadata(md) >>> out = idx.to_metadata() >>> sorted(out.keys()) ['NETCDF_DIM_EXTRA', 'NETCDF_DIM_time_DEF', 'NETCDF_DIM_time_VALUES']
Source code in src/pyramids/netcdf/dimensions.py
MetaData
dataclass
#
Aggregate of dimension structure and per-dimension attributes.
This class ties together two complementary pieces of information commonly
exposed by the GDAL netCDF driver:
- A :class:DimensionsIndex parsed from NETCDF_DIM_* keys, describing
dimension sizes, DEF fields, and VALUES.
- Per-dimension attribute dictionaries collected from keys of the form
"<name>#<attr>" (e.g., time#units, lat#axis).
Examples:
- Build from a combined metadata mapping and inspect
>>> from pyramids.netcdf.dimensions import MetaData >>> md = { ... 'NETCDF_DIM_EXTRA': '{time,level0}', ... 'NETCDF_DIM_time_DEF': '{2,6}', ... 'NETCDF_DIM_time_VALUES': '{0,31}', ... 'NETCDF_DIM_level0_DEF': '{3,6}', ... 'NETCDF_DIM_level0_VALUES': '{1,2,3}', ... 'time#axis': 'T', ... 'time#units': 'days since 1-1-1 0:0:0', ... 'level0#axis': 'Z', ... } >>> meta = MetaData.from_metadata(md) >>> sorted(meta.names) ['level0', 'time'] >>> meta.get_attrs('time')['axis'] 'T'
See Also
- :class:
DimensionsIndex - :func:
parse_dimension_attributes
Source code in src/pyramids/netcdf/dimensions.py
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 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 | |
names
property
#
Return the list of dimension names represented in this metadata.
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: Names present in the underlying :class: |
Examples:
- Inspect names
from_metadata(metadata, *, prefix='NETCDF_DIM_', normalize_attr_keys=True, names=None)
classmethod
#
Build a MetaData object by parsing a GDAL metadata mapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
Mapping[str, str]
|
GDAL metadata map (e.g., |
required |
prefix
|
str
|
Prefix used for dimension entries (defaults to |
'NETCDF_DIM_'
|
normalize_attr_keys
|
bool
|
Normalize attribute keys (the part after |
True
|
names
|
Iterable[str] | None
|
If provided, limit attribute parsing to these names. By default uses the dimension names discovered under the prefix. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
MetaData |
MetaData
|
Combined structure and attributes parsed from metadata. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the input mapping contains non-string keys/values. |
Examples:
- Typical usage
Source code in src/pyramids/netcdf/dimensions.py
get_attrs(name)
#
Return attributes for a given dimension name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Dimension name. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
dict[str, str]: Attribute dictionary; empty if the name is unknown |
dict[str, str]
|
or has no attributes. |
Examples:
- Access attributes safely
Source code in src/pyramids/netcdf/dimensions.py
get_dimension(name)
#
Return a DimMetaData with merged attributes for a given name, if present.
Combines structural info from :class:DimensionsIndex with the
attribute dictionary captured for the same name and returns a new
:class:DimMetaData instance that includes both sets of information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Dimension name. |
required |
Returns:
| Type | Description |
|---|---|
DimMetaData | None
|
DimMetaData | None: The merged view if available, else |
Examples:
- Get a merged DimMetaData and inspect attributes
- Unknown name returns None
Source code in src/pyramids/netcdf/dimensions.py
iter_dimensions()
#
Iterate over merged DimMetaData objects in name-sorted order.
Yields:
| Name | Type | Description |
|---|---|---|
DimMetaData |
Iterable[DimMetaData]
|
Each dimension with merged structure and attributes. |
Examples:
- Iterate and collect names
Source code in src/pyramids/netcdf/dimensions.py
to_metadata(*, prefix='NETCDF_DIM_', include_extra=True, sort_names=True, include_attrs=True)
#
Serialize back to a GDAL metadata mapping.
Combines the dimension keys produced by :meth:DimensionsIndex.to_metadata
with flattened attribute keys of the form "<name>#<attr>".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
Metadata key prefix for dimension keys. Defaults to
|
'NETCDF_DIM_'
|
include_extra
|
bool
|
Include an |
True
|
sort_names
|
bool
|
Sort dimension names when serializing for determinism. |
True
|
include_attrs
|
bool
|
Whether to include |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
dict[str, str]: A single flattened mapping suitable for GDAL. |
Examples:
- Merge structure and attributes
Source code in src/pyramids/netcdf/dimensions.py
__str__()
#
Return a readable summary of dimensions and attributes.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A multi-line summary listing each dimension name with basic |
str
|
statistics (size, number of values, attribute count). |
Examples:
- Pretty-print a MetaData summary
Source code in src/pyramids/netcdf/dimensions.py
parse_gdal_netcdf_dimensions(metadata)
#
Parse netCDF dimension info from GDAL metadata.
A convenience wrapper around :meth:DimensionsIndex.from_metadata that
uses the default NETCDF_DIM_ prefix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
Mapping[str, str]
|
GDAL metadata mapping (e.g., from
|
required |
Returns:
| Name | Type | Description |
|---|---|---|
DimensionsIndex |
DimensionsIndex
|
Parsed index of dimensions. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Examples:
- Typical usage
>>> md = { ... 'NETCDF_DIM_EXTRA': '{time,level0}', ... 'NETCDF_DIM_level0_DEF': '{3,6}', ... 'NETCDF_DIM_level0_VALUES': '{1,2,3}', ... 'NETCDF_DIM_time_DEF': '{2,6}', ... 'NETCDF_DIM_time_VALUES': '{0,31}', ... } >>> idx = parse_gdal_netcdf_dimensions(md) >>> idx.to_dict()['time']['size'] 2 >>> idx.to_dict()['level0']['values'] [1, 2, 3]
See Also
- :class:
DimensionsIndex
Source code in src/pyramids/netcdf/dimensions.py
parse_dimension_attributes(metadata, names=None, *, normalize_attr_keys=True)
#
Extract per-dimension attributes from GDAL netCDF metadata.
This helper scans metadata entries whose keys look like "
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
Mapping[str, str]
|
Mapping of metadata keys to values (e.g., from GDAL). |
required |
names
|
Iterable[str] | None
|
Optional iterable of dimension names to include. If provided, only attributes for these names are captured. |
None
|
normalize_attr_keys
|
bool
|
If True, attribute names after the "#" are converted to lowercase in the output. If False, original case is preserved. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, str]]
|
dict[str, dict[str, str]]: A mapping from dimension name to a dictionary |
dict[str, dict[str, str]]
|
of attributes for that dimension. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Examples:
- Parse all attributes for any name
>>> md = { ... 'lat#bounds': 'bounds_lat', ... 'lat#long_name': 'latitude', ... 'lat#units': 'degrees_north', ... 'time#axis': 'T', ... 'time#long_name': 'time', ... 'time#units': 'days since 1-1-1 0:0:0', ... } >>> parse_dimension_attributes(md) {'lat': {'bounds': 'bounds_lat', 'long_name': 'latitude', 'units': 'degrees_north'}, 'time': {'axis': 'T', 'long_name': 'time', 'units': 'days since 1-1-1 0:0:0'}} - Restrict to provided names and preserve attribute case
See Also
- :class:
MetaData: Combines these attributes with dimension structure.