|
| 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