From 70f3c25a5ea2a90895d7417d8fdc65dc9cc45694 Mon Sep 17 00:00:00 2001 From: stephenworsley <49274989+stephenworsley@users.noreply.github.com> Date: Wed, 30 Aug 2023 14:59:36 +0100 Subject: [PATCH] Fix start index bug (#301) * fix start index bug * extend tests, update changelog --- CHANGELOG.md | 9 +++++++++ esmf_regrid/experimental/unstructured_regrid.py | 4 +++- .../test_GridToMeshESMFRegridder.py | 7 ++++--- .../test_MeshToGridESMFRegridder.py | 7 ++++--- .../tests/unit/schemes/test__mesh_to_MeshInfo.py | 10 +++++----- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49feebc2..34d37401 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/esmf_regrid/experimental/unstructured_regrid.py b/esmf_regrid/experimental/unstructured_regrid.py index e03d3d46..397b8a15 100644 --- a/esmf_regrid/experimental/unstructured_regrid.py +++ b/esmf_regrid/experimental/unstructured_regrid.py @@ -75,9 +75,11 @@ 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 " @@ -85,7 +87,7 @@ def __init__( ) super().__init__( shape=shape, - index_offset=self.esi, + index_offset=index_offset, field_kwargs=field_kwargs, mask=mask, ) diff --git a/esmf_regrid/tests/unit/experimental/unstructured_scheme/test_GridToMeshESMFRegridder.py b/esmf_regrid/tests/unit/experimental/unstructured_scheme/test_GridToMeshESMFRegridder.py index 1704f9e5..03978e68 100644 --- a/esmf_regrid/tests/unit/experimental/unstructured_scheme/test_GridToMeshESMFRegridder.py +++ b/esmf_regrid/tests/unit/experimental/unstructured_scheme/test_GridToMeshESMFRegridder.py @@ -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`. @@ -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. diff --git a/esmf_regrid/tests/unit/experimental/unstructured_scheme/test_MeshToGridESMFRegridder.py b/esmf_regrid/tests/unit/experimental/unstructured_scheme/test_MeshToGridESMFRegridder.py index 2e9be356..246dea3a 100644 --- a/esmf_regrid/tests/unit/experimental/unstructured_scheme/test_MeshToGridESMFRegridder.py +++ b/esmf_regrid/tests/unit/experimental/unstructured_scheme/test_MeshToGridESMFRegridder.py @@ -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`. @@ -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) diff --git a/esmf_regrid/tests/unit/schemes/test__mesh_to_MeshInfo.py b/esmf_regrid/tests/unit/schemes/test__mesh_to_MeshInfo.py index 15534a3e..d998b80e 100644 --- a/esmf_regrid/tests/unit/schemes/test__mesh_to_MeshInfo.py +++ b/esmf_regrid/tests/unit/schemes/test__mesh_to_MeshInfo.py @@ -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. @@ -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, @@ -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)