ecmwf-opendata Python Package

repository·main·Indexed 43 days ago

https://github.com/ecmwf/ecmwf-opendata

A Python package designed to simplify downloading ECMWF open data using a request-based interface. It provides a Client for retrieving specific meteorological data subsets via Client.retrieve(), downloading entire files via Client.download(), and identifying the most recent forecasts with Client.latest(). The library supports multiple data sources including ECMWF servers, AWS, Google Cloud Platform, and Azure, and provides integration with earthkit-data for accessing models like ifs, aifs-single, and aifs-ens.

Tokens
3.6K
Snippets
15
Records
19
Agent score
56%

What's inside ecmwf-opendata

  1. Understand Stream and Type combinations

    main

    ECMWF uses stream and type to categorize forecasting systems and products.

    Systems:

    • HRES: High Resolution Forecast.
    • ENS: Ensemble Forecasts.
    • SEAS: Seasonal Forecast.

    Common Types:

    • HRES: fc (Forecast).
    • ENS: cf (Control), pf (Perturbed), em (Ensemble mean), es (Ensemble standard deviation), ep (Probabilities).

    Common Streams:

    • oper: HRES atmospheric (00/12 UTC).
    • wave: HRES ocean waves.
    • enfo: ENS atmospheric.
    • waef: ENS ocean waves.
    • scda: HRES atmospheric (06/18 UTC).
    • scwv: HRES ocean waves (06/18 UTC).

    If infer_stream_keyword is set to True in the Client, you only need to specify stream=wave for ocean products; the library will infer the rest from type and time.

  2. Specify Date and Time in requests

    main

    All dates and times are in UTC. You can use strings, integers, or Python datetime objects.

    Date Formats:

    • Strings: '20220125', '2022-01-25', or '2022-01-25 12:00:00'.
    • Integers: 20220125.
    • datetime.date or datetime.datetime objects.
    • Relative offsets: date=0 (today), date=-1 (yesterday).

    Time Formats:

    • Integers: 0, 6, 12, 18.
    • Strings: '12', '1200'.
    • datetime.time objects.

    Note: If time is not specified, it is extracted from the date string. If date and time are omitted, the library fetches the most recent available data.

  3. Configure Client retry policy

    main

    The client automatically retries transient HTTP errors (429, 500, 502, 503, 504, 408) and connection errors. You can customize this behavior during Client initialization.

    Parameters:

    • maximum_retries: Max attempts (default: 500).
    • retry_after: Seconds to wait between retries (default: 120).
    • use_server_retry_after: If True (default), honors the server's Retry-After header. If the header is missing, it falls back to retry_after.

    retry_after can also be a tuple (min, max, ratio) for incremental backoff.

    from ecmwf.opendata import Client
    
    client = Client(
        source="ecmwf",
        maximum_retries=500,
        retry_after=120,
        use_server_retry_after=True,
    )
    
    # Using incremental backoff: start at 10s, double each time, cap at 300s
    client = Client(
        source="ecmwf",
        retry_after=(10, 300, 2),
    )
  4. Configure the Client constructor

    main

    The Client constructor accepts several options to define the data source, model, and retrieval behavior.

    Options

    OptionTypeDefaultDescription
    sourcestrecmwfThe server to contact. Options: ecmwf (ECMWF servers), aws (Amazon Web Services), google (Google Cloud Platform), or azure (Microsoft Azure). Note: ecmwf has a limit of 500 simultaneous connections. azure generates an SAS token during retrieval.
    modelstrifsThe model that produced the data. Options: ifs (physics-driven), aifs-single (data-driven), or aifs-ens (ensemble data-driven).
    resolstr0p25The resolution of the data. Currently, only 0p25 (0.25 degree) is available.
    preserve_request_orderboolFalseIf True, the library writes data to the target file in the exact order specified in the request (e.g., param=['2t', 'msl'] ensures 2t is first). If False, the library reorders the request to minimize HTTP requests and increase speed. Warning: Setting this to True for many fields increases server load.
    infer_stream_keywordboolTrue (if model is ifs)If True, the library attempts to automatically determine the correct stream keyword based on other request parameters.
    client = Client(
        source="ecmwf",
        model="ifs",
        resol="0p25",
        preserve_request_order=False,
        infer_stream_keyword=True,
    )
  5. Get the latest forecast date with Client.latest()

    main

    The latest() method returns the date and time of the most recent matching forecast without actually downloading any data. It accepts the same parameters as retrieve().

    from ecmwf.opendata import Client
    
    client = Client(source="ecmwf")
    
    print(client.latest(
        type="fc",
        step=24,
        param=["2t", "msl"],
        target="data.grib2",
    ))
  6. Download whole data files using Client.download()

    main

    The download() method accepts the same parameters as retrieve(), but it downloads the entire data file from the server. It ignores specific filtering keywords like param, levelist, or number. Use this when you want the complete file rather than specific subsets.

    from ecmwf.opendata import Client
    
    client = Client(source="ecmwf")
    
    client.download(
        param="msl",
        type="fc",
        step=24,
        target="data.grib2",
    )
  7. Retrieve data using Client.retrieve()

    main

    The retrieve() method is the primary way to fetch specific meteorological data. You can pass a request object or provide arguments directly to the method. If date or time are not specified, the library queries for the most recent matching data. The method returns a result object containing the datetime of the downloaded forecast.

    from ecmwf.opendata import Client
    
    client = Client(source="ecmwf")
    
    client.retrieve(
        time=0,
        type="fc",
        step=24,
        param=["2t", "msl"],
        target="data.grib2",
    )
  8. Download meteorological data using Client.retrieve()

    main

    The Client.retrieve() method retrieves data from the server based on a request and writes it to a target file. You can provide the request as a dictionary of keyword/value pairs. You can specify a list of values for a given keyword to download multiple fields or steps at once.

    Note: This package is optimized for downloading subsets of the dataset. For downloading very large portions of the dataset, downloading whole files locally may be more efficient.

    from ecmwf.opendata import Client
    
    client = Client(source="ecmwf")
    
    request = {
        "time": 0,
        "type": "fc",
        "step": 24,
        "param": ["2t", "msl"],
    }
    
    client.retrieve(request, "data.grib2")
  9. Configure Azure SAS token authentication

    main

    If you are accessing data from an Azure source, the client can automatically handle SAS (Shared Access Signature) token generation. This is enabled by default if source="azure".

    To use a custom SAS token endpoint, provide sas_known_key and sas_custom_url during Client initialization.

    client = Client(
        source="azure",
        use_sas_token=True,
        sas_known_key="my_custom_key",
        sas_custom_url="https://my-token-endpoint.com/api"
    )
  10. Configure request keywords for data selection

    main

    Requests are composed of keywords that identify which files to access and which parts of those files to download.

    File Identification Keywords (Compulsory/Required):

    • type: The type of data (defaults to fc).
    • stream: The forecast system (e.g., oper, enfo, wave).
    • date: The forecast start date (UTC).
    • time: The forecast start time (UTC).
    • step: The forecast time step in hours (or fcmonth for seasonal).

    Data Filtering Keywords (Optional):

    • param: Meteorological parameters (e.g., 2t, msl).
    • levtype: Selection between single level and pressure levels.
    • levelist: List of pressure levels.
    • number: List of ensemble member numbers.
  11. Plot earthkit data with earthkit-plots

    main

    Once you have retrieved a field list using earthkit-data, you can use earthkit.plots.quickplot to visualize specific parameters. If your dataset contains multiple parameters, you can index into the field list (e.g., ds[1]) to plot a specific field.

    import earthkit.plots
    
    # Assuming ds is a fieldlist from earthkit.data
    earthkit.plots.quickplot(ds[1])