Skip to content
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
5 changes: 4 additions & 1 deletion docs/iris/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ This document explains the changes made to Iris for this release
🐛 Bugs Fixed
=============

* N/A
* `@gcaria`_ fixed :meth:`~iris.cube.Cube.cell_measure_dims` to also accept the string name of a :class:`~iris.coords.CellMeasure`. (:pull:`3931`)
* `@gcaria`_ fixed :meth:`~iris.cube.Cube.ancillary_variable_dims` to also accept the string name of a :class:`~iris.coords.AncillaryVariable`. (:pull:`3931`)


💣 Incompatible Changes
Expand Down Expand Up @@ -53,3 +54,5 @@ This document explains the changes made to Iris for this release
===========

* N/A

.. _@gcaria: https://github.com/gcaria
12 changes: 8 additions & 4 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1400,10 +1400,12 @@ def cell_measure_dims(self, cell_measure):
Returns a tuple of the data dimensions relevant to the given
CellMeasure.

* cell_measure
The CellMeasure to look for.
* cell_measure (string or CellMeasure)
The (name of the) cell measure to look for.

"""
cell_measure = self.cell_measure(cell_measure)

# Search for existing cell measure (object) on the cube, faster lookup
# than equality - makes no functional difference.
matches = [
Expand All @@ -1422,10 +1424,12 @@ def ancillary_variable_dims(self, ancillary_variable):
Returns a tuple of the data dimensions relevant to the given
AncillaryVariable.

* ancillary_variable
The AncillaryVariable to look for.
* ancillary_variable (string or AncillaryVariable)
The (name of the) AncillaryVariable to look for.

"""
ancillary_variable = self.ancillary_variable(ancillary_variable)

# Search for existing ancillary variable (object) on the cube, faster
# lookup than equality - makes no functional difference.
matches = [
Expand Down
26 changes: 26 additions & 0 deletions lib/iris/tests/unit/cube/test_Cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,14 @@ def test_remove_ancilliary_variable(self):
)
self.assertEqual(self.cube._ancillary_variables_and_dims, [])

def test_remove_ancilliary_variable_by_name(self):
self.cube.remove_ancillary_variable("Quality of Detection")
self.assertEqual(self.cube._ancillary_variables_and_dims, [])

def test_fail_remove_ancilliary_variable_by_name(self):
with self.assertRaises(AncillaryVariableNotFoundError):
self.cube.remove_ancillary_variable("notname")


class Test__getitem_CellMeasure(tests.IrisTest):
def setUp(self):
Expand Down Expand Up @@ -2146,6 +2154,16 @@ def test_fail_ancill_variable_dims(self):
with self.assertRaises(AncillaryVariableNotFoundError):
self.cube.ancillary_variable_dims(ancillary_variable)

def test_ancillary_variable_dims_by_name(self):
ancill_var_dims = self.cube.ancillary_variable_dims(
"number_of_observations"
)
self.assertEqual(ancill_var_dims, (0, 1))

def test_fail_ancillary_variable_dims_by_name(self):
with self.assertRaises(AncillaryVariableNotFoundError):
self.cube.ancillary_variable_dims("notname")


class TestCellMeasures(tests.IrisTest):
def setUp(self):
Expand Down Expand Up @@ -2194,6 +2212,14 @@ def test_fail_cell_measure_dims(self):
with self.assertRaises(CellMeasureNotFoundError):
_ = self.cube.cell_measure_dims(a_cell_measure)

def test_cell_measure_dims_by_name(self):
cm_dims = self.cube.cell_measure_dims("area")
self.assertEqual(cm_dims, (0, 1))

def test_fail_cell_measure_dims_by_name(self):
with self.assertRaises(CellMeasureNotFoundError):
self.cube.cell_measure_dims("notname")


class Test_transpose(tests.IrisTest):
def setUp(self):
Expand Down