Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhassell committed Nov 5, 2024
1 parent 73bc323 commit 700f846
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
6 changes: 3 additions & 3 deletions Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ version NEXTVERSION

**2024-??-??**

* Allowed ``'nearest_dtos'`` 2-d regridding to work with discrete
sampling geometry source grids ()
(https://github.com/NCAS-CMS/cf-python/issues/811)
* Allow ``'nearest_dtos'`` 2-d regridding to work with discrete
sampling geometry source grids
(https://github.com/NCAS-CMS/cf-python/issues/832)
* New method: `cf.Field.filled`
(https://github.com/NCAS-CMS/cf-python/issues/811)
* New method: `cf.Field.is_discrete_axis`
Expand Down
12 changes: 9 additions & 3 deletions cf/data/dask_regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,9 @@ def _regrid(
# Note: It is much more efficient to access
# 'weights.indptr', 'weights.indices', and
# 'weights.data' directly, rather than iterating
# over rows of 'weights' and using 'weights.getrow'.
# over rows of 'weights' and using
# 'weights.getrow'. Also, 'np.count_nonzero' is much
# faster than 'np.any' and 'np.all'.
count_nonzero = np.count_nonzero
indptr = weights.indptr.tolist()
indices = weights.indices
Expand Down Expand Up @@ -547,7 +549,9 @@ def _regrid(
# Note: It is much more efficient to access
# 'weights.indptr', 'weights.indices', and
# 'weights.data' directly, rather than iterating
# over rows of 'weights' and using 'weights.getrow'.
# over rows of 'weights' and using
# 'weights.getrow'. Also, 'np.count_nonzero' is much
# faster than 'np.any' and 'np.all'.
count_nonzero = np.count_nonzero
where = np.where
indptr = weights.indptr.tolist()
Expand Down Expand Up @@ -580,7 +584,9 @@ def _regrid(
# Note: It is much more efficient to access
# 'weights.indptr', 'weights.indices', and
# 'weights.data' directly, rather than iterating
# over rows of 'weights' and using 'weights.getrow'.
# over rows of 'weights' and using
# 'weights.getrow'. Also, 'np.count_nonzero' is much
# faster than 'np.any' and 'np.all'.
count_nonzero = np.count_nonzero
indptr = weights.indptr.tolist()
indices = weights.indices
Expand Down
8 changes: 3 additions & 5 deletions cf/docstring/docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,9 @@
mapped to the closest destination point. A
destination point can be mapped to multiple source
points. Some destination points may not be
mapped. Each regridded value is the sum of the
contributing source elements. Masked source grid
cells are mapped, but do not contribute to the
regridded values. Useful for binning or for
categorical data.
mapped. Each regridded value is the sum of its
contributing source elements. Useful for binning or
for categorical data.
* `None`: This is the default and can only be used
when *dst* is a `RegridOperator`.""",
Expand Down
3 changes: 3 additions & 0 deletions cf/regrid/regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,9 @@ def regrid(
)

if return_operator:
# Note: The `RegridOperator.tosparse` method will also set
# 'dst_mask' to False for destination points with all
# zero weights.
regrid_operator.tosparse()
return regrid_operator

Expand Down
17 changes: 7 additions & 10 deletions cf/test/test_regrid_featureType.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@
except ImportError:
pass

disallowed_methods = (
"conservative",
"conservative_2nd",
# "nearest_dtos",
)

methods = (
"linear",
"nearest_stod",
Expand Down Expand Up @@ -162,7 +156,7 @@ def test_Field_regrid_grid_to_featureType_3d(self):
y = esmpy_regrid(coord_sys, method, src, dst, **kwargs)

self.assertEqual(y.size, a.size)
self.assertTrue(np.allclose(y, a, atol=4e-3, rtol=rtol))
self.assertTrue(np.allclose(y, a, atol=atol, rtol=rtol))

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

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

# Expected destination regridded values
y0 = np.ma.array(
[[0, 0, 0, 0], [0, 0, 1122, 0], [0, 1114, 0, 0], [1106, 0, 0, 0]],
mask=[
Expand All @@ -201,13 +197,14 @@ def test_Field_regrid_featureType_to_grid_2d(self):
],
)

coord_sys = "spherical"

for src_masked in (False, True):
y = y0.copy()
if src_masked:
src = src.copy()
src[6:8] = cf.masked
# This following element should be smaller, because it
# now only has two source cells conrtibuting to it,
# rather than four.
y[3, 0] = 547

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

for method in disallowed_methods:
for method in ("conservative", "conservative_2nd"):
with self.assertRaises(ValueError):
src.regrids(dst, method=method)

Expand Down

0 comments on commit 700f846

Please sign in to comment.