Skip to content

Instantly share code, notes, and snippets.

@revodavid
Created May 3, 2016 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save revodavid/8c9232e681c5687d5d5b13fddeab9f58 to your computer and use it in GitHub Desktop.
Save revodavid/8c9232e681c5687d5d5b13fddeab9f58 to your computer and use it in GitHub Desktop.
library(checkpoint)
checkpoint("2016-04-22")
library(weatherData)
city <- "SJC"
cityLongName <- "San Jose"
yearStart <- 1991
yearEnd <- 2015
## check for an existing cached data file
cacheDir <- "c:/Users/davidsmi/Downloads/WeatherData"
filebase <- paste(city, yearStart, yearEnd, sep = "-")
filename <- paste(cacheDir, "/", filebase, ".Rd", sep = "")
if (file.exists(filename)) {
load(filename)
} else {
## download data from Weather Underground
if (!checkDataAvailability(city, paste(yearStart, "-01-01", sep = ""))
|| !checkDataAvailability(city, paste(yearEnd, "-12-31", sep = ""))) {
stop("Data not available for selected city/dates")
}
weatherHistory <- NULL
for (year in yearStart:yearEnd) {
weatherHistory <- rbind(weatherHistory,
getWeatherForYear(city, year))
}
weatherHistory$Date <- as.Date(weatherHistory$Date)
weatherHistory$Year <- as.numeric(format(weatherHistory$Date, "%Y"))
weatherHistory$City <- rep(cityLongName, nrow(weatherHistory))
attr(weatherHistory, "CityName") <- city
attr(weatherHistory, "CityLongName") <- cityLongName
save("weatherHistory", file=filename)
}
assign(paste(city, "weather", sep = ""), weatherHistory)
rm("weatherHistory")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment