-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
36e4268
commit 367342f
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from pvsite_datamodel.sqlmodels import ForecastValueSQL, GenerationSQL, ForecastSQL | ||
from sqlalchemy.sql import func | ||
|
||
from datetime import datetime, timedelta | ||
import pandas as pd | ||
from typing import Optional | ||
|
||
# Wad to get all the forecast for the last 7 days made, at this time. | ||
# And find the ME for each forecast horizon | ||
|
||
""" | ||
Here is the SQL query that it based off | ||
select | ||
AVG(generation.generation_power_kw - forecast_values.forecast_power_kw) | ||
-- generation.generation_power_kw, | ||
-- forecast_values.forecast_power_kw, | ||
-- forecast_values.start_utc, | ||
,horizon_minutes | ||
-- * | ||
from forecast_values | ||
JOIN forecasts ON forecasts.forecast_uuid = forecast_values.forecast_uuid | ||
join generation on generation.start_utc = forecast_values.start_utc | ||
WHERE forecast_values.start_utc >= '2024-11-05' | ||
AND forecasts.site_uuid = 'adaf6be8-4e30-4c98-ac27-964447e9c8e6' | ||
AND generation.site_uuid = 'adaf6be8-4e30-4c98-ac27-964447e9c8e6' | ||
and extract(hour from forecasts.created_utc) = 7 | ||
group by horizon_minutes | ||
-- order by forecast_values.start_utc | ||
""" | ||
|
||
|
||
def get_me_values(session, hour: int, start_datetime: Optional[datetime] = None) -> pd.DataFrame: | ||
""" | ||
Get the ME values for the last 7 days for a given hour, for a given hour creation time | ||
args: | ||
hour: the hour of whent he forecast is created | ||
start_datetime:: the start datetime to filter on. | ||
session: sqlalchemy session | ||
""" | ||
|
||
if start_datetime is None: | ||
start_datetime = datetime.now() - timedelta(days=7) | ||
|
||
query = session.query( | ||
func.avg(ForecastValueSQL.forecast_power_kw - GenerationSQL.generation_power_kw), | ||
ForecastValueSQL.horizon_minutes, | ||
) | ||
|
||
# join | ||
query = query.join(ForecastSQL) | ||
query = query.filter(GenerationSQL.start_utc == ForecastValueSQL.start_utc) | ||
|
||
# only include the last 7 days | ||
query = query.filter(ForecastValueSQL.start_utc >= start_datetime) | ||
query = query.filter(GenerationSQL.start_utc >= start_datetime) | ||
|
||
# filter on site | ||
query = query.filter(ForecastSQL.site_uuid == "adaf6be8-4e30-4c98-ac27-964447e9c8e6") | ||
query = query.filter(GenerationSQL.site_uuid == "adaf6be8-4e30-4c98-ac27-964447e9c8e6") | ||
|
||
# filter on created_utc | ||
query = query.filter((func.extract("hour", ForecastSQL.created_utc) == hour)) | ||
|
||
# group by forecast horizon | ||
query = query.group_by(ForecastValueSQL.horizon_minutes) | ||
|
||
# order by forecast horizon | ||
query = query.order_by(ForecastValueSQL.horizon_minutes) | ||
|
||
me = query.all() | ||
|
||
me_df = pd.DataFrame(me, columns=["me_kw", "horizon_minutes"]) | ||
|
||
return me_df | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters