Metadata Extraction#
Functions for extracting, serializing, and deserializing NetCDF metadata using GDAL's Multidimensional API.
pyramids.netcdf.metadata.get_metadata(source, open_options=None)
#
Read and normalize all NetCDF MDIM metadata.
Accepts several source types and delegates to
MetadataBuilder to produce a NetCDFMetadata instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source
|
Dataset | str | object
|
The data source.
Accepts a GDAL dataset directly, a file path (opened
internally with |
required |
open_options
|
dict[str, Any] | None
|
Optional dictionary of GDAL open-options. Stored in the resulting metadata for provenance but not used to open the file. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
NetCDFMetadata |
NetCDFMetadata
|
Fully populated metadata dataclass. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If source is a string path that cannot be opened as a multidimensional raster. |
Examples:
Open from a file path:
>>> from osgeo import gdal
>>> import pyramids.netcdf.metadata as meta
>>> md = meta.get_metadata(
... "precip.nc"
... )
>>> md.driver
'netCDF'
See Also
MetadataBuilder: The builder class used internally.
Source code in src/pyramids/netcdf/metadata.py
pyramids.netcdf.metadata.to_json(metadata)
#
Serialize NetCDFMetadata to a compact JSON string.
Converts the dataclass tree to plain dicts via to_dict
and then encodes to JSON with no extra whitespace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
NetCDFMetadata
|
A |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
JSON-encoded string with no ASCII escaping and
compact separators (no spaces after |
Examples:
Round-trip a minimal metadata object:
>>> import json
>>> from pyramids.netcdf.metadata import to_json
>>> from pyramids.netcdf.models import (
... NetCDFMetadata, StructuralInfo,
... )
>>> md = NetCDFMetadata(
... driver="netCDF",
... root_group="/",
... groups={},
... variables={},
... dimensions={},
... global_attributes={},
... structural=StructuralInfo(
... driver_name="netCDF"
... ),
... created_with={"library": "GDAL"},
... )
>>> s = to_json(md)
>>> json.loads(s)["driver"]
'netCDF'
See Also
to_dict: Converts to plain dicts without JSON encoding.
from_json: Deserializes the string back to
NetCDFMetadata.
Source code in src/pyramids/netcdf/metadata.py
pyramids.netcdf.metadata.from_json(s)
#
Deserialize NetCDFMetadata from a JSON string.
Parses the JSON produced by to_json and manually
reconstructs the dataclass hierarchy (GroupInfo,
VariableInfo, DimensionInfo, StructuralInfo).
Only the schema produced by to_dict / to_json is
supported; arbitrary JSON will likely raise KeyError.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
A JSON string previously produced by |
required |
Returns:
| Name | Type | Description |
|---|---|---|
NetCDFMetadata |
NetCDFMetadata
|
Reconstructed metadata instance. |
Raises:
| Type | Description |
|---|---|
JSONDecodeError
|
If s is not valid JSON. |
KeyError
|
If required fields are missing from the JSON payload. |
Examples:
Round-trip through JSON:
>>> from pyramids.netcdf.metadata import (
... to_json, from_json,
... )
>>> from pyramids.netcdf.models import (
... NetCDFMetadata, StructuralInfo,
... )
>>> md = NetCDFMetadata(
... driver="netCDF",
... root_group="/",
... groups={},
... variables={},
... dimensions={},
... global_attributes={"history": "created"},
... structural=StructuralInfo(
... driver_name="netCDF"
... ),
... created_with={"library": "GDAL"},
... )
>>> s = to_json(md)
>>> restored = from_json(s)
>>> restored.driver
'netCDF'
>>> restored.global_attributes["history"]
'created'
See Also
to_json: The serialization counterpart.
Source code in src/pyramids/netcdf/metadata.py
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 | |
pyramids.netcdf.metadata.to_dict(metadata)
#
Convert NetCDFMetadata to plain dicts suitable for JSON.
Recursively walks all dataclass fields and converts them to
plain dict / list / scalar types so the result can be
passed directly to json.dumps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
NetCDFMetadata
|
A |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict[str, Any]
|
Nested dictionary with all dataclass fields converted to plain dicts. |
Examples:
Convert a minimal metadata object:
>>> from pyramids.netcdf.metadata import to_dict
>>> from pyramids.netcdf.models import (
... NetCDFMetadata, StructuralInfo,
... )
>>> md = NetCDFMetadata(
... driver="netCDF",
... root_group="/",
... groups={},
... variables={},
... dimensions={},
... global_attributes={"title": "test"},
... structural=StructuralInfo(
... driver_name="netCDF"
... ),
... created_with={"library": "GDAL"},
... )
>>> d = to_dict(md)
>>> d["driver"]
'netCDF'
>>> d["global_attributes"]["title"]
'test'
>>> d["structural"]["driver_name"]
'netCDF'
See Also
to_json: Serializes directly to a JSON string.
from_json: Deserializes a JSON string back to
NetCDFMetadata.