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
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 | |
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
|
|