From 0c57760c78695572125f8d38af5d0b1036b04e2b Mon Sep 17 00:00:00 2001 From: Peter Dudfield <34686298+peterdudfield@users.noreply.github.com> Date: Mon, 2 Dec 2024 12:06:23 +0000 Subject: [PATCH] Issue/fix open meteo (#228) * change to use openmeteo_requests * format --- quartz_solar_forecast/weather/open_meteo.py | 30 +++++++++++++++------ 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/quartz_solar_forecast/weather/open_meteo.py b/quartz_solar_forecast/weather/open_meteo.py index cc6b44f..7da7110 100644 --- a/quartz_solar_forecast/weather/open_meteo.py +++ b/quartz_solar_forecast/weather/open_meteo.py @@ -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: @@ -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)}" @@ -149,14 +150,27 @@ 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"] - df = pd.DataFrame(data) + hourly = response[0].Hourly() + hourly_data = {"time": pd.date_range( + start=pd.to_datetime(hourly.Time(), unit="s", utc=False), + end=pd.to_datetime(hourly.TimeEnd(), unit="s", utc=False), + freq=pd.Timedelta(seconds=hourly.Interval()), + inclusive="left" + )} + + for i, variable in enumerate(variables): + hourly_data[variable] = hourly.Variables(i).ValuesAsNumpy() + + df = pd.DataFrame(hourly_data) df["time"] = pd.to_datetime(df["time"]) # rename time column to date @@ -165,5 +179,5 @@ def get_hourly_weather( "time": "date", } ) - + return df