Download CDS ERA5 data using R

Users can download CDS data using the ecmwfr package in R with “cds” as the "service" although it is not supported by C3S. In the examples below, "KEY" and "UID" are your CDS API key and UID respectively.

Please have a look at the following example to download ERA5 2m temperature for a selected area in netCDF format: 

library(ecmwfr)
 
cds.key <- "Insert_your_CDS_API_KEY_here"
wf_set_key(user = "Insert_your_CDS_UID_here", key = cds.key, service = "cds")
 
request <- list(
dataset_short_name = "reanalysis-era5-single-levels",
product_type   = "reanalysis",
format = "netcdf",
variable = "2m_temperature",
year = "2016",
month = "08",
day = "16",
time = c("00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00"),
# area is specified as N, W, S, E
area = c(50, -20, 30, 20),
target = "download_e5_single.nc"
)
 
file <- wf_request(user = "Insert_your_CDS_UID_here",
                     request = request,
                     transfer = TRUE,
                     path = "~",
                     verbose = TRUE)

This would return the data to a file called download_e5_single.nc.

The code below retrieves ERA5pressure level temperature data at 850hpa for a selected area in netCDF format:

library(ecmwfr)
 
cds.key <- "Insert_your_CDS_API_KEY_here"
wf_set_key(user = "Insert_your_CDS_UID_here", key = cds.key, service = "cds")
 
request <- list(
dataset_short_name = "reanalysis-era5-pressure-levels",
product_type   = "reanalysis",
format = "netcdf",
variable = "temperature",
pressure_level = "850"
year = "2016",
month = "08",
day = "16",
time = c("00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00"),
# area is specified as N, W, S, E
area = c(50, -20, 30, 20),
target = "download_e5_pressure.nc"
)
 
file <- wf_request(user = "Insert_your_CDS_UID_here",
                     request = request,
                     transfer = TRUE,
                     path = "~",
                     verbose = TRUE)

2 Likes

Thanks. This really helps. Just wondering, is there a way to pass the 'area' argument (to subset the data by bounding box) for those datasets that are not on a regular lon lat grid. Example UERRA dataset which is on a Lambert Conformal Conic grid. For such datasets, the 'Geographical area' option does not appear when selecting/filtering the variable. I see there is a related question here (though this question in fact is for another dataset that is already on a regular lon lat grid, yet not available to subset the bounding box). I am not sure why it would be difficult to transform the user defined regular lon lat coordinates to the dataset's native projection on the back-end, i.e. the Show API request should be able to do this in the background and display the 'area' coordinates for subsetting.

Hi Malcolm,

No, at this time I don't think it is possible to perform an area subset in the way you describe using the CDS API, but it is a good suggestion for a future development,

Thanks,

Kevin 

Hi Kevin,

Thanks again! 

Rgds

Malcolm

Hi Kevin, thank you, that helped a lot. Do you know if we ca use “ecmwfr” R package to download cmip6 projections ?
Best,

Hi, ‘ecmwfr’ is a 3rd party package, and I’m afraid I don’t know if it would work with CDS cmip6 projection data.
Kevin

Hello Kevin. Since a new CDS has been launched, new credentials are already released. Should I use the 32-character ID for the user field instead of the usual 6-digit ID?

I’ve tried it but there is an error.

image

Hi Charles,

if you login to CDS-beta and go to
https://cds-beta.climate.copernicus.eu/how-to-api
you should see your CDS-beta login credentials (note there is no longer a ‘UID’).

I do not know if the ecmwfr package will handle these details correctly.

1 Like

Hi Charles, an update to the package is in the works. For now this will only use the old CDS/ADS setup. I’m roughly a week away from a formal release.

1 Like

To update this thread, a new version of {ecmwfr} for R is available and inline with the current API here:

1 Like

Thanks for this, Koen!

Thanks Koen, this new version of {ecmwfr} for R is still valid? For the following code in R I’m getting “:cross_mark: Test failed: Data identifier reanalysis-era5-land is not found in Web API, CDS or ADS datasets.\n Or your login credentials do not match your request.”

Minimal test script for CDS connection

library(ecmwfr)

Set key with validation disabled (if available)

tryCatch({

Try to set key with current .cdsapirc contents

config_path ← “C:/Users/alexk/.cdsapirc”
if(file.exists(config_path)) {
lines ← readLines(config_path, warn = FALSE)
key_line ← grep(“^key:”, lines, value = TRUE)
if(length(key_line) > 0) {
api_key ← trimws(sub(“^key:\s*”, “”, key_line))

  # Try without validate parameter first
  wf_set_key(key = api_key, service = "cds")
  print("✅ Key set successfully")
}

}
}, error = function(e) {
print(paste(“Setting key failed:”, e$message))
})

Test request

test_request ← list(
dataset_short_name = “reanalysis-era5-land”,
product_type = “reanalysis”,
variable = “2m_temperature”,
year = “2020”,
month = “01”,
day = “01”,
time = “00:00”,
area = c(10, -86, 9, -85), # Small area in Costa Rica
format = “netcdf”,
target = “test.nc”
)

print(“Testing download…”)
tryCatch({
result ← wf_request(request = test_request, time_out = 300, path = temp_dir)
print(“:white_check_mark: Test download successful!”)
}, error = function(e) {
print(paste(“:cross_mark: Test failed:”, e$message))
})

Your request passes on my end, but required formatting the quotation marks around entries correctly. Always use the rstudio plugin to convert the python API requests to the R version, manually editting things often leads to hard to trace errors.