Skip to content

Commit

Permalink
Widen cube printout for long ancil or cell-measure names. (#4124)
Browse files Browse the repository at this point in the history
* Widen cube printout for long ancil or cell-measure names.

* Adjust result for fixed cube-units printout.
  • Loading branch information
pp-mo authored Jun 25, 2021
1 parent bfc6373 commit 2e34c0d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -2433,7 +2433,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
Expand Down Expand Up @@ -2490,7 +2490,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(
Expand Down
2 changes: 1 addition & 1 deletion lib/iris/tests/results/cdm/str_repr/simple.__str__.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
thingness (foo: 11)
thingness / (1) (foo: 11)
Dimension coordinates:
foo x
Auxiliary coordinates:
Expand Down
52 changes: 52 additions & 0 deletions lib/iris/tests/unit/cube/test_Cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,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):
Expand Down

0 comments on commit 2e34c0d

Please sign in to comment.