-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatWeather.py
361 lines (289 loc) · 10.3 KB
/
formatWeather.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
# DSC 510
# Week 12
# Programming Assignment Week 12
# Author: Kimberly Cable
# 03/05/2022
from datetime import datetime
from ColorsClass import Colors
def windDegreeToText(degree):
'''
Converts degrees to a direction
args:
degree (int): wind degrees
returns:
direction (str): direction of wind
'''
if degree > 337.5:
direction = "N"
if degree > 292.5:
direction = "NW"
if degree > 247.5:
direction = "W"
if degree > 202.5:
direction = "SW"
if degree > 157.5:
direction = "S"
if degree > 122.5:
direction = "SE"
if degree > 67.5:
direction = "E"
if degree > 22.5:
direction = "NE"
else:
direction = "N"
return direction
def weatherDescription(weatherId):
'''
Changes the color of the weather description based on the id
and adds an emoji
args:
weatherId (int): Weather id
returns:
weatherParm (tuple): weather symbol and color
'''
# Initialize Variables
# Weather Condition codes from OpenWeatherMap API
THUNDERSTORM = range(200, 300)
DRIZZLE = range(300, 400)
RAIN = range(500, 600)
SNOW = range(600, 700)
ATMOSPHERE = range(700, 800)
CLEAR = range(800, 801)
CLOUDY = range(801, 900)
if weatherId in THUNDERSTORM:
weatherParm = ("\U000026C8", Colors.RED)
elif weatherId in DRIZZLE:
weatherParm = ("\U0001F327", Colors.LIGHTBLUE)
elif weatherId in RAIN:
weatherParm = ("\U0001F327", Colors.CYAN)
elif weatherId in SNOW:
weatherParm = ("\U00002744", Colors.LIGHTWHITE)
elif weatherId in ATMOSPHERE:
weatherParm = ("\U0001F32B", Colors.DARKGRAY)
elif weatherId in CLEAR:
weatherParm = ("\U0001F31E", Colors.YELLOW)
elif weatherId in CLOUDY:
weatherParm = ("\U00002601", Colors.LIGHTGRAY)
else:
weatherParm = ("", Colors.RESET)
return weatherParm
def getWeekDay(index):
'''
Get day of the week from date index
args:
index (int): Day of week index
returns:
weekDay (str): Day of the week
'''
if index == 0:
weekDay = "Monday"
elif index == 1:
weekDay = "Tuesday"
elif index == 2:
weekDay = "Wednesday"
elif index == 3:
weekDay = "Thursday"
elif index == 4:
weekDay = "Friday"
elif index == 5:
weekDay = "Saturday"
else:
weekDay = "Sunday"
return weekDay
def getTimeZone(utcDate, offset):
'''
Calculates the Local Date/Time from the UTC Date/Time
args:
utcDate (int): UTC date from OpenWeather App
offset (int): Timezone Offset from OpenWeather App
returns:
utcDateInt (int): Local Date/Time
'''
localUTCDate = utcDate + offset
utcDateInt = datetime.utcfromtimestamp(localUTCDate)
return utcDateInt
def convertDate(utcDate, offset):
'''
Convert UTC Date to MM/DD/YYYY format and get day of week
args:
utcDate (int): UTC date
offset (int): Timezone offset from API
returns:
localDate (str): MM/DD/YYYY format date
dayOfWeek (str): Day of the Week
'''
localDateInt = getTimeZone(utcDate, offset)
localDate = datetime.strftime(localDateInt, "%m/%d")
dayOfWeek = getWeekDay(datetime.date(localDateInt).weekday())
return localDate, dayOfWeek
def convertTime(utcDate, offset):
'''
Converts UTC Date/Time to Local Time
args:
utcDate (int): UTC date
offset (int): Timezone offset from API
returns:
localTime (str): Local time
'''
localDateInt = getTimeZone(utcDate, offset)
localTime = datetime.strftime(localDateInt, "%I:%M %p")
return localTime
def moonPhase(phase):
'''
Converts API decimal moon pase to readable format
args:
phase (int): decimal moon phase
returns:
moonPhase (tuple): {emoji, text}
'''
if phase == 0 or phase == 1:
moonPhase = ("\U0001F311", "New Moon")
elif phase > 0 and phase < 0.25:
moonPhase = ("\U0001F312", "Waxing Cresent")
elif phase == 0.25:
moonPhase = ("\U0001F313", "First Quarter")
elif phase > 0.25 and phase < 0.50:
moonPhase = ("\U0001F314", "Waxing Gibous")
elif phase == 0.51:
moonPhase = ("\U0001F315", "Full Moon")
elif phase > 0.51 and phase < 0.75:
moonPhase = ("\U0001F316", "Waning Gibous")
elif phase == 0.75:
moonPhase = ("\U0001F317", "Last Quarter")
elif phase > 0.75 and phase < 1:
moonPhase = ("\U0001F318", "Waning Cresent")
return moonPhase
def formatForecastPrint(weatherData, units):
'''
Formats and Prints the Forecast
args:
weatherData (json): Weather data from API
units (str): temperature unit of measure
returns:
None
'''
for day in range(len(weatherData["daily"])):
# Convert date to local date
localDate, dayOfWeek = convertDate(
int(weatherData["daily"][day]["dt"]),
int(weatherData["timezone_offset"])
)
# Variables from weatherData
weatherId = weatherData["daily"][day]["weather"][0]["id"]
description = weatherData["daily"][day]["weather"][0]["description"]
temperature = round(weatherData["daily"][day]["temp"]["day"])
feelsLike = round(weatherData["daily"][day]["feels_like"]["day"])
minTemp = round(weatherData["daily"][day]["temp"]["min"])
maxTemp = round(weatherData["daily"][day]["temp"]["max"])
windSpeed = weatherData["daily"][day]["wind_speed"]
if units == "F":
windUnits = "m/h"
else:
windUnits = "m/s"
windDirection = windDegreeToText(
weatherData["daily"][day]["wind_deg"]
)
humidity = weatherData["daily"][day]["humidity"]
clouds = weatherData["daily"][day]["clouds"]
precipitation = weatherData["daily"][day]["pop"]
sunset = convertTime(
weatherData["daily"][day]["sunset"],
int(weatherData["timezone_offset"])
)
sunrise = convertTime(
weatherData["daily"][day]["sunrise"],
int(weatherData["timezone_offset"])
)
moonPhaseIcon, moonPhaseTxt = moonPhase(
weatherData["daily"][day]["moon_phase"]
)
weatherSymbol, color = weatherDescription(weatherId)
# Format and Print Weather
print(f"{'':3}", end="")
print(f"\n{Colors.BLUE}{dayOfWeek}: {localDate}{Colors.RESET}")
print(color)
print(f"{'':3}", end="")
print(f"{weatherSymbol:3} {temperature}°{units}")
print(f"{'':3}", end="")
print(f"feels like {feelsLike}°{units}. {description.capitalize()}")
print(Colors.RESET)
print(f"{'':3}", end="")
line1 = f"{Colors.BOLD}Min Temp: {Colors.RESET}{minTemp}°{units}"
print(f"{line1:<30}", end=" ")
print(f"{Colors.BOLD}Max Temp: {Colors.RESET}{maxTemp}°{units}")
print(f"{'':3}", end="")
line2 = f"{Colors.BOLD}Wind: {Colors.RESET}"
line2 += f"{windSpeed} {windUnits} {windDirection}"
print(f"{line2:<30}", end=" ")
print(f"{Colors.BOLD}Humidity: {Colors.RESET}{humidity}%.")
print(f"{'':3}", end="")
line3 = f"{Colors.BOLD}Clouds: {Colors.RESET}{clouds}%"
print(f"{line3:<30}", end=" ")
print(f"{Colors.BOLD}Precipitation: {Colors.RESET}{precipitation}%")
print(f"{'':3}", end="")
line4 = f"{Colors.BOLD}Sunrise: {Colors.RESET}{sunrise}"
print(f"{line4:<30}", end=" ")
print(f"{Colors.BOLD}Sunset: {Colors.RESET}{sunset}")
print(f"{'':3}", end="")
print(f"{Colors.BOLD}Moon: {Colors.RESET}", end="")
print(f"{moonPhaseIcon:3} {moonPhaseTxt}")
def printData(weatherData, units, cityName):
'''
Formats and prints the current weather, forecast and alerts
args:
weatherData (json): Weather data from API
units (str): temperature unit of measure
cityName (str): Name of City
returns:
None
'''
# Variables from weatherData
city = cityName
weatherId = weatherData["current"]["weather"][0]["id"]
description = weatherData["current"]["weather"][0]["description"]
temperature = round(weatherData["current"]["temp"])
feelsLike = round(weatherData["current"]["feels_like"])
weatherSymbol, color = weatherDescription(weatherId)
forecastLength = len(weatherData["daily"])
if "alerts" in weatherData:
alertEvent = weatherData["alerts"][0]["event"].upper()
alertFrom = weatherData["alerts"][0]["sender_name"]
alertDesc = weatherData["alerts"][0]["description"]
# Convert UTC date to MM/DD
alertStart, alertStartDayOfWeek = convertDate(
int(weatherData["alerts"][0]["start"]),
int(weatherData["timezone_offset"])
)
alertEnd, alertEndDayOfWeek = convertDate(
int(weatherData["alerts"][0]["end"]),
int(weatherData["timezone_offset"])
)
# Print Weather
print(f"\n{'':-<40}")
print(f"{Colors.BOLD}Current temperature in {city} is{Colors.RESET}")
print(f"{'':3}", end="")
print(color)
print(f"{weatherSymbol:3} {temperature}°{units}")
print(f"{'':3}", end="")
print(f"feels like {feelsLike}°{units}. {description.capitalize()}")
print(Colors.RESET)
# Print alerts
if "alerts" in weatherData:
print(f"{'':3}", end="")
print(f"{Colors.RED}{Colors.BOLD}ALERT: {alertEvent}{Colors.RESET}")
print(f"{'':6}", end="")
print(f"{Colors.RED}{Colors.BOLD}From: {Colors.RESET}", end="")
print(f"{Colors.RED}{alertFrom}")
print(f"{'':6}", end="")
print(f"{Colors.RED}{Colors.BOLD}Starts: {Colors.RESET}", end="")
line = f"{Colors.RED}{alertStartDayOfWeek}, {alertStart}{Colors.RESET}"
print(f"{line:<26}", end=" ")
print(f"{Colors.RED}{Colors.BOLD}Ends: {Colors.RESET}", end=" ")
print(f"{Colors.RED}{alertEndDayOfWeek}, {alertEnd}{Colors.RESET}")
print(f"{'':6}", end="")
print(f"{Colors.RED}{alertDesc}{Colors.RESET}")
# Print Forecast
print(f"\n{Colors.BOLD}{Colors.UNDERLINE}", end="")
print(f"{forecastLength} Day Forecast{Colors.RESET}")
formatForecastPrint(weatherData, units)
print(f"{'':-<40}")