Skip to content

Unexpected behaviour when merging cubelist with constant NaN-valued scalar coord #4681

Open
@btrotta-bom

Description

@btrotta-bom

Merge seems to treat scalar coordinates differently when they have value NaN. I am trying to merge a list of cubes, each of which has a scalar coordinate with the same value. If this value is not NaN, the merge works as expected: the resulting cube has a scalar coordinate identical to the one in the input cubes. But if the value is NaN, this coordinate becomes 2-dimensional in the result.

Example below:

import iris
import iris.cube
import numpy as np
from iris.coords import DimCoord, AuxCoord

data = np.random.random((3, 3))
dim_coords_and_dims = [(DimCoord(np.arange(3), long_name="coord1"), 0)]

# aux_coord is constant non-nan value, merged cube contains aux_coord as a scalar coord as expected
aux_coord = AuxCoord([0], long_name="aux_coord")
cubelist = iris.cube.CubeList()
for i in range(2):
    for j in range(2):
        aux_coords_and_dims = [
            (DimCoord([i], long_name="merge_coord1"), None),
            (DimCoord([j], long_name="merge_coord2"), None),
            (aux_coord, None),
        ]
        c = iris.cube.Cube(
            data,
            dim_coords_and_dims=dim_coords_and_dims,
            aux_coords_and_dims=aux_coords_and_dims,
        )
        cubelist.append(c)

cube = cubelist.merge_cube()
print(cube)
print(cube.coord("aux_coord"))

# aux_coord is nan, merged cube contains aux_coord as a 2-dimensional auxiliary coord
aux_coord = iris.coords.AuxCoord([np.nan], long_name="aux_coord")
cubelist = iris.cube.CubeList()
for i in range(2):
    for j in range(2):
        aux_coords_and_dims = [
            (DimCoord([i], long_name="merge_coord1"), None),
            (DimCoord([j], long_name="merge_coord2"), None),
            (aux_coord, None),
        ]
        c = iris.cube.Cube(
            data,
            dim_coords_and_dims=dim_coords_and_dims,
            aux_coords_and_dims=aux_coords_and_dims,
        )
        cubelist.append(c)

cube = cubelist.merge_cube()
print(cube)
print(cube.coord("aux_coord"))

Metadata

Metadata

Assignees

No one assigned

    Labels

    Peloton 🚴‍♂️Target a breakaway issue to be caught and closed by the peloton

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions