Skip to content

Loading cube fails if cell_methods is not valid #6082

@ukmo-ccbunney

Description

@ukmo-ccbunney

🐛 Bug Report

Iris fails to load some netCDF files due to a malformed cell_method attribute, but is throwing a slightly unhelpful error:

File /opt/scitools/conda/deployments/default-2024_02_20/lib/python3.11/site-packages/iris/fileformats/_nc_load_rules/helpers.py:267, in _split_cell_methods(nc_cell_methods)
    265 for ii in range(len(name_start_inds) - 1):
    266     method_indices.append((name_start_inds[ii], name_start_inds[ii + 1]))
--> 267 method_indices.append((name_start_inds[-1], len(nc_cell_methods)))
    269 # Index the string and match against each substring
    270 nc_cell_methods_matches = []

IndexError: list index out of range

Inspection of the stack trace reveals it was failing to correctly match the cell_method attribute value because it was missing a space after the colon separator, i.e.

cell_methods = "time:point"

rather than:

cell_methods = "time: point"

This should probably raise a Warning that the cell_method could not be parsed and continue loading the cube.

How To Reproduce

Steps to reproduce the behaviour:

  1. Create a netCDF file where a variable has the following attribute (no space after colon): cell_methods = "time:point"
  2. Load using iris.load_cube(nc_filename)

Throws error IndexError: list index out of range with stack trace shown above in description.

Expected behaviour

Cube should be loaded but with a Warning stating the cell_method attribute could not be parsed.

Environment

  • Linux; RHEL7.9
  • 3.2.0.dev978

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions