Skip to content

Commit 700f846

Browse files
committed
dev
1 parent 73bc323 commit 700f846

File tree

5 files changed

+25
-21
lines changed

5 files changed

+25
-21
lines changed

Changelog.rst

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

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

6-
* Allowed ``'nearest_dtos'`` 2-d regridding to work with discrete
7-
sampling geometry source grids ()
8-
(https://github.com/NCAS-CMS/cf-python/issues/811)
6+
* Allow ``'nearest_dtos'`` 2-d regridding to work with discrete
7+
sampling geometry source grids
8+
(https://github.com/NCAS-CMS/cf-python/issues/832)
99
* New method: `cf.Field.filled`
1010
(https://github.com/NCAS-CMS/cf-python/issues/811)
1111
* New method: `cf.Field.is_discrete_axis`

cf/data/dask_regrid.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,9 @@ def _regrid(
507507
# Note: It is much more efficient to access
508508
# 'weights.indptr', 'weights.indices', and
509509
# 'weights.data' directly, rather than iterating
510-
# over rows of 'weights' and using 'weights.getrow'.
510+
# over rows of 'weights' and using
511+
# 'weights.getrow'. Also, 'np.count_nonzero' is much
512+
# faster than 'np.any' and 'np.all'.
511513
count_nonzero = np.count_nonzero
512514
indptr = weights.indptr.tolist()
513515
indices = weights.indices
@@ -547,7 +549,9 @@ def _regrid(
547549
# Note: It is much more efficient to access
548550
# 'weights.indptr', 'weights.indices', and
549551
# 'weights.data' directly, rather than iterating
550-
# over rows of 'weights' and using 'weights.getrow'.
552+
# over rows of 'weights' and using
553+
# 'weights.getrow'. Also, 'np.count_nonzero' is much
554+
# faster than 'np.any' and 'np.all'.
551555
count_nonzero = np.count_nonzero
552556
where = np.where
553557
indptr = weights.indptr.tolist()
@@ -580,7 +584,9 @@ def _regrid(
580584
# Note: It is much more efficient to access
581585
# 'weights.indptr', 'weights.indices', and
582586
# 'weights.data' directly, rather than iterating
583-
# over rows of 'weights' and using 'weights.getrow'.
587+
# over rows of 'weights' and using
588+
# 'weights.getrow'. Also, 'np.count_nonzero' is much
589+
# faster than 'np.any' and 'np.all'.
584590
count_nonzero = np.count_nonzero
585591
indptr = weights.indptr.tolist()
586592
indices = weights.indices

cf/docstring/docstring.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,9 @@
161161
mapped to the closest destination point. A
162162
destination point can be mapped to multiple source
163163
points. Some destination points may not be
164-
mapped. Each regridded value is the sum of the
165-
contributing source elements. Masked source grid
166-
cells are mapped, but do not contribute to the
167-
regridded values. Useful for binning or for
168-
categorical data.
164+
mapped. Each regridded value is the sum of its
165+
contributing source elements. Useful for binning or
166+
for categorical data.
169167
170168
* `None`: This is the default and can only be used
171169
when *dst* is a `RegridOperator`.""",

cf/regrid/regrid.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,9 @@ def regrid(
674674
)
675675

676676
if return_operator:
677+
# Note: The `RegridOperator.tosparse` method will also set
678+
# 'dst_mask' to False for destination points with all
679+
# zero weights.
677680
regrid_operator.tosparse()
678681
return regrid_operator
679682

cf/test/test_regrid_featureType.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,6 @@
2525
except ImportError:
2626
pass
2727

28-
disallowed_methods = (
29-
"conservative",
30-
"conservative_2nd",
31-
# "nearest_dtos",
32-
)
33-
3428
methods = (
3529
"linear",
3630
"nearest_stod",
@@ -162,7 +156,7 @@ def test_Field_regrid_grid_to_featureType_3d(self):
162156
y = esmpy_regrid(coord_sys, method, src, dst, **kwargs)
163157

164158
self.assertEqual(y.size, a.size)
165-
self.assertTrue(np.allclose(y, a, atol=4e-3, rtol=rtol))
159+
self.assertTrue(np.allclose(y, a, atol=atol, rtol=rtol))
166160

167161
if isinstance(a, np.ma.MaskedArray):
168162
self.assertTrue((y.mask == a.mask).all())
@@ -173,6 +167,7 @@ def test_Field_regrid_grid_to_featureType_3d(self):
173167
def test_Field_regrid_featureType_to_grid_2d(self):
174168
self.assertFalse(cf.regrid_logging())
175169

170+
# Create some nice data
176171
src = self.dst_featureType
177172
src.del_construct("cellmethod0")
178173
src = src[:12]
@@ -191,6 +186,7 @@ def test_Field_regrid_featureType_to_grid_2d(self):
191186
# Mask some destination grid points
192187
dst[0, 0, 1, 2] = cf.masked
193188

189+
# Expected destination regridded values
194190
y0 = np.ma.array(
195191
[[0, 0, 0, 0], [0, 0, 1122, 0], [0, 1114, 0, 0], [1106, 0, 0, 0]],
196192
mask=[
@@ -201,13 +197,14 @@ def test_Field_regrid_featureType_to_grid_2d(self):
201197
],
202198
)
203199

204-
coord_sys = "spherical"
205-
206200
for src_masked in (False, True):
207201
y = y0.copy()
208202
if src_masked:
209203
src = src.copy()
210204
src[6:8] = cf.masked
205+
# This following element should be smaller, because it
206+
# now only has two source cells conrtibuting to it,
207+
# rather than four.
211208
y[3, 0] = 547
212209

213210
# Loop over whether or not to use the destination grid
@@ -327,7 +324,7 @@ def test_Field_regrid_featureType_bad_methods(self):
327324
dst = self.dst_featureType.copy()
328325
src = self.src_grid.copy()
329326

330-
for method in disallowed_methods:
327+
for method in ("conservative", "conservative_2nd"):
331328
with self.assertRaises(ValueError):
332329
src.regrids(dst, method=method)
333330

0 commit comments

Comments
 (0)