Skip to content
Open
Changes from 2 commits
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
20 changes: 17 additions & 3 deletions compliance_checker/cf/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,16 +930,30 @@ def get_flag_variables(nc):
return flag_variables


def extract_grid_mapping_names(grid_mapping_string):
"""
Extracts all grid mapping variable names from a grid_mapping string.

:param str grid_mapping_string: The grid_mapping attribute string
:return list[str]: List of grid mapping variable names
"""
return re.findall(r"\b\w+(?=:)|\b\w+(?=\s+\w+:)|\b\w+$", grid_mapping_string)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Would just some_grid_mapping: be accepted under this regex? Is it a valid grid_mapping representation in such a case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this is the documentation text for grid_mapping:
"The attribute takes a string value with two possible formats. In the first format, it is a single word, which names a grid mapping variable. In the second format, it is a blank-separated list of words ": [ …​] [: …​]" , which identifies one or more grid mapping variables, and with each grid mapping associates one or more coordinatesVariables, i.e. coordinate variables or auxiliary coordinate variables."

the code works for ALL cases:

  • Words followed by : are picked up.
  • Words not followed by : but separated by spaces (and at the end) are picked up.
  • Single isolated words are picked up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

pre-commit.ci autofix



Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

changed it to include the only two possible formats:

Key-value-like: "key: value" (key is the grid mapping variables
Space-separated names: "x y z" ( x y z are the grid mapping variables)

def get_grid_mapping_variables(nc):
"""
Returns a list of grid mapping variables
Returns a set of all grid mapping variable names that are present in the dataset.

:param netCDF4.Dataset nc: An open netCDF4 Dataset
:return set[str]: Set of grid mapping variable names
"""
grid_mapping_variables = set()
for ncvar in nc.get_variables_by_attributes(grid_mapping=lambda x: x is not None):
if ncvar.grid_mapping in nc.variables:
grid_mapping_variables.add(ncvar.grid_mapping)
grid_mapping_string = ncvar.grid_mapping
grid_mapping_names = extract_grid_mapping_names(grid_mapping_string)
for name in grid_mapping_names:
if name in nc.variables:
grid_mapping_variables.add(name)
return grid_mapping_variables


Expand Down
Loading