Skip to content

Commit bb7994f

Browse files
authored
Optional import of data_request_api (#216)
* Make data_request_api optional * Fix import handling for data_request_api and clean up whitespace in mapping functions
1 parent 454e742 commit bb7994f

1 file changed

Lines changed: 38 additions & 2 deletions

File tree

src/access_moppy/utilities.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,17 @@
1010
import pandas as pd
1111
import xarray as xr
1212
from cftime import date2num, num2date
13-
from data_request_api.content import dump_transformation as dt
14-
from data_request_api.query import data_request as dr
13+
14+
# Optional import for CMIP7 data request functionality
15+
try:
16+
from data_request_api.content import dump_transformation as dt
17+
from data_request_api.query import data_request as dr
18+
19+
DATA_REQUEST_API_AVAILABLE = True
20+
except ImportError:
21+
dt = None
22+
dr = None
23+
DATA_REQUEST_API_AVAILABLE = False
1524

1625
type_mapping = {
1726
"real": np.float32,
@@ -2493,10 +2502,19 @@ def generate_cmip7_to_cmip6_mapping(
24932502
Returns:
24942503
Dictionary mapping CMIP7 compound names to CMIP6 compound names
24952504
2505+
Raises:
2506+
ImportError: If data_request_api package is not available
2507+
24962508
Example:
24972509
>>> mapping = generate_cmip7_to_cmip6_mapping()
24982510
>>> print(mapping["Amon.tas"]) # Should return corresponding CMIP6 name
24992511
"""
2512+
if not DATA_REQUEST_API_AVAILABLE:
2513+
raise ImportError(
2514+
"data_request_api package is required for generating CMIP7 mappings. "
2515+
"Install it with: pip install CMIP7-data-request-api"
2516+
)
2517+
25002518
# Generate both mappings efficiently and return only the forward mapping
25012519
forward_mapping, _ = generate_both_cmip_mappings(
25022520
version=version,
@@ -2568,10 +2586,19 @@ def generate_cmip6_to_cmip7_mapping(
25682586
Returns:
25692587
Dictionary mapping CMIP6 compound names to CMIP7 compound names
25702588
2589+
Raises:
2590+
ImportError: If data_request_api package is not available
2591+
25712592
Example:
25722593
>>> reverse_mapping = generate_cmip6_to_cmip7_mapping()
25732594
>>> print(reverse_mapping["AERmon.abs550aer"]) # Should return corresponding CMIP7 name
25742595
"""
2596+
if not DATA_REQUEST_API_AVAILABLE:
2597+
raise ImportError(
2598+
"data_request_api package is required for generating CMIP7 mappings. "
2599+
"Install it with: pip install CMIP7-data-request-api"
2600+
)
2601+
25752602
# Generate both mappings efficiently and return only the reverse mapping
25762603
_, reverse_mapping = generate_both_cmip_mappings(
25772604
version=version,
@@ -2686,11 +2713,20 @@ def generate_both_cmip_mappings(
26862713
Returns:
26872714
Tuple of (forward_mapping, reverse_mapping) dictionaries
26882715
2716+
Raises:
2717+
ImportError: If data_request_api package is not available
2718+
26892719
Example:
26902720
>>> forward, reverse = generate_both_cmip_mappings()
26912721
>>> cmip6_name = forward["aerosol.abs550aer.tavg-u-hxy-u.mon.GLB"]
26922722
>>> cmip7_name = reverse[cmip6_name]
26932723
"""
2724+
if not DATA_REQUEST_API_AVAILABLE:
2725+
raise ImportError(
2726+
"data_request_api package is required for generating CMIP7 mappings. "
2727+
"Install it with: pip install CMIP7-data-request-api"
2728+
)
2729+
26942730
print("Generating both CMIP7<->CMIP6 compound name mappings...")
26952731
print("This may take a moment as it queries the CMIP7 data request API...")
26962732

0 commit comments

Comments
 (0)