From 85974a56fb2fd2a008e665181f30e8385381cb53 Mon Sep 17 00:00:00 2001 From: Axel Lauer Date: Fri, 4 Feb 2022 14:40:22 +0100 Subject: [PATCH 1/5] remove scalar coordinates p0, ptop and fix altitude <--> pressure conversion --- esmvalcore/cmor/_fixes/shared.py | 4 ---- esmvalcore/preprocessor/_multimodel.py | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/esmvalcore/cmor/_fixes/shared.py b/esmvalcore/cmor/_fixes/shared.py index f6f4400dcd..fbace5d1e7 100644 --- a/esmvalcore/cmor/_fixes/shared.py +++ b/esmvalcore/cmor/_fixes/shared.py @@ -72,9 +72,7 @@ def add_plev_from_altitude(cube): pressure_bounds = altitude_to_pressure(height_coord.core_bounds()) pressure_coord = iris.coords.AuxCoord(pressure_points, bounds=pressure_bounds, - var_name='plev', standard_name='air_pressure', - long_name='pressure', units='Pa') cube.add_aux_coord(pressure_coord, cube.coord_dims(height_coord)) return @@ -108,9 +106,7 @@ def add_altitude_from_plev(cube): altitude_bounds = pressure_to_altitude(plev_coord.core_bounds()) altitude_coord = iris.coords.AuxCoord(altitude_points, bounds=altitude_bounds, - var_name='alt', standard_name='altitude', - long_name='altitude', units='m') cube.add_aux_coord(altitude_coord, cube.coord_dims(plev_coord)) return diff --git a/esmvalcore/preprocessor/_multimodel.py b/esmvalcore/preprocessor/_multimodel.py index 0fbd64ca7a..db5a619a57 100644 --- a/esmvalcore/preprocessor/_multimodel.py +++ b/esmvalcore/preprocessor/_multimodel.py @@ -216,6 +216,10 @@ def _combine(cubes): coord.long_name = None coord.attributes = None + for auxcoord in cube.aux_coords: + if (auxcoord.var_name == 'p0' or auxcoord.var_name == 'ptop'): + cube.remove_coord(auxcoord) + cubes = iris.cube.CubeList(cubes) merged_cube = cubes.merge_cube() From c96e26a8d121e92dab25d9bb58b72710c2a2e017 Mon Sep 17 00:00:00 2001 From: Axel Lauer Date: Mon, 7 Feb 2022 09:52:01 +0100 Subject: [PATCH 2/5] Update esmvalcore/preprocessor/_multimodel.py Co-authored-by: Manuel Schlund <32543114+schlunma@users.noreply.github.com> --- esmvalcore/preprocessor/_multimodel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esmvalcore/preprocessor/_multimodel.py b/esmvalcore/preprocessor/_multimodel.py index db5a619a57..bb156d01f0 100644 --- a/esmvalcore/preprocessor/_multimodel.py +++ b/esmvalcore/preprocessor/_multimodel.py @@ -216,7 +216,7 @@ def _combine(cubes): coord.long_name = None coord.attributes = None - for auxcoord in cube.aux_coords: + for auxcoord in cube.coords(dimensions=()): if (auxcoord.var_name == 'p0' or auxcoord.var_name == 'ptop'): cube.remove_coord(auxcoord) From 57fea477136fd8966905f8f1e9a8573a5d62a618 Mon Sep 17 00:00:00 2001 From: Manuel Schlund Date: Mon, 7 Feb 2022 10:45:12 +0100 Subject: [PATCH 3/5] Fixed tests --- tests/integration/cmor/_fixes/test_common.py | 4 ++-- tests/integration/cmor/_fixes/test_shared.py | 13 ++++--------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/tests/integration/cmor/_fixes/test_common.py b/tests/integration/cmor/_fixes/test_common.py index ace5cb5325..800ec736f5 100644 --- a/tests/integration/cmor/_fixes/test_common.py +++ b/tests/integration/cmor/_fixes/test_common.py @@ -156,9 +156,9 @@ def hybrid_height_coord_fix_metadata(nc_path, short_name, fix): air_pressure_coord = cube.coord('air_pressure') np.testing.assert_allclose(air_pressure_coord.points, PRESSURE_POINTS) np.testing.assert_allclose(air_pressure_coord.bounds, PRESSURE_BOUNDS) - assert air_pressure_coord.var_name == 'plev' + assert air_pressure_coord.var_name is None assert air_pressure_coord.standard_name == 'air_pressure' - assert air_pressure_coord.long_name == 'pressure' + assert air_pressure_coord.long_name is None assert air_pressure_coord.units == 'Pa' diff --git a/tests/integration/cmor/_fixes/test_shared.py b/tests/integration/cmor/_fixes/test_shared.py index 741c5e4233..a1a127066b 100644 --- a/tests/integration/cmor/_fixes/test_shared.py +++ b/tests/integration/cmor/_fixes/test_shared.py @@ -106,20 +106,15 @@ def test_add_aux_coords_from_cubes(coord_dict, output): ALT_COORD = iris.coords.AuxCoord([0.0], bounds=[[-100.0, 500.0]], - var_name='alt', long_name='altitude', standard_name='altitude', units='m') -ALT_COORD_NB = iris.coords.AuxCoord([0.0], var_name='alt', - long_name='altitude', - standard_name='altitude', units='m') +ALT_COORD_NB = iris.coords.AuxCoord([0.0], standard_name='altitude', units='m') ALT_COORD_KM = iris.coords.AuxCoord([0.0], bounds=[[-0.1, 0.5]], var_name='alt', long_name='altitude', standard_name='altitude', units='km') P_COORD = iris.coords.AuxCoord([101325.0], bounds=[[102532.0, 95460.8]], - var_name='plev', standard_name='air_pressure', - long_name='pressure', units='Pa') -P_COORD_NB = iris.coords.AuxCoord([101325.0], var_name='plev', - standard_name='air_pressure', - long_name='pressure', units='Pa') + standard_name='air_pressure', units='Pa') +P_COORD_NB = iris.coords.AuxCoord([101325.0], standard_name='air_pressure', + units='Pa') CUBE_ALT = iris.cube.Cube([1.0], var_name='x', aux_coords_and_dims=[(ALT_COORD, 0)]) CUBE_ALT_NB = iris.cube.Cube([1.0], var_name='x', From db45835460467493c055efdc55b9541eccbf3576 Mon Sep 17 00:00:00 2001 From: Manuel Schlund Date: Mon, 7 Feb 2022 10:56:50 +0100 Subject: [PATCH 4/5] Added test for removal of scalar coords p0 and ptop --- esmvalcore/preprocessor/_multimodel.py | 8 +++++--- .../_multimodel/test_multimodel.py | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/esmvalcore/preprocessor/_multimodel.py b/esmvalcore/preprocessor/_multimodel.py index bb156d01f0..3987ca5277 100644 --- a/esmvalcore/preprocessor/_multimodel.py +++ b/esmvalcore/preprocessor/_multimodel.py @@ -216,9 +216,11 @@ def _combine(cubes): coord.long_name = None coord.attributes = None - for auxcoord in cube.coords(dimensions=()): - if (auxcoord.var_name == 'p0' or auxcoord.var_name == 'ptop'): - cube.remove_coord(auxcoord) + # Remove specific scalar coordinates which are not expected to be equal + scalar_coords_to_remove = ['p0', 'ptop'] + for scalar_coord in cube.coords(dimensions=()): + if scalar_coord.var_name in scalar_coords_to_remove: + cube.remove_coord(scalar_coord) cubes = iris.cube.CubeList(cubes) diff --git a/tests/unit/preprocessor/_multimodel/test_multimodel.py b/tests/unit/preprocessor/_multimodel/test_multimodel.py index 9460c0199d..913a64e065 100644 --- a/tests/unit/preprocessor/_multimodel/test_multimodel.py +++ b/tests/unit/preprocessor/_multimodel/test_multimodel.py @@ -9,6 +9,7 @@ import pytest from cf_units import Unit from iris.cube import Cube +from iris.coords import AuxCoord import esmvalcore.preprocessor._multimodel as mm from esmvalcore.iris_helpers import date2num @@ -371,6 +372,25 @@ def test_combine_inconsistent_var_names_fail(): _ = mm._combine(cubes) +@pytest.mark.parametrize('scalar_coord', ['p0', 'ptop']) +def test_combine_with_scalar_coords_to_remove(scalar_coord): + """Test _combine with scalar coordinates that should be removed.""" + num_cubes = 5 + cubes = [] + + for num in range(num_cubes): + cube = generate_cube_from_dates('monthly') + cubes.append(cube) + + scalar_coord_0 = AuxCoord(0.0, var_name=scalar_coord) + scalar_coord_1 = AuxCoord(1.0, var_name=scalar_coord) + cubes[0].add_aux_coord(scalar_coord_0, ()) + cubes[1].add_aux_coord(scalar_coord_1, ()) + + merged_cube = mm._combine(cubes) + assert merged_cube.shape == (5, 3) + + @pytest.mark.parametrize('span', SPAN_OPTIONS) def test_edge_case_different_time_offsets(span): cubes = ( From c543d78886c28cc4aa2a6d9999b6f9b87e576f66 Mon Sep 17 00:00:00 2001 From: Manuel Schlund Date: Mon, 7 Feb 2022 11:28:59 +0100 Subject: [PATCH 5/5] Ran isort --- tests/unit/preprocessor/_multimodel/test_multimodel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/preprocessor/_multimodel/test_multimodel.py b/tests/unit/preprocessor/_multimodel/test_multimodel.py index 913a64e065..2a9f294934 100644 --- a/tests/unit/preprocessor/_multimodel/test_multimodel.py +++ b/tests/unit/preprocessor/_multimodel/test_multimodel.py @@ -8,8 +8,8 @@ import numpy as np import pytest from cf_units import Unit -from iris.cube import Cube from iris.coords import AuxCoord +from iris.cube import Cube import esmvalcore.preprocessor._multimodel as mm from esmvalcore.iris_helpers import date2num