Skip to content

Commit

Permalink
Add UKMO model for inference as a option (#188)
Browse files Browse the repository at this point in the history
* Add UKMO model as an option in get_nwp()

* add tests for ukmo nwp

* add test for ukmo nwp

* clean up code and add consitency by replacing 'ukmo_seamless' with 'ukmo' where applicable.

---------

Co-authored-by: Clemo97 <[email protected]>
  • Loading branch information
Clemo97 and Clemo97 authored Sep 9, 2024
1 parent 01bab58 commit 0af63ee
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
2 changes: 2 additions & 0 deletions examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def main():
site = PVSite(latitude=51.75, longitude=-1.25, capacity_kwp=1.25)

ts = datetime.today() - timedelta(weeks=1)

# User has three options for the 'nwp_source': 'icon', 'gfs', or 'ukmo'.
predictions_df = run_forecast(site=site, ts=ts, nwp_source="icon")

print(predictions_df)
Expand Down
30 changes: 18 additions & 12 deletions quartz_solar_forecast/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_nwp(site: PVSite, ts: datetime, nwp_source: str = "icon") -> xr.Dataset:
:param site: the PV site
:param ts: the timestamp for when you want the forecast for
:param nwp_source: the nwp data source. Either "gfs" or "icon". Defaults to "icon"
:param nwp_source: the nwp data source. Either "gfs", "icon" or "ukmo". Defaults to "icon"
:return: nwp forecast in xarray
"""

Expand Down Expand Up @@ -55,25 +55,31 @@ def get_nwp(site: PVSite, ts: datetime, nwp_source: str = "icon") -> xr.Dataset:
url = "https://archive-api.open-meteo.com/v1/archive"

else:

# Getting NWP from open meteo weather forecast API by ICON or GFS model within the last 3 months
url_nwp_source = None
# Getting NWP from open meteo weather forecast API by ICON, GFS, or UKMO within the last 3 months
if nwp_source == "icon":
url_nwp_source = "dwd-icon"
url = f"https://api.open-meteo.com/v1/{url_nwp_source}"
elif nwp_source == "gfs":
url_nwp_source = "gfs"
url = f"https://api.open-meteo.com/v1/{url_nwp_source}"
elif nwp_source == "ukmo":
url = "https://api.open-meteo.com/v1/forecast"
else:
raise Exception(f'Source ({nwp_source}) must be either "icon" or "gfs"')

url = f"https://api.open-meteo.com/v1/{url_nwp_source}"
raise Exception(f'Source ({nwp_source}) must be either "icon", "gfs", or "ukmo"')

params = {
"latitude": site.latitude,
"longitude": site.longitude,
"start_date": f"{start}",
"end_date": f"{end}",
"hourly": variables
"latitude": site.latitude,
"longitude": site.longitude,
"start_date": f"{start}",
"end_date": f"{end}",
"hourly": variables
}

# Add the "models" parameter if using "ukmo"
if nwp_source == "ukmo":
params["models"] = "ukmo_seamless"

# Make API call to URL
response = openmeteo.weather_api(url, params=params)
hourly = response[0].Hourly()

Expand Down
4 changes: 2 additions & 2 deletions quartz_solar_forecast/forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def predict_ocf(
:param site: the PV site
:param model: the model to use for prediction
:param ts: the timestamp of the site. If None, defaults to the current timestamp rounded down to 15 minutes.
:param nwp_source: the nwp data source. Either "gfs" or "icon". Defaults to "icon"
:param nwp_source: the nwp data source. Either "gfs", "icon" or "ukmo". Defaults to "icon"
:return: The PV forecast of the site for time (ts) for 48 hours
"""
if ts is None:
Expand Down Expand Up @@ -103,7 +103,7 @@ def run_forecast(
:param model: the model to use for prediction, choose between "ocf" and "tryolabs",
by default "ocf" is used
:param ts: the timestamp of the site. If None, defaults to the current timestamp rounded down to 15 minutes.
:param nwp_source: the nwp data source. Either "gfs" or "icon". Defaults to "icon"
:param nwp_source: the nwp data source. Either "gfs", "icon" or "ukmo". Defaults to "icon"
(only relevant if model=="gb")
:return: The PV forecast of the site for time (ts) for 48 hours
"""
Expand Down
14 changes: 12 additions & 2 deletions tests/test_forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ def test_run_forecast():
site = PVSite(latitude=51.75, longitude=-1.25, capacity_kwp=1.25)
ts = datetime.today() - timedelta(weeks=2)

# run model with icon and gfs nwp
# run model with icon, gfs and ukmo nwp
predications_df_gfs = run_forecast(site=site, model="gb", ts=ts, nwp_source="gfs")
predications_df_icon = run_forecast(site=site, model="gb", ts=ts, nwp_source="icon")
predications_df_ukmo = run_forecast(site=site, model="gb", ts=ts, nwp_source="ukmo")
predications_df_xgb = run_forecast(site=site, ts=ts)

print("\n Prediction based on GFS NWP\n")
Expand All @@ -20,6 +21,10 @@ def test_run_forecast():
print(predications_df_icon)
print(f" Max: {predications_df_icon['power_kw'].max()}")

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)
print(f" Max: {predications_df_xgb['power_kw'].max()}")
Expand All @@ -31,9 +36,10 @@ def test_run_forecast_historical():
site = PVSite(latitude=51.75, longitude=-1.25, capacity_kwp=1.25)
ts = datetime.today() - timedelta(days=200)

# run model with icon and gfs nwp
# run model with icon, gfs and ukmo nwp
predications_df_gfs = run_forecast(site=site, ts=ts, model="gb", nwp_source="gfs")
predications_df_icon = run_forecast(site=site, ts=ts, model="gb", nwp_source="icon")
predications_df_ukmo = run_forecast(site=site, ts=ts, model="gb", nwp_source="ukmo")
predications_df_xgb = run_forecast(site=site, ts=ts, model="xgb")

print("\nPrediction for a date more than 180 days in the past")
Expand All @@ -45,6 +51,10 @@ def test_run_forecast_historical():
print("\n Prediction based on ICON NWP\n")
print(predications_df_icon)
print(f" Max: {predications_df_icon['power_kw'].max()}")

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)
Expand Down

0 comments on commit 0af63ee

Please sign in to comment.