knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
Before downloading the nc file, the registration to the Copernicus CDS site is required (https://cds.climate.copernicus.eu/#!/home). After that be sure to have accepted the terms and conditions here: https://cds.climate.copernicus.eu/cdsapp/#!/terms/licence-to-use-copernicus-products while you are logged in.
After logged in, click on your name in the top right corner, scroll the page and take note of 'UID' and 'API Key'. Then, run the code below:
user_id <- "XXXXX" #change XXXXX with your UID user_key <- "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" #change XXXXX with your API Key N <- 44.3 #try to put known latitude E <- 11.2 #try to put known longitude cdsDownload(U_ID = user_id, API_Key = user_key, lat = N, lon = E, sPath = getwd(), site_id = "")
Once the variables to be passed to the cdsDownload
function are set, data will be downloaded after few passages:
dir.create(sPath, showWarnings = F)
First, the function checks if the directory given as parameter exists, if not it creates it. Without any specification, the directory used to save the downloaded file(s) will be given with getwd()
.
Then, the U_ID
and API_Key
will be passed to the ecmwfr function wf_set_key
in order to connect the user to the cds service:
ecmwfr::wf_set_key(user = U_ID, key = API_Key, service = 'cds')
In the second part, the time-related parameters are set, defining the list of years and months for which we want to download the data. The starting date is the first available in the dataset, while the ending one is the most current:
start.year = 1981 end.year = format(Sys.time(), "%Y") yearL = as.character(start.year:end.year) monthL = stringr::str_pad(1:12, 2, pad = "0")
Successively, the request list (converted from python to R) is created. The target file will be named automatically with the user id and the site id of the area of interest (if given as parameter)
request <- list( format = "netcdf", product_type = "monthly_averaged_reanalysis", variable = c("2m_temperature", "total_precipitation"), year = yearL, month = monthL, time = "00:00", area = c(lat, lon, lat, lon), dataset_short_name = "reanalysis-era5-land-monthly-means", target = paste0(U_ID, "_", site_id, "_t2s_tp.nc") )
Finally, using the ecmwfr wf_request
function the data are downloaded and saved:
file <- ecmwfr::wf_request( user = U_ID, request = request, transfer = TRUE, path = sPath, )
Climate data can be also downloaded by using coordinates in a data frame, therefore downloading various files consequently:
```{R, eval = T}
data <- data.frame(id = c("plot_A", "plot_B", "plot_C", "plot_D", "plot_E"), Lon = c(48.78, 46.52, 48.21, 47.05, 47.18), Lat = c(11.25, -3.32, 5.78, 8.54, -2.8))
knitr::kable(data, col.names = names(data))
```{R, eval = F} # once we have defined our data frame, we set the parameters to run the cdsDownload # function as described in the beginning user_id = user_id <- "XXXXX" #change XXXXX with your UID user_key <- "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" #change XXXXX with your API Key # now it is possible to use a for loop to run the cdsDownload function for every element # of the data frame for(i in seq(1:nrow(data))){ cdsDownload(U_ID = user_id, API_Key = user_key, lat = data$Lat[i], lon = data$Lon[i], ID = data$id[i], sPath = getwd()) } # in this way, in the working directory (sPath is set to getwd()) we will find # all the downloaded data. The cds service can download only one file at time # and while it does it is not possible to do other operations in the current # RStudio sesssion.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.