Skip to content

Commit 32e5baf

Browse files
add bilinear regridding support
1 parent 9e5c6aa commit 32e5baf

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/esmf_regrid/experimental/partition.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
"""Provides an interface for splitting up a large regridding task."""
2-
2+
import esmpy
33
import numpy as np
44

55
from esmf_regrid.constants import Constants
66
from esmf_regrid.experimental._partial import PartialRegridder
77
from esmf_regrid.experimental.io import load_regridder, save_regridder
88
from esmf_regrid.schemes import _get_grid_dims
9+
from pyarrow import dictionary
910

1011

1112
def _get_chunk(cube, sl):
@@ -132,6 +133,12 @@ def __init__(
132133
if scheme._method == Constants.Method.NEAREST:
133134
msg = "The `Nearest` method is not implemented."
134135
raise NotImplementedError(msg)
136+
if scheme._method == Constants.Method.BILINEAR:
137+
pole_method = scheme.esmf_args.get("pole_method")
138+
if pole_method != esmpy.PoleMethod.NONE:
139+
msg = ("Bilinear regridding must have a `pole_method` of `esmpy.PoleMethod.NONE` in "
140+
"the `esmf_args` in order for Partition to work.`")
141+
raise ValueError(msg)
135142
# TODO: Extract a slice of the cube.
136143
self.src = src
137144
if src.mesh is None:

src/esmf_regrid/tests/unit/experimental/partition/test_Partition.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"""Unit tests for :mod:`esmf_regrid.experimental.partition`."""
22

33
import dask.array as da
4+
import esmpy
45
import numpy as np
56
import pytest
67

7-
from esmf_regrid import ESMFAreaWeighted, ESMFNearest
8+
from esmf_regrid import ESMFAreaWeighted, ESMFBilinear, ESMFNearest
89
from esmf_regrid.experimental.partition import Partition
910
from esmf_regrid.tests.unit.schemes.test__cube_to_GridInfo import (
1011
_curvilinear_cube,
@@ -155,6 +156,32 @@ def test_Partition_curv_src(tmp_path):
155156
assert result == expected
156157

157158

159+
def test_Partition_bilinear(tmp_path):
160+
"""Test Partition class for bilinear regridding."""
161+
src = _grid_cube(150, 500, (-180, 180), (-90, 90), circular=True)
162+
src.data = np.arange(150 * 500).reshape([500, 150])
163+
tgt = _grid_cube(16, 36, (-180, 180), (-90, 90), circular=True)
164+
165+
files = [tmp_path / f"partial_{x}.nc" for x in range(5)]
166+
src_chunks = (100, 150)
167+
168+
bad_scheme = ESMFBilinear()
169+
with pytest.raises(ValueError):
170+
_ = Partition(src, tgt, bad_scheme, files, src_chunks=src_chunks)
171+
172+
# The pole_method must be NONE for bilinear regridding partitions to work.
173+
scheme = ESMFBilinear(esmf_args={"pole_method": esmpy.PoleMethod.NONE})
174+
175+
partition = Partition(src, tgt, scheme, files, src_chunks=src_chunks)
176+
177+
partition.generate_files()
178+
179+
result = partition.apply_regridders(src)
180+
expected = src.regrid(tgt, scheme)
181+
assert np.allclose(result.data, expected.data)
182+
assert result == expected
183+
184+
158185
def test_Partition_mesh_tgt(tmp_path):
159186
"""Test Partition class when the target has a mesh."""
160187
src = _grid_cube(150, 500, (-180, 180), (-90, 90), circular=True)

0 commit comments

Comments
 (0)