File tree Expand file tree Collapse file tree 4 files changed +22
-4
lines changed Expand file tree Collapse file tree 4 files changed +22
-4
lines changed Original file line number Diff line number Diff line change @@ -91,6 +91,10 @@ Bugs Fixed
9191 :class: `~iris.cube.Cube ` with :data: `integer ` or :data: `boolean ` data with
9292 a :data: `float ` result will raise an :data: `ArithmeticError ` exception.
9393
94+ * Attempting to use the :meth: `iris.cube.Cube.convert_units ` method on a cube
95+ where the existing unit is unknown will raise a :data: `UnitConversionError `
96+ exception.
97+
9498
9599Incompatible Changes
96100====================
Original file line number Diff line number Diff line change @@ -880,8 +880,11 @@ def convert_units(self, unit):
880880
881881 """
882882 # If the cube has units convert the data.
883- if not self .units .is_unknown ():
884- self .data = self .units .convert (self .data , unit )
883+ if self .units .is_unknown ():
884+ raise iris .exceptions .UnitConversionError (
885+ 'cannot convert from unknown units to {!s}, '
886+ 'set the "units" attribute instead' .format (unit ))
887+ self .data = self .units .convert (self .data , unit )
885888 self .units = unit
886889
887890 def add_cell_method (self , cell_method ):
Original file line number Diff line number Diff line change 1- # (C) British Crown Copyright 2010 - 2016 , Met Office
1+ # (C) British Crown Copyright 2010 - 2017 , Met Office
22#
33# This file is part of Iris.
44#
@@ -152,3 +152,8 @@ def __init__(self, msg):
152152
153153class LazyAggregatorError (Exception ):
154154 pass
155+
156+
157+ class UnitConversionError (IrisError ):
158+ """Raised when Iris is unable to convert a unit."""
159+ pass
Original file line number Diff line number Diff line change 3737from iris .analysis import MEAN
3838from iris .cube import Cube
3939from iris .coords import AuxCoord , DimCoord , CellMeasure
40- from iris .exceptions import CoordinateNotFoundError , CellMeasureNotFoundError
40+ from iris .exceptions import (CoordinateNotFoundError , CellMeasureNotFoundError ,
41+ UnitConversionError )
4142from iris .tests import mock
4243import iris .tests .stock as stock
4344from iris ._lazy_data import as_lazy_data
@@ -1008,6 +1009,11 @@ def test_wrap_radians(self):
10081009 self .assertEqual (result .data [0 , 0 , 0 ], 303 )
10091010 self .assertEqual (result .data [0 , 0 , - 1 ], 28 )
10101011
1012+ def test_convert_unknown_units (self ):
1013+ cube = iris .cube .Cube (1 )
1014+ with self .assertRaises (UnitConversionError ):
1015+ cube .convert_units ('mm day-1' )
1016+
10111017 def test_tolerance_bug (self ):
10121018 # Floating point changes introduced by wrapping mean
10131019 # the resulting coordinate values are not equal to their
You can’t perform that action at this time.
0 commit comments