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

Cellmethod tolerance #5126

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tolerate and warn various 'empty' cell-methods.
  • Loading branch information
pp-mo committed Jan 4, 2023
commit 2700ff1b82e9b7986cd51c986faef84418043d71
15 changes: 10 additions & 5 deletions lib/iris/fileformats/netcdf/_parse_cell_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@ def _split_cell_methods(nc_cell_methods: str) -> List[re.Match]:
name_start_inds.remove(ind)

# List tuples of indices of starts and ends of the cell methods in the string
method_indices = []
for ii in range(len(name_start_inds) - 1):
method_indices.append((name_start_inds[ii], name_start_inds[ii + 1]))
method_indices.append((name_start_inds[-1], len(nc_cell_methods)))
name_start_inds.append(len(nc_cell_methods))
method_indices = list(zip(name_start_inds[:-1], name_start_inds[1:]))

# Index the string and match against each substring
nc_cell_methods_matches = []
Expand Down Expand Up @@ -137,7 +135,14 @@ def parse_cell_methods(nc_cell_methods):

cell_methods = []
if nc_cell_methods is not None:
for m in _split_cell_methods(nc_cell_methods):
splits = _split_cell_methods(nc_cell_methods)
if not splits:
msg = (
f"NetCDF variable cell_methods of {nc_cell_methods!r} "
"contains no valid cell methods."
)
warnings.warn(msg, UserWarning)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be an Error

#5067 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

Well I disagree, for all the reasons listed there.
I think it's just unhelpful to refuse to load a file, if the problem can be stepped around.

Copy link
Member

Choose a reason for hiding this comment

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

In order to work with data from ESGF, it is important for ESMValTool that we can load malformed data and correct the iris cube after loading the bits that are fine. The alternatives are less attractive:

  1. Copy the malformed file, fix it and then load that with iris. This is computationally very inefficient because it requires a lot of writing to disk.
  2. Use xarray to load the malformed file, fix the issues, and convert the DataArray to an iris cube. This would be a good alternative if the conversion reliably worked.

Copy link
Member Author

@pp-mo pp-mo Jan 5, 2023

Choose a reason for hiding this comment

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

Also recalling this passionate appeal ...
#4506 (comment)
!

Copy link
Member Author

Choose a reason for hiding this comment

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

But... we are really serious now about the approach (2.) "Use xarray ... convert"
Relating to #4994, proposals are still working up : see here

Current status : I have functional code, and we have pretty much decided now on a plan...

  • set up a separate scitools repo
  • generic python classes for handling netcdf-data
  • free+fast conversion between these and any of : netcdf file data / iris cubes / xarray datasets

Frankly though, this will take a while to establish properly : I have as yet no home repo, no tests written + plenty of other priorities getting in the way.

for m in splits:
d = m.groupdict()
method = d[_CM_METHOD]
method = method.strip()
Expand Down