-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from aryanbhosale/main
add live data integration
- Loading branch information
Showing
13 changed files
with
164 additions
and
60 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
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
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 |
---|---|---|
@@ -1,18 +1,39 @@ | ||
""" Example code to run the forecast""" | ||
import pandas as pd | ||
from datetime import datetime | ||
from quartz_solar_forecast.forecast import run_forecast | ||
from quartz_solar_forecast.pydantic_models import PVSite | ||
from datetime import datetime, timedelta | ||
from datetime import datetime, timezone | ||
|
||
# Set plotly backend to be plotly, you might have to install plotly | ||
pd.options.plotting.backend = "plotly" | ||
|
||
def main(): | ||
# make input data | ||
site = PVSite(latitude=51.75, longitude=-1.25, capacity_kwp=1.25, inverter_type="enphase") | ||
|
||
ts = datetime.today() - timedelta(weeks=1) | ||
predictions_df = run_forecast(site=site, ts=ts, nwp_source="icon") | ||
|
||
print(predictions_df) | ||
print(f"Max: {predictions_df['power_wh'].max()}") | ||
timestamp = datetime.now().timestamp() | ||
timestamp_str = datetime.fromtimestamp(timestamp, tz=timezone.utc).strftime('%Y-%m-%d %H:%M:%S') | ||
ts = pd.to_datetime(timestamp_str) | ||
|
||
# make input data with live enphase data | ||
site_live = PVSite(latitude=51.75, longitude=-1.25, capacity_kwp=1.25, inverter_type="enphase") | ||
|
||
# make input data with nan data | ||
site_no_live = PVSite(latitude=51.75, longitude=-1.25, capacity_kwp=1.25) | ||
|
||
# run model, with and without recent pv data | ||
predictions_with_recent_pv_df = run_forecast(site=site_live, ts=ts) | ||
predictions_df = run_forecast(site=site_no_live, ts=ts) | ||
|
||
predictions_with_recent_pv_df["power_kw_no_live_pv"] = predictions_df["power_kw"] | ||
|
||
# plot | ||
fig = predictions_with_recent_pv_df.plot( | ||
title="PV Forecast", | ||
template="plotly_dark", | ||
y=["power_kw", "power_kw_no_live_pv"], | ||
labels={"value": "Power (kW)", "index": "Time"}, | ||
) | ||
fig.show(renderer="browser") | ||
|
||
if __name__ == "__main__": | ||
main() |
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
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
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
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
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
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
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
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,63 @@ | ||
import pandas as pd | ||
import numpy as np | ||
import xarray as xr | ||
import pytest | ||
from unittest.mock import patch | ||
from datetime import datetime | ||
from quartz_solar_forecast.pydantic_models import PVSite | ||
|
||
def mock_enphase_data(*args, **kwargs): | ||
return pd.DataFrame({ | ||
'timestamp': [ | ||
datetime(2024, 6, 5, 11, 25), | ||
datetime(2024, 6, 5, 11, 30), | ||
datetime(2024, 6, 5, 11, 35) | ||
], | ||
'power_kw': [0.5, 0.6, 0.7] | ||
}) | ||
|
||
@pytest.mark.parametrize("site, expected_data", [ | ||
(PVSite(latitude=40.7128, longitude=-74.0059, capacity_kwp=8.5, inverter_type='enphase'), mock_enphase_data()), | ||
]) | ||
@patch('quartz_solar_forecast.inverters.enphase.get_enphase_data', side_effect=mock_enphase_data) | ||
def test_make_pv_data_enphase(mock_get_enphase, site, expected_data, ts=pd.Timestamp('2023-06-14 12:15:00')): | ||
from quartz_solar_forecast.data import make_pv_data | ||
result = make_pv_data(site, ts) | ||
expected = expected_data[expected_data['timestamp'] <= ts] | ||
expected_xr = xr.DataArray( | ||
data=expected['power_kw'].values.reshape(1, -1), | ||
dims=['pv_id', 'timestamp'], | ||
coords={ | ||
'longitude': (['pv_id'], [site.longitude]), | ||
'latitude': (['pv_id'], [site.latitude]), | ||
'timestamp': (['timestamp'], expected['timestamp'].values.astype('datetime64[ns]')), | ||
'pv_id': [1], | ||
'kwp': (['pv_id'], [site.capacity_kwp]), | ||
'tilt': (["pv_id"], [site.tilt]), | ||
'orientation': (["pv_id"], [site.orientation]), | ||
} | ||
).to_dataset(name='generation_kw') | ||
|
||
assert result.equals(expected_xr) | ||
|
||
@pytest.mark.parametrize("site, expected_data", [ | ||
(PVSite(latitude=40.7128, longitude=-74.0059, capacity_kwp=8.5, inverter_type='unknown'), np.array([[np.nan]])), | ||
]) | ||
def test_make_pv_data_no_live(site, expected_data, ts=pd.Timestamp('2023-06-14 12:15:00')): | ||
from quartz_solar_forecast.data import make_pv_data | ||
result = make_pv_data(site, ts) | ||
expected_xr = xr.DataArray( | ||
data=expected_data, | ||
dims=['pv_id', 'timestamp'], | ||
coords={ | ||
'longitude': (['pv_id'], [site.longitude]), | ||
'latitude': (['pv_id'], [site.latitude]), | ||
'timestamp': (['timestamp'], [ts]), | ||
'pv_id': [1], | ||
'kwp': (['pv_id'], [site.capacity_kwp]), | ||
'tilt': (["pv_id"], [site.tilt]), | ||
'orientation': (["pv_id"], [site.orientation]), | ||
} | ||
).to_dataset(name='generation_kw') | ||
|
||
assert result.equals(expected_xr) |
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
Oops, something went wrong.