如何通过循环R下载每年的数据

How to download data for each year via a loop R

我可以使用下面的脚本下载一年(即 2008 年)的数据,并找到了详细信息 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)

问题:如何使用上面的代码按年下载2008-2017年的数据?下载的文件名应附加特定年份。

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)
}

此外,专业提示:veloxV1 (https://cran.r-project.org/src/contrib/Archive/velox/) 在栅格值提取方面比栅格包快大约 10 倍。一定要用veloxV1,v2有bug。

ecmwf 中有两个核心数据集选项。您似乎正在访问 Copernicus 数据存储 (cds)。

具体定义每个日期元素。

代码修改如下:

    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)