Skip to content

Commit

Permalink
Merge pull request #111 from MylesPerHour201/main
Browse files Browse the repository at this point in the history
Add more data
  • Loading branch information
ryansurf authored Aug 15, 2024
2 parents 378c59c + ef49210 commit 03cab41
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ Wave Period: 9.8
| color / c | Choose color of wave art. Ex: `color=light_blue` |
| json / j | Output the data in JSON format. Must be the only argument |
| gpt / g | Activates the GPT surf report. Change the `GPT_PROMPT` variable in `.env` to customize responses. Default = off |

| show_cloud_cover / scc | Show the hourly cloud cover |
| show_visibility / sv | Show the hourly visibility |

**Examples**
* Arguments are seperated by commas.
* `curl localhost:8000`
Expand Down
60 changes: 60 additions & 0 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from http import HTTPStatus

import numpy as np
import openmeteo_requests
import pandas as pd
import requests
Expand Down Expand Up @@ -298,6 +299,61 @@ def forecast(lat, long, decimal, days=0):
return forecast_data


def get_hourly_forecast(lat, long, days=1, unit="fahrenheit"):
"""
Gets hourly weather data
"""
# Setup the Open-Meteo API client with cache and retry on error
cache_session = requests_cache.CachedSession(".cache", expire_after=3600)
retry_session = retry(cache_session, retries=5, backoff_factor=0.2)
openmeteo = openmeteo_requests.Client(session=retry_session)

# Make sure all required weather variables are listed here
# The order of variables in hourly or daily is important
# to assign them correctly below
url = "https://api.open-meteo.com/v1/forecast"
params = {
"latitude": lat,
"longitude": long,
"hourly": ["cloud_cover", "visibility"],
"temperature_unit": unit,
"wind_speed_unit": "mph",
"precipitation_unit": "inch",
"forecast_days": days,
}

responses = openmeteo.weather_api(url, params=params)
response = responses[0]

hourly = response.Hourly()
hourly_cloud_cover = hourly.Variables(0).ValuesAsNumpy()
hourly_visibility = hourly.Variables(1).ValuesAsNumpy()

hourly_data = pd.DataFrame({
"date": pd.date_range(
start=pd.to_datetime(hourly.Time(), unit="s", utc=True),
end=pd.to_datetime(hourly.TimeEnd(), unit="s", utc=True),
freq=pd.Timedelta(seconds=hourly.Interval()),
inclusive="left",
),
"cloud_cover": hourly_cloud_cover,
"visibility": hourly_visibility,
})

# Sets variable to get current time
current_time = pd.Timestamp.now(tz="UTC")

# Sets variable to find index of current hour
curr_hour = np.argmin(np.abs(hourly_data["date"] - current_time))

# Creates dictionary for the current hour's weather data
curr_hour_data = {}
for i in ["cloud_cover", "visibility"]:
curr_hour_data[i] = round(float(hourly_data[i].iloc[curr_hour]), 1)

return curr_hour_data


def gather_data(lat, long, arguments):
"""
Calls APIs though python files,
Expand All @@ -312,6 +368,8 @@ def gather_data(lat, long, arguments):

wind_temp = current_wind_temp(lat, long, arguments["decimal"])

hourly_dict = get_hourly_forecast(lat, long, arguments["decimal"])

rain_data = get_rain(lat, long, arguments["decimal"])
air_temp, wind_speed, wind_dir = wind_temp[0], wind_temp[1], wind_temp[2]
rain_sum, precipitation_probability_max = rain_data[0], rain_data[1]
Expand All @@ -337,6 +395,8 @@ def gather_data(lat, long, arguments):
"Unit": arguments["unit"],
"Rain Sum": rain_sum,
"Precipitation Probability Max": precipitation_probability_max,
"Cloud Cover": hourly_dict["cloud_cover"],
"Visibility": hourly_dict["visibility"],
}
return ocean_data_dict

Expand Down
2 changes: 1 addition & 1 deletion src/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def run(lat=0, long=0, args=None):
if arguments["json_output"] == 0:
# Response prints all the outputs & returns the GPT response
response = helper.print_outputs(
city, ocean_data_dict, arguments, gpt_prompt, gpt_info
ocean_data_dict, arguments, gpt_prompt, gpt_info
)
# Returns ocean data, GPT response
return ocean_data_dict, response
Expand Down
13 changes: 10 additions & 3 deletions src/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def arguments_dictionary(lat, long, city, args):
"forecast_days": get_forecast_days(args),
"color": get_color(args),
"gpt": 0,
"show_cloud_cover": 0,
"show_visibility": 0,
}
# Updates the arguments dict with the values from the CLI args
arguments = set_output_values(args, arguments)
Expand Down Expand Up @@ -87,6 +89,10 @@ def set_output_values(args, arguments_dictionary): # noqa
"srs": ("show_rain_sum", 1),
"show_precipitation_prob": ("show_precipitation_prob", 1),
"spp": ("show_precipitation_prob", 1),
"show_cloud_cover": ("show_cloud_cover", 1),
"scc": ("show_cloud_cover", 1),
"show_visibility": ("show_visibility", 1),
"sv": ("show_visibility", 1),
}

# Update arguments_dictionary based on the cli arguments in args
Expand Down Expand Up @@ -155,6 +161,8 @@ def print_ocean_data(arguments_dict, ocean_data_dict):
"Precipitation Probability Max",
"Precipitation Probability Max: ",
),
("show_cloud_cover", "Cloud Cover", "Cloud Cover: "),
("show_visibility", "Visibility", "Visibility: "),
]

# arg_key example: "show_height : 1" from arguments_dict
Expand Down Expand Up @@ -256,14 +264,12 @@ def json_output(data_dict):
return data_dict


def print_outputs(city, ocean_data_dict, arguments, gpt_prompt, gpt_info):
def print_outputs(ocean_data_dict, arguments, gpt_prompt, gpt_info):
"""
Basically the main printing function,
calls all the other printing functions
"""
print("\n")
if city == "No data":
print("No location found")
if ocean_data_dict["Height"] == "No data":
print(ocean_data_dict["Lat"], ocean_data_dict["Long"])
print("No ocean data at this location.")
Expand All @@ -286,6 +292,7 @@ def print_outputs(city, ocean_data_dict, arguments, gpt_prompt, gpt_info):
)
# Prints the forecast(if activated in CLI args)
print_forecast(arguments, forecast)

# Checks if GPT in args, prints GPT response if True
gpt_response = None
if arguments["gpt"] == 1:
Expand Down

1 comment on commit 03cab41

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src
   __init__.py00100% 
   api.py139795%37–41, 59, 81–82, 114–115
   art.py9367%32–33, 36
   cli.py250100% 
   dev_streamlit.py37370%1–86
   gpt.py10640%16–21, 32–45
   helper.py1223770%116, 127–131, 209–217, 229, 242–243, 274–275, 299–300, 363–373, 380–388
   send_email.py24240%5–48
   server.py471079%55, 68–71, 87–91, 103–105
   settings.py220100% 
   streamlit_helper.py33330%5–90
TOTAL46815766% 

Tests Skipped Failures Errors Time
11 0 💤 0 ❌ 0 🔥 28.250s ⏱️

Please sign in to comment.