@@ -16,7 +16,12 @@ class Forecast:
1616
1717 >>> import datapoint
1818 >>> m = datapoint.Manager.Manager(api_key = "blah")
19- >>> f = m.get_forecast(latitude=50, longitude=0, frequency="hourly")
19+ >>> f = m.get_forecast(
20+ latitude=50,
21+ longitude=0,
22+ frequency="hourly",
23+ convert_weather_code=True,
24+ )
2025 >>> f.now()
2126 {
2227 'time': datetime.datetime(2024, 2, 19, 13, 0, tzinfo=datetime.timezone.utc),
@@ -101,12 +106,14 @@ class Forecast:
101106 }
102107 """
103108
104- def __init__ (self , frequency , api_data ):
109+ def __init__ (self , frequency , api_data , convert_weather_code ):
105110 """
106111 :param frequency: Frequency of forecast: 'hourly', 'three-hourly' or 'daily'
107112 :param api_data: Data returned from API call
113+ :param: convert_weather_code: Convert numeric weather codes to string description
108114 :type frequency: string
109115 :type api_data: dict
116+ :type convert_weather_code: bool
110117 """
111118 self .frequency = frequency
112119 # Need to parse format like 2024-02-17T15:00Z. This can only be
@@ -136,6 +143,10 @@ def __init__(self, frequency, api_data):
136143 2
137144 ] #: The elevation of the location of the provided forecast
138145
146+ self .convert_weather_code = (
147+ convert_weather_code #: Convert numeric weather codes to string description
148+ )
149+
139150 forecasts = api_data ["features" ][0 ]["properties" ]["timeSeries" ]
140151 parameters = api_data ["parameters" ][0 ]
141152 if frequency == "daily" :
@@ -203,23 +214,57 @@ def _build_timesteps_from_daily(self, forecasts, parameters):
203214 case_corrected_element = (
204215 trimmed_element [0 ].lower () + trimmed_element [1 :]
205216 )
206- day_step [case_corrected_element ] = {
207- "value" : value ,
208- "description" : parameters [element ]["description" ],
209- "unit_name" : parameters [element ]["unit" ]["label" ],
210- "unit_symbol" : parameters [element ]["unit" ]["symbol" ]["type" ],
211- }
217+
218+ if (
219+ case_corrected_element == "significantWeatherCode"
220+ and self .convert_weather_code
221+ ):
222+ day_step [case_corrected_element ] = {
223+ "value" : WEATHER_CODES [str (value )],
224+ "description" : parameters [element ]["description" ],
225+ "unit_name" : parameters [element ]["unit" ]["label" ],
226+ "unit_symbol" : parameters [element ]["unit" ]["symbol" ][
227+ "type"
228+ ],
229+ }
230+
231+ else :
232+ day_step [case_corrected_element ] = {
233+ "value" : value ,
234+ "description" : parameters [element ]["description" ],
235+ "unit_name" : parameters [element ]["unit" ]["label" ],
236+ "unit_symbol" : parameters [element ]["unit" ]["symbol" ][
237+ "type"
238+ ],
239+ }
212240 elif element .startswith ("night" ):
213241 trimmed_element = element .replace ("night" , "" )
214242 case_corrected_element = (
215243 trimmed_element [0 ].lower () + trimmed_element [1 :]
216244 )
217- night_step [case_corrected_element ] = {
218- "value" : value ,
219- "description" : parameters [element ]["description" ],
220- "unit_name" : parameters [element ]["unit" ]["label" ],
221- "unit_symbol" : parameters [element ]["unit" ]["symbol" ]["type" ],
222- }
245+
246+ if (
247+ case_corrected_element == "significantWeatherCode"
248+ and self .convert_weather_code
249+ ):
250+ night_step [case_corrected_element ] = {
251+ "value" : WEATHER_CODES [str (value )],
252+ "description" : parameters [element ]["description" ],
253+ "unit_name" : parameters [element ]["unit" ]["label" ],
254+ "unit_symbol" : parameters [element ]["unit" ]["symbol" ][
255+ "type"
256+ ],
257+ }
258+
259+ else :
260+ night_step [case_corrected_element ] = {
261+ "value" : value ,
262+ "description" : parameters [element ]["description" ],
263+ "unit_name" : parameters [element ]["unit" ]["label" ],
264+ "unit_symbol" : parameters [element ]["unit" ]["symbol" ][
265+ "type"
266+ ],
267+ }
223268 elif element == "maxUvIndex" :
224269 day_step [element ] = {
225270 "value" : value ,
@@ -260,7 +305,7 @@ def _build_timestep(self, forecast, parameters):
260305 forecast ["time" ], "%Y-%m-%dT%H:%M%z"
261306 )
262307
263- elif element == "significantWeatherCode" :
308+ elif element == "significantWeatherCode" and self . convert_weather_code :
264309 timestep [element ] = {
265310 "value" : WEATHER_CODES [str (value )],
266311 "description" : parameters [element ]["description" ],
0 commit comments