0

我正在尝试使用 rnoaa 包从 NOAA 下载 100 个站的气候数据。我正在使用的代码如下:

siteNumber <- c("GHCND:USW00053156","GHCND:USC00023498","GHCND:USC00023505","GHCND:USC00023573","GHCND:USC00028112","GHCND:USC00029015","GHCND:USC00028494","GHCND:USC00023501")
datatype <- "PRCP"

getweather <- function(siteNumber,datatype) {

  wtr_complete <- list() 

  for (j in 1:length(siteNumber)){
    wtr <- list() #create an empty list to save the data from the station
    begin <- format((as.Date((ncdc_stations(datasetid='GHCND', stationid=siteNumber[j]))[[2]]$mindate)), format="%Y") #extract beginning year of the records
    end <- format((as.Date((ncdc_stations(datasetid='GHCND', stationid=siteNumber[j]))[[2]]$maxdate)), format="%Y") #extract end year of the records
    
    #download the data or each station.
    for (i in begin:end) {
      start_date <- paste0(i, "-01-01")
      end_date <- paste0(i, "-12-31")
      wtr[[as.character(i)]] <- ncdc(datasetid='GHCND', stationid=siteNumber[j], datatypeid=datatype, startdate = start_date, enddate = end_date)$data
    }
    
    wtr1 <- dplyr::bind_rows(wtr)
    
    #save in a list the data frame for all the stations
    wtr_complete[[j]] <- wtr1; name <- paste(datatype,"_",substring(siteNumber[j],7),sep=""); names(wtr_complete)[[j]] <- name
  }
  return(wtr_complete)
}

weatherdata <- getweather(siteNumber,datatype)

我可以成功下载 27 个站的数据,但是在 28 号站,我收到以下错误:

Error: Service Unavailable (HTTP 503)

我相信这是 NOAA 网站上的错误,而不是我的代码。

代码有没有办法克服这个特定站的这个错误,并继续从下一个站(29、30、31 等)下载数据?

4

0 回答 0