Skip to content

Commit

Permalink
Fix start index bug (#301)
Browse files Browse the repository at this point in the history
* fix start index bug

* extend tests, update changelog
  • Loading branch information
stephenworsley committed Aug 30, 2023
1 parent 92ea6f6 commit 70f3c25
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Fixed

- [PR#301](https://github.com/SciTools-incubator/iris-esmf-regrid/pull/301)
Fixed a bug which caused errors when regridding with the node locations
of a mesh whose face_node_connectivity had non-zero start_index.
[@stephenworsley](https://github.com/stephenworsley)

## [0.8] - 2023-08-22

### Added
Expand Down
4 changes: 3 additions & 1 deletion esmf_regrid/experimental/unstructured_regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,19 @@ def __init__(
if location == "face":
field_kwargs = {"meshloc": esmpy.MeshLoc.ELEMENT}
shape = (len(face_node_connectivity),)
index_offset = self.esi
elif location == "node":
field_kwargs = {"meshloc": esmpy.MeshLoc.NODE}
shape = (len(node_coords),)
index_offset = self.nsi
else:
raise ValueError(
f"The mesh location '{location}' is not supported, only "
f"'face' and 'node' are supported."
)
super().__init__(
shape=shape,
index_offset=self.esi,
index_offset=index_offset,
field_kwargs=field_kwargs,
mask=mask,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ def test_flat_cubes():
assert expected_cube == result_transposed


@pytest.mark.parametrize("nsi", [0, 1])
@pytest.mark.parametrize("method", ["bilinear", "nearest"])
def test_node_friendly_methods(method):
def test_node_friendly_methods(method, nsi):
"""
Basic test for :class:`esmf_regrid.experimental.unstructured_scheme.GridToMeshESMFRegridder`.
Expand All @@ -86,8 +87,8 @@ def test_node_friendly_methods(method):
lon_bounds = (-180, 180)
lat_bounds = (-90, 90)
src = _grid_cube(n_lons, n_lats, lon_bounds, lat_bounds, circular=True)
face_tgt = _gridlike_mesh_cube(n_lons, n_lats, location="face")
node_tgt = _gridlike_mesh_cube(n_lons, n_lats, location="node")
face_tgt = _gridlike_mesh_cube(n_lons, n_lats, location="face", nsi=nsi)
node_tgt = _gridlike_mesh_cube(n_lons, n_lats, location="node", nsi=nsi)

src = _add_metadata(src)
src.data[:] = 1 # Ensure all data in the source is one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ def test_flat_cubes():
assert expected_cube == result


@pytest.mark.parametrize("nsi", [0, 1])
@pytest.mark.parametrize("method", ["bilinear", "nearest"])
def test_node_friendly_methods(method):
def test_node_friendly_methods(method, nsi):
"""
Basic test for :class:`esmf_regrid.experimental.unstructured_scheme.MeshToGridESMFRegridder`.
Expand All @@ -80,8 +81,8 @@ def test_node_friendly_methods(method):
lon_bounds = (-180, 180)
lat_bounds = (-90, 90)
tgt = _grid_cube(n_lons, n_lats, lon_bounds, lat_bounds, circular=True)
face_src = _gridlike_mesh_cube(n_lons, n_lats, location="face")
node_src = _gridlike_mesh_cube(n_lons, n_lats, location="node")
face_src = _gridlike_mesh_cube(n_lons, n_lats, location="face", nsi=nsi)
node_src = _gridlike_mesh_cube(n_lons, n_lats, location="node", nsi=nsi)

face_src = _add_metadata(face_src)
node_src = _add_metadata(node_src)
Expand Down
10 changes: 5 additions & 5 deletions esmf_regrid/tests/unit/schemes/test__mesh_to_MeshInfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _example_mesh():
return mesh


def _gridlike_mesh(n_lons, n_lats):
def _gridlike_mesh(n_lons, n_lats, nsi=0):
"""
Generate a global mesh with geometry similar to a rectilinear grid.
Expand Down Expand Up @@ -159,9 +159,9 @@ def _gridlike_mesh(n_lons, n_lats):

# Translate the mesh information into iris objects.
fnc = Connectivity(
fnc_ma,
fnc_ma + nsi,
cf_role="face_node_connectivity",
start_index=0,
start_index=nsi,
)
enc = Connectivity(
enc_array,
Expand Down Expand Up @@ -191,8 +191,8 @@ def _gridlike_mesh(n_lons, n_lats):
return mesh


def _gridlike_mesh_cube(n_lons, n_lats, location="face"):
mesh = _gridlike_mesh(n_lons, n_lats)
def _gridlike_mesh_cube(n_lons, n_lats, location="face", nsi=0):
mesh = _gridlike_mesh(n_lons, n_lats, nsi=nsi)
mesh_coord_x, mesh_coord_y = mesh.to_MeshCoords(location)
data = np.zeros_like(mesh_coord_x.points)
cube = Cube(data)
Expand Down

0 comments on commit 70f3c25

Please sign in to comment.