code123
code123

Reputation: 2146

How to download data for each year via a loop R

I am able to download data for one year (i.e. 2008) using the script below and details are found here:

library (ecmwfr)

# Specify the data set
request <- list("dataset"        = "reanalysis-era5-pressure-levels",
                "product_type"   = "reanalysis",
                "variable"       = "temperature",
                "pressure_level" = "850",
                "year"           = "2008",
                "month"          = "04",
                "day"            = "04",
                "time"           = "00:00",
                "area"           = "70/-20/30/60",
                "format"         = "netcdf",
                "target"         = "era5-demo.nc")


# Start downloading the data, the path of the file
# will be returned as a variable (ncfile)

ncfile <- wf_request(user = "2088",
                      request = request,   
                      transfer = TRUE,  
                      path = "~",
                      verbose = FALSE)

Question: How can I download the data by year for the period 2008-2017 using the code above? The downloaded file names should have the specific year appended to them.

Upvotes: 2

Views: 783

Answers (2)

Blaiso
Blaiso

Reputation: 199

There are two core dataset options in ecmwf. Looks like you are accessing the Copernicus Datastore (cds).

Define each date element specifically.

Code has been amended below:

    request <- list("dataset"        = "reanalysis-era5-pressure-levels",
                "product_type"   = "reanalysis",
                "variable"       = "temperature",
                "pressure_level" = "850",
                "year"           = c("2008", "2009", "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017"),
                "month"          = c("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"),
                "day"            = c("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"),
                "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"           = "70/-20/30/60",
                "format"         = "netcdf",
                "target"         = "era5-demo.nc")


   ncfile <- wf_request(user = "2088",
                      request = request,   
                      transfer = TRUE,  
                      path = "~",
                      verbose = FALSE)

Upvotes: 0

Neal Barsch
Neal Barsch

Reputation: 2940

library(foreach)
library (ecmwfr)
foreach(i=c(2008:2017))%do%{

  # Specify the data set
  request <- list("dataset"        = "reanalysis-era5-pressure-levels",
                "product_type"   = "reanalysis",
                "variable"       = "temperature",
                "pressure_level" = "850",
                "year"           = paste0(i),
                "month"          = "04",
                "day"            = "04",
                "time"           = "00:00",
                "area"           = "70/-20/30/60",
                "format"         = "netcdf",
                "target"         = paste0("era5-demo_",i,".nc"))


  # Start downloading the data, the path of the file
  # will be returned as a variable (ncfile)

  ncfile <- wf_request(user = "2088",
                      request = request,   
                      transfer = TRUE,  
                      path = "~",
                      verbose = FALSE)
}

Also, pro tip: veloxV1 (https://cran.r-project.org/src/contrib/Archive/velox/) is about 10x faster than the raster package at raster value extraction. Be sure to use veloxV1, v2 is buggy.

Upvotes: 3

Related Questions