Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to cell_method parsing #6083

Merged
merged 8 commits into from
Oct 18, 2024
Merged

Conversation

ukmo-ccbunney
Copy link
Contributor

@ukmo-ccbunney ukmo-ccbunney commented Jul 24, 2024

🚀 Pull Request

Description

Iris fails to load cubes from a netCDF file if the cell_method attribute is malformed and throws this 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

For the netCDF files in my particular case, inspection of the stack trace reveals it was failing to correctly parse the cell_method attribute which was missing a space between the colon separator and time method. I.e.

cell_methods = "time:point"

rather than:

cell_methods = "time: point"

This PR makes two changes to the cell method parsing in fileformats/_nc_load_rules/helpers.py:

  1. In _split_cell_methods: If the value of cell_methods does not match the expected name: pattern, a Warning is issued and the function returns an empty list so cube loading can continue. This is consistent with the behaviour further down the function if the name: value pattern cannot be matched.

  2. Updates _CM_PARSE_NAME and _CM_PARSE regexes to make the pattern matching a little more lenient by making the space after the colon separator between the name and method pair optional. The CF Convention states that the pairs are "blank-separated list of name: method pairs". It does show a space after the colon in the examples it gives, but the CF Compliance checker is happy with no space after the colon.

Unit tests have been updated to test that:

  1. Malformed cell_method raises a warning; e.g. name but no method, such as cell_method='time'
  2. Space after colon between name and method is optional; e.g. cell_method='time:mean'

Tested on Iris version 3.10.0.dev96.

Add any of the below labels to trigger actions on this PR:

@CLAassistant
Copy link

CLAassistant commented Jul 24, 2024

CLA assistant check
All committers have signed the CLA.

ukmo-ccbunney and others added 4 commits July 29, 2024 17:22
@ukmo-ccbunney ukmo-ccbunney marked this pull request as ready for review July 29, 2024 16:23
@ukmo-ccbunney
Copy link
Contributor Author

Modified slightly to address some failing unit tests.
All tests passing locally for me now.

@bjlittle bjlittle removed their assignment Sep 2, 2024
@trexfeathers
Copy link
Contributor

trexfeathers commented Sep 11, 2024

From @SciTools/peloton:

Related to #5165.

At the very least there needs to be an informative warning, otherwise what's the point in Iris being strict about CF.

Also, if the CF checker is OK with this, maybe we should be OK with it also? @pp-mo would love your opinion on the CF checker's place in these decisions.

It could be in this case that CF actually doesn't care about the space (why should it), but just that all the examples have one because it's a bit easier to read.

@HGWright
Copy link
Contributor

HGWright commented Oct 9, 2024

From @SciTools/peloton we are accepting this as CF-compliant.

Copy link

codecov bot commented Oct 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.82%. Comparing base (d3071ff) to head (49bf592).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6083   +/-   ##
=======================================
  Coverage   89.81%   89.82%           
=======================================
  Files          88       88           
  Lines       23181    23185    +4     
  Branches     4313     4314    +1     
=======================================
+ Hits        20821    20825    +4     
  Misses       1628     1628           
  Partials      732      732           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@ESadek-MO ESadek-MO left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ukmo-ccbunney, changes all look good to me! I think it's worth just adding a small whatsnew entry for this, then it'll be good to go!

@ukmo-ccbunney
Copy link
Contributor Author

Thanks @ukmo-ccbunney, changes all look good to me! I think it's worth just adding a small whatsnew entry for this, then it'll be good to go!

Done.

Copy link
Contributor

@ESadek-MO ESadek-MO left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong PR number!

docs/src/whatsnew/latest.rst Outdated Show resolved Hide resolved
@ESadek-MO ESadek-MO merged commit 3b8b33c into SciTools:main Oct 18, 2024
21 checks passed
@ukmo-ccbunney ukmo-ccbunney deleted the cell-method-parse branch October 18, 2024 11:04
stephenworsley added a commit to stephenworsley/iris that referenced this pull request Oct 22, 2024
* main:
  [pre-commit.ci] pre-commit autoupdate (SciTools#6175)
  Updated environment lockfiles (SciTools#6183)
  Update to `cell_method` parsing (SciTools#6083)
  Bump scitools/workflows from 2024.10.0 to 2024.10.1 (SciTools#6179)
  `colorbar` keyword for iris.quickplot routines (SciTools#6169)
  Make Iris backwards compatible with Cartopy (SciTools#6172)
  Updated environment lockfiles (SciTools#6173)
  Bump scitools/workflows from 2024.09.9 to 2024.10.0 (SciTools#6170)
  Update lock files, pin Cartopy!=0.23 (SciTools#6171)
HGWright added a commit to HGWright/iris that referenced this pull request Oct 31, 2024
* upstream/main: (194 commits)
  Restore latest Whats New files.
  [pre-commit.ci] pre-commit autoupdate (SciTools#6205)
  correct major minor in whatsnew (SciTools#6202)
  What's new updates for v3.11.0rc0 . (SciTools#6201)
  Update CF standard names table. (SciTools#6200)
  Specify meta in dask.array.map_blocks (SciTools#5989)
  added in a vertical rule for surface fields (SciTools#5734)
  Updated environment lockfiles (SciTools#6197)
  Reduce duplication in cf.py spanning checks. (SciTools#6196)
  Fix attribute array comparison (SciTools#6181)
  Enable factory references to create new dimensions on load. (SciTools#6168) (SciTools#6194)
  Improve handling of masks in concatenate (SciTools#6187)
  Demo Numpy v2 (SciTools#6035)
  Bump scitools/workflows from 2024.10.1 to 2024.10.2 (SciTools#6186)
  Document use of new_axis to control merge (SciTools#6180)
  Updated environment lockfiles (SciTools#6184)
  [pre-commit.ci] pre-commit autoupdate (SciTools#6175)
  Updated environment lockfiles (SciTools#6183)
  Update to `cell_method` parsing (SciTools#6083)
  Bump scitools/workflows from 2024.10.0 to 2024.10.1 (SciTools#6179)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Loading cube fails if cell_methods is not valid
6 participants