-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWeather_APP.py
441 lines (378 loc) · 22.2 KB
/
Weather_APP.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
'''
0. Prerequisites
The following packages are required and need to be installed before continuing:
- requests (https://requests.readthedocs.io/)
- geopy (https://github.com/geopy/geopy)
- matplotlib (https://matplotlib.org/)
- json (built-in in Python)
- tkinter (built-in in Python)
- pandas (https://pandas.pydata.org/)
The following API key is needed:
- OpenWeather (https://openweathermap.org/api)
- Please note, for function 1, 3, and 4 (see below), the free OpenWeather plan suffices.
- For function 2 (see below), "advanced weather maps" (paid plan) is required (https://openweathermap.org/price).
'''
# ______________________________________________________________________________________________________________________
# 1. Setup API to fetch data
# Insert your OpenWeather API key here
api_key = ""
# ______________________________________________________________________________________________________________________
# 2. Preparation
# Import libraries
from tkinter import simpledialog
# Requests is needed for http(s) requests
import requests
# json is needed to handle the received data from API
import json
# Tkinter is needed to for the GUI
import tkinter as tk
# geopy is needed to convert a location (string) into coordinates
from geopy.geocoders import Nominatim
# Matplotlib is needed to create a weather map and the plot for the temperature forecast
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
# Pandas is needed to create the plot for the temperature forecast
import pandas as pd
# ______________________________________________________________________________________________________________________
# 3. Define functions
# Function 1: Get Current Weather Information
def func1(lat, lon):
url = f"https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&appid={api_key}&units=metric"
response1 = requests.get(url)
data = json.loads(response1.text)
# Show selected weather data from pulled data
# General weather data (for all requested locations):
current_temp = data["current"]["temp"]
current_feels_like_temp = data["current"]["feels_like"]
current_pressure = data["current"]["pressure"]
current_wind_speed = data["current"]["wind_speed"]
current_wind_direction = data["current"]["wind_deg"]
current_weather_description = data["current"]["weather"][0]["description"]
# Conditional weather data (depending on the current primary weather conditions for the requested locations)
rain_or_snow_volume = data["current"]["weather"][0]["main"]
# If the current primary weather condition for the requested location is snow, get the snow volume.
if rain_or_snow_volume == "Snow":
rain_or_snow_volume = data["current"]["snow"]["1h"]
rain_or_snow_volume = f"\u2744 {rain_or_snow_volume} mm/h"
rain_or_snow_volume_define = "snow"
# If the current primary condition for the requested location is rain,get the rain volume.
elif rain_or_snow_volume == "Rain":
rain_or_snow_volume = data["current"]["rain"]["1h"]
rain_or_snow_volume = f"\u2602 {rain_or_snow_volume} mm/h"
rain_or_snow_volume_define = "rain"
# if the current primary weather condition is something other than rain or snow.
else:
rain_or_snow_volume = None
rain_or_snow_volume_define = None
# Convert the wind direction from degrees to the corresponding abbreviation
converted_current_wind_direction = current_wind_direction
if 338 <= converted_current_wind_direction <= 360:
converted_current_wind_direction = "N"
elif 0 <= converted_current_wind_direction <= 22:
converted_current_wind_direction = "N"
elif 23 <= converted_current_wind_direction <= 67:
converted_current_wind_direction = "NE"
elif 68 <= converted_current_wind_direction <= 112:
converted_current_wind_direction = "E"
elif 113 <= converted_current_wind_direction <= 157:
converted_current_wind_direction = "SE"
elif 158 <= converted_current_wind_direction <= 202:
converted_current_wind_direction = "S"
elif 203 <= converted_current_wind_direction <= 247:
converted_current_wind_direction = "SW"
elif 248 <= converted_current_wind_direction <= 292:
converted_current_wind_direction = "W"
elif 293 <= converted_current_wind_direction <= 337:
converted_current_wind_direction = "NW"
# Convert the wind direction from degrees to the corresponding arrow
converted_current_wind_direction_arrow = current_wind_direction
if 338 <= converted_current_wind_direction_arrow <= 360:
converted_current_wind_direction_arrow = "\u2191"
elif 0 <= converted_current_wind_direction_arrow <= 22:
converted_current_wind_direction_arrow = "\u2191"
elif 23 <= converted_current_wind_direction_arrow <= 67:
converted_current_wind_direction_arrow = "\u2197"
elif 68 <= converted_current_wind_direction_arrow <= 112:
converted_current_wind_direction_arrow = "\u2192"
elif 113 <= converted_current_wind_direction_arrow <= 157:
converted_current_wind_direction_arrow = "\u2198"
elif 158 <= converted_current_wind_direction_arrow <= 202:
converted_current_wind_direction_arrow = "\u2193"
elif 203 <= converted_current_wind_direction_arrow <= 247:
converted_current_wind_direction_arrow = "\u2199"
elif 248 <= converted_current_wind_direction_arrow <= 292:
converted_current_wind_direction_arrow = "\u2190"
elif 293 <= converted_current_wind_direction_arrow <= 337:
converted_current_wind_direction_arrow = "\u2196"
return current_temp, current_feels_like_temp, current_pressure, current_wind_speed, current_wind_direction, \
current_weather_description, rain_or_snow_volume, rain_or_snow_volume_define, \
converted_current_wind_direction, converted_current_wind_direction_arrow
# Function 2: World Temperature Map
# Ignore function 2 for now
def func2():
# Layer name -> you need to use the code names from https://openweathermap.org/api/weather-map-2#layers
op = "TA2"
# Number of zoom level (can be adjusted)
z = "0"
# Number of x tile coordinate (can be adjusted)
x = "0"
# Number of y tile coordinate (can be adjusted)
y = "0"
mapurl = f"http://maps.openweathermap.org/maps/2.0/weather/{op}/{z}/{x}/{y}?appid={api_key}&opacity=0.6&fill_bound=false"
response2 = requests.get(mapurl)
# the next 3 lines form the color window.
img = mpimg.imread(f'{mapurl}')
plt.imshow(img)
plt.show()
return response2
# Funtion 3: Weather Forecast (1 hour)
def func3(lat, lon):
url = f"https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&appid={api_key}&units=metric"
response3 = requests.get(url)
data = json.loads(response3.text)
# Show selection of weather data from all pulled data
# General weather data (for all requested locations):
hourly_temp = data["hourly"][1]["temp"]
hourly_feels_like_temp = data["hourly"][1]["feels_like"]
hourly_pressure = data["hourly"][1]["pressure"]
hourly_wind_speed = data["hourly"][1]["wind_speed"]
hourly_wind_direction = data["hourly"][1]["wind_deg"]
hourly_weather_description = data["hourly"][1]["weather"][0]["description"]
# Conditional weather data (depending on the hourly primary weather conditions for the requested locations)
hourly_rain_or_snow_volume = data["hourly"][1]["weather"][0]["main"]
# If the current primary weather condition for the requested location is snow, get the snow volume.
if hourly_rain_or_snow_volume == "Snow":
hourly_rain_or_snow_volume = data["hourly"][1]["snow"]["1h"]
hourly_rain_or_snow_volume = f"\u2744 {hourly_rain_or_snow_volume} mm/h"
hourly_rain_or_snow_volume_define = "snow"
# If the current primary condition for the requested location is rain, get the rain volume.
elif hourly_rain_or_snow_volume == "Rain":
hourly_rain_or_snow_volume = data["hourly"][1]["rain"]["1h"]
hourly_rain_or_snow_volume = f"\u2602 {hourly_rain_or_snow_volume} mm/h"
hourly_rain_or_snow_volume_define = "rain"
# if the current primary weather condition is something other than rain or snow.
else:
hourly_rain_or_snow_volume = None
hourly_rain_or_snow_volume_define = None
# Convert the wind direction from degrees to the corresponding abbreviation
converted_hourly_wind_direction = hourly_wind_direction
if 338 <= converted_hourly_wind_direction <= 360:
converted_hourly_wind_direction = "N"
elif 0 <= converted_hourly_wind_direction <= 22:
converted_hourly_wind_direction = "N"
elif 23 <= converted_hourly_wind_direction <= 67:
converted_hourly_wind_direction = "NE"
elif 68 <= converted_hourly_wind_direction <= 112:
converted_hourly_wind_direction = "E"
elif 113 <= converted_hourly_wind_direction <= 157:
converted_hourly_wind_direction = "SE"
elif 158 <= converted_hourly_wind_direction <= 202:
converted_hourly_wind_direction = "S"
elif 203 <= converted_hourly_wind_direction <= 247:
converted_hourly_wind_direction = "SW"
elif 248 <= converted_hourly_wind_direction <= 292:
converted_hourly_wind_direction = "W"
elif 293 <= converted_hourly_wind_direction <= 337:
converted_hourly_wind_direction = "NW"
# Convert the wind direction from degrees to the corresponding arrow
converted_hourly_wind_direction_arrow = hourly_wind_direction
if 338 <= converted_hourly_wind_direction_arrow <= 360:
converted_hourly_wind_direction_arrow = "\u2191"
elif 0 <= converted_hourly_wind_direction_arrow <= 22:
converted_hourly_wind_direction_arrow = "\u2191"
elif 23 <= converted_hourly_wind_direction_arrow <= 67:
converted_hourly_wind_direction_arrow = "\u2197"
elif 68 <= converted_hourly_wind_direction_arrow <= 112:
converted_hourly_wind_direction_arrow = "\u2192"
elif 113 <= converted_hourly_wind_direction_arrow <= 157:
converted_hourly_wind_direction_arrow = "\u2198"
elif 158 <= converted_hourly_wind_direction_arrow <= 202:
converted_hourly_wind_direction_arrow = "\u2193"
elif 203 <= converted_hourly_wind_direction_arrow <= 247:
converted_hourly_wind_direction_arrow = "\u2199"
elif 248 <= converted_hourly_wind_direction_arrow <= 292:
converted_hourly_wind_direction_arrow = "\u2190"
elif 293 <= converted_hourly_wind_direction_arrow <= 337:
converted_hourly_wind_direction_arrow = "\u2196"
return hourly_temp, hourly_feels_like_temp, hourly_pressure, hourly_wind_speed, hourly_wind_direction, \
hourly_weather_description, hourly_rain_or_snow_volume, hourly_rain_or_snow_volume_define, \
converted_hourly_wind_direction, converted_hourly_wind_direction_arrow
# Function 4: Temperature Forecast for the next 10 Hours
def func4(lat, lon):
# get the data from the API
url = f"https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&appid={api_key}&units=metric"
response3 = requests.get(url)
data = json.loads(response3.text)
#abstract the hourly temperature data out of the response
counter = 0
lst = {}
for _ in data["hourly"]:
while counter <= 9:
counter += 1
e = data["hourly"][counter]["temp"]
lst[f'T+{counter}']= (e)
#create a dataframe and a respective plot
h_forecast = pd.DataFrame(lst, index=["Temperature"])
row1 = h_forecast.iloc[0]
ind = h_forecast.transpose().index
plt.plot(ind, row1,'-o', label= "Time")
for i, txt in enumerate(row1):
plt.annotate(txt, (ind[i], row1[i]))
plt.xlabel("Time in T+ hours (T=0 is now)")
plt.ylabel("Temperature °C")
plt.title(f"Temperature Forecast for {place}\n for the next {counter} hours")
plt.show()
# ______________________________________________________________________________________________________________________
# 4. Ask the user what he wants to do
while True:
# Create the first pop-up window that asks the user what they want to do
options_window = tk.Tk()
options_window.withdraw()
task = simpledialog.askstring(title="Weather Information", prompt="What do you want to do?\n\nGet Current Weather "
"Information: 1\nGet World Temperature Map: 2\n"
"Get Weather Forecast (Next Hour): 3\n"
"Get Temperature Forecast (Next 10h): 4")
if task == '1':
# Ask the user for input (location of interest)
coord_window_place = tk.Tk()
coord_window_place.withdraw()
place = simpledialog.askstring(title="Get Current Weather Information",
prompt="For which location do you want the current weather?",
initialvalue="City, Country")
# set default value to St. Gallen & go back to main menu if user clicks cancel button
if place == "City, Country":
place = "St. Gallen, Switzerland"
elif place == None:
continue
else:
None
# Convert the requested location into latitude and longitude
geolocator = Nominatim(user_agent="Weather_APP")
location = geolocator.geocode(place)
if location == None:
answer = tk.messagebox.showerror(title=f"Error", message="Next time please enter a valid location.")
continue
lat = location.latitude
lon = location.longitude
# Call respective function and save the outcome under the variables
current_temp, current_feels_like_temp, current_pressure, current_wind_speed, current_wind_direction,\
current_weather_description, rain_or_snow_volume, rain_or_snow_volume_define, converted_current_wind_direction,\
converted_current_wind_direction_arrow = func1(lat, lon)
# The relevant output is printed based on the primary weather condition for the user's requested location:
# If the current primary weather condition is rain:
if rain_or_snow_volume_define == "rain":
answer = tk.messagebox.showinfo(title=f"Current Weather Information {place}",
message=f"{place} \n"
f"Temperature: {current_temp}°C\n"
f"Feels Like Temperature: {current_feels_like_temp}°C.\n"
f"Weather Condition: {current_weather_description}.\n"
f"{rain_or_snow_volume}\n"
f"Wind: {current_wind_speed}m/s {converted_current_wind_direction}({converted_current_wind_direction_arrow})\n"
f"Atmospheric Pressure: {current_pressure}hPa")
# If the current primary weather condition is snow:
elif rain_or_snow_volume_define == "snow":
answer = tk.messagebox.showinfo(title=f"Current Weather Information {place}",
message=f"{place} \n"
f"Temperature: {current_temp}°C\n"
f"Feels Like Temperature: {current_feels_like_temp}°C.\n"
f"Weather Condition: {current_weather_description}.\n"
f"{rain_or_snow_volume}\n"
f"Wind: {current_wind_speed}m/s {converted_current_wind_direction}({converted_current_wind_direction_arrow})\n"
f"Atmospheric Pressure: {current_pressure}hPa")
# If the current primary weather condition is something else than rain or snow:
else:
answer = tk.messagebox.showinfo(title=f"Current Weather Information {place}",
message=f"{place} \n"
f"Temperature: {current_temp}°C\n"
f"Feels Like Temperature: {current_feels_like_temp}°C.\n"
f"Weather Condition: {current_weather_description}.\n"
f"Wind: {current_wind_speed}m/s {converted_current_wind_direction}({converted_current_wind_direction_arrow})\n"
f"Atmospheric Pressure: {current_pressure}hPa")
if task == '2':
map = func2()
print(map)
if task == '3':
# Ask the user for input (location of interest)
coord_window_place = tk.Tk()
coord_window_place.withdraw()
place = simpledialog.askstring(title="Get Weather Forecast (Next Hour)",
prompt="For which location do you want the weather forecast (next hour)?",
initialvalue="City, Country")
# Set default value to St. Gallen & go back to main menu if user clicks cancel button.
if place == "City, Country":
place = "St. Gallen, Switzerland"
elif place == None:
continue
else:
None
# Convert the Location into Latitude and Longitude
geolocator = Nominatim(user_agent="Weather_APP")
location = geolocator.geocode(place)
if location == None:
answer = tk.messagebox.showerror(title=f"Error", message="Next time please enter a valid location.")
continue
lat = location.latitude
lon = location.longitude
# Call respective function and save the outcome under the variables
hourly_temp, hourly_feels_like_temp, hourly_pressure, hourly_wind_speed, hourly_wind_direction,\
hourly_weather_description, hourly_rain_or_snow_volume, hourly_rain_or_snow_volume_define, \
converted_hourly_wind_direction, converted_hourly_wind_direction_arrow = func3(lat, lon)
# The relevant output is printed based on the primary weather condition for the user's requested location:
# If the current primary weather condition is rain:
if hourly_rain_or_snow_volume_define == "rain":
answer = tk.messagebox.showinfo(title=f"Weather Forecast (Next Hour) {place}",
message=f"Weather Forecast (Next Hour){place} \n"
f"Temperature: {hourly_temp}°C\n"
f"Feels Like Temperature: {hourly_feels_like_temp}°C.\n"
f"Weather Condition: {hourly_weather_description}.\n"
f"{hourly_rain_or_snow_volume}\n"
f"Wind: {hourly_wind_speed}m/s {converted_hourly_wind_direction}({converted_hourly_wind_direction_arrow})\n"
f"Atmospheric Pressure: {hourly_pressure}hPa")
# If the current primary weather condition is snow:
elif hourly_rain_or_snow_volume_define == "snow":
answer = tk.messagebox.showinfo(title=f"Weather Forecast (Next Hour) {place}",
message=f"Weather Forecast (Next Hour){place}\n"
f"Temperature: {hourly_temp}°C\n"
f"Feels Like Temperature: {hourly_feels_like_temp}°C.\n"
f"Weather Condition: {hourly_weather_description}.\n"
f"{hourly_rain_or_snow_volume}\n"
f"Wind: {hourly_wind_speed}m/s {converted_hourly_wind_direction}({converted_hourly_wind_direction_arrow})\n "
f"Atmospheric Pressure: {hourly_pressure}hPa")
# If the current primary weather condition is something else than rain or snow:
else:
answer = tk.messagebox.showinfo(title=f"Weather Forecast (Next Hour) {place}",
message=f"Weather Forecast (Next Hour) {place}\n"
f"Temperature: {hourly_temp}°C\n"
f"Feels Like Temperature: {hourly_feels_like_temp}°C.\n"
f"Weather Condition: {hourly_weather_description}.\n"
f"Wind: {hourly_wind_speed}m/s {converted_hourly_wind_direction}({converted_hourly_wind_direction_arrow})\n"
f"Atmospheric Pressure: {hourly_pressure}hPa")
if task == "4":
# Ask the user for input (location of interest)
coord_window_place = tk.Tk()
coord_window_place.withdraw()
place = simpledialog.askstring(title="Get Temperature Forecast 10h",
prompt="For which location do you want the Forecast?",
initialvalue="City, Country")
# Set Default Value to St. Gallen & go back to main menu if user clicks cancel button
if place == "City, Country":
place = "St. Gallen, Switzerland"
elif place == None:
continue
else:
None
# Convert the requested location into latitude and longitude
geolocator = Nominatim(user_agent="Weather_APP")
location = geolocator.geocode(place)
if location == None:
answer = tk.messagebox.showerror(title=f"Error", message="Next time please enter a valid location.")
continue
lat = location.latitude
lon = location.longitude
func4(lat, lon)
#to define what happens if the user clicks the cancel button.
if task == None:
exit("The program has ended.")
# Check for invalid input
if task != '1' and task != '2' and task != '3' and task != '4':
answer = tk.messagebox.showerror(title=f"Error", message="Please choose a valid option.")