首页 > 解决方案 > 如何通过循环R下载每年的数据

问题描述

我可以使用下面的脚本下载一年(即 2008 年)的数据,详情可在此处找到:

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 年期间的数据?下载的文件名应附加特定年份。

标签: rdatabaseloopsdownload

解决方案


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 有问题。


推荐阅读