Skip to content

Commit e17e216

Browse files
committed
fix convert_units from unknown
1 parent 87b72a2 commit e17e216

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

docs/iris/src/whatsnew/2.0a0.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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

9599
Incompatible Changes
96100
====================

lib/iris/cube.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff 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):

lib/iris/exceptions.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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

153153
class LazyAggregatorError(Exception):
154154
pass
155+
156+
157+
class UnitConversionError(IrisError):
158+
"""Raised when Iris is unable to convert a unit."""
159+
pass

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
from iris.analysis import MEAN
3838
from iris.cube import Cube
3939
from iris.coords import AuxCoord, DimCoord, CellMeasure
40-
from iris.exceptions import CoordinateNotFoundError, CellMeasureNotFoundError
40+
from iris.exceptions import (CoordinateNotFoundError, CellMeasureNotFoundError,
41+
UnitConversionError)
4142
from iris.tests import mock
4243
import iris.tests.stock as stock
4344
from 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

0 commit comments

Comments
 (0)