Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Note

Tokenized access to data cubes is a Beta service which is closely monitored. The Data Store Service administrator reserves the right to take the necessary actions at any moment, including closing the service to maintain the overall performance of the Data Store Service.

...

Code Block
languagepy
titleAccess ERA5 single levels data with xarray
linenumberstrue
collapsetrue
import os
import xarray as xr

# Get the cdsapi key from anapre-defined environment variable, 
# (or specify explicitly in the script - available from https://cds.climate.copernicus.eu/profile):
cdsapi_key = os.getenv('CDSAPI_KEY', "<CDS-API-KEY>")

# Geo-chunked data for access optimised along the time dimension (e.g. for time-series at a single point)
geochunked_url = "https://arco.datastores.ecmwf.int/cadl-arco-geo-002/arco/reanalysis_era5_single_levels/sfc/geoChunked.zarr"

# Time-chunked data for access optimised across spatial dimensions (e.g. a global map plot for a single time-step)
timechunked_url = "https://arco.datastores.ecmwf.int/cadl-arco-time-002/arco/reanalysis_era5_single_levels/sfc/timeChunked.zarr"

# Open the geochunked_url with xarray, users must insert their CDS API key where indicated.
ds = xr.open_zarr(
    geochunked_url,
    consolidated=True,
     storage_options = {
        "headers": {"Authorization": f"Bearer {cdsapi_key}"}
    }
)

# Inspect the variables
print(ds)

...

Code Block
languagepy
titleAccess the ERA5 zarr data using an obstore custom store
collapsetrue
import os
import xarray as xr

from obstore.store import HTTPStore
from zarr.storage import ObjectStore

# Get the cdsapi key from ana pre-defined environment variable, 
# (or specify explicitly in the script - available from https://cds.climate.copernicus.eu/profile):
cdsapi_key = os.getenv('CDSAPI_KEY', "<CDS-API-KEY>")

# Geo-chunked data for access optimised along the time dimension (e.g. for time-series at a single point)
geochunked_url = "https://arco.datastores.ecmwf.int/cadl-arco-geo-002/arco/reanalysis_era5_single_levels/sfc/geoChunked.zarr"
 
# Use obstore's HTTPStore to create a store with retry configuration,
# and then wrap it in a zarr ObjectStore to read with xarray.
# See https://github.com/developmentseed/obstore/blob/main/obstore/python/obstore/_store/_retry.pyi
# for more details on the retry configuration options.
http_store = HTTPStore(
    geochunked_url,
    client_options={
        "default_headers": {"Authorization": f"Bearer {cdsapi_key}"},
    },
)
store = ObjectStore(http_store, read_only=True)
ds = xr.open_zarr(store)
print(ds)

...

Code Block
languagepy
titleAccess the ERA5 zarr data using an aiohttp retry-client
collapsetrue
import xarray as xr
from aiohttp_retry import ExponentialRetry, RetryClient

# Get the cdsapi key from a anpre-defined environment variable, 
# (or specify explicitly in the script - available from https://cds.climate.copernicus.eu/profile):
cdsapi_key = os.getenv('CDSAPI_KEY', "<CDS-API-KEY>")

# Geo-chunked data for access optimised along the time dimension (e.g. for time-series at a single point)
geochunked_url = "https://arco.datastores.ecmwf.int/cadl-arco-geo-002/arco/reanalysis_era5_single_levels/sfc/geoChunked.zarr"

# Define a custom get_client function that returns a RetryClient with the
# desired retry configuration. See https://github.com/inyutin/aiohttp_retry
# for more details on the retry configuration options.
async def get_client(**kwargs):
    retry_options = ExponentialRetry()
    retry_client = RetryClient(
        **kwargs, raise_for_status=False, retry_options=retry_options
    )
    return retry_client


ds = xr.open_zarr(
    geochunked_url,
    storage_options={
        "headers": {"Authorization": f"Bearer {cdsapi_key}"},
        "get_client": get_client,
    },
)
print(ds)

...