Skip to content

Commit

Permalink
Improved Aux factory error handling: better message, delivered earlier (
Browse files Browse the repository at this point in the history
SciTools#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
  • Loading branch information
hdyson authored and znicholls committed Jun 15, 2019
1 parent 6ee6c69 commit 442fc55
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,12 @@ def add_aux_factory(self, aux_factory):
if not isinstance(aux_factory, iris.aux_factory.AuxCoordFactory):
raise TypeError('Factory must be a subclass of '
'iris.aux_factory.AuxCoordFactory.')
cube_coords = self.coords()
for dependency in aux_factory.dependencies:
ref_coord = aux_factory.dependencies[dependency]
if ref_coord is not None and ref_coord not in cube_coords:
msg = "{} coordinate for factory is not present on cube {}"
raise ValueError(msg.format(ref_coord.name(), self.name()))
self._aux_factories.append(aux_factory)

def add_cell_measure(self, cell_measure, data_dims=None):
Expand Down
27 changes: 26 additions & 1 deletion lib/iris/tests/unit/cube/test_Cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
import iris.aux_factory
import iris.coords
import iris.exceptions
from iris import FUTURE
from iris.analysis import WeightedAggregator, Aggregator
from iris.analysis import MEAN
from iris.aux_factory import HybridHeightFactory
from iris.cube import Cube
from iris.coords import AuxCoord, DimCoord, CellMeasure
from iris.exceptions import (CoordinateNotFoundError, CellMeasureNotFoundError,
Expand Down Expand Up @@ -1546,6 +1546,31 @@ def test_add_cell_measure(self):
cube.add_cell_measure(a_cell_measure, [0, 1])
self.assertEqual(cube.cell_measure('area'), a_cell_measure)

def test_add_valid_aux_factory(self):
cube = Cube(np.arange(8).reshape(2, 2, 2))
delta = AuxCoord(points=[0, 1], long_name='delta', units='m')
sigma = AuxCoord(points=[0, 1], long_name='sigma')
orog = AuxCoord(np.arange(4).reshape(2, 2), units='m')
cube.add_aux_coord(delta, 0)
cube.add_aux_coord(sigma, 0)
cube.add_aux_coord(orog, (1, 2))
factory = HybridHeightFactory(delta=delta, sigma=sigma, orography=orog)
self.assertIsNone(cube.add_aux_factory(factory))

def test_error_for_add_invalid_aux_factory(self):
cube = Cube(np.arange(8).reshape(2, 2, 2), long_name='bar')
delta = AuxCoord(points=[0, 1], long_name='delta', units='m')
sigma = AuxCoord(points=[0, 1], long_name='sigma')
orog = AuxCoord(np.arange(4).reshape(2, 2), units='m', long_name='foo')
cube.add_aux_coord(delta, 0)
cube.add_aux_coord(sigma, 0)
# Note orography is not added to the cube here
factory = HybridHeightFactory(delta=delta, sigma=sigma, orography=orog)
expected_error = ("foo coordinate for factory is not present on cube "
"bar")
with self.assertRaisesRegexp(ValueError, expected_error):
cube.add_aux_factory(factory)


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

0 comments on commit 442fc55

Please sign in to comment.