Skip to content

Commit eafc3f2

Browse files
committed
Add pyremap example
1 parent 092cf9f commit eafc3f2

2 files changed

Lines changed: 54 additions & 0 deletions

File tree

04-regrid.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ It helps you compute weights (using `ESMF_RegridWeightGen` or the MOAB CLI[^mbcl
1616
and apply them,
1717
and works with global and limited-area meshes.
1818

19+
pyremap [is available](https://mpas-dev.github.io/pyremap/2.1.0/quick_start.html#installation) on conda-forge.
20+
21+
```{include} pyremap-example.py
22+
:lang: python
23+
24+
```
25+
1926
## NCO
2027

2128
[NCO](https://nco.sourceforge.net/) [ncremap](https://nco.sourceforge.net/nco.html#ncremap-netCDF-Remapper)
@@ -172,6 +179,8 @@ nlon=59
172179
convert_mpas x1.10242.init.nc diag.*.nc
173180
```
174181

182+
Note that the start/end points are cell edges, not centers.
183+
175184
# Recommendations
176185

177186
[](#convert_mpas) for a quick remap, e.g. for checking your data with tools like ncview

pyremap-example.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from pathlib import Path
2+
3+
import xarray as xr
4+
from pyremap import Remapper, get_lat_lon_descriptor
5+
6+
HERE = Path(__file__).parent
7+
8+
mesh_id = "x1.2562"
9+
src_path = HERE / f"{mesh_id}.grid.nc"
10+
out_dir = HERE
11+
in_path = HERE / f"{mesh_id}.static.nc"
12+
variables = ["ter"]
13+
14+
ds_in = xr.open_dataset(in_path, decode_times=False)
15+
16+
dst = get_lat_lon_descriptor(
17+
dlon=0.25,
18+
dlat=0.25,
19+
lon_min=-125,
20+
lon_max=-66,
21+
lat_min=25,
22+
lat_max=50,
23+
)
24+
# Note that the min/max lon/lat are cell edges, not centers.
25+
26+
for method in ["bilinear", "conserve"]:
27+
# Create weights with ESMF_RegridWeightGen
28+
# (It is also possible to use MOAB)
29+
map_path = out_dir / f"{mesh_id}_map_{method}.nc"
30+
remapper = Remapper(map_filename=map_path.as_posix(), method=method, ntasks=1, use_tmp=True)
31+
remapper.src_from_mpas(filename=src_path.as_posix(), mesh_name=mesh_id)
32+
remapper.dst_descriptor = dst
33+
remapper.build_map()
34+
35+
# Apply weights with NCO ncremap, producing a file
36+
remapper.ncremap(
37+
in_filename=(HERE / f"{mesh_id}.static.nc").as_posix(),
38+
out_filename=(out_dir / f"out_{method[:3]}.nc").as_posix(),
39+
overwrite=True,
40+
variable_list=variables,
41+
)
42+
43+
# Apply weights with numpy, returning an xarray Dataset
44+
ds = remapper.remap_numpy(ds_in[variables])
45+
print(ds)

0 commit comments

Comments
 (0)