Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
peterdudfield committed Nov 4, 2024
1 parent 3f99dda commit 76bbd72
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pvnet_app/data/satellite.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ def check_for_zeros():
if (data == 0).sum() / n_data_points_per_timestep > ERROR_ZERO_PERCENTAGE:
time = ds_sat.time[i].values
message = (
f"Satellite data contains zeros, (greater than {ERROR_ZERO_PERCENTAGE})"
f"Satellite data contains zeros (greater than {ERROR_ZERO_PERCENTAGE}), "
f"This is for time step {time}"
)
raise Exception(message)
Expand Down
10 changes: 9 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,16 @@ def config_filename():
return f"{os.path.dirname(os.path.abspath(__file__))}/test_data/test.yaml"


def make_sat_data(test_t0, delay_mins, freq_mins):
def make_sat_data(test_t0, delay_mins, freq_mins, small=False):
# Load dataset which only contains coordinates, but no data
ds = xr.open_zarr(
f"{os.path.dirname(os.path.abspath(__file__))}/test_data/non_hrv_shell.zarr"
)

if small:
# only select 10 by 10
ds = ds.isel(x_geostationary=slice(0, 10), y_geostationary=slice(0, 10))

# remove tim dim and expand time dim to be len 36 = 3 hours of 5 minute data
ds = ds.drop_vars("time")
n_hours = 3
Expand Down Expand Up @@ -185,6 +189,10 @@ def sat_5_data_delayed(test_t0):
def sat_15_data(test_t0):
return make_sat_data(test_t0, delay_mins=0, freq_mins=15)

@pytest.fixture()
def sat_15_data_small(test_t0):
return make_sat_data(test_t0, delay_mins=0, freq_mins=15,small=True)


@pytest.fixture()
def gsp_yields_and_systems(db_session, test_t0):
Expand Down
25 changes: 25 additions & 0 deletions tests/data/test_satellite.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import os
import tempfile

import pytest
import zarr
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -237,3 +239,26 @@ def test_extend_satellite_data_with_nans_over_3_hours(sat_5_data, test_t0):
ds = xr.open_zarr(filename)
assert len(time) + 3*12 == len(ds.time)
assert ds.time.values[-1] == t0


def test_zeros_in_sat_data(sat_15_data_small, test_t0):
"""Download and process only the 15 minute satellite data"""

# make temporary directory
with tempfile.TemporaryDirectory() as tmpdirname:

# Change to temporary working directory
os.chdir(tmpdirname)

# make half the values zeros
sat_15_data_small.data[::2] = 0

# Make 15-minutely satellite data available
save_to_zarr_zip(sat_15_data_small, filename="latest.zarr.zip")

os.environ["SATELLITE_ZARR_PATH"] = "latest.zarr.zip"
download_all_sat_data()

# check an error is made
with pytest.raises(Exception):
preprocess_sat_data(test_t0)

0 comments on commit 76bbd72

Please sign in to comment.