Skip to content
Open
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
48 changes: 26 additions & 22 deletions src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ def ocean_information(
"latitude": lat,
"longitude": long,
"current": [
"wave_height",
"wave_direction",
"wave_period",
"swell_wave_height",
"swell_wave_direction",
"swell_wave_period",
"sea_surface_temperature",
],
"length_unit": unit,
Expand All @@ -210,15 +210,15 @@ def ocean_information(
current = response.Current()
if current is None:
return [0, 0, 0, 0]
current_wave_height = round(current.Variables(0).Value(), decimal)
current_wave_direction = round(current.Variables(1).Value(), decimal)
current_wave_period = round(current.Variables(2).Value(), decimal)
current_swell_wave_height = round(current.Variables(0).Value(), decimal)
current_swell_wave_direction = round(current.Variables(1).Value(), decimal)
current_swell_wave_period = round(current.Variables(2).Value(), decimal)
current_sea_surface_temperature = current.Variables(3).Value()

return [
current_wave_height,
current_wave_direction,
current_wave_period,
current_swell_wave_height,
current_swell_wave_direction,
current_swell_wave_period,
current_sea_surface_temperature,
]

Expand Down Expand Up @@ -261,7 +261,11 @@ def ocean_information_history(
params = {
"latitude": lat,
"longitude": long,
"hourly": ["wave_height", "wave_direction", "wave_period"],
"hourly": [
"swell_wave_height",
"swell_wave_direction",
"swell_wave_period",
],
"length_unit": unit,
"timezone": "auto",
"start_date": formatted_date_one_year_ago,
Expand All @@ -280,15 +284,15 @@ def ocean_information_history(

# Extract hourly values for the specified metrics
hourly = response.Hourly()
hourly_wave_height = hourly.Variables(0).ValuesAsNumpy()
hourly_wave_direction = hourly.Variables(1).ValuesAsNumpy()
hourly_wave_period = hourly.Variables(2).ValuesAsNumpy()
hourly_swell_wave_height = hourly.Variables(0).ValuesAsNumpy()
hourly_swell_wave_direction = hourly.Variables(1).ValuesAsNumpy()
hourly_swell_wave_period = hourly.Variables(2).ValuesAsNumpy()

# Retrieve data for the current hour from one year ago
return [
f"{hourly_wave_height[current_hour]:.{decimal}f}",
f"{hourly_wave_direction[current_hour]:.{decimal}f}",
f"{hourly_wave_period[current_hour]:.{decimal}f}",
f"{hourly_swell_wave_height[current_hour]:.{decimal}f}",
f"{hourly_swell_wave_direction[current_hour]:.{decimal}f}",
f"{hourly_swell_wave_period[current_hour]:.{decimal}f}",
]


Expand Down Expand Up @@ -368,9 +372,9 @@ def forecast(lat: float, long: float, decimal: int, days: int = 0) -> dict:
"latitude": lat,
"longitude": long,
"daily": [
"wave_height_max",
"wave_direction_dominant",
"wave_period_max",
"swell_wave_height_max",
"swell_wave_direction_dominant",
"swell_wave_period_max",
],
"length_unit": "imperial",
"timezone": "auto",
Expand Down Expand Up @@ -438,9 +442,9 @@ def forecast(lat: float, long: float, decimal: int, days: int = 0) -> dict:

forecast_data = {
"date": daily_data["date"],
"wave_height_max": marine_data[0],
"wave_direction_dominant": marine_data[1],
"wave_period_max": marine_data[2],
"swell_wave_height_max": marine_data[0],
"swell_wave_direction_dominant": marine_data[1],
"swell_wave_period_max": marine_data[2],
"uv_index_max": general_data[0],
"temperature_2m_max": general_data[1],
"temperature_2m_min": general_data[2],
Expand Down
37 changes: 32 additions & 5 deletions src/art.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
All ASCII art in this file
"""

import codecs
import logging
import sys

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -71,9 +73,34 @@ def print_wave(show_wave, show_large_wave, color):
color = "blue"

if show_large_wave:
print(
colors[color]
+ """
encoding = getattr(sys.stdout, "encoding", None) or ""
try:
is_utf8 = codecs.lookup(encoding).name == "utf-8"
except LookupError:
is_utf8 = False

if sys.platform == "win32" and not is_utf8:
print(
colors[color]
+ r"""
_
_//_
_///__`-.
_////__ \
_/////__ \
_//////__ |
_///////__ |
_////////__ /
_/////////__ .-'
_//////////__ .-'
///////////_______/
"""
+ colors["end"]
)
else:
print(
colors[color]
+ """
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣴⣶⠾⠿⠿⠯⣷⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣾⠛⠁⠀⠀⠀⠀⠀⠀⠈⢻⣦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⣾⠿⠁⠀⠀⠀⢀⣤⣾⣟⣛⣛⣶⣬⣿⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
Expand All @@ -84,8 +111,8 @@ def print_wave(show_wave, show_large_wave, color):
⢀⣄⣠⣶⣿⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⠓⠚⠋⠉⠀⠀⠀⠀⠀⠀⠈⠛⡛⡻⠿⠿⠙⠓⢒⣺⡿⠋⠁
⠉⠉⠉⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠉⠁⠀
"""
+ colors["end"]
)
+ colors["end"]
)
elif show_wave:
print(
colors[color]
Expand Down
50 changes: 24 additions & 26 deletions src/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,32 +103,30 @@ def _build_args_string(ns):
tokens.append("metric")
if ns.imperial:
tokens.append("imperial")
flag_map = {
"json": "json",
"gpt": "gpt",
"hide_wave": "hide_wave",
"hide_uv": "hide_uv",
"hide_height": "hide_height",
"hide_direction": "hide_direction",
"hide_period": "hide_period",
"hide_location": "hide_location",
"hide_date": "hide_date",
"show_large_wave": "show_large_wave",
"show_past_uv": "show_past_uv",
"show_height_history": "show_height_history",
"show_direction_history": "show_direction_history",
"show_period_history": "show_period_history",
"show_air_temp": "show_air_temp",
"show_wind_speed": "show_wind_speed",
"show_wind_direction": "show_wind_direction",
"show_rain_sum": "show_rain_sum",
"show_precipitation_prob": "show_precipitation_prob",
"show_cloud_cover": "show_cloud_cover",
"show_visibility": "show_visibility",
}
for attr, token in flag_map.items():
if getattr(ns, attr, False):
tokens.append(token)
flags = [
"json",
"gpt",
"hide_wave",
"hide_uv",
"hide_height",
"hide_direction",
"hide_period",
"hide_location",
"hide_date",
"show_large_wave",
"show_past_uv",
"show_height_history",
"show_direction_history",
"show_period_history",
"show_air_temp",
"show_wind_speed",
"show_wind_direction",
"show_rain_sum",
"show_precipitation_prob",
"show_cloud_cover",
"show_visibility",
]
tokens.extend([flag for flag in flags if getattr(ns, flag, False)])
return ",".join(tokens)


Expand Down
47 changes: 32 additions & 15 deletions src/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,16 @@ def print_location(city, show_city):
print("\n")


def _print_mapped_data(mappings, arguments_dict, data_dict):
"""
Helper function to print mapped data from a dictionary
if the argument is set.
"""
for arg_key, data_key, label in mappings:
if arguments_dict[arg_key] and data_key in data_dict:
print(f"{label}{data_dict[data_key]}")


def print_ocean_data(arguments_dict, ocean_data_dict):
"""
Prints ocean data (height, wave direction, period, etc).
Expand Down Expand Up @@ -242,9 +252,7 @@ def print_ocean_data(arguments_dict, ocean_data_dict):
("show_sea_temp", "Sea Surface Temperature", "Sea Surface Temp: "),
]

for arg_key, data_key, label in mappings:
if arguments_dict.get(arg_key) and data_key in ocean_data_dict:
print(f"{label}{ocean_data_dict[data_key]}")
_print_mapped_data(mappings, arguments_dict, ocean_data_dict)

if arguments_dict.get("show_tide") and ocean_data_dict.get("Tide"):
tide = ocean_data_dict["Tide"]
Expand Down Expand Up @@ -272,9 +280,13 @@ def print_forecast(ocean, forecast):
mappings = [
("show_date", "date", "Date: "),
("show_uv", "uv_index_max", "UV Index: "),
("show_height", "wave_height_max", "Wave Height: "),
("show_direction", "wave_direction_dominant", "Wave Direction: "),
("show_period", "wave_period_max", "Wave Period: "),
("show_height", "swell_wave_height_max", "Wave Height: "),
(
"show_direction",
"swell_wave_direction_dominant",
"Wave Direction: ",
),
("show_period", "swell_wave_period_max", "Wave Period: "),
("show_air_temp", "temperature_2m_max", "Air Temp Max: "),
("show_air_temp", "temperature_2m_min", "Air Temp Min: "),
("show_rain_sum", "rain_sum", "Rain Sum: "),
Expand All @@ -292,14 +304,18 @@ def print_forecast(ocean, forecast):
]

for day in range(ocean["forecast_days"]):
for arg_key, data_key, label in mappings:
if ocean[arg_key]:
# Extract day's data into a temporary dictionary
day_data = {}
for _, data_key, _ in mappings:
if data_key in forecast:
try:
data = forecast[data_key][day]
formatted = round(float(data), ocean["decimal"])
print(f"{label}{formatted}")
raw = forecast[data_key][day]
formatted = round(float(raw), ocean["decimal"])
except TypeError:
print(f"{label}{forecast[data_key][day]}")
formatted = forecast[data_key][day]
day_data[data_key] = formatted

_print_mapped_data(mappings, ocean, day_data)
print("\n")


Expand Down Expand Up @@ -369,13 +385,14 @@ def forecast_to_json(forecast_data, decimal):
forecast = {
"date": str(date.date()),
"surf height": round(
float(forecast_data["wave_height_max"][i]), decimal
float(forecast_data["swell_wave_height_max"][i]), decimal
),
"swell direction": round(
float(forecast_data["wave_direction_dominant"][i]), decimal
float(forecast_data["swell_wave_direction_dominant"][i]),
decimal,
),
"swell period": round(
float(forecast_data["wave_period_max"][i]), decimal
float(forecast_data["swell_wave_period_max"][i]), decimal
),
"uv index": round(
float(forecast_data["uv_index_max"][i]), decimal
Expand Down
6 changes: 3 additions & 3 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ def test_forecast(mock_create_client):

FORECAST_LENGTH = 7

assert len(fc["wave_height_max"]) == FORECAST_LENGTH
assert len(fc["wave_direction_dominant"]) == FORECAST_LENGTH
assert len(fc["wave_period_max"]) == FORECAST_LENGTH
assert len(fc["swell_wave_height_max"]) == FORECAST_LENGTH
assert len(fc["swell_wave_direction_dominant"]) == FORECAST_LENGTH
assert len(fc["swell_wave_period_max"]) == FORECAST_LENGTH


@patch("src.api.ocean_information", return_value=[3.5, 180.0, 12.0, 20.0])
Expand Down
Loading