Skip to content

Printing similar coordinates when one has numpy array attributes #3921

@rcomer

Description

@rcomer

🐛 Bug Report

If you print a cube which has two similar coordinates, there is some logic which (as far as I can tell) creates a string to distinguish the two coordinates. This logic fails if one of the coordinates happens to have attributes whose values are numpy arrays.

How To Reproduce

Minimal example:

import iris
import numpy as np

cube = iris.cube.Cube(range(5))
coord1 = iris.coords.DimCoord(
    range(5), long_name='foo', attributes=dict(bar=np.array([7, 42])))
coord2 = coord1.copy()
coord2.units = 'K'

cube.add_dim_coord(coord1, 0)
cube.add_aux_coord(coord2, 0)

print(cube)

Result:

Traceback (most recent call last):
  File "iris3_cube_print_fail.py", line 13, in <module>
    print(cube)
  File "[site-packages-path]/lib/python3.6/site-packages/iris/cube.py", line 2599, in __str__
    return self.summary()
  File "[site-packages-path]/lib/python3.6/site-packages/iris/cube.py", line 2445, in summary
    vector_dim_coords, cube_header, max_line_offset
  File "[site-packages-path]/lib/python3.6/site-packages/iris/cube.py", line 2423, in vector_summary
    vectors, vector_summary, extra_indent
  File "[site-packages-path]/lib/python3.6/site-packages/iris/cube.py", line 2215, in _summary_extra
    extra = self._summary_coord_extra(coord, indent)
  File "[site-packages-path]/lib/python3.6/site-packages/iris/cube.py", line 2198, in _summary_coord_extra
    if attributes.setdefault(key, value) != value:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Expected behaviour

I should be able to print this cube.

Environment

  • OS & Version: RHEL7.9
  • Iris Version: v3 release candidate

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions