【发布时间】:2016-11-19 02:41:28
【问题描述】:
我正在尝试从此NASA FTP server 下载给定时间跨度的 TRMM 3B42 3 小时二进制数据。
有一个由 Florian Detsch 编写的优秀代码,用于下载包含在 GitHub-only Rsenal 包中的日常产品(这里是链接:https://github.com/environmentalinformatics-marburg/Rsenal/blob/master/R/downloadTRMM.R)。不幸的是,它不适用于 3 小时的数据。
我改了代码:
downloadTRMM <- function(begin, end, dsn = ".", format = "%Y-%m-%d.%H") {
## transform 'begin' and 'end' to 'Date' object if necessary
if (!class(begin) == "Date")
begin <- as.Date(begin, format = format)
if (!class(end) == "Date")
end <- as.Date(end, format = format)
## trmm ftp server
ch_url <-"ftp://disc2.nascom.nasa.gov/data/TRMM/Gridded/3B42_V7/"
## loop over daily sequence
ls_fls_out <- lapply(seq(begin, end, 1), function(i) {
# year and julian day (name of the corresponding folder)
tmp_ch_yr <- strftime(i, format = "%Y%m")
#tmp_ch_dy <- strftime(i, format = "%j")
# trmm date format
tmp_dt <- strftime(i+1, format = "%Y%m%d.%H")
# list files available on server
tmp_ch_url <- paste(ch_url, tmp_ch_yr, "", sep = "/")
tmp_ch_fls <- tmp_ch_fls_out <- character(2L)
for (j in 1:2) {
tmp_ch_fls[j] <- paste0("3B42.", tmp_dt, "z.7.precipitation",
ifelse(j == 1, ".bin"))
tmp_ch_fls[j] <- paste(tmp_ch_url, tmp_ch_fls[j], sep = "/")
tmp_ch_fls_out[j] <- paste(dsn, basename(tmp_ch_fls[j]), sep = "/")
download.file(tmp_ch_fls[j], tmp_ch_fls_out[j], mode = "wb")
}
# return data frame with *.bin and *.xml filenames
tmp_id_xml <- grep("xml", tmp_ch_fls_out)
data.frame(bin = tmp_ch_fls_out[-tmp_id_xml],
xml = tmp_ch_fls_out[tmp_id_xml],
stringsAsFactors = FALSE)
})
## join and return names of processed files
ch_fls_out <- do.call("rbind",ls_fls_out)
return(ch_fls_out)
}
getwd()
setwd("C:/Users/joaoreis/Documents/Bases_Geograficas/trmm_3h/")
fls_trmm <- downloadTRMM(begin = "2008-01-01.00", end = "2008-01-05.00")
fls_trmm
但我收到以下错误:
尝试网址 'ftp://disc2.nascom.nasa.gov/data/TRMM/Gridded/3B42_V7//200801//3B42.20080102.00z.7.precipitation.bin' 下载文件中的错误(tmp_ch_fls[j],tmp_ch_fls_out[j],mode = “wb”) : 无法打开网址 'ftp://disc2.nascom.nasa.gov/data/TRMM/Gridded/3B42_V7//200801//3B42.20080102.00z.7.precipitation.bin' 另外:警告信息:在download.file(tmp_ch_fls[j], tmp_ch_fls_out[j], mode = "wb") : InternetOpenUrl 失败: '' 调用 来自:download.file(tmp_ch_fls[j], tmp_ch_fls_out[j], mode = "wb")
有人知道如何使用 R 修复它吗?
谢谢!
【问题讨论】:
-
修改原始代码以适应3小时的数据看起来并不难。你都尝试了些什么?你遇到了什么错误?
-
@thecatalyst,数据的存储方式存在一些差异。例如文件名,一个使用朱利安日(daily data),另一个使用一年中每个月的一个文件夹来存储数据文件(3-hour data)。我认为这就是为什么不起作用。我正在尝试解决,但我还不能在 R 中做很多事情,我只是通常使用 raster 包。