Helper utilities¶
This section provides supporting utilities for storage access, caching, and chunk-size selection. These helpers are mainly intended to support efficient IO and scalable data layout decisions.
S3 Output handling¶
Once a pyramid has been created, it can be written to object storage for cloud-native access and downstream analysis.
save_pyramid(pyramid, path, s3_options=None, *, mode='a', compute=True, region='auto', zarr_format=2, encoding=None)
¶
Write a HEALPix pyramid to Zarr stores on S3 or local disk.
Each level is stored below "<path>/level_<level>.zarr".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pyramid
|
dict[int, Dataset]
|
Mapping of HEALPix level to dataset. |
required |
path
|
str
|
Target prefix. An |
required |
s3_options
|
dict[str, Any] | None
|
Options forwarded to :class: |
None
|
mode
|
Literal['a', 'w', 'r+']
|
Zarr write mode. |
'a'
|
compute
|
bool
|
Trigger Dask execution immediately when |
True
|
region
|
Literal['auto'] | dict[str, slice]
|
Region writes for partial updates. |
'auto'
|
zarr_format
|
Literal[2, 3]
|
Zarr format version. |
2
|
encoding
|
dict[int, dict[str, dict[str, Any]]] | None
|
Per-level encoding dictionaries. |
None
|
Source code in .tox/docs/lib/python3.13/site-packages/grid_doctor/helpers.py
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 | |
Storage configuration¶
These utilities simplify access to S3-compatible object storage by collecting and normalizing the options needed by downstream code.
get_s3_options(endpoint_url, secrets_file, **kwargs)
¶
Build an S3 options dictionary from an endpoint and credentials file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint_url
|
str
|
S3-compatible endpoint URL. |
required |
secrets_file
|
str | Path
|
JSON file containing |
required |
**kwargs
|
str
|
Additional options merged into the returned dictionary. |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Options for |
Source code in .tox/docs/lib/python3.13/site-packages/grid_doctor/utils.py
Chunking¶
Choosing appropriate chunk sizes is important for balancing storage efficiency and access performance. The function below helps determine a chunk layout for a desired target store size.
chunk_for_target_store_size(*, level, dtype='float32', target_stored_mib=16.0, compression_ratio=2.0, access='map', ntime=None, max_time_chunk=None, max_cell_chunk=None)
¶
Compute (time, cell) chunks for a HEALPix dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int
|
HEALPix order / level. |
required |
dtype
|
str | dtype
|
Variable dtype. |
'float32'
|
target_stored_mib
|
float
|
Desired approximate compressed chunk size on disk. |
16.0
|
compression_ratio
|
float
|
Estimated ratio: uncompressed_bytes / compressed_bytes |
2.0
|
access
|
Literal['time_series', 'map']
|
"map" or "time_series". |
'map'
|
ntime
|
int | None
|
Total time size. Needed for time_series mode unless max_time_chunk is given. |
None
|
max_time_chunk
|
int | None
|
Optional cap for time chunk. |
None
|
max_cell_chunk
|
int | None
|
Optional cap for cell chunk. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, int]
|
Chunk sizes, e.g. {"time": 5, "cell": 786432}. |
Source code in .tox/docs/lib/python3.13/site-packages/grid_doctor/utils.py
Caching¶
These helpers provide lightweight caching for repeatedly used datasets and weight files to avoid unnecessary reopen or recomputation overhead.
cached_open_dataset(files, **kwargs)
¶
Open multiple files and cache the merged dataset as a pickle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files
|
Collection[str]
|
Input file paths or glob-expanded file names. |
required |
**kwargs
|
Any
|
Extra keyword arguments for |
{}
|
Returns:
| Type | Description |
|---|---|
Dataset
|
The opened dataset. |
Source code in .tox/docs/lib/python3.13/site-packages/grid_doctor/utils.py
cached_weights(ds, level=None, *, method='conservative', nest=True, source_units='auto', cache_path=None, **kwargs)
¶
Compute or load a cached HEALPix weight file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
Source dataset whose grid geometry defines the weights. |
required |
level
|
int | None
|
HEALPix level. |
None
|
method
|
RemapMethod
|
Weight-generation method. Supported values are |
'conservative'
|
nest
|
bool
|
Use nested HEALPix ordering when |
True
|
source_units
|
SourceUnits
|
Unit convention of the source coordinates. |
'auto'
|
cache_path
|
str | Path | None
|
Cache directory or explicit file name. When omitted, the default package cache directory is used. |
None
|
**kwargs
|
Any
|
Any additional keyword arguments for
|
{}
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the cached NetCDF weight file. |
Examples:
Source code in .tox/docs/lib/python3.13/site-packages/grid_doctor/utils.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 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 | |