Skip to content

Cannot load cubes that contain multiple variables with array-type attributes with different sizes #6027

Closed
@schlunma

Description

@schlunma

🐛 Bug Report

How To Reproduce

Steps to reproduce the behaviour:

import iris
from iris.cube import Cube, CubeList
print("iris version", iris.__version__)

iris.FUTURE.save_split_attrs = True  # commenting this does not change the outcome

cubes = CubeList([
    Cube(0.0, var_name='a', attributes={'att': [0, 1]}),
    Cube(0.0, var_name='b', attributes={'att': [0, 1, 2]}),
])

path = 'data/cubes_with_attributes_bug.nc'
iris.save(cubes, path)
print("Saved", path)

loaded_cubes = iris.load(path)

gives

iris version 3.9.0
Saved data/cubes_with_attributes_bug.nc
Traceback (most recent call last):
  File "/home/b/b309141/scripts/iris/attributes.py", line 20, in <module>
    loaded_cubes = iris.load(path)
                   ^^^^^^^^^^^^^^^
  File "/work/bd0854/b309141/miniforge3/envs/esm/lib/python3.11/site-packages/iris/__init__.py", line 326, in load
    return _load_collection(uris, constraints, callback).merged().cubes()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/work/bd0854/b309141/miniforge3/envs/esm/lib/python3.11/site-packages/iris/cube.py", line 126, in merged
    return _CubeFilterCollection([pair.merged(unique) for pair in self.pairs])
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/work/bd0854/b309141/miniforge3/envs/esm/lib/python3.11/site-packages/iris/cube.py", line 126, in <listcomp>
    return _CubeFilterCollection([pair.merged(unique) for pair in self.pairs])
                                  ^^^^^^^^^^^^^^^^^^^
  File "/work/bd0854/b309141/miniforge3/envs/esm/lib/python3.11/site-packages/iris/cube.py", line 85, in merged
    return _CubeFilter(self.constraint, self.cubes.merge(unique))
                                        ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/work/bd0854/b309141/miniforge3/envs/esm/lib/python3.11/site-packages/iris/cube.py", line 482, in merge
    if target_proto_cube.register(cube):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/work/bd0854/b309141/miniforge3/envs/esm/lib/python3.11/site-packages/iris/_merge.py", line 1310, in register
    match = cube_signature.match(other, error_on_mismatch)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/work/bd0854/b309141/miniforge3/envs/esm/lib/python3.11/site-packages/iris/_merge.py", line 427, in match
    msgs = self._defn_msgs(other.defn)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/work/bd0854/b309141/miniforge3/envs/esm/lib/python3.11/site-packages/iris/_merge.py", line 372, in _defn_msgs
    if self_defn.attributes != other_defn.attributes:
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/work/bd0854/b309141/miniforge3/envs/esm/lib/python3.11/site-packages/iris/cube.py", line 910, in __eq__
    result = self.locals == other.locals and self.globals == other.globals
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/work/bd0854/b309141/miniforge3/envs/esm/lib/python3.11/site-packages/iris/common/mixin.py", line 104, in __eq__
    match = value == other[key]
            ^^^^^^^^^^^^^^^^^^^
ValueError: operands could not be broadcast together with shapes (2,) (3,)

It looks like this is related to the new local/global attribute change introduced in iris 3.8.0. Replacing the simple == with a np.array_equal in two places of the code fixes this (this should also works with other types, not only with numpy arrays).

Expected behaviour

No error when loading the data.

Environment

  • OS & Version: Red Hat Enterprise Linux 8.8 (Ootpa)
  • Iris Version: 3.9.0

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions