Download ERA5 Data from Copernicus Data Store API#
This notebook demonstrates how to download ERA5 reanalysis data from the Copernicus Data Store (CDS) using the cdsapi library. It provides a step-by-step guide to retrieve, process, and visualize meteo data.
Key Steps#
Import Required Libraries
The notebook begins by importing essential Python libraries such ascdsapifor data retrieval,xarrayfor handling NetCDF files,matplotlibfor visualization, andcartopyfor geospatial plotting.Load Environment Variables
Environment variables for the CDS API URL and key are loaded using thedotenvlibrary to ensure secure and flexible configuration.Initialize the CDS Client
Thecdsapi.Clientis initialized using the credentials stored in the environment variables, enabling access to the Copernicus Data Store.Retrieve ERA5 Data
The notebook retrieves ERA5 single-level reanalysis data for a specific time period and variable (e.g., 2m temperature). The data is saved in NetCDF format for further analysis.Visualize the Data
The downloaded data is visualized usingmatplotlibandcartopy. A sample plot of 2m temperature is generated, showcasing the spatial distribution of the variable.
Import the required libraries#
import os
import cdsapi
import xarray as xr
from matplotlib import pyplot as plt
import cartopy.crs as ccrs
from dotenv import load_dotenv
load_dotenv("../.env")
True
Get environment variables#
CDS_URL = os.getenv("CDS_URL")
CDS_KEY = os.getenv("CDS_KEY")
Initialize CDS Client#
client = cdsapi.Client(url=CDS_URL,key=CDS_KEY)
2025-06-20 12:49:36,421 INFO [2025-06-16T00:00:00] CC-BY licence to replace Licence to use Copernicus Products on 02 July 2025. More information available [here](https://forum.ecmwf.int/t/cc-by-licence-to-replace-licence-to-use-copernicus-products-on-02-july-2025/13464)
2025-06-20 12:49:36,422 INFO [2025-06-10T00:00:00] To improve our C3S service, we need to hear from you! Please complete this very short [survey](https://confluence.ecmwf.int/x/E7uBEQ/). Thank you.
2025-06-20 12:49:36,422 INFO [2024-09-26T00:00:00] Watch our [Forum](https://forum.ecmwf.int/) for Announcements, news and other discussed topics.
Retrieve data#
output_path = "../data/era5_sample.nc"
os.makedirs(os.path.dirname(output_path),exist_ok=True)
client.retrieve(
"reanalysis-era5-single-levels",
{
"product_type": "reanalysis",
"variable": [
"2m_temperature"
],
"year": "2023",
"month": "01",
"day": ["01", "02", "03"],
"time": [
"00:00", "06:00", "12:00", "18:00"
],
"data_format": "netcdf",
"download_format": "unarchived"
},
output_path,
)
2025-06-20 12:49:49,927 INFO Request ID is 2e94cb50-74f3-4da4-abe3-3d6fc79f8900
2025-06-20 12:49:50,009 INFO status has been updated to accepted
2025-06-20 12:49:58,540 INFO status has been updated to running
2025-06-20 12:50:22,892 INFO status has been updated to successful
'../data/era5_sample.nc'
Visualize a variable from the downloaded data#
ds = xr.open_dataset(output_path,engine="netcdf4")
print (ds)
<xarray.Dataset> Size: 50MB
Dimensions: (valid_time: 12, latitude: 721, longitude: 1440)
Coordinates:
number int64 8B ...
* valid_time (valid_time) datetime64[ns] 96B 2023-01-01 ... 2023-01-03T18:...
* latitude (latitude) float64 6kB 90.0 89.75 89.5 ... -89.5 -89.75 -90.0
* longitude (longitude) float64 12kB 0.0 0.25 0.5 0.75 ... 359.2 359.5 359.8
expver (valid_time) <U4 192B ...
Data variables:
t2m (valid_time, latitude, longitude) float32 50MB ...
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
history: 2025-06-20T10:50 GRIB to CDM+CF via cfgrib-0.9.1...
t2m = ds['t2m'].isel(valid_time=0)
fig = plt.figure(figsize=(12, 6))
ax = plt.axes(projection=ccrs.PlateCarree())
t2m.plot(ax=ax, transform=ccrs.PlateCarree(), cmap="coolwarm", cbar_kwargs={"label": "K"})
ax.coastlines()
ax.set_title("2m Temperature")
plt.show()