Skip to content

Update get_grid_mapping_variables(nc) #1165

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Update get_grid_mapping_variables(nc) #1165

wants to merge 6 commits into from

Conversation

leilabbb
Copy link
Contributor

@leilabbb leilabbb commented Apr 28, 2025

  • Added a function extract_grid_mapping_names(grid_mapping_string) to extracts all grid mapping variable names from the grid_mapping variable's attribute.
  • Updated get_grid_mapping_variables(nc) to include the new function and returns a set of all grid mapping variable names that are present in the dataset.

- added a function extract_crs_names(grid_mapping_string) to extracts all grid mapping variable names from a grid_mapping
- updated get_grid_mapping_variables(nc) to include the new function and returns a set of all grid mapping variable names that are present in the dataset.
@benjwadams
Copy link
Contributor

pre-commit.ci autofix

: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
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
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
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

Added _check_gmattr_existence_condition_crs_name() in cf_1_7.py to satisfy: If projected_crs_name is defined then geographic_crs_name must be also.

Fixed _check_gmattr_existence_condition_ell_pmerid_hdatum in cf_1_7.py to satisfy: If any of reference_ellipsoid_name, prime_meridian_name, or horizontal_datum_name or geographic_crs_name are defined, all must be defined.

Rewritted and added missing messages in cf_base.py

Fixed
extract_grid_mapping_names() in util.py to extract the grid mapping variable names from a grid mapping string

Changed
test_check_grid_mapping() in test_cf.py
to mimic netcdf variables and pass them to check_grid_mapping() in cf_base.py to test the grid mapping variable's attributes.
@leilabbb leilabbb marked this pull request as ready for review May 1, 2025 00:08
put back "Could not consume entire grid_mapping expression, please check for well-formedness"
reverted to f"Coordinate-related variable {ref_var} referenced by grid_mapping variable {grid_var_name} must exist in this dataset"
@leilabbb
Copy link
Contributor Author

leilabbb commented May 1, 2025

pre-commit.ci autofix

else grid_mapping_string.split()
)


Copy link
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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve CF Conformance: 5.6 Grid Mappings and Projections
2 participants