From 457d5da763e93e539ba78f9e006a06c134af70c8 Mon Sep 17 00:00:00 2001 From: "scott.robinson" Date: Mon, 13 Mar 2023 09:30:53 +0000 Subject: [PATCH 1/4] Fix --- lib/iris/tests/unit/util/test_new_axis.py | 10 ++++++++++ lib/iris/util.py | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/iris/tests/unit/util/test_new_axis.py b/lib/iris/tests/unit/util/test_new_axis.py index d81f2c40d7..bc3ad4d6bf 100644 --- a/lib/iris/tests/unit/util/test_new_axis.py +++ b/lib/iris/tests/unit/util/test_new_axis.py @@ -116,8 +116,18 @@ def test_promote_scalar_auxcoord(self, stock_cube): ] self._assert_cube_notis(result, stock_cube) + def test_existing_dim_coord(self, stock_cube): + # Provide an existing dimensional coordinate + coord = iris.coords.DimCoord(1, long_name="dim") + stock_cube.add_aux_coord(coord) + + new_cube = iris.util.new_axis(stock_cube, coord) + with pytest.raises(iris.exceptions.CoordinateNotFoundError): + iris.util.new_axis(new_cube, coord) + def test_promote_non_scalar(self, stock_cube): # Provide a dimensional coordinate which is not scalar + iris.util.demote_dim_coord_to_aux_coord(stock_cube, "foo") with pytest.raises(ValueError, match="is not a scalar coordinate."): new_axis(stock_cube, "foo") diff --git a/lib/iris/util.py b/lib/iris/util.py index 9e0db9e66e..9018913a68 100644 --- a/lib/iris/util.py +++ b/lib/iris/util.py @@ -1171,7 +1171,7 @@ def _handle_dimensional_metadata( cube_add_method(new_dm_item, new_dims) if scalar_coord is not None: - scalar_coord = src_cube.coord(scalar_coord) + scalar_coord = src_cube.coord(scalar_coord, dim_coords=False) if not scalar_coord.shape == (1,): emsg = scalar_coord.name() + "is not a scalar coordinate." raise ValueError(emsg) From 1eeaf664318a4bfec345303d3cb8761fa70cfa75 Mon Sep 17 00:00:00 2001 From: "scott.robinson" Date: Tue, 14 Mar 2023 15:18:45 +0000 Subject: [PATCH 2/4] Two cases --- docs/src/whatsnew/latest.rst | 5 +++++ lib/iris/tests/unit/util/test_new_axis.py | 2 +- lib/iris/util.py | 10 ++++++++-- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index b34816e616..146b3dcc68 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -56,6 +56,11 @@ This document explains the changes made to Iris for this release cubes with masked :class:`iris.coords.CellMeasure`. (:issue:`5147`, :pull:`5181`) +#. `@scottrobinson02`_ fixed :class:`iris.util.new_axis` creating an anonymous new + dimension, when the scalar coord provided is already a dim coord. + (:issue:`4415`, :pull:`5194`) + + 💣 Incompatible Changes ======================= diff --git a/lib/iris/tests/unit/util/test_new_axis.py b/lib/iris/tests/unit/util/test_new_axis.py index bc3ad4d6bf..009787f80e 100644 --- a/lib/iris/tests/unit/util/test_new_axis.py +++ b/lib/iris/tests/unit/util/test_new_axis.py @@ -122,7 +122,7 @@ def test_existing_dim_coord(self, stock_cube): stock_cube.add_aux_coord(coord) new_cube = iris.util.new_axis(stock_cube, coord) - with pytest.raises(iris.exceptions.CoordinateNotFoundError): + with pytest.raises(ValueError, match="is already a dimension coordinate."): iris.util.new_axis(new_cube, coord) def test_promote_non_scalar(self, stock_cube): diff --git a/lib/iris/util.py b/lib/iris/util.py index 9018913a68..d2af4c1085 100644 --- a/lib/iris/util.py +++ b/lib/iris/util.py @@ -1171,9 +1171,15 @@ def _handle_dimensional_metadata( cube_add_method(new_dm_item, new_dims) if scalar_coord is not None: - scalar_coord = src_cube.coord(scalar_coord, dim_coords=False) + scalar_coord = src_cube.coord(scalar_coord) + try: + src_cube.coord(scalar_coord, dim_coords=False) + except iris.exceptions.CoordinateNotFoundError: + emsg = scalar_coord.name() + " is already a dimension coordinate." + raise ValueError(emsg) + if not scalar_coord.shape == (1,): - emsg = scalar_coord.name() + "is not a scalar coordinate." + emsg = scalar_coord.name() + " is not a scalar coordinate." raise ValueError(emsg) expand_extras = [ From 7fed137e90a6d51d29261f474b5e7e1839cb5f6e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 14 Mar 2023 15:20:23 +0000 Subject: [PATCH 3/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- lib/iris/tests/unit/util/test_new_axis.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/iris/tests/unit/util/test_new_axis.py b/lib/iris/tests/unit/util/test_new_axis.py index 009787f80e..a6374f97ad 100644 --- a/lib/iris/tests/unit/util/test_new_axis.py +++ b/lib/iris/tests/unit/util/test_new_axis.py @@ -122,7 +122,9 @@ def test_existing_dim_coord(self, stock_cube): stock_cube.add_aux_coord(coord) new_cube = iris.util.new_axis(stock_cube, coord) - with pytest.raises(ValueError, match="is already a dimension coordinate."): + with pytest.raises( + ValueError, match="is already a dimension coordinate." + ): iris.util.new_axis(new_cube, coord) def test_promote_non_scalar(self, stock_cube): From def0e0f904d38f692a8c27b8c2cce68bac6e4e9e Mon Sep 17 00:00:00 2001 From: "scott.robinson" Date: Tue, 14 Mar 2023 16:00:50 +0000 Subject: [PATCH 4/4] latest.rst --- docs/src/whatsnew/latest.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index 146b3dcc68..f53a6021fb 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -140,6 +140,7 @@ This document explains the changes made to Iris for this release .. _@fnattino: https://github.com/fnattino .. _@ed-hawkins: https://github.com/ed-hawkins +.. _@scottrobinson02: https://github.com/scottrobinson02 .. comment Whatsnew resources in alphabetical order: