From c114026d627488a5eebfefb1b1b74f5d6d140efa Mon Sep 17 00:00:00 2001 From: Simon Osborne Date: Tue, 12 May 2026 15:14:45 +0100 Subject: [PATCH 1/3] Added remove scalar coordinates operator --- src/CSET/operators/misc.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/CSET/operators/misc.py b/src/CSET/operators/misc.py index 34e59aefb..60dc89ec4 100644 --- a/src/CSET/operators/misc.py +++ b/src/CSET/operators/misc.py @@ -81,6 +81,22 @@ def remove_attribute( return cubes +def remove_scalar_coords(cubes, coords): + """Remove scalar coordinates. + + examples would be: realization, forecast_reference_time from model cubes. + """ + if not isinstance(cubes, CubeList): + cubes = CubeList([cubes]) + + for cube in cubes: + for coord in coords: + if cube.coords(coord): + cube.remove_coord(coord) + + return cubes + + def addition(addend_1, addend_2): """Addition of two fields. From d2db4f4d58d0735d9d461980da6032300190c8dc Mon Sep 17 00:00:00 2001 From: Simon Osborne Date: Wed, 27 May 2026 14:27:26 +0100 Subject: [PATCH 2/3] Update coordinate removal logic in misc.py Refactor coordinate removal to check for scalar dimensions before removal. --- src/CSET/operators/misc.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/CSET/operators/misc.py b/src/CSET/operators/misc.py index 60dc89ec4..f6412dcca 100644 --- a/src/CSET/operators/misc.py +++ b/src/CSET/operators/misc.py @@ -90,9 +90,12 @@ def remove_scalar_coords(cubes, coords): cubes = CubeList([cubes]) for cube in cubes: - for coord in coords: - if cube.coords(coord): - cube.remove_coord(coord) + for coord_name in coords: + if cube.coords(coord_name): + coord = cube.coord(coord_name) + # only remove if scalar + if cube.coord_dims(coord) == (): + cube.remove_coord(coord) return cubes From 4377daae683c725aefd50a6959821be0565c3dc3 Mon Sep 17 00:00:00 2001 From: Simon Osborne Date: Wed, 27 May 2026 14:45:16 +0100 Subject: [PATCH 3/3] Add two tests to remove_scalar_coords function in misc.py --- tests/operators/test_misc.py | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/operators/test_misc.py b/tests/operators/test_misc.py index 41cfc7266..156dda765 100644 --- a/tests/operators/test_misc.py +++ b/tests/operators/test_misc.py @@ -541,3 +541,41 @@ def test_extract_common_points_nocommonpoints(vertical_profile_cube): misc.extract_common_points( cubes=iris.cube.CubeList([cube1, cube2]), coordinate="pressure" ) + + +def test_remove_scalar_coord(): + """Test that scalar coordinate be removed.""" + # Create simple 1D cube + data = np.arange(5) + time = iris.coords.DimCoord( + np.arange(5), standard_name="time", units="hours since 1970-01-01" + ) + cube = iris.cube.Cube(data, dim_coords_and_dims=[(time, 0)]) + # Add a scalar coord + realization = iris.coords.AuxCoord(1, long_name="realization") + cube.add_aux_coord(realization) + # Check it's present and scalar + assert cube.coords("realization") + assert cube.coord_dims("realization") == () + # Run function + out = misc.remove_scalar_coords(cube, ["realization"]) + # Check it’s removed + cube_out = out[0] + assert not cube_out.coords("realization") + + +def test_not_remove_non_scalar_coord(): + """Test that non-scalar coordinate is not removed.""" + # Create 1D cube + data = np.arange(5) + time = iris.coords.DimCoord( + np.arange(5), standard_name="time", units="hours since 1970-01-01" + ) + cube = iris.cube.Cube(data, dim_coords_and_dims=[(time, 0)]) + # Confirm it's non-scalar + assert cube.coord_dims("time") != () + # Run function + out = misc.remove_scalar_coords(cube, ["time"]) + # Check it is still present + cube_out = out[0] + assert cube_out.coords("time")