Skip to content

Commit e94cd3d

Browse files
committed
Merge branch 'main' of github.com:NCAS-CMS/cf-python into ugrid-0
2 parents 094a60a + 88febb7 commit e94cd3d

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

Changelog.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
version 3.15.4
2+
--------------
3+
4+
**2023-??-??**
5+
6+
* Fix bug in `cf.Field.match_by_construct` that always returned True for
7+
1-d constructs whose axis is not in the data, even when the
8+
criterion was not matched
9+
(https://github.com/NCAS-CMS/cf-python/issues/691)
10+
111
version 3.15.3
212
--------------
313

@@ -54,7 +64,7 @@ version 3.15.1
5464
(https://github.com/NCAS-CMS/cf-python/pull/654)
5565
* Fix `set_data` when the units are invalid
5666
(https://github.com/NCAS-CMS/cf-python/pull/646)
57-
* Fix `cf.Field.laplacian_xy`, cf.Field.grad_xy`, `cf.curl_xy` and
67+
* Fix `cf.Field.laplacian_xy`, `cf.Field.grad_xy`, `cf.curl_xy` and
5868
`cf.div_xy` to work in cases when the dimension coordinates are
5969
missing standard names
6070
(https://github.com/NCAS-CMS/cf-python/pull/643)

cf/field.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10324,7 +10324,24 @@ def indices(self, *mode, **kwargs):
1032410324
indices = []
1032510325

1032610326
# Add the indices that apply to the field's data dimensions
10327-
indices.extend([domain_indices["indices"][axis] for axis in data_axes])
10327+
axis_indices = domain_indices["indices"]
10328+
indices.extend([axis_indices[axis] for axis in data_axes])
10329+
10330+
# Check that there are no invalid indices for size 1 axes not
10331+
# spanned by the data
10332+
if len(axis_indices) > len(data_axes):
10333+
for axis, index in axis_indices.items():
10334+
if axis in data_axes or index == slice(None):
10335+
continue
10336+
10337+
import dask.array as da
10338+
10339+
shape = da.from_array([0])[index].compute_chunk_sizes().shape
10340+
if 0 in shape:
10341+
raise IndexError(
10342+
"Can't create size 0 indices for the size 1 "
10343+
f"{self.constructs.domain_axis_identity(axis)!r} axis"
10344+
)
1032810345

1032910346
return tuple(indices)
1033010347

cf/mixin/fielddomain.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,8 @@ def _indices(self, mode, data_axes, ancillary_mask, kwargs):
344344

345345
if debug:
346346
logger.debug(
347-
f" item_axes = {item_axes!r}\n keys = {keys!r}"
347+
f" item_axes = {item_axes!r}\n"
348+
f" keys = {keys!r}"
348349
) # pragma: no cover
349350

350351
if n_axes == 1:
@@ -369,9 +370,9 @@ def _indices(self, mode, data_axes, ancillary_mask, kwargs):
369370
if isinstance(value, (list, slice, tuple, np.ndarray)):
370371
# 1-d CASE 1: Value is already an index, e.g. [0],
371372
# [7,4,2], slice(0,4,2),
372-
# numpy.array([2,4,7]), [True, False,
373-
# True]
374-
373+
# numpy.array([2,4,7]),
374+
# [True,False,True]
375+
index = value
375376
if debug:
376377
logger.debug(" 1-d CASE 1:") # pragma: no cover
377378

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

476477
if debug:
477478
logger.debug(
478-
f" index = {index}\n ind = {ind}"
479+
f" index = {index}\n ind = {ind}"
479480
) # pragma: no cover
480481

481482
# Put the index into the correct place in the list of

cf/test/test_Field.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,9 @@ def test_Field_match(self):
16371637
self.assertFalse(g.match_by_naxes(3))
16381638
self.assertFalse(g.match_by_naxes(99, 88))
16391639

1640-
# Match by construct
1640+
def test_Field_match_by_construct(self):
1641+
f = self.f.copy()
1642+
16411643
for OR in (True, False):
16421644
self.assertTrue(f.match_by_construct(OR=OR))
16431645
self.assertTrue(f.match_by_construct("X", OR=OR))
@@ -1673,6 +1675,11 @@ def test_Field_match(self):
16731675
)
16741676
)
16751677

1678+
# Check match for size 1 axes that are not spanned by the data
1679+
f = cf.example_field(0)
1680+
self.assertTrue(f.match_by_construct(T=cf.dt("2019-01-01")))
1681+
self.assertFalse(f.match_by_construct(T=cf.dt("9876-12-31")))
1682+
16761683
def test_Field_autocyclic(self):
16771684
f = self.f.copy()
16781685

0 commit comments

Comments
 (0)