Skip to content

Commit a41db2d

Browse files
hdysoncorinnebosley
authored andcommitted
Improved Aux factory error handling: better message, delivered earlier (#3182)
* Tests for expected behaviour * Raise error if aux factory relies on a coordinate external to cube * Include coordinate name in error message * Don't check whether dependencies that are "None" are in the coords. * Review changes: check cube name in error and variable rename * Truncated variable for flake8
1 parent e2c47f9 commit a41db2d

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

lib/iris/cube.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,12 @@ def add_aux_factory(self, aux_factory):
994994
if not isinstance(aux_factory, iris.aux_factory.AuxCoordFactory):
995995
raise TypeError('Factory must be a subclass of '
996996
'iris.aux_factory.AuxCoordFactory.')
997+
cube_coords = self.coords()
998+
for dependency in aux_factory.dependencies:
999+
ref_coord = aux_factory.dependencies[dependency]
1000+
if ref_coord is not None and ref_coord not in cube_coords:
1001+
msg = "{} coordinate for factory is not present on cube {}"
1002+
raise ValueError(msg.format(ref_coord.name(), self.name()))
9971003
self._aux_factories.append(aux_factory)
9981004

9991005
def add_cell_measure(self, cell_measure, data_dims=None):

lib/iris/tests/unit/cube/test_Cube.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@
3434
import iris.aux_factory
3535
import iris.coords
3636
import iris.exceptions
37-
from iris import FUTURE
3837
from iris.analysis import WeightedAggregator, Aggregator
3938
from iris.analysis import MEAN
39+
from iris.aux_factory import HybridHeightFactory
4040
from iris.cube import Cube
4141
from iris.coords import AuxCoord, DimCoord, CellMeasure
4242
from iris.exceptions import (CoordinateNotFoundError, CellMeasureNotFoundError,
@@ -1546,6 +1546,31 @@ def test_add_cell_measure(self):
15461546
cube.add_cell_measure(a_cell_measure, [0, 1])
15471547
self.assertEqual(cube.cell_measure('area'), a_cell_measure)
15481548

1549+
def test_add_valid_aux_factory(self):
1550+
cube = Cube(np.arange(8).reshape(2, 2, 2))
1551+
delta = AuxCoord(points=[0, 1], long_name='delta', units='m')
1552+
sigma = AuxCoord(points=[0, 1], long_name='sigma')
1553+
orog = AuxCoord(np.arange(4).reshape(2, 2), units='m')
1554+
cube.add_aux_coord(delta, 0)
1555+
cube.add_aux_coord(sigma, 0)
1556+
cube.add_aux_coord(orog, (1, 2))
1557+
factory = HybridHeightFactory(delta=delta, sigma=sigma, orography=orog)
1558+
self.assertIsNone(cube.add_aux_factory(factory))
1559+
1560+
def test_error_for_add_invalid_aux_factory(self):
1561+
cube = Cube(np.arange(8).reshape(2, 2, 2), long_name='bar')
1562+
delta = AuxCoord(points=[0, 1], long_name='delta', units='m')
1563+
sigma = AuxCoord(points=[0, 1], long_name='sigma')
1564+
orog = AuxCoord(np.arange(4).reshape(2, 2), units='m', long_name='foo')
1565+
cube.add_aux_coord(delta, 0)
1566+
cube.add_aux_coord(sigma, 0)
1567+
# Note orography is not added to the cube here
1568+
factory = HybridHeightFactory(delta=delta, sigma=sigma, orography=orog)
1569+
expected_error = ("foo coordinate for factory is not present on cube "
1570+
"bar")
1571+
with self.assertRaisesRegexp(ValueError, expected_error):
1572+
cube.add_aux_factory(factory)
1573+
15491574

15501575
class Test_remove_metadata(tests.IrisTest):
15511576
def setUp(self):

0 commit comments

Comments
 (0)