Skip to content

Commit

Permalink
change to use openmeteo_requests
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdudfield committed Dec 1, 2024
1 parent ffbaad7 commit 9d3a760
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions quartz_solar_forecast/weather/open_meteo.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from datetime import datetime
from typing import List

import openmeteo_requests
import pandas as pd
import requests
import requests_cache
from retry_requests import retry


class WeatherService:
Expand Down Expand Up @@ -91,9 +94,7 @@ def _validate_date_format(self, start_date: str, end_date: str) -> None:
try:
start_datetime = datetime.strptime(start_date, "%Y-%m-%d")
end_datetime = datetime.strptime(end_date, "%Y-%m-%d")
assert (
end_datetime > start_datetime
), "End date must be greater than start date."
assert end_datetime > start_datetime, "End date must be greater than start date."
except (ValueError, AssertionError) as e:
raise ValueError(
f"Invalid date format or range. Please use YYYY-MM-DD and ensure end_date is greater than start_date. Error: {str(e)}"
Expand Down Expand Up @@ -149,12 +150,16 @@ def get_hourly_weather(
"terrestrial_radiation",
]
url = self._build_url(latitude, longitude, start_date, end_date, variables)

cache_session = requests_cache.CachedSession(".cache", expire_after=-1)
retry_session = retry(cache_session, retries=5, backoff_factor=0.2)
try:
response = requests.get(url)
openmeteo = openmeteo_requests.Client(session=retry_session)
response = openmeteo.weather_api(url, params={})
except requests.exceptions.Timeout:

raise TimeoutError(f"Request to OpenMeteo API timed out. URl - {url}")
data = response.json()["hourly"]

data = response[0].Hourly()

df = pd.DataFrame(data)
df["time"] = pd.to_datetime(df["time"])
Expand All @@ -165,5 +170,5 @@ def get_hourly_weather(
"time": "date",
}
)

return df

0 comments on commit 9d3a760

Please sign in to comment.