Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mygetStationByID <- function(stationID, station.list, begin, end) {
- st <- station.list[station.list$USAF == stationID, ]
- st$BEGIN_Year <- as.numeric(substr(st$BEGIN, 1, 4))
- st$END_Year <- as.numeric(substr(st$END, 1, 4))
- location <- suppressMessages(revgeocode(as.numeric(geocode(paste(st$NAME,
- st$CTRY))), output = "more"))
- st$city <- paste(location$locality, location$country, sep = ", ")
- st$rank <- 1
- weatherDFs <- mydlStationData(st, begin, end)
- combined.list <- mycombineWeatherDFs(weatherDFs)
- return(combined.list)
- }
- mydlStationData <- function(kns, beg, end) {
- base.url <- "ftp://ftp.ncdc.noaa.gov/pub/data/noaa/"
- nstations <- nrow(kns)
- yrs <- seq(beg, end, 1)
- nyrs <- end - beg + 1
- usaf <- as.numeric(kns$USAF)
- wban <- as.numeric(kns$WBAN)
- status <- data.frame()
- temp <- as.data.frame(matrix(NA, nstations, 5))
- names(temp) <- c("File", "Status", "City", "rank", "kilo_distance")
- temp$City <- kns$city
- temp$rank <- kns$rank
- temp$kilo_distance <- kns$kilo_distance
- temp.list <- df.list <- list()
- city.names <- unlist(lapply(strsplit(kns$city, ", "), function(x) x[1]))
- df.names <- paste(city.names, kns$USAF, sep = "_")
- for (i in 1:nyrs) {
- for (j in 1:nstations) {
- temp[j, 1] <- paste(usaf[j], "-", wban[j], "-", yrs[i],
- ".gz", sep = "")
- tryCatch({
- gz.url <- paste(base.url, yrs[i], "/", temp[j,
- 1], sep = "")
- con <- gzcon(url(gz.url))
- raw <- textConnection(readLines(con))
- temp.list[[j]] <- read.fwf(raw, col.width)
- close(con)
- names(temp.list)[j] <- df.names[j]
- names(temp.list[[j]]) <- col.names
- temp.list[[j]]$LAT <- temp.list[[j]]$LAT/1000
- temp.list[[j]]$LONG <- temp.list[[j]]$LONG/1000
- #temp.list[[j]]$WIND.SPD <- temp.list[[j]]$WIND.SPD/10
- #temp.list[[j]]$TEMP <- temp.list[[j]]$TEMP/10
- #temp.list[[j]]$DEW.POINT <- temp.list[[j]]$DEW.POINT/10
- #temp.list[[j]]$ATM.PRES <- temp.list[[j]]$ATM.PRES/10
- #temp.list[[j]]$city <- city.names[j]
- #temp.list[[j]]$distance <- kns$kilo_distance[j]
- #temp.list[[j]]$rank <- kns$rank[j]
- temp[j, 2] <- "Success"
- }, error = function(cond) {
- return(NA)
- next
- }, finally = {
- if (is.na(temp[j, 2]) == "TRUE")
- temp[j, 2] <- "Failed"
- })
- }
- status <- rbind(status, temp)
- status <- status[order(status[, 3], status[, 4], status[,
- 1]), ]
- df.list <- append(df.list, temp.list)
- }
- output.list <- list(status, df.list)
- names(output.list) <- c("dl_status", "station_data")
- return(output.list)
- }
- mycombineWeatherDFs <- function(dfList) {
- combined.list <- list()
- trackNull <- which(names(dfList$station_data) == "" | names(dfList$station_data) %in%
- NA)
- if (length(trackNull)>0) dfList$station_data <- dfList$station_data[-trackNull]
- keys <- unique(names(dfList$station_data))
- nkeys <- length(keys)
- for (i in 1:nkeys) {
- track <- which(names(dfList$station_data) == keys[i])
- combined.list[[i]] <- suppressWarnings(Reduce(function(...) rbind(...),
- dfList$station_data[track]))
- names(combined.list)[i] <- keys[i]
- }
- output.list <- list(dl_status = dfList$dl_status, station_data = combined.list)
- return(output.list)
- }
Advertisement
Add Comment
Please, Sign In to add comment