-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeather Scrape.py
More file actions
157 lines (106 loc) · 4.33 KB
/
Weather Scrape.py
File metadata and controls
157 lines (106 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import mechanize
import json
import time as T
import numpy as np
import scipy.io
import csv
'''Author Grant Gunnison, Last modified 1/21/16'''
url ='https://api.forecast.io/forecast/4500ed9ab5c368b9ea32eca07d5c942b/'
'''
Arguments:
location: latitude,longitude (String)
Coordinates must be given with
a comma in between, but no spaces
keys: name of the data entry required from the json (List)
Returns: Json as a dictionary
'''
def get_forecast_json(location, keys = 'all'):
br = mechanize.Browser()
br.set_handle_robots(False)
response = br.open(url+ location).read()
max_qsol = 300
if keys == 'all':
json_dict = json.loads(response)
elif isinstance(keys, (list)):
forecast_dict = {}
json_dict = json.loads(response)
for key in keys:
forecast_dict[key] = json_dict[key]
json_dict = forecast_dict
sunrise = json_dict['daily']['data'][0]['sunriseTime']
sunset = json_dict['daily']['data'][0]['sunsetTime']
hour_list = []
for entry in json_dict['hourly']['data']:
hour_list.append((entry['apparentTemperature'], entry['time'], entry['icon'], entry['cloudCover']))
with open('weatherdata1.csv', 'wb') as csvfile:
wr = csv.writer(csvfile, quoting=csv.QUOTE_ALL)
wr.writerow(hour_list)
# min_tmp_list = []
# min_qsol_list= []
# for hour in range(len(hour_list)-1):
# difference = (hour_list[hour+1][0] - hour_list[hour][0])/60
# temp, time = hour_list[hour][0:2]
# if (hour != (len(hour_list) -2)):
# for minute in range(61):
# if minute %10 == 0:
# temp += difference
# tmp = ((temp-32)*5/9 + 273.15)-15;
# time += 600
# min_tmp_list.append(round(tmp, 3))
# if time < sunrise or time > sunset:
# min_qsol_list.append(0.0)
# elif time > sunrise and time < sunset and hour_list[hour][2] in ['rain', 'snow', 'sleet', 'fog', 'cloudy', 'partly-cloudy-day']:
# coverage_qsol = (1-hour_list[hour][4])*max_qsol
# total_day_time = sunset -sunrise
# mid_day = total_day_time/2
# qsol_percentage = abs(time - mid_day)/mid_day
# min_qsol_list.append(round(coverage_qsol*qsol_percentage*max_qsol),2)
# elif time > sunrise and time < sunset:
# total_day_time = sunset -sunrise
# mid_day = total_day_time/2.0
# if time < (mid_day + sunrise):
# qsol_percentage = (time-sunrise)/mid_day
# min_qsol_list.append(round((qsol_percentage*max_qsol),2))
# elif time == (mid_day+ sunrise):
# qsol_percentage = 1
# min_qsol_list.append(round((qsol_percentage*max_qsol),2))
# elif time > (mid_day + sunrise):
# qsol_percentage = abs(2*mid_day - (time - sunrise))/mid_day
# min_qsol_list.append(round((qsol_percentage*max_qsol),2))
# else:
# continue
# else:
# for minute in range(60):
# if minute %10 == 0:
# temp += difference
# tmp = ((temp-32)*5/9 + 273.15)-15;
# time += 600
# min_tmp_list.append(round(tmp, 3))
# if time < sunrise or time > sunset:
# min_qsol_list.append(0.0)
# elif time > sunrise and time < sunset and hour_list[hour][2] in ['rain', 'snow', 'sleet', 'fog', 'cloudy', 'partly-cloudy-day']:
# coverage_qsol = (1-hour_list[hour][4])*max_qsol
# total_day_time = sunset -sunrise
# mid_day = total_day_time/2
# qsol_percentage = abs(time - mid_day)/mid_day
# min_qsol_list.append(round(coverage_qsol*qsol_percentage*max_qsol),2)
# elif time > sunrise and time < sunset:
# total_day_time = sunset -sunrise
# mid_day = total_day_time/2.0
# if time < (mid_day + sunrise):
# qsol_percentage = (time-sunrise)/mid_day
# min_qsol_list.append(round((qsol_percentage*max_qsol),2))
# elif time == (mid_day+ sunrise):
# qsol_percentage = 1
# min_qsol_list.append(round((qsol_percentage*max_qsol),2))
# elif time > (mid_day + sunrise):
# qsol_percentage = abs(2*mid_day - (time - sunrise))/mid_day
# min_qsol_list.append(round((qsol_percentage*max_qsol),2))
# else:
# continue
# forecast = np.zeros((2, len(min_tmp_list)), dtype=np.object)
# forecast[0], forecast[1] = min_tmp_list, min_qsol_list
# print forecast
# scipy.io.savemat('forecast.mat', mdict={'forecast': forecast})
if __name__ == '__main__':
get_forecast_json('42.514794,-71.652153', ['hourly', 'daily'])