Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 870915a

Browse files
committed
feat: add wind direction to output.
1 parent d0b8299 commit 870915a

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

projects/API Based Weather Report/API Based Weather Report.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import requests
22
from datetime import datetime
3+
from Util_Functions import wind_degree_to_direction
34

45

56
# Function to fetch weather data from OpenWeatherMap API
@@ -68,6 +69,12 @@ def write_to_file(location, weather_data):
6869
)
6970
)
7071

72+
# Writing wind direction information to the file
73+
if "wind" in weather_data and "deg" in weather_data["wind"]:
74+
f.write(
75+
"\tCurrent wind direction : " +
76+
wind_degree_to_direction(weather_data["wind"]["deg"]) + " \n")
77+
7178
# Printing confirmation message after writing to file
7279
print("Weather information written to weatherinfo.txt")
7380

@@ -108,6 +115,7 @@ def main():
108115
print("Current weather desc : " + weather_data["weather"][0]["description"])
109116
print("Current Humidity :", weather_data["main"]["humidity"], "%")
110117
print("Current wind speed :", weather_data["wind"]["speed"], "kmph")
118+
print("Current wind direction:", wind_degree_to_direction(weather_data["wind"]["deg"]))
111119
else:
112120
# Printing error message if weather data fetching fails
113121
print("Failed to fetch weather data. Please check your input and try again.")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def wind_degree_to_direction(str_wind_degree):
2+
"""
3+
Convert wind degree to wind direction.
4+
5+
:param str_wind_degree: str, Wind degree from API (0 to 360)
6+
:return: Wind direction as a string (e.g., N, NE, E, etc.)
7+
"""
8+
# convert wind degree from str to int.
9+
try:
10+
wind_degree = int(str_wind_degree)
11+
except ValueError:
12+
return "API Wind Degree data error!"
13+
14+
directions = [
15+
"N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
16+
"S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"
17+
]
18+
index = int((wind_degree + 11.25) // 22.5) % 16
19+
return directions[index]

0 commit comments

Comments
 (0)