Skip to content

Commit

Permalink
Merge pull request #15 from EdFage/icon-data
Browse files Browse the repository at this point in the history
Use Icon Data
  • Loading branch information
peterdudfield authored Dec 6, 2023
2 parents af0e147 + 7a0e944 commit 22c7b6e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
39 changes: 34 additions & 5 deletions quartz_solar_forecast/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
ssl._create_default_https_context = ssl._create_unverified_context


def get_gfs_nwp(site: PVSite, ts:datetime) -> xr.Dataset:
def get_nwp(site: PVSite, ts: datetime, nwp_source: str = "icon") -> xr.Dataset:
"""
Get GFS NWP data for a point time space and time
: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"
:return: nwp forecast in xarray
"""

Expand All @@ -37,16 +38,42 @@ def get_gfs_nwp(site: PVSite, ts:datetime) -> xr.Dataset:
start = ts.date()
end = start + pd.Timedelta(days=7)

# Getting GFS, from OPEN METEO
# Getting NWP, from OPEN METEO
url_nwp_source = None
if nwp_source == "icon":
url_nwp_source = "dwd-icon"
elif nwp_source == "gfs":
url_nwp_source = "gfs"
else:
raise Exception(f'Source ({nwp_source}) must be either "icon" or "gfs"')

# Pull data from the nwp_source provided
url = (
f"https://api.open-meteo.com/v1/gfs?"
f"https://api.open-meteo.com/v1/{url_nwp_source}?"
f"latitude={site.latitude}&longitude={site.longitude}"
f"&hourly={','.join(variables)}"
f"&start_date={start}&end_date={end}"
)
r = requests.get(url)
d = json.loads(r.text)

# If the nwp_source is ICON, get visibility data from GFS as its not available for icon on Open Meteo
if nwp_source == "icon":
url = (
f"https://api.open-meteo.com/v1/gfs?"
f"latitude={site.latitude}&longitude={site.longitude}"
f"&hourly=visibility"
f"&start_date={start}&end_date={end}"
)
r_gfs = requests.get(url)
d_gfs = json.loads(r_gfs.text)

# extract visibility data from gfs reponse
gfs_visibility_data = d_gfs["hourly"]["visibility"]

# add visibility to the icon reponse to make a complete json file
d["hourly"]["visibility"] = gfs_visibility_data

# convert data into xarray
df = pd.DataFrame(d["hourly"])
df["time"] = pd.to_datetime(df["time"])
Expand All @@ -72,8 +99,10 @@ def get_gfs_nwp(site: PVSite, ts:datetime) -> xr.Dataset:
variable=df.columns,
),
)
data_xr = data_xr.to_dataset(name="gfs")
data_xr = data_xr.assign_coords({"x": [site.longitude], "y": [site.latitude], "time": [df.index[0]]})
data_xr = data_xr.to_dataset(name=nwp_source)
data_xr = data_xr.assign_coords(
{"x": [site.longitude], "y": [site.latitude], "time": [df.index[0]]}
)

return data_xr

Expand Down
14 changes: 7 additions & 7 deletions quartz_solar_forecast/forecast.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@


import os
import pandas as pd
from psp.data_sources.nwp import NwpDataSource
from psp.data_sources.pv import NetcdfPvDataSource
from psp.serialization import load_model
from psp.typings import X

from quartz_solar_forecast.data import get_gfs_nwp, make_pv_data
from quartz_solar_forecast.data import get_nwp, make_pv_data
from quartz_solar_forecast.pydantic_models import PVSite

from datetime import datetime

dir_path = os.path.dirname(os.path.realpath(__file__))


def run_forecast(site: PVSite, ts: datetime | str) -> pd.DataFrame:
def run_forecast(site: PVSite, ts: datetime | str, nwp_source: str = "icon") -> pd.DataFrame:
"""
Run the forecast from NWP data
:param site: the PV site
:param ts: the timestamp of the site
:param nwp_source: the nwp data source. Either "gfs" or "icon". Defaults to "icon"
:return: The PV forecast of the site for time (ts) for 48 hours
"""

if isinstance(ts, str):
ts = datetime.fromisoformat(ts)

# make pv and nwp data from GFS
nwp_xr = get_gfs_nwp(site=site, ts=ts)
nwp_xr = get_nwp(site=site, ts=ts)
pv_xr = make_pv_data(site=site, ts=ts)

# load model
Expand All @@ -42,8 +41,9 @@ def run_forecast(site: PVSite, ts: datetime | str) -> pd.DataFrame:
rename={"generation_wh": "power", "kwp": "capacity"},
ignore_pv_ids=[],
)
nwp = NwpDataSource(nwp_xr, value_name="gfs")
model.set_data_sources(pv_data_source=pv_data_source, nwp_data_sources={"GFS": nwp})
# make NwpDataSource
nwp = NwpDataSource(nwp_xr, value_name=nwp_source)
model.set_data_sources(pv_data_source=pv_data_source, nwp_data_sources={nwp_source: nwp})

# make prediction
x = X(pv_id="1", ts=ts)
Expand Down

0 comments on commit 22c7b6e

Please sign in to comment.