-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
298 lines (242 loc) · 7.79 KB
/
main.py
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import numpy as np
import requests
import json
import matplotlib.pyplot as plt
from pprint import pprint
from sklearn.svm import SVR
from scipy.optimize import fsolve
cities = [
'Mumbai',
'Thane',
'Badlapur',
'Pune',
'Solapur',
'Shirdi',
'Ahmednagar',
'Ratnagiri',
'Aurangabad',
'Ichalkaranji'
]
distances = {
'Mumbai': 0,
'Thane': 27,
'Badlapur': 49,
'Pune': 120,
'Solapur': 182,
'Shirdi': 195,
'Ahmednagar': 202,
'Ratnagiri': 223,
'Aurangabad': 261,
'Ichalkaranji': 304
}
dates = [x for x in range(1, 21)]
def get_data(city, date1='2019-07-01', date2='2019-07-20', api_key = ''):
api_link = ('http://api.worldweatheronline.com/premium/v1/past-weather' +
'.ashx?key={}&q={}' +
'&format=json&date={}&enddate={}')
try:
values = requests.get(api_link.format(api_key, city, date1, date2))
except:
print("Invalid City")
return 0
values = values.text
values = json.loads(values)
'''
What we get from the website, in the values
variable is a dict, which has one key called data,
the value of this key data is another dict,
This dict contains 2 keys :
weather and request
request contains information about the call
to the api
the weather contains the actual values of weather
you had requested.
The value of the weather key is a list.
Continued later
'''
return values['data']['weather']
def clean_data(values):
'''
Info:
-- Continued --
values is a list of dicts
each dict is another day
each day has a key called hourly
(among other keys, like avgtemp)
whose value is a dict of 8 readings
of temperatures throughout the day.
These 8 reading are in the form of
dicts with readings in order,
with 3hr differences
so 24hrs / 8 reading = 3 hour gaps.
'''
data = [{
'avgtemp': '',
'temperature': [],
'humidity': [],
'pressure': [],
'weather_description': [],
'wind_speed': [],
'wind_degree': [],
'hour': [0, 300, 600, 900, 1200, 1500, 1800, 2100, 2400]
} for i in range(20)]
# Created a list of 20 dicts for 20 days
features = {
'temperature': 'tempC',
'humidity': 'humidity',
'pressure': 'pressure',
'weather_description': 'weatherDesc',
'wind_speed': 'windspeedKmph',
'wind_degree': 'winddirDegree'
}
for each_day, current_data in zip(data, values):
for j in range(8):
each_day['avgtemp'] = (current_data['avgtempC'])
for item in features:
if item == 'weather_description':
each_day[item].append(
current_data['hourly'][j][features[item]][0]['value'])
else:
each_day[item].append(float(
current_data['hourly'][j][features[item]]))
return data
def save_cities_data():
for city in cities:
city_data = get_data(city)
with open(city+"Report.txt", 'w') as f:
f.write(str(city_data))
def load_city_data(city):
with open(city+"Report.txt") as f:
data = eval(f.read())
return data
def final_data():
data = {}
for city in cities:
print("Working on ", city)
temp = load_city_data(city)
print("Loaded Data")
data[city] = clean_data(temp)
print("Completed", city)
print()
return data
def plot_graph_1(plot_type, top_label, y_label):
close_and_far = ['Mumbai',
'Thane',
'Badlapur',
'Ratnagiri',
'Aurangabad',
'Ichalkaranji']
data = final_data()
x_labels = [str(x)+":00" for x in range(0, 24, 3)]
y_plots = []
# {[{[]},],}
for city in close_and_far:
day = data[city][0]
y_plots.append(day[plot_type])
count = 1
x_vals = [x for x in range(8)]
for plots in y_plots:
if count < 4:
color = 'g'
label = 'Close to sea'
else:
color = 'r'
label = "Far from sea"
print(count, color, plots)
if count in (3, 4):
plt.plot(x_vals, plots, color=color, label=label)
else:
plt.plot(x_vals, plots, color=color)
count += 1
plt.title(top_label)
plt.xlabel("Time")
plt.ylabel(y_label)
plt.xticks(np.arange(8), x_labels, rotation=290)
plt.grid(True)
plt.legend()
plt.show()
def max_and_mins(plot_type):
data = final_data()
maximun = []
minimum = []
dist = []
for city in data:
day = data[city][0]
temp = day[plot_type]
maximun.append(max(temp))
minimum.append(min(temp))
for city in cities:
dist.append(distances[city])
return maximun, minimum, dist
def plot_graph_2(max_min, plot_type, title, y_label):
maximun, minimum, dist = max_and_mins(plot_type)
# plt.plot(dist, maximun, 'ro', color='r', label="Max temps")
# plt.plot(dist, minimum, 'ro', color='g', label="Min temps")
plt.title(title)
plt.xlabel("Distance in Km")
plt.ylabel(y_label)
x = np.array(dist)
if max_min.lower() == "max":
y = np.array(maximun)
if max_min.lower() == "min":
y = np.array(minimum)
x1 = x[x < 100]
x1 = x1.reshape((x1.size, 1))
y1 = y[x < 100]
x2 = x[x > 50]
x2 = x2.reshape((x2.size, 1))
y2 = y[x > 50]
svr_line1 = SVR(kernel="linear", C=1e3)
svr_line2 = SVR(kernel="linear", C=1e3)
svr_line1.fit(x1, y1)
svr_line2.fit(x2, y2)
x_predict1 = np.arange(10, 100, 10).reshape((9, 1))
x_predict2 = np.arange(50, 400, 50).reshape((7, 1))
y_predict1 = svr_line1.predict(x_predict1)
y_predict2 = svr_line2.predict(x_predict2)
plt.plot(x_predict1, y_predict1, c='r', label='Strong sea effect')
plt.plot(x_predict2, y_predict2, c='b', label='Light sea effect')
plt.axis((0, 400, 27, 32))
plt.scatter(x, y, c='purple', label='data')
print(svr_line1.coef_)
print(svr_line1.intercept_)
print(svr_line2.coef_)
print(svr_line2.intercept_)
def line1(x):
m = svr_line1.coef_[0][0]
c = svr_line1.intercept_[0]
return m*x + c
def line2(x):
m = svr_line2.coef_[0][0]
c = svr_line2.intercept_[0]
return m*x + c
def POI(fun1, fun2, x0):
return fsolve(lambda x: fun1(x) - fun2(x), x0)
result = POI(line1, line2, 0.0)
print("[x,y] = [ %d , %d ]" % (result, line1(result)))
x = np.linspace(0, 300, 31)
plt.plot(x, line1(x), x, line2(x), result, line1(result), 'ro')
plt.legend()
plt.grid(True)
plt.show()
def main():
plot_graph_1('temperature',
'Temperature of Six Cities\n3 far from sea and 3 close',
'Temp in C')
plot_graph_2('max', 'temperature',
" Max Temperature of 10 cities",
"Temp in C")
plot_graph_2('min', 'temperature',
" Min Temperature of 10 cities",
"Temp in C")
plot_graph_1('humidity',
'Humidity of Six Cities\n3 far from sea and 3 close',
'Humidity')
plot_graph_2('max', 'humidity',
" Max Humidity of 10 cities",
"Humidity")
plot_graph_2('min', 'humidity',
" Min Humidity of 10 cities",
"Humidity")
if __name__ == '__main__':
main()