Skip to content
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

PyAPS三次下载数据失败 #46

Open
2511250589 opened this issue Feb 24, 2025 · 1 comment
Open

PyAPS三次下载数据失败 #46

2511250589 opened this issue Feb 24, 2025 · 1 comment

Comments

@2511250589
Copy link

The following table describes the procedure:
******************** step - correct_troposphere ******************** Atmospheric correction using Weather Re-analysis dataset (PyAPS, Jolivet et al., 2011) Weather Re-analysis dataset: ERA5 tropo_pyaps3.py -f /root/work113/Stacks/mintpy/timeseries.h5 --model ERA5 -g /root/work113/Stacks/mintpy/inputs/geometryRadar.h5 -w /root/weather_data/ERA5 weather model: ERA5 - dry (hydrostatic) and wet delay weather directory: /root/weather_data/ERA5 output tropospheric delay time-series file: /root/work113/Stacks/mintpy/inputs/ERA5.h5 output corrected displacement time-series file: /root/work113/Stacks/mintpy/timeseries_ERA5.h5 read dates/time info from file: /root/work113/Stacks/mintpy/timeseries.h5 time of cloest available product: 10:00 UTC -------------------------------------------------------------------------------- Download global atmospheric model files... update mode: ON output file: /root/work113/Stacks/mintpy/inputs/ERA5.h5 1) output file either do NOT exist or is NOT newer than all GRIB files. run or skip: run -------------------------------------------------- downloading weather model data using PyAPS ... number of grib files to download: 24 -------------------------------------------------- filedir:/root/weather_data/ERA5/ERA5 INFO: You are using the latest ECMWF platform for downloading datasets: https://cds.climate.copernicus.eu/api url is None key is None url is None or key is None key;276bec02-357f-4d1d-85b1-33658fa1ec9b url:https://cds.climate.copernicus.eu/api url is None key is None url is None or key is None key;276bec02-357f-4d1d-85b1-33658fa1ec9b url:https://cds.climate.copernicus.eu/api 2025-02-24 13:51:43,991 INFO [2024-09-26T00:00:00] Watch our [Forum](https://forum.ecmwf.int/) for Announcements, news and other discussed topics. 2025-02-24 13:51:43,992 WARNING [2024-06-16T00:00:00] CDS API syntax is changed and some keys or parameter names may have also changed. To avoid requests failing, please use the "Show API request code" tool on the dataset Download Form to check you are using the correct syntax for your API request. Downloading 1 of 24: /root/weather_data/ERA5/ERA5/ERA5_N30_N40_E110_E120_20220104_10.grb {'product_type': ['reanalysis'], 'variable': ['geopotential', 'temperature', 'specific_humidity'], 'year': ['2022'], 'month': ['01'], 'day': ['04'], 'time': ['10:00'], 'pressure_level': ['1', '2', '3', '5', '7', '10', '20', '30', '50', '70', '100', '125', '150', '175', '200', '225', '250', '300', '350', '400', '450', '500', '550', '600', '650', '700', '750', '775', '800', '825', '850', '875', '900', '925', '950', '975', '1000'], 'data_format': 'grib', 'area': [40, 110, 30, 120]} WARNING: the 1 attempt to download failed, retry it. filedir:/root/weather_data/ERA5/ERA5 INFO: You are using the latest ECMWF platform for downloading datasets: https://cds.climate.copernicus.eu/api url is None key is None url is None or key is None key;276bec02-357f-4d1d-85b1-33658fa1ec9b url:https://cds.climate.copernicus.eu/api url is None key is None url is None or key is None key;276bec02-357f-4d1d-85b1-33658fa1ec9b url:https://cds.climate.copernicus.eu/api 2025-02-24 13:51:44,779 INFO [2024-09-26T00:00:00] Watch our [Forum](https://forum.ecmwf.int/) for Announcements, news and other discussed topics. 2025-02-24 13:51:44,783 WARNING [2024-06-16T00:00:00] CDS API syntax is changed and some keys or parameter names may have also changed. To avoid requests failing, please use the "Show API request code" tool on the dataset Download Form to check you are using the correct syntax for your API request. Downloading 1 of 24: /root/weather_data/ERA5/ERA5/ERA5_N30_N40_E110_E120_20220104_10.grb {'product_type': ['reanalysis'], 'variable': ['geopotential', 'temperature', 'specific_humidity'], 'year': ['2022'], 'month': ['01'], 'day': ['04'], 'time': ['10:00'], 'pressure_level': ['1', '2', '3', '5', '7', '10', '20', '30', '50', '70', '100', '125', '150', '175', '200', '225', '250', '300', '350', '400', '450', '500', '550', '600', '650', '700', '750', '775', '800', '825', '850', '875', '900', '925', '950', '975', '1000'], 'data_format': 'grib', 'area': [40, 110, 30, 120]} WARNING: the 2 attempt to download failed, retry it. filedir:/root/weather_data/ERA5/ERA5 INFO: You are using the latest ECMWF platform for downloading datasets: https://cds.climate.copernicus.eu/api url is None key is None url is None or key is None key;276bec02-357f-4d1d-85b1-33658fa1ec9b url:https://cds.climate.copernicus.eu/api url is None key is None url is None or key is None key;276bec02-357f-4d1d-85b1-33658fa1ec9b url:https://cds.climate.copernicus.eu/api 2025-02-24 13:51:45,563 INFO [2024-09-26T00:00:00] Watch our [Forum](https://forum.ecmwf.int/) for Announcements, news and other discussed topics. 2025-02-24 13:51:45,566 WARNING [2024-06-16T00:00:00] CDS API syntax is changed and some keys or parameter names may have also changed. To avoid requests failing, please use the "Show API request code" tool on the dataset Download Form to check you are using the correct syntax for your API request. Downloading 1 of 24: /root/weather_data/ERA5/ERA5/ERA5_N30_N40_E110_E120_20220104_10.grb {'product_type': ['reanalysis'], 'variable': ['geopotential', 'temperature', 'specific_humidity'], 'year': ['2022'], 'month': ['01'], 'day': ['04'], 'time': ['10:00'], 'pressure_level': ['1', '2', '3', '5', '7', '10', '20', '30', '50', '70', '100', '125', '150', '175', '200', '225', '250', '300', '350', '400', '450', '500', '550', '600', '650', '700', '750', '775', '800', '825', '850', '875', '900', '925', '950', '975', '1000'], 'data_format': 'grib', 'area': [40, 110, 30, 120]} ************************************************** WARNING: downloading failed for 3 times, stop trying and continue. ************************************************** -------------------------------------------------------------------------------- Calculate tropospheric delay and write to HDF5 file... update mode: ON output file: /root/work113/Stacks/mintpy/inputs/ERA5.h5 1) output file either do NOT exist or is NOT newer than all GRIB files. run or skip: run open geometry file: geometryRadar.h5 reading incidenceAngle data from file: /root/work113/Stacks/mintpy/inputs/geometryRadar.h5 ... reading height data from file: /root/work113/Stacks/mintpy/inputs/geometryRadar.h5 ... reading latitude data from file: /root/work113/Stacks/mintpy/inputs/geometryRadar.h5 ... reading longitude data from file: /root/work113/Stacks/mintpy/inputs/geometryRadar.h5 ... -------------------------------------------------- create HDF5 file: /root/work113/Stacks/mintpy/inputs/ERA5.h5 with w mode create dataset : date of |S1 in size of (0,) with compression = None create dataset : timeseries of <class 'numpy.float32'> in size of (0, 914, 1322) with compression = None close HDF5 file: /root/work113/Stacks/mintpy/inputs/ERA5.h5 -------------------------------------------------- calculating absolute delay for each date using PyAPS (Jolivet et al., 2011; 2014) ... number of grib files used: 0 Traceback (most recent call last): File "/root/tools/miniconda3/envs/insar/bin/smallbaselineApp.py", line 10, in <module> sys.exit(main()) File "/root/tools/miniconda3/envs/insar/lib/python3.10/site-packages/mintpy/cli/smallbaselineApp.py", line 209, in main run_smallbaselineApp(inps) File "/root/tools/miniconda3/envs/insar/lib/python3.10/site-packages/mintpy/smallbaselineApp.py", line 1155, in run_smallbaselineApp app.run(steps=inps.runSteps) File "/root/tools/miniconda3/envs/insar/lib/python3.10/site-packages/mintpy/smallbaselineApp.py", line 935, in run self.run_tropospheric_delay_correction(sname) File "/root/tools/miniconda3/envs/insar/lib/python3.10/site-packages/mintpy/smallbaselineApp.py", line 682, in run_tropospheric_delay_correction mintpy.cli.tropo_pyaps3.main(iargs) File "/root/tools/miniconda3/envs/insar/lib/python3.10/site-packages/mintpy/cli/tropo_pyaps3.py", line 166, in main run_tropo_pyaps3(inps) File "/root/tools/miniconda3/envs/insar/lib/python3.10/site-packages/mintpy/tropo_pyaps3.py", line 742, in run_tropo_pyaps3 calc_delay_timeseries(inps) File "/root/tools/miniconda3/envs/insar/lib/python3.10/site-packages/mintpy/tropo_pyaps3.py", line 672, in calc_delay_timeseries prog_bar = ptime.progressBar(maxValue=num_date, print_msg=~inps.verbose) File "/root/tools/miniconda3/envs/insar/lib/python3.10/site-packages/mintpy/objects/progress.py", line 84, in __init__ self.reset() File "/root/tools/miniconda3/envs/insar/lib/python3.10/site-packages/mintpy/objects/progress.py", line 90, in reset self.update_amount(0) # Build progress bar string File "/root/tools/miniconda3/envs/insar/lib/python3.10/site-packages/mintpy/objects/progress.py", line 109, in update_amount percentDone = (diffFromMin / float(self.span)) * 100.0 ZeroDivisionError: float division by zero


I had no problem downloading manually

import cdsapi

创建CDS API客户端

c = cdsapi.Client()

定义下载参数

years = ['2023']
months = ['12']
days = ['01']
times = ['10:00']
area = [40, 110, 30, 120] # 区域 [北, 西, 南, 东]
pressure_levels = ['1', '2', '3', '5', '7', '10', '20', '30', '50', '70', '100', '125', '150', '175', '200', '225', '250', '300', '350', '400', '450', '500', '550', '600', '650', '700', '750', '775', '800', '825', '850', '875', '900', '925', '950', '975', '1000']
variables = ['geopotential', 'temperature', 'specific_humidity']
product_type = 'reanalysis'
data_format = 'grib'

循环下载每个日期的数据

for year in years:
for month in months:
for day in days:
for time in times:
# 构建文件名
filename = f'ERA5_N30_N40_E110_E120_{year}{month}{day}_{time.replace(":", "")}.grb'
print(f'Downloading {filename}...')

            # 发起下载请求
            c.retrieve(
                'reanalysis-era5-pressure-levels',
                {
                    'product_type': product_type,
                    'variable': variables,
                    'year': year,
                    'month': month,
                    'day': day,
                    'time': time,
                    'pressure_level': pressure_levels,
                    'format': data_format,
                    'area': area,
                },
                filename
            )

            print(f'{filename} downloaded successfully.')

But we can't identify his location

Copy link

codeautopilot bot commented Feb 24, 2025

Potential solution

To solve the bug, we need to address the issues related to API credentials, request syntax, and error handling. The primary plan involves ensuring that the API credentials are correctly set, updating the API request syntax to match the latest ECMWF CDS API requirements, and improving error handling to provide more informative messages and retries. Additionally, we need to fix the ZeroDivisionError by ensuring that the progress bar logic is robust against cases where no data is available for processing.

What is causing this bug?

The bug is primarily caused by a combination of issues:

  1. API Credentials: The API key and URL are not correctly set, leading to failed authentication attempts. This is evident from the error messages indicating that the URL or key is None.

  2. API Request Syntax: The CDS API syntax has changed, and the current request parameters may not align with the latest requirements. This results in failed download attempts.

  3. Error Handling: The current error handling and retry logic are insufficient. The script retries downloads without addressing the underlying issues, leading to repeated failures.

  4. ZeroDivisionError: The progress bar logic encounters a division by zero error due to num_date being zero, which happens when no data is available for processing.

Code

Here are the implementation details and code snippets to address the issues:

  1. Verify and Update API Credentials:

    • Ensure that the model.cfg file contains the correct API key and URL for the ECMWF CDS API.
    • Verify the .cdsapirc file if used, ensuring it contains valid credentials.
  2. Update API Request Syntax:

    • Review and update the API request parameters in the ECMWFdload function to match the latest ECMWF CDS API documentation.
    • Use the "Show API request code" tool on the ECMWF website to verify the correct syntax.
  3. Improve Error Handling:

    • Implement more robust error handling and retry logic in the ECMWFdload function. Consider using exponential backoff for retries and logging detailed error messages.
  4. Fix ZeroDivisionError:

    • Ensure that num_date is correctly calculated and non-zero before initializing the progress bar. Add checks to handle cases where no dates are available for processing.
# Example of improved error handling and retry logic
def ECMWFdload():
    try:
        # Existing logic for constructing API request
        # ...

        # Improved retry logic with exponential backoff
        for attempt in range(max_retries):
            try:
                # Make API request
                # ...
                break  # Exit loop if successful
            except Exception as e:
                if attempt < max_retries - 1:
                    time.sleep(2 ** attempt)  # Exponential backoff
                else:
                    raise e  # Raise exception if all retries fail

    except Exception as e:
        print(f"Error downloading data: {e}")
        # Additional logging or error handling

How to replicate the bug

  1. Ensure that the API credentials in model.cfg or .cdsapirc are incorrect or missing.
  2. Run the script that attempts to download data using the ECMWF CDS API.
  3. Observe the error messages indicating failed authentication and download attempts.
  4. Check for the ZeroDivisionError in the progress bar logic when no data is available for processing.

By following these steps, you should be able to replicate the bug and verify the effectiveness of the proposed solution.

Click here to create a Pull Request with the proposed solution

Files used for this task:

Changes on src/pyaps3/ecmwfapi.py

Analysis of src/pyaps3/ecmwfapi.py

Overview

The file ecmwfapi.py is responsible for handling interactions with the ECMWF (European Centre for Medium-Range Weather Forecasts) API. It includes functionality for retrieving API keys, making HTTP requests, handling retries, and managing connections to the ECMWF services.

Key Components

  1. API Key Retrieval: The file contains functions to retrieve API keys from environment variables or a configuration file (~/.ecmwfapirc). This is crucial for authenticating requests to the ECMWF API.

  2. HTTP Request Handling: The Connection class manages HTTP requests, including setting headers, handling redirects, and managing retries for failed requests. The robust decorator is used to retry requests in case of transient errors.

  3. Error Handling: Custom exceptions like APIKeyFetchError, RetryError, and APIException are defined to handle specific error scenarios, such as missing API keys or HTTP errors.

  4. Data Transfer: The APIRequest class handles the execution of requests and the transfer of data from the ECMWF API to a local target file.

Potential Issues

  1. API Key Retrieval: If the API key or URL is not correctly set in the environment or the configuration file, requests will fail. The error message in the issue suggests that the URL or key might be None, indicating a potential problem in retrieving these values.

  2. API Request Syntax: The issue mentions that the CDS API syntax has changed. This file should be reviewed to ensure that the request payloads and headers align with the latest ECMWF API specifications.

  3. Error Handling and Retries: The current retry mechanism retries up to 10 times with a delay of 60 seconds. If the API syntax is incorrect, retries will not resolve the issue. The error handling should be updated to provide more informative messages and possibly suggest checking the API syntax.

Recommendations

  • Verify API Key Retrieval: Ensure that the environment variables or the configuration file (~/.ecmwfapirc) are correctly set with the latest API key, URL, and email. This can be done by checking the output of the get_apikey_values function.

  • Update API Request Syntax: Review the payload structure and headers in the Connection.call method to ensure they match the latest ECMWF API requirements. This may involve updating the Content-Type or other headers.

  • Improve Error Messages: Enhance the error messages to provide more context, especially when retries are exhausted. This can help users diagnose issues more effectively.

  • Test with Latest API: Conduct tests using the latest ECMWF API to ensure compatibility and identify any further changes needed in the request handling logic.

By addressing these areas, the functionality of the ECMWF API interactions should be improved, potentially resolving the issues described in the ticket.

Changes on src/pyaps3/model.cfg

Analysis Report for src/pyaps3/model.cfg

Overview

The model.cfg file is a configuration file intended to store API credentials and other necessary information for accessing various atmospheric data sources, including ECMWF and NASA Earthdata. The file is structured to hold keys and login information for different datasets like ERA5, ERA-Interim, and MERRA2.

Potential Issues

  1. Missing ECMWF API Key: The configuration for ECMWF under the [ECMWF] section is missing a key. This is crucial for authenticating requests to the ECMWF data servers. The absence of this key could lead to failed data retrieval attempts, as seen in the issue description.

  2. Outdated Information: The comment regarding ERA-Interim is marked as outdated. This might not directly affect the current issue if ERA-Interim is not being used, but it indicates that the configuration file may not be up-to-date with the latest data access methods.

  3. Placeholder Values: The file contains placeholder values such as your-personal-access-token and [email protected]. These need to be replaced with actual credentials for the system to function correctly.

Recommendations

  1. Update ECMWF Credentials: Ensure that the [ECMWF] section contains a valid API key. This key should be obtained from the ECMWF website and correctly input into the configuration file.

  2. Verify CDS API Key: The [CDS] section should also have a valid personal access token. Verify that this token is correct and has not expired.

  3. Remove Outdated Sections: If ERA-Interim is no longer used, consider removing or updating this section to avoid confusion.

  4. Secure Credentials: Ensure that the credentials are stored securely and are not exposed in public repositories.

  5. Documentation: Update comments and documentation within the file to reflect the current usage and requirements for accessing the data sources.

By addressing these issues, the configuration file will be better aligned with the current requirements for accessing ECMWF and other atmospheric data, potentially resolving the download failures described in the issue.

Changes on src/pyaps3/autoget.py

Analysis of src/pyaps3/autoget.py

The file autoget.py is responsible for downloading atmospheric model data from various sources, including ECMWF (ERA5, ERA-Interim, HRES), MERRA, and NARR. The function ECMWFdload is particularly relevant to the issue described, as it handles downloading data from the ECMWF platform using the CDS API.

Key Points and Potential Issues

  1. API Credentials and Configuration:

    • The script reads API credentials from a configuration file model.cfg. If the file is missing or the credentials are incorrect, the download will fail.
    • The script checks for a .cdsapirc file in the user's home directory for CDS API credentials. If this file is missing or misconfigured, it defaults to using credentials from model.cfg.
  2. API Request Syntax:

    • The script constructs a dictionary indict with parameters for the API request. This includes product type, variables, date, time, pressure levels, and area.
    • The error message in the issue suggests that there might be a problem with the API request syntax, possibly due to changes in the CDS API.
  3. Error Handling and Retries:

    • The script does not appear to have robust error handling for failed downloads. The issue description indicates that downloads fail after three attempts, suggesting a need for improved retry logic or error handling.
  4. ZeroDivisionError:

    • The traceback in the issue description shows a ZeroDivisionError in the progress bar update logic. This is likely due to self.span being zero, which could occur if num_date is zero, indicating no dates were processed due to download failures.

Recommendations

  • Verify and Update API Credentials:

    • Ensure that the model.cfg file contains the correct URL and key for the ECMWF CDS API. Also, verify the .cdsapirc file if used.
  • Update API Request Syntax:

    • Review the API request parameters in indict to ensure they align with the latest ECMWF CDS API documentation. Use the "Show API request code" tool on the ECMWF website to verify the correct syntax.
  • Improve Error Handling:

    • Implement more robust error handling and retry logic in the ECMWFdload function. Consider exponential backoff or logging detailed error messages to help diagnose issues.
  • Fix ZeroDivisionError:

    • Ensure that num_date is correctly calculated and non-zero before initializing the progress bar. Add checks to handle cases where no dates are available for processing.

By addressing these points, the script should be more resilient to API changes and network issues, reducing the likelihood of download failures and related errors.

This comment was generated by AI. Information provided may be incorrect.

Current plan usage: 0%

Have feedback or need help?
Documentation
[email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant