From 75ef0a5b312a79b97a671ae071f8bb28304250e5 Mon Sep 17 00:00:00 2001 From: Peter Dudfield <34686298+peterdudfield@users.noreply.github.com> Date: Fri, 4 Oct 2024 16:50:08 +0100 Subject: [PATCH] Large capacity (#205) * TDD: update test first * make forecast work for large sites * round results in test --- quartz_solar_forecast/forecast.py | 16 ++++++++++++++++ tests/test_forecast.py | 20 +++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/quartz_solar_forecast/forecast.py b/quartz_solar_forecast/forecast.py index 7cc7f2d..a75647b 100644 --- a/quartz_solar_forecast/forecast.py +++ b/quartz_solar_forecast/forecast.py @@ -1,4 +1,5 @@ from datetime import datetime, timedelta +import logging import pandas as pd @@ -6,6 +7,7 @@ from quartz_solar_forecast.forecasts import forecast_v1_tilt_orientation, TryolabsSolarPowerPredictor from quartz_solar_forecast.pydantic_models import PVSite +log = logging.getLogger(__name__) def predict_ocf( site: PVSite, model=None, ts: datetime | str = None, nwp_source: str = "icon" @@ -25,6 +27,16 @@ def predict_ocf( if isinstance(ts, str): ts = datetime.fromisoformat(ts) + if site.capacity_kwp > 4: + log.warning("Your site capacity is greater than 4kWp, " + "however the model is trained on sites with capacity <= 4kWp." + "We therefore will run the model with a capacity of 4 kWp, " + "and we'll scale the results afterwards.") + capacity_kwp_original = site.capacity_kwp + site.capacity_kwp = 4 + else: + capacity_kwp_original = site.capacity_kwp + # make pv and nwp data from nwp_source nwp_xr = get_nwp(site=site, ts=ts, nwp_source=nwp_source) pv_xr = make_pv_data(site=site, ts=ts) @@ -32,6 +44,10 @@ def predict_ocf( # load and run models pred_df = forecast_v1_tilt_orientation(nwp_source, nwp_xr, pv_xr, ts, model=model) + # scale the results if the capacity is different + if capacity_kwp_original != site.capacity_kwp: + pred_df["power_kw"] = pred_df["power_kw"] * capacity_kwp_original / site.capacity_kwp + return pred_df diff --git a/tests/test_forecast.py b/tests/test_forecast.py index 975103a..c2d0c70 100644 --- a/tests/test_forecast.py +++ b/tests/test_forecast.py @@ -2,6 +2,9 @@ from quartz_solar_forecast.pydantic_models import PVSite from datetime import datetime, timedelta +import numpy as np + + def test_run_forecast(): # make input data site = PVSite(latitude=51.75, longitude=-1.25, capacity_kwp=1.25) @@ -55,7 +58,22 @@ def test_run_forecast_historical(): print("\n Prediction based on UKMO NWP\n") print(predications_df_ukmo) print(f" Max: {predications_df_ukmo['power_kw'].max()}") - + print("\n Prediction based on XGB\n") print(predications_df_xgb) + +def test_large_capacity(): + + # make input data + site = PVSite(latitude=51.75, longitude=-1.25, capacity_kwp=4) + site_large = PVSite(latitude=51.75, longitude=-1.25, capacity_kwp=4000) + ts = datetime.today() - timedelta(weeks=2) + + # run model with icon, gfs and ukmo nwp + predications_df = run_forecast(site=site, model="gb", ts=ts, nwp_source="gfs") + predications_df_large = run_forecast(site=site_large, model="gb", ts=ts, nwp_source="gfs") + + assert np.round(predications_df["power_kw"].sum() * 1000, 8) == np.round( + predications_df_large["power_kw"].sum(), 8 + )