Utilities¶
XRegrid provides several utility functions for creating standard grids and loading ESMF-formatted files.
Grid Generation¶
create_global_grid¶
xregrid.create_global_grid(res_lat, res_lon, add_bounds=True, chunks=None)
¶
Create a global rectilinear grid dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
res_lat
|
float
|
Latitude resolution in degrees. |
required |
res_lon
|
float
|
Longitude resolution in degrees. |
required |
add_bounds
|
bool
|
Whether to add cell boundary coordinates. |
True
|
chunks
|
int or dict
|
Chunk sizes for the resulting dask-backed dataset. If None (default), returns an eager NumPy-backed dataset. |
None
|
Returns:
| Type | Description |
|---|---|
Dataset
|
The global grid dataset containing 'lat' and 'lon'. |
Source code in src/xregrid/utils.py
Create a global rectilinear grid dataset with a specified resolution.
from xregrid import create_global_grid
# Create a 1x1 degree global grid with bounds
ds = create_global_grid(res_lat=1.0, res_lon=1.0)
create_regional_grid¶
xregrid.create_regional_grid(lat_range, lon_range, res_lat, res_lon, add_bounds=True, chunks=None)
¶
Create a regional rectilinear grid dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lat_range
|
tuple of float
|
(min_lat, max_lat). |
required |
lon_range
|
tuple of float
|
(min_lon, max_lon). |
required |
res_lat
|
float
|
Latitude resolution in degrees. |
required |
res_lon
|
float
|
Longitude resolution in degrees. |
required |
add_bounds
|
bool
|
Whether to add cell boundary coordinates. |
True
|
chunks
|
int or dict
|
Chunk sizes for the resulting dask-backed dataset. If None (default), returns an eager NumPy-backed dataset. |
None
|
Returns:
| Type | Description |
|---|---|
Dataset
|
The regional grid dataset containing 'lat' and 'lon'. |
Source code in src/xregrid/utils.py
Create a regional rectilinear grid dataset for a specific geographic bounding box.
from xregrid import create_regional_grid
# Create a regional grid over Europe
ds = create_regional_grid(
lat_range=(35, 70),
lon_range=(-10, 40),
res_lat=0.25,
res_lon=0.25
)
create_grid_from_crs¶
xregrid.create_grid_from_crs(crs, extent, res, add_bounds=True, chunks=None)
¶
Create a structured grid dataset from a CRS and extent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
crs
|
str, int, or pyproj.CRS
|
The CRS of the grid (Proj4 string, EPSG code, WKT, or CRS object). |
required |
extent
|
tuple of float
|
Grid extent in CRS units: (min_x, max_x, min_y, max_y). |
required |
res
|
float or tuple of float
|
Grid resolution in CRS units. If float, same resolution in x and y. If tuple, (res_x, res_y). |
required |
add_bounds
|
bool
|
Whether to add cell boundary coordinates. |
True
|
chunks
|
int or dict
|
Chunk sizes for the resulting dask-backed dataset. If None (default), returns an eager NumPy-backed dataset. |
None
|
Returns:
| Type | Description |
|---|---|
Dataset
|
The grid dataset containing 'lat', 'lon' and projected coordinates 'x', 'y'. |
Source code in src/xregrid/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 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 620 621 622 623 624 625 626 627 628 629 630 | |
Create a structured grid dataset from a Coordinate Reference System (CRS) and extent.
from xregrid import create_grid_from_crs
# Create a Lambert Conformal Conic grid over North America
extent = (-2500000, 2500000, -2000000, 2000000)
res = (12000, 12000) # 12km
crs = "+proj=lcc +lat_1=33 +lat_2=45 +lat_0=40 +lon_0=-97 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs"
ds = create_grid_from_crs(crs, extent, res)
create_grid_from_ioapi¶
xregrid.create_grid_from_ioapi(metadata, add_bounds=True, chunks=None)
¶
Create a structured grid dataset from IOAPI-compliant metadata.
Supports GDTYP: - 1: Lat-Lon - 2: Lambert Conformal - 5: Polar Stereographic - 6: Albers Equal Area - 7: Mercator
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
dict
|
IOAPI metadata containing GDTYP, P_ALP, P_BET, P_GAM, XCENT, YCENT, XORIG, YORIG, XCELL, YCELL, NCOLS, NROWS. |
required |
add_bounds
|
bool
|
Whether to add cell boundary coordinates. |
True
|
chunks
|
int or dict
|
Chunk sizes for the resulting dask-backed dataset. |
None
|
Returns:
| Type | Description |
|---|---|
Dataset
|
The grid dataset. |
Source code in src/xregrid/utils.py
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 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 | |
Create a structured grid dataset from IOAPI-compliant metadata.
from xregrid.utils import create_grid_from_ioapi
metadata = {
"GDTYP": 2,
"P_ALP": 30.0,
"P_BET": 60.0,
"XCENT": -97.0,
"YCENT": 40.0,
"XORIG": -1000.0,
"YORIG": -1000.0,
"XCELL": 500.0,
"YCELL": 500.0,
"NCOLS": 100,
"NROWS": 100,
}
ds = create_grid_from_ioapi(metadata)
ESMF File Support¶
load_esmf_file¶
xregrid.load_esmf_file(filepath)
¶
Load an ESMF mesh, mosaic, or grid file into an xarray Dataset.
Automatically recognizes SCRIP/ESMF standard variable names and renames them to 'lat', 'lon', 'lat_b', 'lon_b' while adding CF attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to the ESMF file. |
required |
Returns:
| Type | Description |
|---|---|
Dataset
|
The dataset representation of the ESMF file. |
Source code in src/xregrid/utils.py
Load an ESMF mesh, mosaic, or grid file into an xarray Dataset.