Regridding¶
This section covers the core remapping functionality of grid_doctor.
The functions below are the main entry points for generating reusable
weight files and transforming data onto HEALPix grids.
Weight file generation¶
These functions prepare reusable remapping weights so that later transformations can be applied efficiently without recomputing the full mapping each time.
grid_doctor.utils.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 | |
compute_healpix_weights(ds, level, *, method='nearest', nest=True, source_units='auto', weights_path=None, grid=None, source_kind='auto', ignore_unmapped=None, large_file=True, prefer_offline=None, nproc=1, esmf_regrid_weightgen='ESMF_RegridWeightGen', keep_intermediates=False, workdir=None, spectral_transform_command=None)
¶
Generate a reusable NetCDF weight file for HEALPix remapping.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset | str | Path
|
Source dataset or a source path. |
required |
level
|
int
|
HEALPix refinement level. |
required |
method
|
RemapMethod
|
|
'nearest'
|
nest
|
bool
|
Use nested HEALPix ordering when |
True
|
source_units
|
SourceUnits
|
Unit convention of the source coordinates. |
'auto'
|
weights_path
|
str | Path | None
|
Output NetCDF file. A temporary file is created when omitted. |
None
|
grid
|
Dataset | None
|
Optional external geometry dataset. |
None
|
source_kind
|
SourceKind
|
Explicit source representation or |
'auto'
|
ignore_unmapped
|
bool | None
|
Ignore unmapped destination cells. |
None
|
large_file
|
bool
|
Forwarded to the in-memory ESMPy workflow. |
True
|
prefer_offline
|
bool | None
|
Force or disable the offline ESMF path. |
None
|
nproc
|
int
|
Number of MPI ranks for the offline path. |
1
|
esmf_regrid_weightgen
|
str
|
Name or path of the offline ESMF executable. |
'ESMF_RegridWeightGen'
|
keep_intermediates
|
bool
|
Keep intermediate mesh files. |
False
|
workdir
|
str | Path | None
|
Working directory for offline intermediate files. |
None
|
spectral_transform_command
|
list[str] | tuple[str, ...] | None
|
External command for |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to the generated NetCDF weight file. |
See Also
apply_weight_file:
Apply a previously generated weight file.
Source code in .tox/docs/lib/python3.13/site-packages/grid_doctor/remap.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 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 | |
Applying remapping¶
The following functions use either direct remapping logic or precomputed weights to move data onto the target HEALPix representation.
regrid_to_healpix(ds, level, *, nest=True, method='conservative', source_units='auto', weights_path=None, missing_policy='renormalize', backend='auto', grid=None, source_kind='auto', ignore_unmapped=None, large_file=True, prefer_offline=None, nproc=1, esmf_regrid_weightgen='ESMF_RegridWeightGen', keep_intermediates=False, workdir=None, spectral_transform_command=None)
¶
Regrid ds to a HEALPix target grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
Source dataset on a regular, curvilinear, or unstructured grid. |
required |
level
|
int
|
HEALPix refinement level. |
required |
nest
|
bool
|
Use nested HEALPix ordering when |
True
|
method
|
RemapMethod
|
|
'conservative'
|
source_units
|
SourceUnits
|
Coordinate units for the source grid. |
'auto'
|
weights_path
|
str | Path | None
|
Optional existing or target weight file. |
None
|
missing_policy
|
MissingPolicy
|
How NaN values in the source grid are treated.
|
'renormalize'
|
backend
|
ApplyBackend
|
Application backend ( |
'auto'
|
grid
|
Dataset | None
|
Optional external geometry dataset. |
None
|
source_kind
|
SourceKind
|
Explicit source representation or |
'auto'
|
ignore_unmapped
|
bool | None
|
Ignore unmapped destination cells. |
None
|
large_file
|
bool
|
Forwarded to the in-memory ESMPy workflow. |
True
|
prefer_offline
|
bool | None
|
Force or disable the offline ESMF path. |
None
|
nproc
|
int
|
Number of MPI ranks for the offline path. |
1
|
esmf_regrid_weightgen
|
str
|
Name or path of the offline ESMF executable. |
'ESMF_RegridWeightGen'
|
keep_intermediates
|
bool
|
Keep intermediate mesh files. |
False
|
workdir
|
str | Path | None
|
Working directory for offline intermediate files. |
None
|
spectral_transform_command
|
list[str] | tuple[str, ...] | None
|
External command for |
None
|
Returns:
| Type | Description |
|---|---|
Dataset
|
Regridded dataset on the HEALPix grid. |
Source code in .tox/docs/lib/python3.13/site-packages/grid_doctor/remap.py
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 | |
apply_weight_file(ds, weights_path, *, missing_policy='renormalize', grid=None, source_dims=None, source_units='auto', backend='auto')
¶
Apply a previously generated ESMF weight file to ds.
Uses the batched engine in
grid_doctor.remap_apply which replaces per-slice
vectorize=True with a single batched sparse matmul for a
typical 10–50× speedup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
Source dataset to remap. |
required |
weights_path
|
str | Path
|
Path to the NetCDF weight file. |
required |
missing_policy
|
MissingPolicy
|
Strategy for handling NaN (missing) values in the source grid during weight application.
|
'renormalize'
|
grid
|
Dataset | None
|
Optional grid dataset with source geometry. |
None
|
source_dims
|
tuple[str, ...] | None
|
Optional explicit source dimensions. |
None
|
source_units
|
SourceUnits
|
Unit convention for geometry-based inference. |
'auto'
|
backend
|
ApplyBackend
|
Application backend ( |
'auto'
|
Returns:
| Type | Description |
|---|---|
Dataset
|
Dataset on the HEALPix target grid with a |
Examples:
Source code in .tox/docs/lib/python3.13/site-packages/grid_doctor/remap.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 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 | |
Pyramid post-processing¶
After remapping, these helpers can be used to derive coarser representations for building multiresolution HEALPix pyramids.
coarsen_healpix(ds, target_level, coarsen_mode='auto', min_valid_fraction=0.5)
¶
Coarsen a HEALPix dataset to a lower-resolution level.
The coarsening is performed as a single reshape + reduction over all batch dimensions simultaneously — no per-slice Python loops.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ds
|
Dataset
|
HEALPix dataset containing a |
required |
target_level
|
int
|
Target HEALPix level (must be lower than the current level). |
required |
coarsen_mode
|
CoarsenMode
|
|
'auto'
|
min_valid_fraction
|
float
|
Minimum fraction of valid (non-NaN) children required to
produce a valid parent cell. Parents with fewer valid
children are set to NaN. Default |
0.5
|
Returns:
| Type | Description |
|---|---|
Dataset
|
Coarsened dataset. |
Notes
Nested HEALPix indices have a direct parent-child relationship:
pixel i at level L contains children 4*i to 4*i+3 at
level L+1. Coarsening therefore reduces to grouping contiguous
blocks of 4**delta_level child cells and averaging (or taking
the mode for categorical data).
Ring-ordered datasets do not have contiguous parent-child layout and must be remapped directly at each target level.
Raises:
| Type | Description |
|---|---|
ValueError
|
When the ordering is not nested, or target_level is not lower than the current level. |
Source code in .tox/docs/lib/python3.13/site-packages/grid_doctor/helpers.py
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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | |