Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

iris.util.new_axis anonymous new dimension fix #5194

Merged
merged 4 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
scottrobinson02 marked this conversation as resolved.
Show resolved Hide resolved
dimension, when the scalar coord provided is already a dim coord.
(:issue:`4415`, :pull:`5194`)



馃挘 Incompatible Changes
=======================
Expand Down Expand Up @@ -135,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:
Expand Down
12 changes: 12 additions & 0 deletions lib/iris/tests/unit/util/test_new_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,20 @@ 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(
ValueError, match="is already a dimension coordinate."
):
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")

Expand Down
8 changes: 7 additions & 1 deletion lib/iris/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1172,8 +1172,14 @@ def _handle_dimensional_metadata(

if scalar_coord is not None:
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 = [
Expand Down