|
10 | 10 | import pandas as pd |
11 | 11 | import xarray as xr |
12 | 12 | 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 |
15 | 24 |
|
16 | 25 | type_mapping = { |
17 | 26 | "real": np.float32, |
@@ -2493,10 +2502,19 @@ def generate_cmip7_to_cmip6_mapping( |
2493 | 2502 | Returns: |
2494 | 2503 | Dictionary mapping CMIP7 compound names to CMIP6 compound names |
2495 | 2504 |
|
| 2505 | + Raises: |
| 2506 | + ImportError: If data_request_api package is not available |
| 2507 | +
|
2496 | 2508 | Example: |
2497 | 2509 | >>> mapping = generate_cmip7_to_cmip6_mapping() |
2498 | 2510 | >>> print(mapping["Amon.tas"]) # Should return corresponding CMIP6 name |
2499 | 2511 | """ |
| 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 | + |
2500 | 2518 | # Generate both mappings efficiently and return only the forward mapping |
2501 | 2519 | forward_mapping, _ = generate_both_cmip_mappings( |
2502 | 2520 | version=version, |
@@ -2568,10 +2586,19 @@ def generate_cmip6_to_cmip7_mapping( |
2568 | 2586 | Returns: |
2569 | 2587 | Dictionary mapping CMIP6 compound names to CMIP7 compound names |
2570 | 2588 |
|
| 2589 | + Raises: |
| 2590 | + ImportError: If data_request_api package is not available |
| 2591 | +
|
2571 | 2592 | Example: |
2572 | 2593 | >>> reverse_mapping = generate_cmip6_to_cmip7_mapping() |
2573 | 2594 | >>> print(reverse_mapping["AERmon.abs550aer"]) # Should return corresponding CMIP7 name |
2574 | 2595 | """ |
| 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 | + |
2575 | 2602 | # Generate both mappings efficiently and return only the reverse mapping |
2576 | 2603 | _, reverse_mapping = generate_both_cmip_mappings( |
2577 | 2604 | version=version, |
@@ -2686,11 +2713,20 @@ def generate_both_cmip_mappings( |
2686 | 2713 | Returns: |
2687 | 2714 | Tuple of (forward_mapping, reverse_mapping) dictionaries |
2688 | 2715 |
|
| 2716 | + Raises: |
| 2717 | + ImportError: If data_request_api package is not available |
| 2718 | +
|
2689 | 2719 | Example: |
2690 | 2720 | >>> forward, reverse = generate_both_cmip_mappings() |
2691 | 2721 | >>> cmip6_name = forward["aerosol.abs550aer.tavg-u-hxy-u.mon.GLB"] |
2692 | 2722 | >>> cmip7_name = reverse[cmip6_name] |
2693 | 2723 | """ |
| 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 | + |
2694 | 2730 | print("Generating both CMIP7<->CMIP6 compound name mappings...") |
2695 | 2731 | print("This may take a moment as it queries the CMIP7 data request API...") |
2696 | 2732 |
|
|
0 commit comments