python - How to get historical weather data (Temperature) on hourly basis for Chicago, IL -
i need historical weather data (temperature) on hourly basis chicago, il (zip code 60603)
basically need month of june , july 2017 either hourly or in 15 mins interval.
i have searched on noaa, weather underground etc. haven't found relevant use case. tried hands on scraping using r , python, no luck.
here snippet same
r :
library(httr) library(xml) url <- "http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdxmlclient.php" response <- get(url,query=list(zipcodelist="10001", product="time-series", begin=format(sys.date(),"%y-%m-%d"), unit="e", temp="temp",rh="rh",wspd="wspd")) doc <- content(response,type="text/xml", encoding = "utf-8") # xml document data # extract date-times dates <- doc["//time-layout/start-valid-time"] dates <- as.posixct(xmlsapply(dates,xmlvalue),format="%y-%m-%dt%h:%m:%s") # extract actual data data <- doc["//parameters/*"] data <- sapply(data,function(d)removechildren(d,kids=list("name"))) result <- do.call(data.frame,lapply(data,function(d)xmlsapply(d,xmlvalue))) colnames(result) <- sapply(data,xmlname) # combine data frame result <- data.frame(dates,result) head(result) error :
error in usemethod("xmlsapply") : no applicable method 'xmlsapply' applied object of class "list" python :
from pydap.client import open_url # setup connection url = 'http://nomads.ncdc.noaa.gov/dods/ncep_narr_daily/197901/197901/narr- a_221_197901dd_hh00_000' modelconn = open_url(url) tmp2m = modelconn['tmp2m'] # grab data lat_index = 200 # tie tmp2m.lat[:] lon_index = 200 # tie tmp2m.lon[:] print(tmp2m.array[:,lat_index,lon_index] ) error :
httperror: 503 service temporarily unavailable any other solution appreciated either in r or python or related online dataset link
there r package rwunderground, i've not had success getting want out of it. in honesty, i'm not sure if that's package, or if me.
eventually, broke down , wrote quick diddy daily weather history personal weather stations. you'll need sign weather underground api token (i'll leave you). can use following:
library(rjson) api_key <- "your_key_here" date <- seq(as.date("2017-06-01"), as.date("2017-07-31"), = 1) pws <- "kilchica403" weather <- vector("list", length = length(date)) for(i in seq_along(weather)){ url <- paste0("http://api.wunderground.com/api/", api_key, "/history_", format(date[i], format = "%y%m%d"), "/q/pws:", pws, ".json") result <- rjson::fromjson(paste0(readlines(url), collapse = " ")) weather[[i]] <- do.call("rbind", lapply(result[[2]][[3]], as.data.frame, stringsasfactors = false)) sys.sleep(6) } weather <- do.call("rbind", weather) there's call sys.sleep, causes loop wait 6 seconds before going next iteration. done because free api allows ten calls per minute (up 500 per day).
also, days may not have data. remember connects personal weather station. there number of reasons stopped uploading data, including internet outages, power outages, or owner turned off link weather underground. if can't data off of 1 station, try nearby , fill in gaps.
to weather station code, go weatherunderground.com. enter desired zip code search bar
click on "change" link
you can see station code of current station, , options other stations nearby.



Comments
Post a Comment