-
Notifications
You must be signed in to change notification settings - Fork 1
Add function to retrieve requested variables from data request API #228
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
rbeucher
wants to merge
1
commit into
main
Choose a base branch
from
add_utility
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 all commits
Commits
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 |
|---|---|---|
|
|
@@ -2649,6 +2649,60 @@ def load_cmip6_to_cmip7_mapping(mapping_path: Optional[str] = None) -> Dict[str, | |
| return mapping | ||
|
|
||
|
|
||
| def get_requested_variables_from_data_request( | ||
| experiment: str = "historical", | ||
| priority: str = "Core", | ||
| variable_name: str = "CMIP6", | ||
| dreq_version: str = "v1.2.2.3", | ||
| ) -> List[str]: | ||
| """ | ||
| Return requested variables for a given experiment and priority from a data request. | ||
|
|
||
| Args: | ||
| experiment: Experiment key under ``requested["experiment"]``. | ||
| priority: Priority class key (for example ``"Core"``). | ||
| variable_name: Variable naming convention prefix used by | ||
| ``data_request_api.utilities.config.update_config``. | ||
| dreq_version: Data request version string to retrieve. | ||
|
|
||
| Returns: | ||
| A list of requested variable names. | ||
|
|
||
| Raises: | ||
| ImportError: If ``DATA_REQUEST_API_AVAILABLE`` is ``False``. | ||
|
|
||
| Example: | ||
| >>> get_requested_variables_from_data_request("historical", "Core") | ||
| """ | ||
| if not DATA_REQUEST_API_AVAILABLE: | ||
| raise ImportError( | ||
| "data_request_api package is required for querying requested variables. " | ||
| "Install it with: pip install CMIP7-data-request-api" | ||
| ) | ||
|
|
||
| from data_request_api.content import dreq_content as dc | ||
| from data_request_api.query import dreq_query as dq | ||
| from data_request_api.utilities.config import update_config | ||
|
|
||
| update_config("variable_name", f"{variable_name} Compound Name") | ||
|
|
||
| dc.retrieve(dreq_version) | ||
| dreq_content = dc.load(dreq_version) | ||
| dreq_tables = dq.create_dreq_tables_for_request( | ||
| content=dreq_content, | ||
| dreq_version=dreq_version, | ||
| ) | ||
| requested = dq.get_requested_variables( | ||
| content=dreq_tables, | ||
| dreq_version=dreq_version, | ||
| verbose=False, | ||
| check_core_variables=False, | ||
| priority_cutoff=priority.lower(), | ||
| ) | ||
|
|
||
| return list(requested["experiment"][experiment][priority.capitalize()]) | ||
|
Collaborator
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. If experiment or priority is not present in the returned dictionary, a KeyError will be raised directly, and the error message is not very user-friendly. It is recommended to add explicit checks and raise a more meaningful exception. For example: |
||
|
|
||
|
|
||
| def get_cmip_mapping_metadata(mapping_type: str = "forward") -> Dict: | ||
| """ | ||
| Get metadata about the CMIP7↔CMIP6 compound name mapping files. | ||
|
|
||
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.
Not sure what will happen on this line if
variable_nameis invalid.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.
Actually, during test, if I define
CMIP3asvariable_name, there will be no issue raise here, but in the following processdq.get_requested_variables, following issue will occured:So it's better if we add a process here to explicitly defined which
variable_nameis valid.