Skip to content

Commit 8508a85

Browse files
authored
Merge pull request #99 from xylar/reorganize-remapper
A major reorganization of the `Remapper`
2 parents f5d1468 + 8dd9dae commit 8508a85

24 files changed

+2209
-1643
lines changed

Diff for: examples/make_mpas_to_Antarctic_stereo_mapping.py renamed to examples/make_mpas_to_antarctic_stereo_mapping.py

+24-23
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,46 @@
1919
resolution.
2020
"""
2121

22-
import xarray
22+
import xarray as xr
2323

24-
from pyremap import MpasCellMeshDescriptor, Remapper, get_polar_descriptor
24+
from pyremap import Remapper, get_polar_descriptor
2525

2626

2727
# replace with the MPAS mesh name
28-
inGridName = 'oQU240'
28+
src_mesh_name = 'oQU240'
2929

3030
# replace with the path to the desired mesh or restart file
3131
# As an example, use:
3232
# https://web.lcrc.anl.gov/public/e3sm/inputdata/ocn/mpas-o/oQU240/ocean.QU.240km.151209.nc
33-
inGridFileName = 'ocean.QU.240km.151209.nc'
33+
src_mesh_filename = 'ocean.QU.240km.151209.nc'
3434

35-
inDescriptor = MpasCellMeshDescriptor(inGridFileName, inGridName)
35+
# Configure the remapper to build the mapping file using 4 MPI tasks and
36+
# bilinear remapping
37+
remapper = Remapper(ntasks=4, method='bilinear')
3638

37-
# modify the size and resolution of the Antarctic grid as desired
38-
outDescriptor = get_polar_descriptor(Lx=6000., Ly=5000., dx=10., dy=10.,
39-
projection='antarctic')
40-
outGridName = outDescriptor.meshName
39+
remapper.src_from_mpas(filename=src_mesh_filename, mesh_name=src_mesh_name)
4140

42-
mappingFileName = f'map_{inGridName}_to_{outGridName}_bilinear.nc'
41+
# modify the size and resolution of the Antarctic grid as desired
42+
remapper.dst_descriptor = get_polar_descriptor(
43+
lx=6000., ly=5000., dx=10., dy=10., projection='antarctic')
4344

44-
remapper = Remapper(inDescriptor, outDescriptor, mappingFileName)
45+
dst_grid_name = remapper.dst_descriptor.mesh_name
4546

46-
# conservative remapping with 4 MPI tasks (using mpirun)
47-
remapper.esmf_build_map(method='bilinear', mpi_tasks=4)
47+
# build the mapping file
48+
remapper.build_map()
4849

4950
# select the SST at the initial time as an example data set
50-
srcFileName = f'temp_{inGridName}.nc'
51-
ds = xarray.open_dataset(inGridFileName)
52-
dsOut = xarray.Dataset()
53-
dsOut['temperature'] = ds['temperature'].isel(nVertLevels=0, Time=0)
54-
dsOut.to_netcdf(srcFileName)
51+
src_filename = f'temp_{src_mesh_name}.nc'
52+
ds = xr.open_dataset(src_mesh_filename)
53+
ds_out = xr.Dataset()
54+
ds_out['temperature'] = ds['temperature'].isel(nVertLevels=0, Time=0)
55+
ds_out.to_netcdf(src_filename)
5556

5657
# do remapping with ncremap
57-
outFileName = f'temp_{outGridName}_file.nc'
58-
remapper.remap_file(srcFileName, outFileName)
58+
out_filename = f'temp_{dst_grid_name}_file.nc'
59+
remapper.ncremap(src_filename, out_filename)
5960

6061
# do remapping again, this time with python remapping
61-
outFileName = f'temp_{outGridName}_array.nc'
62-
dsOut = remapper.remap(dsOut)
63-
dsOut.to_netcdf(outFileName)
62+
out_filename = f'temp_{dst_grid_name}_array.nc'
63+
ds_out = remapper.remap_numpy(ds_out)
64+
ds_out.to_netcdf(out_filename)

Diff for: examples/make_mpas_to_lat_lon_mapping.py

+27-16
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,45 @@
1919
resolution.
2020
"""
2121

22-
import xarray
22+
import xarray as xr
23+
24+
from pyremap import Remapper, get_lat_lon_descriptor
2325

24-
from pyremap import MpasCellMeshDescriptor, Remapper, get_lat_lon_descriptor
2526

2627
# replace with the MPAS mesh name
27-
inGridName = 'oQU240'
28+
src_mesh_name = 'oQU240'
2829

2930
# replace with the path to the desired mesh or restart file
3031
# As an example, use:
3132
# https://web.lcrc.anl.gov/public/e3sm/inputdata/ocn/mpas-o/oQU240/ocean.QU.240km.151209.nc
32-
inGridFileName = 'ocean.QU.240km.151209.nc'
33+
src_mesh_filename = 'ocean.QU.240km.151209.nc'
34+
35+
# Configure the remapper to build the mapping file using 1 MPI tasks and
36+
# bilinear remapping
37+
remapper = Remapper(ntasks=1, method='bilinear')
3338

34-
inDescriptor = MpasCellMeshDescriptor(inGridFileName, inGridName)
39+
remapper.src_from_mpas(filename=src_mesh_filename, mesh_name=src_mesh_name)
3540

3641
# modify the resolution of the global lat-lon grid as desired
37-
outDescriptor = get_lat_lon_descriptor(dLon=0.5, dLat=0.5)
38-
outGridName = outDescriptor.meshName
42+
remapper.dst_descriptor = get_lat_lon_descriptor(dlon=0.5, dlat=0.5)
3943

40-
mappingFileName = f'map_{inGridName}_to_{outGridName}_bilinear.nc'
44+
dst_grid_name = remapper.dst_descriptor.mesh_name
4145

46+
# build the mapping file
47+
remapper.build_map()
4248

43-
remapper = Remapper(inDescriptor, outDescriptor, mappingFileName)
49+
# select the SST at the initial time as an example data set
50+
src_filename = f'temp_{src_mesh_name}.nc'
51+
ds = xr.open_dataset(src_mesh_filename)
52+
ds_out = xr.Dataset()
53+
ds_out['temperature'] = ds['temperature'].isel(nVertLevels=0, Time=0)
54+
ds_out.to_netcdf(src_filename)
4455

45-
remapper.esmf_build_map(method='bilinear', mpi_tasks=1)
56+
# do remapping with ncremap
57+
out_filename = f'temp_{dst_grid_name}_file.nc'
58+
remapper.ncremap(src_filename, out_filename)
4659

47-
outFileName = f'temp_{outGridName}.nc'
48-
ds = xarray.open_dataset(inGridFileName)
49-
dsOut = xarray.Dataset()
50-
dsOut['temperature'] = ds['temperature']
51-
dsOut = remapper.remap(dsOut)
52-
dsOut.to_netcdf(outFileName)
60+
# do remapping again, this time with python remapping
61+
out_filename = f'temp_{dst_grid_name}_array.nc'
62+
ds_out = remapper.remap_numpy(ds_out)
63+
ds_out.to_netcdf(out_filename)

Diff for: examples/make_mpas_vertex_to_Antarctic_stereo_mapping.py renamed to examples/make_mpas_vertex_to_antarctic_stereo_mapping.py

+25-24
Original file line numberDiff line numberDiff line change
@@ -22,45 +22,46 @@
2222
import numpy as np
2323
import xarray as xr
2424

25-
from pyremap import MpasVertexMeshDescriptor, Remapper, get_polar_descriptor
25+
from pyremap import Remapper, get_polar_descriptor
2626

2727

2828
# replace with the MPAS mesh name
29-
inGridName = 'oQU240'
29+
src_mesh_name = 'oQU240_vertex'
3030

3131
# replace with the path to the desired mesh or restart file
3232
# As an example, use:
3333
# https://web.lcrc.anl.gov/public/e3sm/inputdata/ocn/mpas-o/oQU240/ocean.QU.240km.151209.nc
34-
inGridFileName = 'ocean.QU.240km.151209.nc'
34+
src_mesh_filename = 'ocean.QU.240km.151209.nc'
3535

36-
inDescriptor = MpasVertexMeshDescriptor(inGridFileName, inGridName)
36+
# Configure the remapper to build the mapping file using 4 MPI tasks and
37+
# conservative remapping
38+
remapper = Remapper(ntasks=4, method='conserve')
3739

38-
# modify the size and resolution of the Antarctic grid as desired
39-
outDescriptor = get_polar_descriptor(Lx=6000., Ly=5000., dx=10., dy=10.,
40-
projection='antarctic')
41-
outGridName = outDescriptor.meshName
40+
remapper.src_from_mpas(filename=src_mesh_filename, mesh_name=src_mesh_name,
41+
mesh_type='vertex')
4242

43-
mappingFileName = f'map_{inGridName}_vertex_to_{outGridName}_conserve.nc'
43+
# modify the size and resolution of the Antarctic grid as desired
44+
remapper.dst_descriptor = get_polar_descriptor(
45+
lx=6000., ly=5000., dx=10., dy=10., projection='antarctic')
4446

45-
remapper = Remapper(inDescriptor, outDescriptor, mappingFileName)
47+
dst_grid_name = remapper.dst_descriptor.mesh_name
4648

47-
# conservative remapping with 4 MPI tasks (using mpirun)
48-
remapper.esmf_build_map(method='conserve', mpi_tasks=4,
49-
parallel_exec='mpirun', include_logs=True)
49+
# build the mapping file
50+
remapper.build_map()
5051

5152
# select the SST at the initial time as an example data set
52-
srcFileName = f'vertex_id_{inGridName}.nc'
53-
ds = xr.open_dataset(inGridFileName)
54-
dsOut = xr.Dataset()
55-
dsOut['indexToVertexID'] = ds['indexToVertexID'].astype(float)
56-
dsOut['random'] = ('nVertices', np.random.random(ds.sizes['nVertices']))
57-
dsOut.to_netcdf(srcFileName)
53+
src_filename = f'vertex_id_{src_mesh_name}.nc'
54+
ds = xr.open_dataset(src_mesh_filename)
55+
ds_out = xr.Dataset()
56+
ds_out['indexToVertexID'] = ds['indexToVertexID'].astype(float)
57+
ds_out['random'] = ('nVertices', np.random.random(ds.sizes['nVertices']))
58+
ds_out.to_netcdf(src_filename)
5859

5960
# do remapping with ncremap
60-
outFileName = f'vertex_id_{outGridName}_file.nc'
61-
remapper.remap_file(srcFileName, outFileName)
61+
out_filename = f'vertex_id_{dst_grid_name}_file.nc'
62+
remapper.ncremap(src_filename, out_filename)
6263

6364
# do remapping again, this time with python remapping
64-
outFileName = f'vertex_id_{outGridName}_array.nc'
65-
dsOut = remapper.remap(dsOut)
66-
dsOut.to_netcdf(outFileName)
65+
out_filename = f'vertex_id_{dst_grid_name}_array.nc'
66+
ds_out = remapper.remap_numpy(ds_out)
67+
ds_out.to_netcdf(out_filename)

Diff for: examples/remap_stereographic.py

+34-35
Original file line numberDiff line numberDiff line change
@@ -9,65 +9,64 @@
99
Usage: Copy this script into the main MPAS-Analysis directory (up one level).
1010
"""
1111

12-
import numpy
13-
import xarray
1412
import argparse
1513

14+
import numpy as np
15+
import xarray as xr
16+
1617
from pyremap import Remapper, ProjectionGridDescriptor
1718
from pyremap.polar import get_antarctic_stereographic_projection
1819

1920

2021
parser = argparse.ArgumentParser(
2122
description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
22-
parser.add_argument('-i', dest='inFileName', required=True, type=str,
23-
help="Input file name")
24-
parser.add_argument('-o', dest='outFileName', required=True, type=str,
25-
help="Output file name")
23+
parser.add_argument('-i', dest='in_filename', required=True, type=str,
24+
help='Input file name')
25+
parser.add_argument('-o', dest='out_filename', required=True, type=str,
26+
help='Output file name')
2627
parser.add_argument('-r', dest='resolution', required=True, type=float,
27-
help="Output resolution")
28-
parser.add_argument('-m', dest='method', required=False, default="bilinear",
29-
help="Method: {'bilinear', 'neareststod', 'conserve'}")
30-
parser.add_argument('-t', dest='mpiTasks', required=False, type=int, default=1,
31-
help="Number of MPI tasks (default = 1)")
28+
help='Output resolution')
29+
parser.add_argument('-m', dest='method', required=False, default='bilinear',
30+
help='Method: {"bilinear", "neareststod", "conserve"}')
31+
parser.add_argument('-t', dest='mpi_tasks', required=False, type=int,
32+
default=1,
33+
help='Number of MPI tasks (default = 1)')
3234
args = parser.parse_args()
3335

3436

3537
if args.method not in ['bilinear', 'neareststod', 'conserve']:
3638
raise ValueError(f'Unexpected method {args.method}')
3739

38-
dsIn = xarray.open_dataset(args.inFileName)
40+
ds_in = xr.open_dataset(args.in_filename)
3941

40-
x = dsIn.x.values
41-
y = dsIn.y.values
42-
dx = int((x[1] - x[0]) / 1000.)
43-
Lx = int((x[-1] - x[0]) / 1000.)
44-
Ly = int((y[-1] - y[0]) / 1000.)
42+
x = ds_in.x.values
43+
y = ds_in.y.values
44+
dx = int((x[1] - x[0]) / 1000.0)
45+
lx = int((x[-1] - x[0]) / 1000.0)
46+
ly = int((y[-1] - y[0]) / 1000.0)
4547

46-
inMeshName = f'{Lx}x{Ly}km_{dx}km_Antarctic_stereo'
48+
src_mesh_name = f'{lx}x{ly}km_{dx}km_Antarctic_stereo'
4749

4850
projection = get_antarctic_stereographic_projection()
4951

50-
inDescriptor = ProjectionGridDescriptor.create(projection, x, y, inMeshName)
51-
52-
outRes = args.resolution * 1e3
53-
54-
nxOut = int((x[-1] - x[0]) / outRes + 0.5) + 1
55-
nyOut = int((y[-1] - y[0]) / outRes + 0.5) + 1
56-
57-
xOut = x[0] + outRes * numpy.arange(nxOut)
58-
yOut = y[0] + outRes * numpy.arange(nyOut)
52+
remapper = Remapper(ntasks=args.mpi_tasks, method=args.method)
53+
remapper.src_descriptor = ProjectionGridDescriptor.create(
54+
projection, x, y, src_mesh_name)
5955

56+
out_res = args.resolution * 1e3
6057

61-
outMeshName = f'{Lx}x{Ly}km_{args.resolution}km_Antarctic_stereo'
58+
nx_out = int((x[-1] - x[0]) / out_res + 0.5) + 1
59+
ny_out = int((y[-1] - y[0]) / out_res + 0.5) + 1
6260

63-
outDescriptor = ProjectionGridDescriptor.create(projection, xOut, yOut,
64-
outMeshName)
61+
x_out = x[0] + out_res * np.arange(nx_out)
62+
y_out = y[0] + out_res * np.arange(ny_out)
6563

66-
mappingFileName = f'map_{inMeshName}_to_{outMeshName}_{args.method}.nc'
64+
dst_mesh_name = f'{lx}x{ly}km_{args.resolution}km_Antarctic_stereo'
6765

68-
remapper = Remapper(inDescriptor, outDescriptor, mappingFileName)
66+
remapper.dst_descriptor = ProjectionGridDescriptor.create(
67+
projection, x_out, y_out, dst_mesh_name)
6968

70-
remapper.esmf_build_map(method=args.method, mpi_tasks=args.mpiTasks)
69+
remapper.build_map()
7170

72-
dsOut = remapper.remap(dsIn, renormalizationThreshold=0.01)
73-
dsOut.to_netcdf(args.outFileName)
71+
ds_out = remapper.remap_numpy(ds_in, renormalization_threshold=0.01)
72+
ds_out.to_netcdf(args.out_filename)

0 commit comments

Comments
 (0)