Skip to content
Open
Changes from all 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
54 changes: 54 additions & 0 deletions src/access_moppy/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Collaborator

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_name is invalid.

Copy link
Collaborator

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 CMIP3 as variable_name, there will be no issue raise here, but in the following process dq.get_requested_variables , following issue will occured:

ValueError: Unrecognized unique variable identifier: cmip3_compound_name
Set "variable_name" in API configuration

So it's better if we add a process here to explicitly defined which variable_name is valid.


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()])
Copy link
Collaborator

Choose a reason for hiding this comment

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

if experiment not in requested.get("experiment", {}):
    raise KeyError(f"Experiment '{experiment}' not found in data request.")



def get_cmip_mapping_metadata(mapping_type: str = "forward") -> Dict:
"""
Get metadata about the CMIP7↔CMIP6 compound name mapping files.
Expand Down
Loading