Skip to content
Closed
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
8 changes: 7 additions & 1 deletion lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,13 @@ def add_dim_coord(self, dim_coord, data_dim):

def _add_unique_dim_coord(self, dim_coord, data_dim):
if isinstance(dim_coord, iris.coords.AuxCoord):
raise ValueError('The dim_coord may not be an AuxCoord instance.')
try:
dim_coord = iris.coords.DimCoord.from_coord(dim_coord)
warnings.warn('converting AuxCoord to DimCoord',
stacklevel=2)
except ValueError as e:
raise ValueError('Could not convert coord to DimCoord. '
+ str(e))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What extra information is contained in str(e) (not necessarily a criticism, I'm at a windows machine so I can't check myself)?

If it doesn't add much useful I'd say ditch it and go for a more descriptive static message. On the flip side, if e already contains a descriptive message that is good to use as is then we don't even necessarily have to re-raise it...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ValueError: Could not convert coord to DimCoord: The points array must be strictly monotonic.
Probably worth keeping, do you think? Could do with being changed from a colon after DimCoord thought

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, then I guess what I want to know is will it ever be anything other than that? If not then you could just write that... If it can be one of any number of things then maybe this is OK.


# Convert data_dim to a single integer
if isinstance(data_dim, collections.Container):
Expand Down
12 changes: 9 additions & 3 deletions lib/iris/tests/test_cube.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) British Crown Copyright 2010 - 2013, Met Office
# (C) British Crown Copyright 2010 - 2014, Met Office
#
# This file is part of Iris.
#
Expand Down Expand Up @@ -81,11 +81,17 @@ def test_no_dim(self):
self.cube.add_dim_coord,
iris.coords.DimCoord(np.arange(2), "latitude"))

def test_adding_aux_coord(self):
coord = iris.coords.AuxCoord(np.arange(2), "latitude")
def test_adding_nonmonotonic_aux_coord(self):
coord = iris.coords.AuxCoord([0, 1, 1, 2], "latitude")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a new test that exercises the new functionality. Check that a strictly monotonic AuxCoord can be accepted and also check the warning is raised perhaps.

with self.assertRaises(ValueError):
self.cube.add_dim_coord(coord, 0)

def test_adding_aux_coord(self):
coord = iris.coords.AuxCoord(np.arange(2), "latitude")
self.cube.add_dim_coord(coord, 0)
self.assertIsInstance(self.cube.coord("latitude"),
iris.coords.DimCoord)


class TestEquality(tests.IrisTest):
def test_not_implmemented(self):
Expand Down