Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New constructs keyword to cf.Field.insert_dimension #720

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ version 3.17.0

**2024-??-??**

* New keyword parameter to `cf.Field.insert_dimension`:
``constructs`` (https://github.com/NCAS-CMS/cf-python/issues/719)
* Added the ``cell_measures`` and ``coordinates`` keyword arguments to
`cf.Field.weights`
(https://github.com/NCAS-CMS/cf-python/issues/709)
Expand Down
32 changes: 15 additions & 17 deletions cf/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -8687,7 +8687,9 @@ def _update_cell_methods(
) # pragma: no cover

@_inplace_enabled(default=False)
def insert_dimension(self, axis, position=0, inplace=False):
def insert_dimension(
self, axis, position=0, constructs=False, inplace=False
):
"""Insert a size 1 axis into the data array.

.. versionadded:: 3.0.0
Expand All @@ -8712,6 +8714,13 @@ def insert_dimension(self, axis, position=0, inplace=False):
data array. By default the new axis has position 0, the
slowest varying position.

constructs: `bool`, optional
If True then also insert the new axis into all
metadata constructs that don't already include it. By
default, metadata constructs are not changed.

.. versionadded:: 3.17.0

{{inplace: `bool`, optional}}

:Returns:
Expand Down Expand Up @@ -8755,24 +8764,13 @@ def insert_dimension(self, axis, position=0, inplace=False):
: time(1) = [2019-01-01 00:00:00]

"""
f = _inplace_enabled_define_and_cleanup(self)

if axis is None:
axis = f.set_construct(self._DomainAxis(1))
else:
axis = f.domain_axis(
axis,
key=True,
default=ValueError("Can't identify a unique axis to insert"),
)

# Expand the dims in the field construct's data array
super(Field, f).insert_dimension(
axis=axis, position=position, inplace=True
return super().insert_dimension(
axis=axis,
position=position,
constructs=constructs,
inplace=inplace,
)

return f

def indices(self, *mode, **kwargs):
"""Create indices that define a subspace of the field construct.

Expand Down
4 changes: 4 additions & 0 deletions cf/test/test_Field.py
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,10 @@ def test_Field_insert_dimension(self):
self.assertEqual(g.ndim, f.ndim + 1)
self.assertEqual(g.get_data_axes()[1:], f.get_data_axes())

self.assertEqual(g.cell_measure().ndim, 2)
h = g.insert_dimension(None, constructs=True)
self.assertEqual(h.cell_measure().ndim, 3)

with self.assertRaises(ValueError):
f.insert_dimension(1, "qwerty")

Expand Down