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
43 changes: 43 additions & 0 deletions lib/iris/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,22 @@ def copy(self, values=None):

return new_metadata

@abstractmethod
def cube_dims(self, cube):
"""
Identify the cube dims of any _DimensionalMetadata object.

Return the dimensions in the cube of a matching _DimensionalMetadata
object, if any.

Equivalent to cube.coord_dims(self) for a Coord,
or cube.cell_measure_dims for a CellMeasure, and so on.
Simplifies generic code to handle any _DimensionalMetadata objects.

"""
# Only makes sense for specific subclasses.
raise NotImplementedError()

def _sanitise_array(self, src, ndmin):
if _lazy.is_lazy_data(src):
# Lazy data : just ensure ndmin requirement.
Expand Down Expand Up @@ -744,6 +760,15 @@ def has_lazy_data(self):
"""
return super()._has_lazy_values()

def cube_dims(self, cube):
"""
Return the cube dimensions of this AncillaryVariable.

Equivalent to "cube.ancillary_variable_dims(self)".

"""
return cube.ancillary_variable_dims(self)


class CellMeasure(AncillaryVariable):
"""
Expand Down Expand Up @@ -846,6 +871,15 @@ def _as_defn(self):
)
return defn

def cube_dims(self, cube):
"""
Return the cube dimensions of this CellMeasure.

Equivalent to "cube.cell_measure_dims(self)".

"""
return cube.cell_measure_dims(self)


class CoordDefn(
namedtuple(
Expand Down Expand Up @@ -1596,6 +1630,15 @@ def _as_defn(self):
def __hash__(self):
return hash(id(self))

def cube_dims(self, cube):
"""
Return the cube dimensions of this Coord.

Equivalent to "cube.coord_dims(self)".

"""
return cube.coord_dims(self)

def convert_units(self, unit):
r"""
Change the coordinate's units, converting the values in its points
Expand Down
16 changes: 16 additions & 0 deletions lib/iris/tests/unit/coords/test_AncillaryVariable.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
# importing anything else.
import iris.tests as tests

from unittest import mock

import dask.array as da
import numpy as np
import numpy.ma as ma
Expand All @@ -17,6 +19,7 @@

from cf_units import Unit
from iris.coords import AncillaryVariable
from iris.cube import Cube
from iris._lazy_data import as_lazy_data


Expand Down Expand Up @@ -683,5 +686,18 @@ def test_nanpoints_eq_copy(self):
self.assertEqual(av1, av2)


class Test_cube_dims(tests.IrisTest):
def test(self):
# Check that "coord.cube_dims(cube)" calls "cube.coord_dims(coord)".
mock_dims_result = mock.sentinel.AV_DIMS
mock_dims_call = mock.Mock(return_value=mock_dims_result)
mock_cube = mock.Mock(Cube, ancillary_variable_dims=mock_dims_call)
test_var = AncillaryVariable([1], long_name="test_name")

result = test_var.cube_dims(mock_cube)
self.assertEqual(result, mock_dims_result)
self.assertEqual(mock_dims_call.call_args_list, [mock.call(test_var)])


if __name__ == "__main__":
tests.main()
16 changes: 16 additions & 0 deletions lib/iris/tests/unit/coords/test_CellMeasure.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
# importing anything else.
import iris.tests as tests

from unittest import mock

import numpy as np

from iris.coords import CellMeasure
from iris.cube import Cube
from iris._lazy_data import as_lazy_data


Expand Down Expand Up @@ -119,5 +122,18 @@ def test__eq__(self):
self.assertEqual(self.measure, self.measure)


class Test_cube_dims(tests.IrisTest):
def test(self):
# Check that "coord.cube_dims(cube)" calls "cube.coord_dims(coord)".
mock_dims_result = mock.sentinel.CM_DIMS
mock_dims_call = mock.Mock(return_value=mock_dims_result)
mock_cube = mock.Mock(Cube, cell_measure_dims=mock_dims_call)
test_cm = CellMeasure([1], long_name="test_name")

result = test_cm.cube_dims(mock_cube)
self.assertEqual(result, mock_dims_result)
self.assertEqual(mock_dims_call.call_args_list, [mock.call(test_cm)])


if __name__ == "__main__":
tests.main()
16 changes: 16 additions & 0 deletions lib/iris/tests/unit/coords/test_Coord.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import numpy as np

import iris
from iris.cube import Cube
from iris.coords import DimCoord, AuxCoord, Coord
from iris.exceptions import UnitConversionError
from iris.tests.unit.coords import CoordTestMixin
Expand Down Expand Up @@ -1020,5 +1021,20 @@ def test(self):
_ = Coord(points=[0, 1])


class Test_cube_dims(tests.IrisTest):
def test(self):
# Check that "coord.cube_dims(cube)" calls "cube.coord_dims(coord)".
mock_dims_result = mock.sentinel.COORD_DIMS
mock_dims_call = mock.Mock(return_value=mock_dims_result)
mock_cube = mock.Mock(Cube, coord_dims=mock_dims_call)
test_coord = AuxCoord([1], long_name="test_name")

result = test_coord.cube_dims(mock_cube)
self.assertEqual(result, mock_dims_result)
self.assertEqual(
mock_dims_call.call_args_list, [mock.call(test_coord)]
)


if __name__ == "__main__":
tests.main()