Skip to content

Commit 397b0b5

Browse files
committed
offline energy functionality
1 parent ebcd4e8 commit 397b0b5

File tree

4 files changed

+439
-12
lines changed

4 files changed

+439
-12
lines changed

codegreen_core/data/entsoe.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ def _convert_to_60min_interval(rawData):
231231
# determining how many rows need to be combined to get data in 60 min format.
232232
groupingFactor = int(60 / duration)
233233
oldData = rawData["data"]
234+
# check if there is enough data to convert to 60 min
235+
if (len(oldData) < groupingFactor):
236+
raise ValueError("Data cannot be converted into 60 min interval since there is inadequate number of rows in the data")
237+
234238
oldData["startTimeUTC"] = pd.to_datetime(oldData["startTimeUTC"])
235239
start_time = oldData["startTimeUTC"].min()
236240
end_time = oldData["startTimeUTC"].max()
@@ -256,6 +260,15 @@ def _convert_date_to_entsoe_format(dt: datetime):
256260
return dt.replace(minute=0, second=0, microsecond=0).strftime("%Y%m%d%H%M")
257261

258262

263+
def _format_energy_data(df):
264+
start_time_column = df.pop("startTimeUTC")
265+
df.insert(0, "startTime", start_time_column)
266+
local_timezone = datetime.now().astimezone().tzinfo
267+
df["startTime"] = pd.to_datetime(df["startTime"], format="%Y%m%d%H%M").dt.tz_localize("UTC").dt.tz_convert(local_timezone)
268+
df.insert(1, "startTimeUTC", start_time_column)
269+
return df
270+
271+
259272
# the main methods
260273

261274

@@ -295,13 +308,14 @@ def get_actual_production_percentage(country, start, end, interval60=False) -> d
295308
raise ValueError("Invalid end date. Generation data is only available for the past and not the future. Use the forecast API instead")
296309
# this is not allowed because the entsoe-py returns error if it's greater than the present
297310
#warnings.warn("End date is in the future. Will fetch data only till the present")
298-
311+
299312
options = {
300313
"country": country,
301-
"start": start,
302-
"end": end,
314+
"start": start.replace(minute=0,second=0),
315+
"end": end.replace(second=0,minute=0),
303316
"interval60": interval60,
304317
}
318+
# print(options)
305319
# get actual generation data per production type and convert it into 60 min interval if required
306320
totalRaw = _entsoe_get_actual_generation(options)
307321
total = totalRaw["data"]
@@ -354,7 +368,7 @@ def get_actual_production_percentage(country, start, end, interval60=False) -> d
354368
table[fieldName] = table[fieldName].astype(int)
355369

356370
return {
357-
"data": table,
371+
"data": _format_energy_data(table),
358372
"data_available": True,
359373
"time_interval": duration,
360374
}
@@ -424,7 +438,7 @@ def get_forecast_percent_renewable(
424438
windsolar["startTimeUTC"], format="%Y%m%d%H%M"
425439
)
426440
windsolar["posix_timestamp"] = windsolar["startTimeUTC"].astype(int) // 10**9
427-
return {"data": windsolar, "data_available": True, "time_interval": 60}
441+
return {"data": _format_energy_data(windsolar), "data_available": True, "time_interval": 60}
428442
except Exception as e:
429443
print(e)
430444
print(traceback.format_exc())

0 commit comments

Comments
 (0)