Climate Data Store API Client

0.7.7 · active · verified Tue Apr 14

The `cdsapi` library provides a Python interface to access the Copernicus Climate Change Service (C3S) Climate Data Store (CDS) API. It allows users to programmatically search, retrieve, and download vast amounts of climate data. Currently at version 0.7.7, it receives active maintenance and updates, with minor releases typically every few weeks to months, often addressing compatibility with the evolving CDS backend.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `cdsapi.Client` and make a basic data retrieval request for ERA5 reanalysis data. Ensure your CDS API credentials are set up either in `~/.cdsapirc` or via environment variables (`CDSAPI_URL`, `CDSAPI_KEY`) before running, as the example uses placeholders.

import cdsapi
import os

# Your CDS API Key and URL are required for authentication.
# These are typically stored in a file ~/.cdsapirc or as environment variables.
# For this quickstart, we simulate fetching from environment variables.
# In a real setup, ensure CDSAPI_URL and CDSAPI_KEY are properly configured.
# Example:
# export CDSAPI_URL="https://cds.climate.copernicus.eu/api/v2"
# export CDSAPI_KEY="YOUR_UID:YOUR_API_KEY"

cds_url = os.environ.get('CDSAPI_URL', 'https://cds.climate.copernicus.eu/api/v2')
cds_key = os.environ.get('CDSAPI_KEY', '12345:some-dummy-key-for-test-validation')

if 'some-dummy-key' in cds_key:
    print("WARNING: CDSAPI_URL and CDSAPI_KEY environment variables are not set, "
          "or ~/.cdsapirc is missing. This code will likely fail without proper credentials.")

try:
    # Initialize the client. It will automatically look for .cdsapirc or environment variables.
    # Explicitly passing url/key is also an option.
    c = cdsapi.Client(url=cds_url, key=cds_key)

    print("Attempting to retrieve 2m temperature for Jan 1, 2023, 12:00 from ERA5 single levels...")
    c.retrieve(
        'reanalysis-era5-single-levels',
        {
            'product_type': 'reanalysis',
            'variable': '2m_temperature',
            'year': '2023',
            'month': '01',
            'day': '01',
            'time': '12:00',
            'format': 'grib',
            'area': '50/-10/40/0' # N/W/S/E, e.g., for a small area over UK/France
        },
        'era5-2m-temperature-2023-01-01-subset.grib' # Output filename
    )
    print("Retrieval request sent. Check for 'era5-2m-temperature-2023-01-01-subset.grib' file.")
except Exception as e:
    print(f"An error occurred during data retrieval: {e}")
    print("Please ensure your CDS API credentials are correctly configured in ~/.cdsapirc or as environment variables.")

view raw JSON →