Guest User

mygetStationByID.r

a guest
Jul 13th, 2017
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. mygetStationByID <- function(stationID, station.list, begin, end) {
  2. st <- station.list[station.list$USAF == stationID, ]
  3. st$BEGIN_Year <- as.numeric(substr(st$BEGIN, 1, 4))
  4. st$END_Year <- as.numeric(substr(st$END, 1, 4))
  5. location <- suppressMessages(revgeocode(as.numeric(geocode(paste(st$NAME,
  6. st$CTRY))), output = "more"))
  7. st$city <- paste(location$locality, location$country, sep = ", ")
  8. st$rank <- 1
  9. weatherDFs <- mydlStationData(st, begin, end)
  10. combined.list <- mycombineWeatherDFs(weatherDFs)
  11. return(combined.list)
  12. }
  13.  
  14. mydlStationData <- function(kns, beg, end) {
  15. base.url <- "ftp://ftp.ncdc.noaa.gov/pub/data/noaa/"
  16. nstations <- nrow(kns)
  17. yrs <- seq(beg, end, 1)
  18. nyrs <- end - beg + 1
  19. usaf <- as.numeric(kns$USAF)
  20. wban <- as.numeric(kns$WBAN)
  21. status <- data.frame()
  22. temp <- as.data.frame(matrix(NA, nstations, 5))
  23. names(temp) <- c("File", "Status", "City", "rank", "kilo_distance")
  24. temp$City <- kns$city
  25. temp$rank <- kns$rank
  26. temp$kilo_distance <- kns$kilo_distance
  27. temp.list <- df.list <- list()
  28. city.names <- unlist(lapply(strsplit(kns$city, ", "), function(x) x[1]))
  29. df.names <- paste(city.names, kns$USAF, sep = "_")
  30. for (i in 1:nyrs) {
  31. for (j in 1:nstations) {
  32. temp[j, 1] <- paste(usaf[j], "-", wban[j], "-", yrs[i],
  33. ".gz", sep = "")
  34. tryCatch({
  35. gz.url <- paste(base.url, yrs[i], "/", temp[j,
  36. 1], sep = "")
  37. con <- gzcon(url(gz.url))
  38. raw <- textConnection(readLines(con))
  39. temp.list[[j]] <- read.fwf(raw, col.width)
  40. close(con)
  41. names(temp.list)[j] <- df.names[j]
  42. names(temp.list[[j]]) <- col.names
  43. temp.list[[j]]$LAT <- temp.list[[j]]$LAT/1000
  44. temp.list[[j]]$LONG <- temp.list[[j]]$LONG/1000
  45. #temp.list[[j]]$WIND.SPD <- temp.list[[j]]$WIND.SPD/10
  46. #temp.list[[j]]$TEMP <- temp.list[[j]]$TEMP/10
  47. #temp.list[[j]]$DEW.POINT <- temp.list[[j]]$DEW.POINT/10
  48. #temp.list[[j]]$ATM.PRES <- temp.list[[j]]$ATM.PRES/10
  49. #temp.list[[j]]$city <- city.names[j]
  50. #temp.list[[j]]$distance <- kns$kilo_distance[j]
  51. #temp.list[[j]]$rank <- kns$rank[j]
  52. temp[j, 2] <- "Success"
  53. }, error = function(cond) {
  54. return(NA)
  55. next
  56. }, finally = {
  57. if (is.na(temp[j, 2]) == "TRUE")
  58. temp[j, 2] <- "Failed"
  59. })
  60. }
  61. status <- rbind(status, temp)
  62. status <- status[order(status[, 3], status[, 4], status[,
  63. 1]), ]
  64. df.list <- append(df.list, temp.list)
  65. }
  66. output.list <- list(status, df.list)
  67. names(output.list) <- c("dl_status", "station_data")
  68. return(output.list)
  69. }
  70.  
  71. mycombineWeatherDFs <- function(dfList) {
  72. combined.list <- list()
  73. trackNull <- which(names(dfList$station_data) == "" | names(dfList$station_data) %in%
  74. NA)
  75. if (length(trackNull)>0) dfList$station_data <- dfList$station_data[-trackNull]
  76. keys <- unique(names(dfList$station_data))
  77. nkeys <- length(keys)
  78. for (i in 1:nkeys) {
  79. track <- which(names(dfList$station_data) == keys[i])
  80. combined.list[[i]] <- suppressWarnings(Reduce(function(...) rbind(...),
  81. dfList$station_data[track]))
  82. names(combined.list)[i] <- keys[i]
  83. }
  84. output.list <- list(dl_status = dfList$dl_status, station_data = combined.list)
  85. return(output.list)
  86. }
Advertisement
Add Comment
Please, Sign In to add comment