Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
✨ Add support for predictions using MOGREPS zarr data (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwhite committed Jan 19, 2021
1 parent fbbefa0 commit f1812ff
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 18 deletions.
Binary file not shown.
12 changes: 6 additions & 6 deletions metoffice_ec2/nwp_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ def plot_xarray_data_array(
dataset: xr.Dataset, da: xr.DataArray, file: IO[Any]
) -> None:
"""Create an image plot for a data array"""
central_latitude = dataset.lambert_azimuthal_equal_area.latitude_of_projection_origin[
0
]
central_longitude = dataset.lambert_azimuthal_equal_area.longitude_of_projection_origin[
0
]
central_latitude = (
dataset.lambert_azimuthal_equal_area.latitude_of_projection_origin[0]
)
central_longitude = (
dataset.lambert_azimuthal_equal_area.longitude_of_projection_origin[0]
)
mogreps_crs = ccrs.LambertAzimuthalEqualArea(
central_latitude=central_latitude, central_longitude=central_longitude
)
Expand Down
6 changes: 6 additions & 0 deletions metoffice_ec2/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ def load_model(path: str) -> pd.DataFrame:

def load_irradiance_data(path: str) -> xr.Dataset:
"""Load the NWP irradiance data"""
if ".zarr" in path:
return xr.open_zarr(path)
return xr.open_dataset(path, engine="netcdf4")


Expand All @@ -29,6 +31,10 @@ def predict(irradiance_dataset: xr.Dataset, model_df: pd.DataFrame) -> pd.DataFr
coords=[model_df["system_id"].values],
dims="system_id",
)
# MOGREPS has multiple realizations (UKV doesn't)
if "realization" in irradiance_dataset:
# just get the first realization
irradiance_dataset = irradiance_dataset.isel(realization=0)
nwp_interp = irradiance_dataset.interp(
projection_x_coordinate=easting, projection_y_coordinate=northing
)
Expand Down
8 changes: 7 additions & 1 deletion metoffice_ec2/tests/e2e_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,13 @@ def test_handles_some_message_types(
"var_name,sns_message_filename,netcdf_path,netcdf_name", not_wanted_messages
)
def test_handles_unwanted_messages(
queue, s3, caplog, var_name, sns_message_filename, netcdf_path, netcdf_name,
queue,
s3,
caplog,
var_name,
sns_message_filename,
netcdf_path,
netcdf_name,
):
# MOCKING
sns_message = load_sns_message_from_file(
Expand Down
15 changes: 4 additions & 11 deletions metoffice_ec2/tests/nwp_plot_test.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
import imghdr
from pathlib import Path

from fsspec.implementations.local import LocalFileSystem
import pytest
from zarr.storage import ZipStore

import xarray as xr
from metoffice_ec2.nwp_plot import (
extract_wind_from_direction,
filename_for_plot,
find_zarr,
plot_xarray_data_array,
)


@pytest.mark.skip(reason="Coastline data download often times out")
def test_local_plot_pipeline(tmp_path):
height = 100.0
fs = LocalFileSystem()

# Find the first Zarr input file in the "data/mogreps" directory
listing = find_zarr(fs, "data/mogreps", suffix=".zarr.zip")
assert len(listing) == 1
input_file = listing[0]
assert input_file.endswith(
"MOGREPS-UK__wind_from_direction__2020-03-15T15__2020-03-16T07.zarr.zip"
)

input_file = "data/mogreps/MOGREPS-UK__wind_from_direction__2020-03-15T15__2020-03-16T07.zarr.zip"

# Create a suitable filename for the output plot png file
output_file = filename_for_plot(
Expand Down
16 changes: 16 additions & 0 deletions metoffice_ec2/tests/predict_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,19 @@ def test_predict():
# import geojson
# with open("predictions_2020-06-04T17.geojson", 'w') as f:
# geojson.dump(feature_collection, f, indent=4)


def test_predict_zarr():
irradiance_dataset = load_irradiance_data(
"data/mogreps/MOGREPS-UK__surface_downwelling_shortwave_flux_in_air__2020-09-08T12__2020-09-08T13.zarr.zip"
)
model_df = load_model("model/predict_pv_yield_nwp.csv")
predictions = predict(irradiance_dataset, model_df)
assert predictions.at[0, "pv_yield_predicted"] > 0

feature_collection = predict_as_geojson(irradiance_dataset, model_df)
feature0 = feature_collection["features"][0]
assert feature0["geometry"]["coordinates"] == [-1.299834, 54.3686]
assert feature0["properties"]["system_id"] == 973
assert feature0["properties"]["time"] == "2020-09-08T13:00:00"
assert feature0["properties"]["pv_yield_predicted"] > 0

0 comments on commit f1812ff

Please sign in to comment.