From 922d32e20c4844459c7c083267d8504bbd95b7ab Mon Sep 17 00:00:00 2001 From: Patrick Peglar Date: Tue, 2 Apr 2024 15:31:03 +0100 Subject: [PATCH] Add tests for cube printout of real/lazy/hybrid scalar coordinates. --- .../cube_summary/test_CubeSummary.py | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py b/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py index 573baeff52..4d40bcf5ff 100644 --- a/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py +++ b/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py @@ -3,10 +3,12 @@ # This file is part of Iris and is released under the BSD license. # See LICENSE in the root of the repository for full licensing details. """Unit tests for :class:`iris._representation.cube_summary.CubeSummary`.""" +import dask.array as da import numpy as np import pytest from iris._representation.cube_summary import CubeSummary +from iris.aux_factory import HybridHeightFactory from iris.coords import AncillaryVariable, AuxCoord, CellMeasure, CellMethod, DimCoord from iris.cube import Cube from iris.tests.stock.mesh import sample_mesh_cube @@ -136,6 +138,59 @@ def test_scalar_coord(self): assert text_summary_awkward.lines == ["a is", "b", " and c"] assert text_summary_awkward.extra == "" + @pytest.mark.parametrize("bounds", ["withbounds", "nobounds"]) + def test_lazy_scalar_coord(self, bounds): + """Check when we print 'lazy' instead of values for a lazy scalar coord.""" + coord = AuxCoord(da.ones((), dtype=float), long_name="foo") + if bounds == "withbounds": + # These might be real or lazy -- it makes no difference. + coord.bounds = np.arange(2.0) + cube = Cube([0.0], aux_coords_and_dims=[(coord, ())]) + + rep = CubeSummary(cube) + + summary = rep.scalar_sections["Scalar coordinates:"].contents[0] + assert summary.name == "foo" + expect_content = "" + if bounds == "withbounds": + expect_content += "+bound" + assert summary.content == expect_content + + @pytest.mark.parametrize("deps", ["deps_all_real", "deps_some_lazy"]) + def test_hybrid_scalar_coord(self, deps): + """Check whether we print a value or '', for a hybrid scalar coord.""" + # NOTE: hybrid coords are *always* lazy (at least for now). However, as long as + # no dependencies are lazy, then we print a value rather than "". + + # Construct a test hybrid coord, using HybridHeight as a template because that + # is both a common case and a fairly simple one (only 3 dependencies). + # Note: *not* testing with bounds, since lazy bounds always print the same way. + all_deps_real = deps == "deps_all_real" + aux_coords = [ + AuxCoord(1.0, long_name=name, units=units) + for name, units in (("delta", "m"), ("sigma", "1"), ("orography", "m")) + ] + if not all_deps_real: + # Make one dependency lazy + aux_coords[0].points = aux_coords[0].lazy_points() + + cube = Cube( + [0.0], + aux_coords_and_dims=[(co, ()) for co in aux_coords], + aux_factories=[HybridHeightFactory(*aux_coords)], + ) + + rep = CubeSummary(cube) + + summary = rep.scalar_sections["Scalar coordinates:"].contents[0] + assert summary.name == "altitude" + # Check that the result shows lazy with lazy deps, or value when all real + if all_deps_real: + expect_content = "2.0 m" + else: + expect_content = " m" + assert summary.content == expect_content + def test_cell_measure(self): cube = self.cube cell_measure = CellMeasure([1, 2, 3], long_name="foo")