-
Notifications
You must be signed in to change notification settings - Fork 78
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
leilabbb
wants to merge
6
commits into
main
Choose a base branch
from
grid-mappings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
066a9cc
Update get_grid_mapping_variables(nc)
leilabbb 4f94c05
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1c1e801
grid mapping fixes for CF section 5.6
leilabbb 86fe390
reverted one change in cf_base.py
leilabbb 739214e
reverted a change
leilabbb 0aea77f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
|
||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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 | ||
|
|
||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 validgrid_mappingrepresentation in such a case?There was a problem hiding this comment.
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:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pre-commit.ci autofix