Skip to content

Commit 0c84d90

Browse files
committed
dev
1 parent dd5df6a commit 0c84d90

1 file changed

Lines changed: 69 additions & 121 deletions

File tree

cf/regrid/regrid.py

Lines changed: 69 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,22 @@ class Grid:
5555
# expected by `Data._regrid`. E.g. ['domainaxis3', 'domainaxis2']
5656
axis_keys: list = field(default_factory=list)
5757
# The positions of the regrid axes, in the order expected by
58-
# `Data._regrid`. E.g. [3, 2]
58+
# `Data._regrid`. E.g. [3, 2] or [3]
5959
axis_indices: list = field(default_factory=list)
60-
# The value of the *src_axes* or *dst_axes* parameter, as
61-
# appropriate.
62-
axes: Any = None
6360
# The domain axis identifiers of the regridding axes. E.g. {'X':
6461
# 'domainaxis2', 'Y': 'domainaxis1'} or ['domainaxis1',
65-
# 'domainaxis2']
62+
# 'domainaxis2'], or {'M': 'domainaxis3'}
63+
axes: Any = None
64+
# The shape of the regridding axes, in the same order as the
65+
# 'axis_keys' attribute. E.g. (96, 73) or (1243,)
6666
shape: tuple = None
6767
# The regrid axis coordinates, in the order expected by
68-
# `esmpy.Grid`. If the coordinates are 2-d (or more) then the axis
69-
# order of each coordinate object must be as expected by
70-
# `esmpy.Grid`. E.g. (73, 96)
68+
# `esmpy`. If the coordinates are 2-d (or more) then the axis
69+
# order of each coordinate object must be as expected by `esmpy`.
7170
coords: list = field(default_factory=list)
72-
# Only used if `mesh` is False. The regrid axis coordinate bounds,
73-
# in the order expected by `esmpy.Grid`. If the coordinates are
74-
# 2-d (or more) then the axis order of each bounds object must be
75-
# as expected by `esmpy.Grid`.
71+
# The regrid axis coordinate bounds, in the order expected by
72+
# `esmpy`. If the coordinates are 2-d (or more) then the axis
73+
# order of each bounds object must be as expected by `esmpy`.
7674
bounds: list = field(default_factory=list)
7775
# Only used if `mesh` is False. For spherical regridding, whether
7876
# or not the longitude axis is cyclic.
@@ -934,15 +932,22 @@ def spherical_grid(f, name=None, method=None, cyclic=None, axes=None):
934932
coords[dim] = coords[dim].transpose(esmpy_order)
935933

936934
# Set cyclicity of X axis
937-
if cyclic is None:
935+
if mesh:
936+
cyclic = None
937+
elif cyclic is None:
938938
cyclic = f.iscyclic(x_axis)
939-
939+
else:
940+
cyclic = bool(cylcic)
941+
940942
axis_keys = [y_axis, x_axis]
941-
axis_sizes = (y_size, x_size)
943+
shape = (y_size, x_size)
942944
if mesh:
943945
axis_keys = axis_keys[0]
944-
axis_sizes = axis_sizes[0]
945-
946+
shape = axis_sizes[0]
947+
axes = {"X": x_axis, "Y": y_axis}
948+
else:
949+
axes = {"M": x_axis}
950+
946951
if f.construct_type == "domain":
947952
axis_indices = [0, 1]
948953
else:
@@ -961,11 +966,11 @@ def spherical_grid(f, name=None, method=None, cyclic=None, axes=None):
961966
return Grid(
962967
axis_keys=axis_keys,
963968
axis_indices=axis_indices,
964-
axes={"X": x_axis, "Y": y_axis},
965-
shape=axis_sizes,
969+
axes=axes,
970+
shape=shape,
966971
coords=coords,
967972
bounds=get_bounds(method, coords, mesh_location),
968-
cyclic=bool(cyclic),
973+
cyclic=cyclic,
969974
coord_sys="spherical",
970975
method=method,
971976
name=name,
@@ -974,85 +979,8 @@ def spherical_grid(f, name=None, method=None, cyclic=None, axes=None):
974979
mesh_location=mesh_location
975980
)
976981

977-
#def spherical_mesh(f, name=None, method=None):
978-
# """Get latitude and longitude coordinate information.
979-
#
980-
# Retrieve the latitude and longitude coordinates of a field, as
981-
# well as some associated information. If 1-d lat/lon coordinates
982-
# are found then these are returned. Otherwise if 2-d lat/lon
983-
# coordinates found then these are returned. TODOUGRID
984-
#
985-
# .. versionadded:: UGRIDVER
986-
#
987-
# .. seealso:: `Cartesian_mesh`
988-
#
989-
# :Parameters:
990-
#
991-
# f: `Field` or `Domain`
992-
# The construct from which to get the grid information.
993-
#
994-
# name: `str`
995-
# A name to identify the mesh.
996-
#
997-
# method: `str`
998-
# The regridding method.
999-
#
1000-
# :Returns:
1001-
#
1002-
# `Grid`
1003-
# The mesh topology definition.
1004-
#
1005-
# """
1006-
# dt_key, domain_topology = f.domain_topology(item=True, default=(None, None))
1007-
# if domain_topology is None:
1008-
# raise ValueError(
1009-
# "Could not find 1-d latitude and longitude mesh coordinates: "
1010-
# "Missing domain topology construct"
1011-
# )
1012-
#
1013-
# mesh_axis = f.get_data_axes(dt_key)
1014-
#
1015-
# # Look for 1-d X and Y auxiliary coordinates
1016-
# lon = f.auxiliary_coordinate("X", filter_by_axis=mesh_axis, axis_mode='exact', default=None)
1017-
# lat = f.auxiliary_coordinate("Y", filter_by_axis=mesh_axis, axis_mode='exact', default=None)
1018-
#
1019-
# if lon is None or not (lon.Units.islongitude or lon.get_property('standard_name', None) == 'grid_longitude'):
1020-
# raise ValueError(
1021-
# "Could not find 1-d longitude mesh coordinates"
1022-
# )
1023-
#
1024-
# if lat is None or not (lat.Units.islatitude or lat.get_property('standard_name', None) == 'grid_latitude'):
1025-
# raise ValueError(
1026-
# "Could not find 1-d latitude mesh coordinates"
1027-
# )
1028-
#
1029-
# if f.construct_type == "domain":
1030-
# axis_indices = [0]
1031-
# else:
1032-
# # Make sure that the data array spans the regridding
1033-
# # axis. This might change 'f' in-place.
1034-
# data_axes = f.get_data_axes()
1035-
# if mesh_axis[0] not in f.get_data_axes():
1036-
# f.insert_dimension(mesh_axis[0], position=-1, inplace=True)
1037-
# data_axes = f.get_data_axes()
1038-
#
1039-
# # Find the index of the regridding axis
1040-
# axis_indices = [data_axes.index(mesh_axis]]
1041-
#
1042-
# return Grid(
1043-
# mesh=True,
1044-
# axis_keys=mesh_axis,
1045-
# axis_indices=axis_indices,
1046-
# shape=lon.shape,
1047-
# coords=[lon, lat], # esmpy order
1048-
# coord_sys="spherical",
1049-
# method=method,
1050-
# name=name,
1051-
# domain_topology=domain_topology,
1052-
# )
1053-
1054-
1055-
def Cartesian_grid(f, name=None, method=None, axes=None, mesh=False):
982+
983+
def Cartesian_grid(f, name=None, method=None, axes=None):
1056984
"""Retrieve the specified Cartesian dimension coordinates of the
1057985
field and their corresponding keys.
1058986
@@ -1073,17 +1001,12 @@ def Cartesian_grid(f, name=None, method=None, axes=None, mesh=False):
10731001
Specifiers for the dimension coordinates to be
10741002
retrieved. See `cf.Field.domain_axes` for details.
10751003
1076-
mesh: TODOUGRID
1077-
10781004
:Returns:
10791005
10801006
`Grid`
10811007
The grid definition.
10821008
10831009
"""
1084-
if mesh:
1085-
return Cartesian_mesh(f, name=name, method=method, axes=axes)
1086-
10871010
if not axes:
10881011
if name == "source":
10891012
raise ValueError(
@@ -1116,6 +1039,16 @@ def Cartesian_grid(f, name=None, method=None, axes=None, mesh=False):
11161039
axis_keys.append(key)
11171040
axis_sizes.append(domain_axis.size)
11181041

1042+
# mesh = False
1043+
# domain_topology, mesh_axis, mesh_location = ffff(f, name)
1044+
# if mesh_axis is not None:
1045+
# if [mesh_axis] == axis_keys:
1046+
# mesh = True
1047+
# elif mesh_axis in axis_keys:
1048+
# raise ValueError(
1049+
# "TODOUGRID Can't regrid a combo of mesh and non-mesh axes"
1050+
# )
1051+
11191052
if f.construct_type == "domain":
11201053
axis_indices = list(range(len(axis_keys)))
11211054
else:
@@ -1131,16 +1064,25 @@ def Cartesian_grid(f, name=None, method=None, axes=None, mesh=False):
11311064
data_axes = f.get_data_axes()
11321065
axis_indices = [data_axes.index(key) for key in axis_keys]
11331066

1134-
coords = []
1135-
for key in axis_keys[::-1]:
1136-
coord = f.dimension_coordinate(filter_by_axis=(key,), default=None)
1137-
if coord is None:
1138-
raise ValueError(
1139-
f"No unique {name} dimension coordinate for domain axis "
1140-
f"{key!r}."
1141-
)
1067+
mesh = False
1068+
domain_topology, mesh_axis, mesh_location = ffff(f, name)
11421069

1143-
coords.append(coord)
1070+
cyclic = False
1071+
coords = []
1072+
if domain_topology is not None:
1073+
# TODOUGRID: check for the mesh spanning the two axes
1074+
# ... somehow. Also maybe set mesh=True and cyclic=None.
1075+
#
1076+
else:
1077+
for key in axis_keys[::-1]:
1078+
coord = f.dimension_coordinate(filter_by_axis=(key,), default=None)
1079+
if coord is None:
1080+
raise ValueError(
1081+
f"No unique {name} dimension coordinate for domain axis "
1082+
f"{key!r}."
1083+
)
1084+
1085+
coords.append(coord)
11441086

11451087
bounds = get_bounds(method, coords, mesh_location)
11461088

@@ -1166,11 +1108,13 @@ def Cartesian_grid(f, name=None, method=None, axes=None, mesh=False):
11661108
shape=tuple(axis_sizes),
11671109
coords=coords,
11681110
bounds=bounds,
1169-
cyclic=False,
1111+
cyclic=cyclic,
11701112
coord_sys="Cartesian",
11711113
method=method,
11721114
name=name,
11731115
dummy_size_2_dimension=dummy_size_2_dimension,
1116+
mesh=mesh,
1117+
domain_topology = domain_topology,
11741118
)
11751119

11761120
def Cartesian_mesh(f, name=None, method=None):
@@ -1369,14 +1313,15 @@ def esmpy_initialise():
13691313

13701314
return esmpy.Manager(debug=bool(regrid_logging()))
13711315

1372-
def create_esmpy_grid(grid=None, mask=None):
1373-
"""Create an `esmpy` Grid or Mesh
1316+
def create_esmpy_grid(grid, mask=None):
1317+
"""Create an `esmpy.Grid` or `esmpy.Mesh`.
13741318
13751319
.. versionadded:: 3.14.0
13761320
13771321
:Parameters:
13781322
13791323
grid: `Grid`
1324+
TODOUGRID
13801325
13811326
mask: array_like, optional
13821327
The grid mask. If `None` (the default) then there are no
@@ -1387,6 +1332,7 @@ def create_esmpy_grid(grid=None, mask=None):
13871332
:Returns:
13881333
13891334
`esmpy.Grid` or `esmpy.Mesh`
1335+
TODOUGRID
13901336
13911337
"""
13921338
if g.mesh:
@@ -1594,8 +1540,8 @@ def create_esmpy_grid(grid=None, mask=None):
15941540
return esmpy_grid
15951541

15961542

1597-
def create_esmpy_mesh(grid=None, mask=None):
1598-
"""Create an `esmpy` Mesh.
1543+
def create_esmpy_mesh(grid, mask=None):
1544+
"""Create an `esmpy.Mesh`.
15991545
16001546
.. versionadded:: UGRIDVER
16011547
@@ -1604,6 +1550,7 @@ def create_esmpy_mesh(grid=None, mask=None):
16041550
:Parameters:
16051551
16061552
grid: `Grid`
1553+
TODOUGRID
16071554
16081555
mask: array_like, optional
16091556
The mesh mask. If `None` (the default) then there are no
@@ -1613,6 +1560,7 @@ def create_esmpy_mesh(grid=None, mask=None):
16131560
:Returns:
16141561
16151562
`esmpy.Mesh`
1563+
TODOUGRID
16161564
16171565
"""
16181566
if grid.coord_sys == "spherical":
@@ -2275,8 +2223,8 @@ def update_non_coordinates(
22752223
def ffff(f, name):
22762224
"""TODOUGRID"""
22772225
key, domain_topology = f.domain_topology(item=True, default=(None, None))
2278-
if domain_topology is None:
2279-
raise ValueError("TODOUGRID")
2226+
if domain_topology is None:
2227+
return (None, None, None)
22802228

22812229
mesh_location = domain_topology.get_cell(None)
22822230
if mesh_location != "face":
@@ -2285,4 +2233,4 @@ def ffff(f, name):
22852233
f"of {mesh_location!r} cells"
22862234
)
22872235

2288-
return domain_topology, f.get_data_axes(key)[0], mesh_location
2236+
return (domain_topology, f.get_data_axes(key)[0], mesh_location )

0 commit comments

Comments
 (0)