Skip to content

Commit f63c9b5

Browse files
Merge branch 'main' into pad
2 parents 57fe942 + e3183da commit f63c9b5

4 files changed

Lines changed: 43 additions & 21 deletions

File tree

Changelog.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ version 3.17.0
55

66
* New methods: `cf.Field.pad_missing` and `cf.Data.pad_missing`
77
(https://github.com/NCAS-CMS/cf-python/issues/717)
8+
* Fix occasional bug when calculating UGRID cell areas when
9+
non-spatial coordinates span the discrete axis
10+
(https://github.com/NCAS-CMS/cf-python/issues/721)
11+
* New keyword parameter to `cf.Field.insert_dimension`:
12+
``constructs`` (https://github.com/NCAS-CMS/cf-python/issues/719)
813
* Added the ``cell_measures`` and ``coordinates`` keyword arguments to
914
`cf.Field.weights`
1015
(https://github.com/NCAS-CMS/cf-python/issues/709)
@@ -17,6 +22,9 @@ version 3.17.0
1722
* Fix bug that caused `cf.Field.del_file_location` to fail when
1823
updating its metdata constructs
1924
(https://github.com/NCAS-CMS/cf-python/issues/707)
25+
* Fix bug that caused incorrect data arrays in some cyclic subspaces
26+
created by `cf.Field.subspace` and `cf.Field.__getitem__`
27+
(https://github.com/NCAS-CMS/cf-python/issues/713)
2028

2129
version 3.16.0
2230
--------------

cf/field.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,12 @@ def __getitem__(self, indices):
405405
f"{self.constructs.domain_axis_identity(_)!r} axis"
406406
)
407407

408-
new = new.roll(shift=shift, axis=iaxis)
408+
new = new.roll(axis=iaxis, shift=shift)
409409
else:
410410
new = self.copy()
411411

412+
data = new.data
413+
412414
# ------------------------------------------------------------
413415
# Subspace the field construct's data
414416
# ------------------------------------------------------------
@@ -8685,7 +8687,9 @@ def _update_cell_methods(
86858687
) # pragma: no cover
86868688

86878689
@_inplace_enabled(default=False)
8688-
def insert_dimension(self, axis, position=0, inplace=False):
8690+
def insert_dimension(
8691+
self, axis, position=0, constructs=False, inplace=False
8692+
):
86898693
"""Insert a size 1 axis into the data array.
86908694

86918695
.. versionadded:: 3.0.0
@@ -8710,6 +8714,13 @@ def insert_dimension(self, axis, position=0, inplace=False):
87108714
data array. By default the new axis has position 0, the
87118715
slowest varying position.
87128716

8717+
constructs: `bool`, optional
8718+
If True then also insert the new axis into all
8719+
metadata constructs that don't already include it. By
8720+
default, metadata constructs are not changed.
8721+
8722+
.. versionadded:: 3.17.0
8723+
87138724
{{inplace: `bool`, optional}}
87148725

87158726
:Returns:
@@ -8753,24 +8764,13 @@ def insert_dimension(self, axis, position=0, inplace=False):
87538764
: time(1) = [2019-01-01 00:00:00]
87548765

87558766
"""
8756-
f = _inplace_enabled_define_and_cleanup(self)
8757-
8758-
if axis is None:
8759-
axis = f.set_construct(self._DomainAxis(1))
8760-
else:
8761-
axis = f.domain_axis(
8762-
axis,
8763-
key=True,
8764-
default=ValueError("Can't identify a unique axis to insert"),
8765-
)
8766-
8767-
# Expand the dims in the field construct's data array
8768-
super(Field, f).insert_dimension(
8769-
axis=axis, position=position, inplace=True
8767+
return super().insert_dimension(
8768+
axis=axis,
8769+
position=position,
8770+
constructs=constructs,
8771+
inplace=inplace,
87708772
)
87718773

8772-
return f
8773-
87748774
def indices(self, *mode, **kwargs):
87758775
"""Create indices that define a subspace of the field construct.
87768776

cf/test/test_Field.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,13 @@ def test_Field__getitem__(self):
656656
with self.assertRaises(IndexError):
657657
f[..., [False] * f.shape[-1]]
658658

659+
# Test with cyclic subspace
660+
f.cyclic("grid_longitude")
661+
g = f[:, -3:-5:1]
662+
self.assertEqual(g.shape, (10, 7))
663+
self.assertTrue(np.allclose(f[:, -3:].array, g[:, :3].array))
664+
self.assertTrue(f[:, :4].equals(g[:, 3:]))
665+
659666
def test_Field__setitem__(self):
660667
f = self.f.copy().squeeze()
661668

@@ -1136,6 +1143,10 @@ def test_Field_insert_dimension(self):
11361143
self.assertEqual(g.ndim, f.ndim + 1)
11371144
self.assertEqual(g.get_data_axes()[1:], f.get_data_axes())
11381145

1146+
self.assertEqual(g.cell_measure().ndim, 2)
1147+
h = g.insert_dimension(None, constructs=True)
1148+
self.assertEqual(h.cell_measure().ndim, 3)
1149+
11391150
with self.assertRaises(ValueError):
11401151
f.insert_dimension(1, "qwerty")
11411152

cf/weights.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,9 @@ def _geometry_ugrid_cells(cls, f, domain_axis, cell_type, auto=False):
16521652
)
16531653

16541654
for key, aux in auxiliary_coordinates_1d.items():
1655+
if str(aux.ctype) not in "XYZ":
1656+
continue
1657+
16551658
aux_axis = f.get_data_axes(key)[0]
16561659

16571660
ugrid = f.domain_topology(default=None, filter_by_axis=(aux_axis,))
@@ -1665,8 +1668,8 @@ def _geometry_ugrid_cells(cls, f, domain_axis, cell_type, auto=False):
16651668
):
16661669
continue
16671670

1668-
# Still here? Then this auxiliary coordinate has either UGRID
1669-
# or geometry cells.
1671+
# Still here? Then this X, Y, or Z auxiliary coordinate is
1672+
# for either UGRID or geometry cells.
16701673
if aux.X:
16711674
aux_X = aux.copy()
16721675
x_axis = aux_axis
@@ -1701,7 +1704,7 @@ def _geometry_ugrid_cells(cls, f, domain_axis, cell_type, auto=False):
17011704

17021705
raise ValueError(
17031706
"Can't create weights: X and Y cells span different domain "
1704-
"axes"
1707+
f"axes: {x_axis} != {y_axis}"
17051708
)
17061709

17071710
axis = x_axis

0 commit comments

Comments
 (0)