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 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 grid_doctor/remap.py
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 246 247 | |
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, cell_chunks='auto')
¶
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
|
cell_chunks
|
int | Literal['auto'] | None
|
Output chunk size along |
'auto'
|
Returns:
| Type | Description |
|---|---|
Dataset
|
Regridded dataset on the HEALPix grid. |
Source code in grid_doctor/remap.py
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 | |
apply_weight_file(ds, weights_path, *, missing_policy='renormalize', grid=None, source_dims=None, source_units='auto', backend='auto', cell_chunks='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'
|
cell_chunks
|
int | Literal['auto'] | None
|
Chunk size of the output along |
'auto'
|
Returns:
| Type | Description |
|---|---|
Dataset
|
Dataset on the HEALPix target grid with a |
Examples:
Source code in grid_doctor/remap.py
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 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 | |
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, valid_fraction=False)
¶
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
|
valid_fraction
|
ValidFraction
|
Add |
False
|
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.
With coarsen_mode="mean", always coarsen from the finest level
rather than chaining level by level. A chained mean weights every
valid parent equally, regardless of how many valid finest-level
cells it was built from, so data with NaNs (land, sea ice,
observation gaps) drifts between levels.
Raises:
| Type | Description |
|---|---|
ValueError
|
When the ordering is not nested, or target_level is not lower than the current level. |
Source code in grid_doctor/helpers.py
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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |