Metadata¶
Every attribute, coordinate name and grid mapping that grid-doctor
stores is defined in grid_doctor.metadata. Code that produces data
builds a HealpixMetadata and
calls apply; code that reads data calls from_dataset. The stored
layout follows the CF-1.13 healpix grid mapping; see
Technical decisions
for the full description.
from grid_doctor.metadata import HealpixGrid, HealpixMetadata, Provenance
meta = HealpixMetadata(HealpixGrid(level=8), Provenance(method="conservative"))
ds = meta.apply(ds)
HealpixMetadata.from_dataset(ds).grid.level # 8
grid_doctor.metadata
¶
Metadata of grid-doctor datasets, written and read in one place.
Every attribute, coordinate name and grid mapping grid-doctor stores is
defined in this package. Code that produces data builds a
HealpixMetadata and calls
apply; code that reads
data calls
HealpixMetadata.from_dataset.
Changing the stored layout therefore never touches the write sites.
_grid:HealpixGrid, the grid itself (level, indexing scheme, figure of the earth)._provenance:Provenance, how the values were produced._cf: the CF-1.13healpixgrid mapping spelling of a grid._dataset:HealpixMetadata, which combines the above and applies it to a dataset._multiscales:PyramidMetadata, the layout of a pyramid stored as one datatree, spelled with the Zarrmultiscalesanddggsconventions._pyramid: attributes of such a datatree's root group._keys: every stored name.
The written layout is CF-1.13: a scalar crs grid mapping variable
(grid_mapping_name = "healpix", indexing_scheme,
refinement_level), an integer cell index coordinate with
standard_name = "healpix_index", and grid_mapping = "crs" on
every variable along cell. xdggs.decode(ds, convention="cf")
reads it. Pyramids written as one datatree additionally describe their
levels on the root group (see
PyramidMetadata).
HealpixMetadata(grid, provenance=Provenance(), sparse=False, implicit_coords=False)
dataclass
¶
Everything grid-doctor records about a HEALPix dataset.
Write sites build one of these and call
apply; readers call
from_dataset.
Neither ever touches attribute names, so the stored layout can
change here without touching any code that produces or consumes
data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
HealpixGrid
|
The HEALPix grid. |
required |
provenance
|
Provenance
|
How the values were produced. |
Provenance()
|
sparse
|
bool
|
The |
False
|
implicit_coords
|
bool
|
A dense store written without |
False
|
Examples:
>>> meta = HealpixMetadata(HealpixGrid(level=2), Provenance(method="nearest"))
>>> ds = meta.apply(ds)
>>> HealpixMetadata.from_dataset(ds).grid.level
2
__post_init__()
¶
Reject contradictory layouts.
replace(**changes)
¶
Return a copy with changes applied (see dataclasses.replace).
apply(ds)
¶
Return ds with all grid-doctor metadata written.
Writes
- a scalar
crsgrid mapping coordinate (replacing any existing one), - CF attributes on the
cellindex coordinate and thelatitude/longitudecoordinates, where present, grid_mapping = "crs"on every variable alongcell,- the global
Conventions, provenance and layout attributes.
Global attributes owned by grid-doctor are rewritten from this object, so stale values copied from an input dataset never survive. Coordinate values are left to the caller.
Raises:
| Type | Description |
|---|---|
ValueError
|
When the |
Source code in grid_doctor/metadata/_dataset.py
from_dataset(ds)
classmethod
¶
Read the metadata written by apply.
Only the CF grid mapping is required, so CF HEALPix datasets
written by other tools (e.g. xdggs) are read as dense,
with unknown provenance.
Raises:
| Type | Description |
|---|---|
ValueError
|
When ds has no HEALPix grid mapping variable, or was written by a newer metadata layout than this grid-doctor understands. |
Source code in grid_doctor/metadata/_dataset.py
HealpixGrid(level, indexing_scheme='nested', earth_radius=None, ellipsoid='sphere')
dataclass
¶
A HEALPix grid: refinement level, indexing scheme and figure of the earth.
Holds facts only; how they are spelled in a dataset is up to
HealpixMetadata. Values
are validated and normalised on construction, so a HealpixGrid
is always a valid grid (NumPy integers from Zarr attributes are
accepted and converted).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int
|
Refinement level (CF |
required |
indexing_scheme
|
IndexingScheme
|
|
'nested'
|
earth_radius
|
float | None
|
Radius of the spherical earth in metres, if it should be recorded. Only valid on a sphere. |
None
|
ellipsoid
|
Ellipsoid
|
Ellipsoid the cell geometry is computed on, as a |
'sphere'
|
Examples:
nside
property
¶
Number of cells along a base-cell side, 2**level.
npix
property
¶
Number of cells on the full sphere, 12 * nside**2.
nest
property
¶
True for nested indexing (healpy's nest flag).
__post_init__()
¶
Validate and normalise the fields.
Source code in grid_doctor/metadata/_grid.py
from_nest(level, *, nest, ellipsoid='sphere')
classmethod
¶
Build a grid from the healpy-style nest flag.
Source code in grid_doctor/metadata/_grid.py
Provenance(version=_package_version(), method=None, coarsened_from_level=None, coarsen_mode=None, min_valid_fraction=None)
dataclass
¶
Provenance attributes of a grid-doctor dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
str
|
grid-doctor version that produced the values. Defaults to the running version. |
_package_version()
|
method
|
str | None
|
How the finest values were produced: a remapping method
( |
None
|
coarsened_from_level
|
int | None
|
Level the values were coarsened from (coarsened levels only). |
None
|
coarsen_mode
|
str | None
|
|
None
|
min_valid_fraction
|
float | None
|
Threshold below which coarsened cells were set to NaN (coarsened levels only). |
None
|
to_attrs()
¶
Return the provenance as global attributes, skipping unset fields.
Source code in grid_doctor/metadata/_provenance.py
from_attrs(attrs)
classmethod
¶
Read provenance from global attributes; missing fields stay unset.
A missing version is reported as "unknown" rather than the
running version, which would misattribute foreign data.
Source code in grid_doctor/metadata/_provenance.py
PyramidMetadata(levels)
dataclass
¶
Layout of a HEALPix pyramid stored as one datatree.
Build it with from_pyramid
and write to_attrs
to the root group; read it back with
from_attrs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
levels
|
tuple[PyramidLevel, ...]
|
The levels, finest first. |
required |
__post_init__()
¶
Check that every derived_from points to a described level.
Source code in grid_doctor/metadata/_multiscales.py
from_pyramid(pyramid)
classmethod
¶
Describe pyramid ({level: dataset}) from its levels' metadata.
A level is derived from the level its provenance names in
coarsened_from_level, when that level is part of the
pyramid. Levels without HEALPix metadata are listed by path.
Source code in grid_doctor/metadata/_multiscales.py
to_attrs()
¶
Return the zarr_conventions and multiscales attributes.
Source code in grid_doctor/metadata/_multiscales.py
from_attrs(attrs)
classmethod
¶
Read the layout written by to_attrs.
Raises:
| Type | Description |
|---|---|
ValueError
|
When attrs have no |
Source code in grid_doctor/metadata/_multiscales.py
asset(level)
¶
Return the group path that holds HEALPix level.
Source code in grid_doctor/metadata/_multiscales.py
PyramidLevel(asset, grid, coordinate=CELL_DIM, derived_from=None, resampling_method=None)
dataclass
¶
One level of a multiscales pyramid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
asset
|
str
|
Path of the level's group, relative to the root. |
required |
grid
|
HealpixGrid | None
|
The level's HEALPix grid, |
required |
coordinate
|
str | None
|
Name of the cell-index coordinate, |
CELL_DIM
|
derived_from
|
str | None
|
Asset the level was coarsened from, if any. |
None
|
resampling_method
|
str | None
|
How it was coarsened ( |
None
|
Helpers¶
has_healpix_metadata(ds)
¶
Whether ds carries a HEALPix grid mapping variable.
variable_method(attrs)
¶
Return the per-variable production method, if recorded.
Variables binned with different aggregations carry their own method
next to the dataset-level one in Provenance.
Source code in grid_doctor/metadata/_provenance.py
variable_method_attrs(method)
¶
grid_mapping_attrs(grid)
¶
Return the attributes of the CF grid mapping variable for grid.
refinement_level is always written: CF requires it for the
nested and ring schemes, including level 0.
Source code in grid_doctor/metadata/_cf.py
merge_conventions(existing)
¶
Merge the CF version into an existing Conventions attribute.
Any CF-x.y token is replaced by the version grid-doctor writes;
other conventions (CMIP-6.2, ACDD-1.3, ...) are kept in
order.
Source code in grid_doctor/metadata/_cf.py
level_group(level)
¶
IndexingScheme = Literal['nested', 'ring']
module-attribute
¶
HEALPix indexing schemes grid-doctor produces.
CF also defines nuniq and zuniq; grid-doctor does not write
them, so they are rejected rather than half supported.