diff --git a/lib/iris/cube.py b/lib/iris/cube.py index a15951900b..f69bae1b7c 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -2424,7 +2424,7 @@ def vector_summary( delta = max_line_offset - min_alignment + 5 cube_header = "%-*s (%s)" % ( int(name_padding + delta), - self.name() or "unknown", + nameunit, dimension_header, ) alignment += delta @@ -2481,7 +2481,11 @@ def vector_summary( # Calculate the maximum line offset. max_line_offset = 0 - for coord in all_coords: + for coord in ( + list(all_coords) + + self.ancillary_variables() + + self.cell_measures() + ): max_line_offset = max( max_line_offset, len( diff --git a/lib/iris/tests/results/cdm/str_repr/simple.__str__.txt b/lib/iris/tests/results/cdm/str_repr/simple.__str__.txt index b174d346d2..59e3aba236 100644 --- a/lib/iris/tests/results/cdm/str_repr/simple.__str__.txt +++ b/lib/iris/tests/results/cdm/str_repr/simple.__str__.txt @@ -1,4 +1,4 @@ -thingness (foo: 11) +thingness / (1) (foo: 11) Dimension coordinates: foo x Auxiliary coordinates: diff --git a/lib/iris/tests/unit/cube/test_Cube.py b/lib/iris/tests/unit/cube/test_Cube.py index ded401cab3..9cf64b6b89 100644 --- a/lib/iris/tests/unit/cube/test_Cube.py +++ b/lib/iris/tests/unit/cube/test_Cube.py @@ -596,6 +596,58 @@ def test_similar_coords(self): self.cube.add_aux_coord(coord) self.assertIn("baz", self.cube.summary()) + def test_long_components(self): + # Check that components with long names 'stretch' the printout correctly. + cube = Cube(np.zeros((20, 20, 20)), units=1) + dimco = DimCoord(np.arange(20), long_name="dimco") + auxco = AuxCoord(np.zeros(20), long_name="auxco") + ancil = AncillaryVariable(np.zeros(20), long_name="ancil") + cellm = CellMeasure(np.zeros(20), long_name="cellm") + cube.add_dim_coord(dimco, 0) + cube.add_aux_coord(auxco, 0) + cube.add_cell_measure(cellm, 1) + cube.add_ancillary_variable(ancil, 2) + + original_summary = cube.summary() + long_name = "long_name______________________________________" + for component in (dimco, auxco, ancil, cellm): + # For each (type of) component, set a long name so the header columns get shifted. + old_name = component.name() + component.rename(long_name) + new_summary = cube.summary() + component.rename( + old_name + ) # Put each back the way it was afterwards + + # Check that the resulting 'stretched' output has dimension columns aligned correctly. + lines = new_summary.split("\n") + header = lines[0] + colon_inds = [ + i_char for i_char, char in enumerate(header) if char == ":" + ] + for line in lines[1:]: + # Replace all '-' with 'x' to make checking easier, and add a final buffer space. + line = line.replace("-", "x") + " " + if " x " in line: + # For lines with any columns : check that columns are where expected + for col_ind in colon_inds: + # Chop out chars before+after each expected column. + self.assertEqual( + line[col_ind - 1 : col_ind + 2], " x " + ) + + # Finally also: compare old with new, but replacing new name and ignoring spacing differences + def collapse_space(string): + # Replace all multiple spaces with a single space. + while " " in string: + string = string.replace(" ", " ") + return string + + self.assertEqual( + collapse_space(new_summary).replace(long_name, old_name), + collapse_space(original_summary), + ) + class Test_is_compatible(tests.IrisTest): def setUp(self):