我有一个函数可以下载.bz2文件,扩展它们,并将数据写入磁盘。该函数在过去运行,但现在循环中的第二个文件出错:“download.file(current_file,destfile)中的错误...无法打开URL”我已经更改了所请求文件的顺序并且还进行了测试使用ftp客户端下载文件。似乎download.file()正在超时。当我手动下载文件时,会出现明显的暂停。
Here is my code:
# BuildMetFilesMain.R
# Author: John Orr
# This is the top-level R script to build the meteorological forcing files for
# the Clear Creek at Golden VIC model.
#
BuildMetFilesMain <- function(getFiles = TRUE) {
# Downloads and/or builds the meteorological forcing files for the
# Clear Creek at Golden VIC model
if(getFiles == TRUE) {
# Read the Clear Creek soil.06719505latandlongonly file
# for lat and long for the desired .bz2 file from Livneh
# Research Group
file_with_path <- paste(getwd(), "/parameters/soil.06719505latandlongonly.txt", sep = "")
conn <- file(file_with_path)
linn <- readLines(conn)
# Construct the download path and download the forcing file by
# iterating across the lat and long file. For this project the lat is always
# length 8 and long is always length 10. .BZ2 files to download are located at
# ftp://gdo-dcp.ucllnl.org/pub/dcp/archive/OBS/livneh2014.1_16deg/ascii/daily/. Directories
# start with "latitude.<latitude> and the filename is
# Meteorology_Livneh_NAmerExt_15Oct2014_<latitude>_<longitude>.bz2.
for(i in 1:length(linn)){
#for(i in 1:1){get
current_lat_long <- linn[i]
current_lat <- substr(current_lat_long, 0, 8)
current_long <- substr(current_lat_long, 10,19)
# Build the filename for current_lat and current_long
current_file <- paste("ftp://gdo-dcp.ucllnl.org/pub/dcp/archive/OBS/livneh2014.1_16deg/ascii/daily/latitude.",
current_lat, "/Meteorology_Livneh_NAmerExt_15Oct2014_", current_lat, "_",
current_long, ".bz2", sep = "")
# Build the filename for the tab delimited file
destfile <- paste("./forcings/", basename(current_file), sep = "")
destfile <- substr(destfile, 0, nchar(destfile) - 4)
destfile <- paste(destfile, sep = "")
# Retrieve the file for the current_lat and current_long from the Internet
download.file(current_file, destfile)
# Write the data in tab delimited format in the "forcings" directory
data <- read.table(destfile, header = FALSE, sep = "")
write.table(data, file = destfile, quote = FALSE, sep = "\t", na = "NA", row.names = FALSE, col.names = FALSE)
}
}
}
RStudio控制台的输出:
> source('~/Documents/ClearCreek/BuildMetFilesMain.R')
> BuildMetFilesMain()
trying URL 'ftp://gdo-dcp.ucllnl.org/pub/dcp/archive/OBS/livneh2014.1_16deg/ascii/daily/latitude.39.59375/Meteorology_Livneh_NAmerExt_15Oct2014_39.59375_-105.65625.bz2'
Content type 'unknown' length 135942 bytes (132 KB)
==================================================
trying URL 'ftp://gdo-dcp.ucllnl.org/pub/dcp/archive/OBS/livneh2014.1_16deg/ascii/daily/latitude.39.59375/Meteorology_Livneh_NAmerExt_15Oct2014_39.59375_-105.59375.bz2'
Error in download.file(current_file, destfile) :
cannot open URL 'ftp://gdo-dcp.ucllnl.org/pub/dcp/archive/OBS/livneh2014.1_16deg/ascii/daily/latitude.39.59375/Meteorology_Livneh_NAmerExt_15Oct2014_39.59375_-105.59375.bz2'
In addition: Warning message:
In download.file(current_file, destfile) :
URL 'ftp://gdo-dcp.ucllnl.org/pub/dcp/archive/OBS/livneh2014.1_16deg/ascii/daily/latitude.39.59375/Meteorology_Livneh_NAmerExt_15Oct2014_39.59375_-105.59375.bz2': status was 'Failure when receiving data from the peer'
>