Generate C3S CDS cubes
xcube Data Store Framework – Climate Data Store¶
This notebook demonstrates how to access Climate Data Store (CDS) data via the dedicated xcube store, which provides dynamic data cube views into each gridded data set.
Please, also refer to the DeepESDL documentation and visit the platform's website for further information!
Brockmann Consult, 2025
This notebook runs with the python environment deepesdl-xcube-1.9.1
, please checkout the documentation for help on changing the environment.
Please note:
To access data from the Climate Data Store, you need a CDS API key.
Obtain a CDS Personal Access Token¶
You can obtain a CDS Personal Access Token as follows:
- Create a user account on the CDS Website.
- Log in to the website with your username and password.
- Navigate to your user page, where you can find your Personal Access Token.
Configure CDS API access¶
Your CDS Personal Access Token must be made available to the CDS API library.
You can do this by creating a file named .cdsapirc
in your home directory,
with the following format:
url: https://cds.climate.copernicus.eu/api
key: <PERSONAL-ACCESS-TOKEN>
Replace <PERSONAL-ACCESS-TOKEN>
with your Personal Access Token.
Then export the CDSAPI_URL
and CDSAPI_KEY
environment variables:
export CDSAPI_URL=https://cds.climate.copernicus.eu/api
export CDSAPI_KEY=[PERSONAL-ACCESS-TOKEN]
Agree to the terms of use for the datasets you require¶
The datasets available through CDS have associated terms of use. Before accessing a dataset via the API, you must agree to its terms of use, which can only be done via the CDS website, as follows:
- Log in to the CDS website, and go to 'Datasets' to find the dataset you require.
- On the dataset's web page, select the ‘Download’ tab.
- Scroll to the bottom of the page, and you will see a section titled ‘Terms of use’, which will contain either an ‘Accept terms’ button to allow you to accept the terms, or a confirmation that you have already accepted the terms.
Once you have accepted the terms on the website, the dataset will also be made available to you through the API.
import os
os.environ['CDSAPI_URL'] = 'https://cds.climate.copernicus.eu/api'
os.environ['CDSAPI_KEY'] = '[PERSONAL-ACCESS-TOKEN]'
# mandatory imports
from xcube.core.store import find_data_store_extensions
from xcube.core.store import get_data_store_params_schema
from xcube.core.store import new_data_store
# Utilities for notebook visualization
from IPython.display import JSON
import matplotlib as mpl
import matplotlib.pyplot as plt
Configure matplotlib to display graphs inline directly in the notebook and set a sensible default figure size.
%matplotlib inline
plt.rcParams["figure.figsize"] = 16,12
Check whether the cds
store is among the available stores, if not please follow the installation information from the top of this notebook.
JSON({e.name: e.metadata for e in find_data_store_extensions()})
<IPython.core.display.JSON object>
Usually we need more information to get the actual data store object. Which data store parameters are available for cds
?
get_data_store_params_schema('cds')
<xcube.util.jsonschema.JsonObjectSchema at 0x7f8ea1d37070>
Provide mandatory parameters to instantiate the store class:
store = new_data_store('cds')
store
<xcube_cds.store.CDSDataStore at 0x7f8ea1d21be0>
Which datasets are provided? (the list may contain both gridded and vector datasets):
list(store.get_data_ids())
['reanalysis-era5-single-levels:ensemble_mean', 'reanalysis-era5-single-levels:ensemble_members', 'reanalysis-era5-single-levels:ensemble_spread', 'reanalysis-era5-single-levels:reanalysis', 'reanalysis-era5-land', 'reanalysis-era5-single-levels-monthly-means:monthly_averaged_ensemble_members', 'reanalysis-era5-single-levels-monthly-means:monthly_averaged_reanalysis', 'reanalysis-era5-land-monthly-means:monthly_averaged_reanalysis', 'satellite-soil-moisture:saturation:daily', 'satellite-soil-moisture:saturation:10-day', 'satellite-soil-moisture:saturation:monthly', 'satellite-soil-moisture:volumetric:daily', 'satellite-soil-moisture:volumetric:10-day', 'satellite-soil-moisture:volumetric:monthly', 'satellite-sea-ice-thickness:envisat', 'satellite-sea-ice-thickness:cryosat-2']
Get more info about a specific dataset. This includes a description of the possible open formats:
store.describe_data('reanalysis-era5-single-levels-monthly-means:monthly_averaged_reanalysis')
<xcube.core.store.descriptor.DatasetDescriptor at 0x7f8ea1bf9940>
There are 4 required parameters, so we need to provide them to open a dataset:
dataset = store.open_data('reanalysis-era5-single-levels-monthly-means:monthly_averaged_reanalysis',
variable_names=['2m_temperature'],
bbox=[-10, 45, 40, 65],
spatial_res=0.25,
time_range=['2006-01-01', '2010-12-31'])
dataset
xcube-cds version 1.0.0 2025-05-22 11:14:09,725 INFO [2024-09-26T00:00:00] Watch our [Forum](https://forum.ecmwf.int/) for Announcements, news and other discussed topics. 2025-05-22 11:14:09,726 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. 2025-05-22 11:14:09,999 INFO Request ID is 0a697425-fd31-4e16-a434-ea84b58a8b5b 2025-05-22 11:14:10,257 INFO status has been updated to accepted 2025-05-22 11:14:31,570 INFO status has been updated to running 2025-05-22 11:14:43,006 WARNING Structural differences in grib fields detected when opening in xarray. Opening the grib file safely, however this may result in files with non-intuitive filenames. 2025-05-22 11:14:43,007 INFO status has been updated to successful
912734e8f40bbf1551deb4ce84f5f54.nc: 0%| | 0.00/1.61M [00:00<?, ?B/s]
<xarray.Dataset> Size: 4MB Dimensions: (time: 60, lat: 80, lon: 200) Coordinates: number int64 8B ... * time (time) datetime64[ns] 480B 2006-01-01 2006-02-01 ... 2010-12-01 * lat (lat) float64 640B 64.88 64.62 64.38 64.12 ... 45.62 45.38 45.12 * lon (lon) float64 2kB -9.875 -9.625 -9.375 -9.125 ... 39.38 39.62 39.88 expver (time) <U4 960B ... Data variables: t2m (time, lat, lon) float32 4MB ... Attributes: GRIB_centre: ecmf GRIB_centreDescription: European Centre for Medium-Range Weather Forecasts GRIB_subCentre: 0 Conventions: CF-1.7 institution: European Centre for Medium-Range Weather Forecasts
Plot the differences between successive time points in the dataset. We can see that the times are monotonically increasing (all the difference values are positive), but not equally spaced, since months are not all of the same length. The lowest values correspond to February; the four-year leap year cycle can also be discerned.
dataset.time.diff(dim='time').plot.line(figsize=(20, 4))
[<matplotlib.lines.Line2D at 0x7f8ea1637250>]
We can explore these data by plotting a temperature map for selected time points. First, we select January 2001. Land areas – and mountain ranges in particular – show up on the map as colder regions.
t2m_2001_jan = dataset.t2m.sel(time='2001-01-01 00:00:00', method='nearest')
t2m_2001_jan.plot.imshow(vmin=260, vmax=285, figsize=(14, 8), cmap='plasma')
<matplotlib.image.AxesImage at 0x7f8ea1673380>
For a more elegant and informative map, we define a function to plot a customized orthographic projection with overlaid coastlines and a grid.