import xarray as xr
import cdsapi
client = cdsapi.Client()
2025-01-21 10:17:29,931 INFO [2024-09-28T00:00:00] **Welcome to the New Climate Data Store (CDS)!** This new system is in its early days of full operations and still undergoing enhancements and fine tuning. Some disruptions are to be expected. Your [feedback](https://jira.ecmwf.int/plugins/servlet/desk/portal/1/create/202) is key to improve the user experience on the new CDS for the benefit of everyone. Thank you. 2025-01-21 10:17:29,932 INFO [2024-09-26T00:00:00] Watch our [Forum](https://forum.ecmwf.int/) for Announcements, news and other discussed topics. 2025-01-21 10:17:29,933 INFO [2024-09-16T00:00:00] Remember that you need to have an ECMWF account to use the new CDS. **Your old CDS credentials will not work in new CDS!** 2025-01-21 10:17:29,933 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.
grid = [0.75, 0.75]
area = [90, -180+0.75/2, -90, 180]
year = ['{}'.format(y) for y in range(1979, 2025)]
month = ['{:02d}'.format(m) for m in range(1, 13)]
dl_dir = 'tmp/'
filename = 'ERA5_LowRes_Invariant.nc'
dataset = "reanalysis-era5-single-levels-monthly-means"
request = {
"product_type": ["monthly_averaged_reanalysis"],
"variable": [
"geopotential",
"land_sea_mask"
],
"grid": grid,
"area": area,
"year": ["1979"],
"month": ["01"],
"time": ["00:00"],
"data_format": "netcdf",
"download_format": "unarchived",
}
client.retrieve(dataset, request).download(dl_dir + filename)
print('+++ Done!')
2025-01-21 10:23:01,417 INFO Request ID is 154c2994-9ff9-4941-bf4a-983e335ee10f 2025-01-21 10:23:01,526 INFO status has been updated to accepted 2025-01-21 10:23:15,417 INFO status has been updated to successful
5922a90b4ae2639fa1b8f3f8ee440dfd.nc: 0%| | 0.00/434k [00:00<?, ?B/s]
2025-01-21 10:23:17,901 INFO Request ID is 7b145c45-70ed-4873-8b3a-95c36878d2e6 2025-01-21 10:23:18,257 INFO status has been updated to accepted 2025-01-21 10:23:32,237 INFO status has been updated to successful
5922a90b4ae2639fa1b8f3f8ee440dfd.nc: 0%| | 0.00/434k [00:00<?, ?B/s]
+++ Done!
# Unfortunate necessary cleaning
with xr.open_dataset(dl_dir + filename) as ds:
# ds = ds.rename({"valid_time": "time"}).drop_vars(["expver", "number"]) #, errors="ignore")
ds = ds.isel(valid_time=0).drop_vars(["expver", "number", "valid_time"])
ds['orography'] = ds['z'] / 9.80665
ds['orography'].attrs.update(long_name='Orography', units='m', standard_name='orography')
ds = ds.drop_vars(["z"])
ds['lsm'].attrs = {k: v for k, v in ds['lsm'].attrs.items() if not k.startswith("GRIB_")}
ds.attrs = {k: v for k, v in ds.attrs.items() if not k.startswith("GRIB_")}
ds.to_netcdf(filename)
filename = 'ERA5_LowRes_Monthly_t2m.nc'
dataset = "reanalysis-era5-single-levels-monthly-means"
request = {
"product_type": ["monthly_averaged_reanalysis"],
"variable": ["2m_temperature"],
"grid": grid,
"area": area,
"year": year,
"month": month,
"time": ["00:00"],
"data_format": "netcdf",
"download_format": "unarchived",
}
client.retrieve(dataset, request).download(dl_dir + filename)
print('+++ Done!')
2025-01-21 10:54:18,233 INFO Request ID is d067a552-8c4c-4323-99a9-520a58ba6527 2025-01-21 10:54:18,319 INFO status has been updated to accepted 2025-01-21 10:54:39,641 INFO status has been updated to running 2025-01-21 10:56:12,626 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-01-21 10:57:10,562 INFO status has been updated to successful
f6090c8ca0f20f3acad182b5cc0df08f.nc: 0%| | 0.00/96.1M [00:00<?, ?B/s]
+++ Done!
# Unfortunate necessary cleaning
with xr.open_dataset(dl_dir + filename) as ds:
ds = ds.rename({"valid_time": "time"}).drop_vars(["expver", "number"])
ds.attrs = {k: v for k, v in ds.attrs.items() if not k.startswith("GRIB_")}
for var in ds.variables.keys():
ds[var].attrs = {k: v for k, v in ds[var].attrs.items() if not k.startswith("GRIB_")}
ds.to_netcdf(filename)
filename = 'ERA5_LowRes_Monthly_tp.nc'
dataset = "reanalysis-era5-single-levels-monthly-means"
request = {
"product_type": ["monthly_averaged_reanalysis"],
"variable": ["total_precipitation"],
"grid": grid,
"area": area,
"year": year,
"month": month,
"time": ["00:00"],
"data_format": "netcdf",
"download_format": "unarchived",
}
client.retrieve(dataset, request).download(dl_dir + filename)
print('+++ Done!')
2025-01-21 11:02:06,637 INFO Request ID is 6533f982-8975-4013-b179-a450df5b0097 2025-01-21 11:02:06,714 INFO status has been updated to accepted 2025-01-21 11:02:20,592 INFO status has been updated to running 2025-01-21 11:03:23,478 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-01-21 11:03:23,480 INFO status has been updated to successful
e45b7c37af7a73ef9b416b35ce1c1f59.nc: 0%| | 0.00/97.1M [00:00<?, ?B/s]
+++ Done!
# Unfortunate necessary cleaning
with xr.open_dataset(dl_dir + filename) as ds:
ds = ds.rename({"valid_time": "time"}).drop_vars(["expver", "number"])
ds.attrs = {k: v for k, v in ds.attrs.items() if not k.startswith("GRIB_")}
for var in ds.variables.keys():
ds[var].attrs = {k: v for k, v in ds[var].attrs.items() if not k.startswith("GRIB_")}
ds.to_netcdf(filename)