Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include UV index in forecast #57

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ def forecast(lat, long, decimal, days=0):
retry_session = retry(cache_session, retries=5, backoff_factor=0.2)
openmeteo = openmeteo_requests.Client(session=retry_session)

url = "https://marine-api.open-meteo.com/v1/marine"
# First URL is the marine API. Second is for general weather/UV index
urls = (
"https://marine-api.open-meteo.com/v1/marine",
"https://api.open-meteo.com/v1/forecast",
)
params = {
"latitude": lat,
"longitude": long,
Expand All @@ -172,9 +176,14 @@ def forecast(lat, long, decimal, days=0):
"timezone": "auto",
"forecast_days": days,
}
responses = openmeteo.weather_api(url, params=params)

params_uv = {"latitude": lat, "longitude": long, "daily": "uv_index_max"}

responses = openmeteo.weather_api(urls[0], params=params)
responses_uv = openmeteo.weather_api(urls[1], params=params_uv)

response = responses[0]
response_uv = responses_uv[0]

daily_height_max = helper.round_decimal(
response.Daily().Variables(0).ValuesAsNumpy(), decimal
Expand All @@ -186,6 +195,10 @@ def forecast(lat, long, decimal, days=0):
response.Daily().Variables(2).ValuesAsNumpy(), decimal
)

daily_uv_index_max = helper.round_decimal(
response_uv.Daily().Variables(0).ValuesAsNumpy(), decimal
)

daily_data = {
"date": pd.date_range(
start=pd.to_datetime(response.Daily().Time(), unit="s", utc=True),
Expand All @@ -194,12 +207,12 @@ def forecast(lat, long, decimal, days=0):
inclusive="left",
)
}

return [
daily_height_max,
daily_direction_dominant,
daily_period_max,
daily_data["date"],
daily_uv_index_max,
]


Expand Down
5 changes: 4 additions & 1 deletion src/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ def print_forecast(ocean, forecast):
print("Wave Direction: ", day[1])
if int(ocean["show_period"]) == 1:
print("Wave Period: ", day[2])
if int(ocean["show_uv"]) == 1:
print("UV Index: ", day[4])
print("\n")


Expand Down Expand Up @@ -248,7 +250,7 @@ def forecast_to_json(data, decimal):
"""
Takes forecast() as input and returns it in JSON format
"""
surf_height, swell_direction, swell_period, dates = data
surf_height, swell_direction, swell_period, dates, uv_index = data

# Formatting into JSON
forecasts = []
Expand All @@ -258,6 +260,7 @@ def forecast_to_json(data, decimal):
"surf height": round(float(surf_height[i]), decimal),
"swell direction": round(float(swell_direction[i]), decimal),
"swell period": round(float(swell_period[i]), decimal),
"uv index": round(float(uv_index[i]), decimal),
}
forecasts.append(forecast)

Expand Down
Loading