Skip to content

Commit

Permalink
Merge branch 'main' of github.com:NCAS-CMS/cf-python into ugrid-0
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhassell committed Sep 21, 2023
2 parents 094a60a + 88febb7 commit e94cd3d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
12 changes: 11 additions & 1 deletion Changelog.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
version 3.15.4
--------------

**2023-??-??**

* Fix bug in `cf.Field.match_by_construct` that always returned True for
1-d constructs whose axis is not in the data, even when the
criterion was not matched
(https://github.com/NCAS-CMS/cf-python/issues/691)

version 3.15.3
--------------

Expand Down Expand Up @@ -54,7 +64,7 @@ version 3.15.1
(https://github.com/NCAS-CMS/cf-python/pull/654)
* Fix `set_data` when the units are invalid
(https://github.com/NCAS-CMS/cf-python/pull/646)
* Fix `cf.Field.laplacian_xy`, cf.Field.grad_xy`, `cf.curl_xy` and
* Fix `cf.Field.laplacian_xy`, `cf.Field.grad_xy`, `cf.curl_xy` and
`cf.div_xy` to work in cases when the dimension coordinates are
missing standard names
(https://github.com/NCAS-CMS/cf-python/pull/643)
Expand Down
19 changes: 18 additions & 1 deletion cf/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -10324,7 +10324,24 @@ def indices(self, *mode, **kwargs):
indices = []

# Add the indices that apply to the field's data dimensions
indices.extend([domain_indices["indices"][axis] for axis in data_axes])
axis_indices = domain_indices["indices"]
indices.extend([axis_indices[axis] for axis in data_axes])

# Check that there are no invalid indices for size 1 axes not
# spanned by the data
if len(axis_indices) > len(data_axes):
for axis, index in axis_indices.items():
if axis in data_axes or index == slice(None):
continue

import dask.array as da

shape = da.from_array([0])[index].compute_chunk_sizes().shape
if 0 in shape:
raise IndexError(
"Can't create size 0 indices for the size 1 "
f"{self.constructs.domain_axis_identity(axis)!r} axis"
)

return tuple(indices)

Expand Down
11 changes: 6 additions & 5 deletions cf/mixin/fielddomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,8 @@ def _indices(self, mode, data_axes, ancillary_mask, kwargs):

if debug:
logger.debug(
f" item_axes = {item_axes!r}\n keys = {keys!r}"
f" item_axes = {item_axes!r}\n"
f" keys = {keys!r}"
) # pragma: no cover

if n_axes == 1:
Expand All @@ -369,9 +370,9 @@ def _indices(self, mode, data_axes, ancillary_mask, kwargs):
if isinstance(value, (list, slice, tuple, np.ndarray)):
# 1-d CASE 1: Value is already an index, e.g. [0],
# [7,4,2], slice(0,4,2),
# numpy.array([2,4,7]), [True, False,
# True]

# numpy.array([2,4,7]),
# [True,False,True]
index = value
if debug:
logger.debug(" 1-d CASE 1:") # pragma: no cover

Expand Down Expand Up @@ -475,7 +476,7 @@ def _indices(self, mode, data_axes, ancillary_mask, kwargs):

if debug:
logger.debug(
f" index = {index}\n ind = {ind}"
f" index = {index}\n ind = {ind}"
) # pragma: no cover

# Put the index into the correct place in the list of
Expand Down
9 changes: 8 additions & 1 deletion cf/test/test_Field.py
Original file line number Diff line number Diff line change
Expand Up @@ -1637,7 +1637,9 @@ def test_Field_match(self):
self.assertFalse(g.match_by_naxes(3))
self.assertFalse(g.match_by_naxes(99, 88))

# Match by construct
def test_Field_match_by_construct(self):
f = self.f.copy()

for OR in (True, False):
self.assertTrue(f.match_by_construct(OR=OR))
self.assertTrue(f.match_by_construct("X", OR=OR))
Expand Down Expand Up @@ -1673,6 +1675,11 @@ def test_Field_match(self):
)
)

# Check match for size 1 axes that are not spanned by the data
f = cf.example_field(0)
self.assertTrue(f.match_by_construct(T=cf.dt("2019-01-01")))
self.assertFalse(f.match_by_construct(T=cf.dt("9876-12-31")))

def test_Field_autocyclic(self):
f = self.f.copy()

Expand Down

0 comments on commit e94cd3d

Please sign in to comment.