Skip to content

Commit 393b351

Browse files
authored
Merge pull request #768 from mattjbr123/return_esmpy_regrid_operator
Added return_esmpy_regrid_operator to regrids and regridc per
2 parents e1af68d + 2970ada commit 393b351

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

Changelog.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ version 3.16.2
33

44
**2024-04-??**
55

6+
* New keyword parameter to `cf.Field.regrids` and `cf.Field.regridc`:
7+
``return_esmpy_regrid_operator``
8+
(https://github.com/NCAS-CMS/cf-python/issues/766)
69
* Allow a halo to be added by `cf.Field.indices` and
710
`cf.Field.subspace`
811
(https://github.com/NCAS-CMS/cf-python/issues/759)

cf/docstring/docstring.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,11 @@
651651
operation and define a halo to be
652652
added to the subspaced axes.
653653
============== ======================================""",
654+
# return_esmpy_regrid_operator
655+
"{{return_esmpy_regrid_operator: `bool`, optional}}": """return_esmpy_regrid_operator: `bool`, optional
656+
If True then do not perform the regridding, rather
657+
return the `esmpy.Regrid` instance that defines the
658+
regridding operation.""",
654659
# ----------------------------------------------------------------
655660
# Method description substitutions (4 levels of indentation)
656661
# ----------------------------------------------------------------

cf/field.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13488,6 +13488,7 @@ def regrids(
1348813488
z=None,
1348913489
ln_z=None,
1349013490
verbose=None,
13491+
return_esmpy_regrid_operator=False,
1349113492
inplace=False,
1349213493
i=False,
1349313494
_compute_field_mass=None,
@@ -13728,6 +13729,10 @@ def regrids(
1372813729

1372913730
{{inplace: `bool`, optional}}
1373013731

13732+
{{return_esmpy_regrid_operator: `bool`, optional}}
13733+
13734+
.. versionadded:: 3.16.2
13735+
1373113736
axis_order: sequence, optional
1373213737
Deprecated at version 3.14.0.
1373313738

@@ -13744,7 +13749,8 @@ def regrids(
1374413749
`Field` or `None` or `RegridOperator`
1374513750
The regridded field construct; or `None` if the
1374613751
operation was in-place; or the regridding operator if
13747-
*return_operator* is True.
13752+
*return_operator* is True; or the `esmpy.Regrid` operator
13753+
object if *return_esmpy_regrid_operator* is True.
1374813754

1374913755
**Examples**
1375013756

@@ -13819,6 +13825,7 @@ def regrids(
1381913825
dst_z=dst_z,
1382013826
z=z,
1382113827
ln_z=ln_z,
13828+
return_esmpy_regrid_operator=return_esmpy_regrid_operator,
1382213829
inplace=inplace,
1382313830
)
1382413831

@@ -13842,6 +13849,7 @@ def regridc(
1384213849
dst_z=None,
1384313850
z=None,
1384413851
ln_z=None,
13852+
return_esmpy_regrid_operator=False,
1384513853
inplace=False,
1384613854
i=False,
1384713855
_compute_field_mass=None,
@@ -14017,6 +14025,10 @@ def regridc(
1401714025

1401814026
{{inplace: `bool`, optional}}
1401914027

14028+
{{return_esmpy_regrid_operator: `bool`, optional}}
14029+
14030+
.. versionadded:: 3.16.2
14031+
1402014032
axis_order: sequence, optional
1402114033
Deprecated at version 3.14.0.
1402214034

@@ -14033,7 +14045,8 @@ def regridc(
1403314045
`Field` or `None` or `RegridOperator`
1403414046
The regridded field construct; or `None` if the
1403514047
operation was in-place; or the regridding operator if
14036-
*return_operator* is True.
14048+
*return_operator* is True; or the `esmpy.Regrid` operator
14049+
object if *return_esmpy_regrid_operator* is True.
1403714050

1403814051
**Examples**
1403914052

@@ -14107,6 +14120,7 @@ def regridc(
1410714120
dst_z=dst_z,
1410814121
z=z,
1410914122
ln_z=ln_z,
14123+
return_esmpy_regrid_operator=return_esmpy_regrid_operator,
1411014124
inplace=inplace,
1411114125
)
1411214126

cf/test/test_regrid.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,25 @@ def test_Field_regrid_weights_file(self):
791791
src.regrids(r, method="linear", weights_file=tmpfile)
792792
)
793793

794+
@unittest.skipUnless(esmpy_imported, "Requires esmpy/ESMF package.")
795+
def test_return_esmpy_regrid_operator(self):
796+
"""esmpy regrid operator returns esmpy.Regrid in regrids and regridc"""
797+
dst = self.dst
798+
src = self.src
799+
800+
opers = src.regrids(
801+
dst, method="conservative", return_esmpy_regrid_operator=True
802+
)
803+
operc = src.regridc(
804+
dst,
805+
axes=["Y", "X"],
806+
method="conservative",
807+
return_esmpy_regrid_operator=True,
808+
)
809+
810+
self.assertIsInstance(opers, esmpy.api.regrid.Regrid)
811+
self.assertIsInstance(operc, esmpy.api.regrid.Regrid)
812+
794813

795814
if __name__ == "__main__":
796815
print("Run date:", datetime.datetime.now())

0 commit comments

Comments
 (0)