Skip to content

Commit

Permalink
Merge pull request #152 from openclimatefix/fix-ecmwf
Browse files Browse the repository at this point in the history
fix to use ecmwf model only
  • Loading branch information
peterdudfield authored Nov 5, 2024
2 parents cbf0dd3 + 2ef45be commit 71f4320
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 17 deletions.
4 changes: 2 additions & 2 deletions pvnet_app/model_configs/all_models.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ models:
- name: pvnet_ecmwf # this name is important as it used for blending
pvnet:
repo: openclimatefix/pvnet_uk_region
version: 35d55181a82440bdd087f380d650bfd0b64bd322
version: c14f7427d9854d63430aa936ce45f55d3818d033
summation:
repo: openclimatefix/pvnet_v2_summation
version: 9002baf1e9dc1ec141f3c4a1fa8447b6316a4558
version: 4fe6b1441b6dd549292c201ed85eee156ecc220c
ecmwf_only: True
uses_satellite_data: False
uses_ocf_data_sampler: False
Expand Down
95 changes: 80 additions & 15 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
from pvnet_app.model_configs.pydantic_models import get_all_models



def test_app(
db_session, nwp_ukv_data, nwp_ecmwf_data, sat_5_data, gsp_yields_and_systems, me_latest
):

"""Test the app running the intraday models"""

with tempfile.TemporaryDirectory() as tmpdirname:

os.chdir(tmpdirname)
Expand Down Expand Up @@ -93,7 +92,7 @@ def test_app_day_ahead_model(
"""Test the app running the day ahead model"""

with tempfile.TemporaryDirectory() as tmpdirname:

os.chdir(tmpdirname)

temp_nwp_path = "temp_nwp_ukv.zarr"
Expand Down Expand Up @@ -152,13 +151,14 @@ def test_app_day_ahead_model(
== expected_forecast_results * expected_forecast_timesteps
)


def test_app_no_sat(
db_session, nwp_ukv_data, nwp_ecmwf_data, sat_5_data, gsp_yields_and_systems, me_latest
):
"""Test the app for the case when no satellite data is available"""

with tempfile.TemporaryDirectory() as tmpdirname:

os.chdir(tmpdirname)

temp_nwp_path = "temp_nwp_ukv.zarr"
Expand All @@ -168,8 +168,8 @@ def test_app_no_sat(
temp_nwp_path = "temp_nwp_ecmwf.zarr"
os.environ["NWP_ECMWF_ZARR_PATH"] = temp_nwp_path
nwp_ecmwf_data.to_zarr(temp_nwp_path)
# There is no satellite data available at the environ path

# There is no satellite data available at the environ path
os.environ["SATELLITE_ZARR_PATH"] = "nonexistent_sat.zarr.zip"

os.environ["RUN_EXTRA_MODELS"] = "True"
Expand All @@ -182,7 +182,7 @@ def test_app_no_sat(
from pvnet_app.app import app

app(gsp_ids=list(range(1, 318)), num_workers=2)

# Only the models which don't use satellite will be run in this case
# The models below are the only ones which should have been run
all_models = get_all_models(run_extra_models=True)
Expand Down Expand Up @@ -218,11 +218,75 @@ def test_app_no_sat(
assert len(db_session.query(ForecastValueSevenDaysSQL).all()) == expected_forecast_results * 16


# test legacy models
# Its nice to have this here, so we can run the latest version in production, but still use the old models
# Once we have re trained PVnet summation models we can remove this
def test_app_ecwmf_only(db_session, nwp_ecmwf_data, gsp_yields_and_systems, me_latest):
"""Test the app for the case running model just on ecmwf"""

with tempfile.TemporaryDirectory() as tmpdirname:

os.chdir(tmpdirname)

temp_nwp_path = "temp_nwp_ecmwf.zarr"
os.environ["NWP_ECMWF_ZARR_PATH"] = temp_nwp_path
nwp_ecmwf_data.to_zarr(temp_nwp_path)

# There is no satellite or ukv data available at the environ path
os.environ["SATELLITE_ZARR_PATH"] = "nonexistent_sat.zarr.zip"
os.environ["NWP_UKV_ZARR_PATH"] = "nonexistent_nwp.zarr.zip"

os.environ["RUN_EXTRA_MODELS"] = "False"
os.environ["SAVE_GSP_SUM"] = "True"
os.environ["DAY_AHEAD_MODEL"] = "False"
os.environ["USE_OCF_DATA_SAMPLER"] = "False"
os.environ["USE_ECMWF_ONLY"] = "True"

# Run prediction
# Thes import needs to come after the environ vars have been set
from pvnet_app.app import app

app(gsp_ids=list(range(1, 318)), num_workers=2)

# Only the models which don't use satellite will be run in this case
# The models below are the only ones which should have been run
all_models = get_all_models(get_ecmwf_only=True)

# Check correct number of forecasts have been made
# (317 GSPs + 1 National + maybe GSP-sum) = 318 or 319 forecasts
# Forecast made with multiple models
expected_forecast_results = 0
for model_config in all_models:
expected_forecast_results += 318 + model_config.save_gsp_sum

forecasts = db_session.query(ForecastSQL).all()
# Doubled for historic and forecast
assert len(forecasts) == expected_forecast_results * 2

# Check probabilistic added
assert "90" in forecasts[0].forecast_values[0].properties
assert "10" in forecasts[0].forecast_values[0].properties

# 318 GSPs * 16 time steps in forecast
assert len(db_session.query(ForecastValueSQL).all()) == expected_forecast_results * 16
assert len(db_session.query(ForecastValueLatestSQL).all()) == expected_forecast_results * 16

expected_forecast_results = 0
for model_config in all_models:
# National
expected_forecast_results += 1
# GSP
expected_forecast_results += 317 * model_config.save_gsp_to_recent
expected_forecast_results += model_config.save_gsp_sum # optional Sum of GSPs

assert len(db_session.query(ForecastValueSevenDaysSQL).all()) == expected_forecast_results * 16


# test legacy models
# Its nice to have this here, so we can run the latest version in production, but still use the old models
# Once we have re trained PVnet summation models we can remove this
def test_app_ocf_datapipes(
db_session, nwp_ukv_data, nwp_ecmwf_data, sat_5_data, gsp_yields_and_systems, me_latest
db_session, nwp_ukv_data, nwp_ecmwf_data, sat_5_data, gsp_yields_and_systems, me_latest
):
"""Test the app running the day ahead model"""

Expand All @@ -245,6 +309,7 @@ def test_app_ocf_datapipes(
os.environ["DAY_AHEAD_MODEL"] = "False"
os.environ["RUN_EXTRA_MODELS"] = "False"
os.environ["USE_OCF_DATA_SAMPLER"] = "False"
os.environ["USE_ECMWF_ONLY"] = "False"

# Run prediction
# Thes import needs to come after the environ vars have been set
Expand Down Expand Up @@ -273,14 +338,14 @@ def test_app_ocf_datapipes(
expected_forecast_timesteps = 16

assert (
len(db_session.query(ForecastValueSQL).all())
== expected_forecast_results * expected_forecast_timesteps
len(db_session.query(ForecastValueSQL).all())
== expected_forecast_results * expected_forecast_timesteps
)
assert (
len(db_session.query(ForecastValueLatestSQL).all())
== expected_forecast_results * expected_forecast_timesteps
len(db_session.query(ForecastValueLatestSQL).all())
== expected_forecast_results * expected_forecast_timesteps
)
assert (
len(db_session.query(ForecastValueSevenDaysSQL).all())
== expected_forecast_results * expected_forecast_timesteps
)
len(db_session.query(ForecastValueSevenDaysSQL).all())
== expected_forecast_results * expected_forecast_timesteps
)

0 comments on commit 71f4320

Please sign in to comment.