1- import openmeteo_requests
2-
3- import pandas as pd
4- import requests_cache
5- from retry_requests import retry
6-
7- # All supported hourly fields exposed by this service
8- ALL_HOURLY_FIELDS = [
9- "temperature_2m" ,
10- "relative_humidity_2m" ,
11- "rain" ,
12- "apparent_temperature" ,
13- "precipitation_probability" ,
14- "precipitation" ,
15- "wind_direction_10m" ,
16- "wind_speed_10m" ,
17- "soil_temperature_0cm" ,
18- "uv_index" ,
19- ]
20-
1+ from app .services .external_apis .weather_api import WeatherAPI
212
223class ExternalAPIsCoordinator :
234 def __init__ (self ):
24- pass
25-
26- def get_weather (self , latitude : float , longitude : float , hourly_fields : list [str ] | None = None ):
27- """
28- Get weather data for a given latitude and longitude.
29-
30- Args:
31- latitude (float): The latitude of the location.
32- longitude (float): The longitude of the location.
33- hourly_fields (list[str] | None): Subset of hourly variables to request.
34- Defaults to ALL_HOURLY_FIELDS when None or empty.
35-
36- Returns:
37- dict: A dictionary containing the weather data.
38- """
39- requested = [f for f in (hourly_fields or []) if f in ALL_HOURLY_FIELDS ]
40- if not requested :
41- requested = list (ALL_HOURLY_FIELDS )
42-
43- # Setup the Open-Meteo API client with cache and retry on error
44- cache_session = requests_cache .CachedSession ('.cache' , expire_after = 3600 )
45- retry_session = retry (cache_session , retries = 5 , backoff_factor = 0.2 )
46- openmeteo = openmeteo_requests .Client (session = retry_session )
47-
48- url = "https://api.open-meteo.com/v1/forecast"
49- params = {
50- "latitude" : latitude ,
51- "longitude" : longitude ,
52- "hourly" : requested ,
53- }
54- responses = openmeteo .weather_api (url , params = params )
55-
56- response = responses [0 ]
57- print (f"Coordinates: { response .Latitude ()} °N { response .Longitude ()} °E" )
58- print (f"Elevation: { response .Elevation ()} m asl" )
59- print (f"Timezone difference to GMT+0: { response .UtcOffsetSeconds ()} s" )
60-
61- hourly = response .Hourly ()
62-
63- hourly_data : dict = {
64- "date" : pd .date_range (
65- start = pd .to_datetime (hourly .Time (), unit = "s" , utc = True ),
66- end = pd .to_datetime (hourly .TimeEnd (), unit = "s" , utc = True ),
67- freq = pd .Timedelta (seconds = hourly .Interval ()),
68- inclusive = "left" ,
69- )
70- }
71-
72- for i , field in enumerate (requested ):
73- hourly_data [field ] = hourly .Variables (i ).ValuesAsNumpy ().tolist ()
74-
75- # Convert pandas Timestamp objects to ISO strings for JSON serialization
76- hourly_data ["date" ] = [d .isoformat () for d in hourly_data ["date" ]]
77-
78- return {
79- "latitude" : response .Latitude (),
80- "longitude" : response .Longitude (),
81- "elevation" : response .Elevation (),
82- "timezone" : response .Timezone (),
83- "timezone_abbreviation" : response .TimezoneAbbreviation (),
84- "utc_offset_seconds" : response .UtcOffsetSeconds (),
85- "hourly" : hourly_data ,
86- }
5+ self .weather_api = WeatherAPI ()
6+
7+ def get_weather (self , latitude : float , longitude : float , start_date : str , end_date : str , hourly_fields : list [str ] | None = None ):
8+ return self .weather_api .get_weather (latitude , longitude , start_date , end_date , hourly_fields )
0 commit comments