Skip to content

Instantly share code, notes, and snippets.

@tian2992
Last active December 22, 2015 12:29
Show Gist options
  • Save tian2992/6c67a250029b88e48a58 to your computer and use it in GitHub Desktop.
Save tian2992/6c67a250029b88e48a58 to your computer and use it in GitHub Desktop.
A veces los dias son nublados.
requests
#Optional for process_cloudy
#numpy
import datetime
import cPickle as pickle
import time
import csv
import requests
API_KEY = "e6b7b4cc5f23b40f"
def generateTimeStamp(daysago, base_time = datetime.datetime.now()):
delta = datetime.timedelta(daysago)
date = base_time - delta
return date.strftime("%Y%m%d")
def getWeatherData():
li = []
baseurl = "http://api.wunderground.com/api/"+API_KEY+"/history_{0}/q/MGGT.json"
for i in range(63):
print("Getting day {0}:".format(i))
timestamp = generateTimeStamp(i)
r = requests.get(baseurl.format(timestamp))
j = r.json()
li.append(j)
time.sleep(10)
pickle.dump(li, open("tmp_weather.p", "wb"))
pickle.dump(li, open("weather.p", "wb"))
return li
def process_cloudy():
import numpy
try:
with open("weather.p","r") as f:
data = pickle.load(f)
except:
data = getWeatherData()
vism_array = map(lambda x: float(x["history"]["dailysummary"][0]["minvism"]),data)
t_without = numpy.mean(vism_array[30:])
t_with = numpy.mean(vism_array[:30])
try:
ratio = (t_without / t_with) - 1
ratio_per = int(ratio*100)
print("Ha sido {0}% mas nublado desde los ultimos 30 dias.".format(ratio_per))
except:
print("Nunca llovio (o nunca paro de llover).")
def write_daily_observations_average(daily_property = "tempm", filename = 'temps.csv'):
'''Writes the aritmetic mean of property contained in the daily observations.'''
try:
with open("weather.p","r") as f:
data = pickle.load(f)
except:
data = getWeatherData()
observations_array = map(lambda x: x["history"]["observations"], data)
temperature_values_tuple_array = map(lambda individual_observations_array: map(lambda h: (int(h["date"]["hour"]), h[daily_property]), individual_observations_array), observations_array)
dict_hour = {}
for day in temperature_values_tuple_array:
for hour_tuple in day:
if hour_tuple[1] != '':
temp = float(hour_tuple[1])
hour = hour_tuple[0]
if not dict_hour.has_key(hour):
dict_hour[hour]=[]
if temp > -9999:
dict_hour[hour].append(temp)
with open(filename, 'wb') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
for k in dict_hour:
v = dict_hour[k]
#manual average to avoid numpy
csvwriter.writerow([k, sum(v)/float(len(v))])
print("Saved daily variable: {0} to file: {1}".format(daily_property, filename))
if __name__ == '__main__':
write_daily_observations_average()
0 17.516129032258064
1 17.06451612903226
2 16.79032258064516
3 16.576271186440678
4 16.295081967213115
5 16.133333333333333
6 15.934579439252337
7 16.379310344827587
8 18.083333333333332
9 19.79032258064516
10 21.677419354838708
11 23.516129032258064
12 24.76068376068376
13 25.70967741935484
14 26.049180327868854
15 25.870967741935484
16 25.016129032258064
17 23.868852459016395
18 22.08080808080808
19 20.59016393442623
20 19.725806451612904
21 19.193548387096776
22 18.57377049180328
23 18.032258064516128
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment