NetCDF Utilities#
Low-level GDAL helpers for attribute reading, no-data extraction, scale/offset handling, time conversion, and dtype utilities.
pyramids.netcdf.utils
#
resolve_full_name(obj, group_full_name, fallback_name)
#
Full hierarchical name of a GDAL dimension / array, with a qualified fallback.
Tries obj.GetFullName(); when that raises (some drivers don't expose it on every
object), composes the path from the parent group's full name and fallback_name —
"<group>/<name>", or "/<name>" when the parent is the root group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
A GDAL dimension or MDArray. |
required |
group_full_name
|
str
|
Full name of the parent group (e.g. |
required |
fallback_name
|
str
|
The object's short name, used to build the fallback path. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The resolved full-name string. |
Source code in src/pyramids/netcdf/utils.py
create_time_conversion_func(units, out_format='%Y-%m-%d %H:%M:%S', calendar='standard')
#
Create a converter that maps numeric CF time offsets to date strings.
Parses CF-compliant time unit strings (e.g.,
"days since 1979-01-01") and returns a callable that
converts numeric offsets to formatted date strings.
For standard/proleptic_gregorian calendars, uses Python's
datetime + timedelta. For non-standard calendars
(360_day, noleap, all_leap, julian), uses
cftime.num2date() (optional dependency).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
units
|
str
|
CF time unit string in the format
|
required |
out_format
|
str
|
strftime format for the output strings.
Defaults to |
'%Y-%m-%d %H:%M:%S'
|
calendar
|
str
|
CF calendar type. Defaults to |
'standard'
|
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
A function that takes a numeric value and returns a formatted date string. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the unit string cannot be parsed or uses an unsupported time unit. |
Examples:
-
Convert day offsets from a 1979 origin:
-
Use hour-based units with a custom format:
See Also
_parse_units_origin: Parses the unit string.
Source code in src/pyramids/netcdf/utils.py
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 | |
decode_cf_time(values, unit, calendar='standard')
#
Decode numeric CF time offsets to datetimes.
Standard / gregorian / proleptic_gregorian calendars yield datetime64[ns] when
representable; non-standard calendars (360_day / noleap …) yield cftime
objects. Arrays whose unit is not a "<interval> since <origin>" string are
returned unchanged.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
ndarray
|
The numeric values already read for the coordinate. |
required |
unit
|
str | None
|
The coordinate's CF unit string (e.g. |
required |
calendar
|
str
|
The CF calendar name. Defaults to |
'standard'
|
Returns:
| Type | Description |
|---|---|
NDArray
|
np.ndarray: Decoded datetimes for a time axis, else |
Source code in src/pyramids/netcdf/utils.py
encode_cf_time(value, unit, calendar='standard')
#
Convert a date string / datetime to a coordinate's numeric CF scale.
The inverse of :func:decode_cf_time for a single value: used to translate a
user-supplied selection bound back to the time axis's stored numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
A date string, :class: |
required |
unit
|
str
|
The CF unit string the numeric scale is expressed in. |
required |
calendar
|
str
|
The CF calendar name. |
'standard'
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
|
Source code in src/pyramids/netcdf/utils.py
read_cf_attributes(obj)
#
Read a GDAL object's attributes the way a CF consumer needs them.
The CF reader for every consumer of units / calendar / standard_name /
axis. Use this rather than :func:_read_attributes wherever the attributes are
interpreted as CF: attributes alone omit units, because GDAL moves it to the object's
unit slot, so a site that reads them raw silently loses it — which is how a time axis came
back undecodable, CF axis detection by units never fired, and a UGRID variable's unit
vanished on a round trip (#1078).
:func:_read_attributes remains the right call for a verbatim view of what the file
declares (serialisation, round-trip writers); this one is for interpreting CF.
The slot is the CF units for the netCDF and HDF5 drivers, which is where this matters.
Another multidim driver (Zarr, HDF-EOS) may fill it from a format-specific field, in which
case the reported units is that driver's unit rather than a CF attribute the file
literally declares — a reason to read attributes raw when the question is "what does this
file say", not "what does this axis mean".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Any GDAL object exposing |
required |
Returns:
| Type | Description |
|---|---|
dict[str, AttributeValue]
|
dict[str, AttributeValue]: The object's attributes, including |