Skip to content

Commit

Permalink
Add integration test for recipe runner
Browse files Browse the repository at this point in the history
  • Loading branch information
BSchilperoort committed Aug 8, 2023
1 parent e49d0de commit 37dd2e2
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 1 deletion.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ dev = [
"types-PyYAML",
"pytest",
"pytest-cov",
"pytest-mock",
"pre-commit",
]
docs = [
Expand Down
2 changes: 1 addition & 1 deletion src/zampy/utils/regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def _groupby_regrid(
ds_out = ds_out.swap_dims(
{"latitude_bins": "latitude", "longitude_bins": "longitude"}
)
ds_out = ds_out.drop(["latitude_bins", "longitude_bins"])
ds_out = ds_out.drop_vars(["latitude_bins", "longitude_bins"])
return ds_out.transpose("time", "latitude", "longitude", ...)


Expand Down
72 changes: 72 additions & 0 deletions tests/test_recipes/generate_test_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""Generates test data for running the recipe tests."""
from pathlib import Path
import numpy as np
import pandas as pd
import xarray as xr
from zampy.datasets.dataset_protocol import SpatialBounds
from zampy.datasets.dataset_protocol import TimeBounds


def generate_era5_file(
varname: str,
time_bounds: TimeBounds,
spatial_bounds: SpatialBounds,
test_value: float,
resolution: float,
time_res="1H",
) -> xr.Dataset:
time_coords = pd.date_range(
start=time_bounds.start, end=time_bounds.end, freq=time_res, inclusive="left"
)
lat_coords = np.arange(
start=np.round(spatial_bounds.south - 1),
stop=np.round(spatial_bounds.north + 1),
step=resolution,
)
lon_coords = np.arange(
start=np.round(spatial_bounds.west - 1),
stop=np.round(spatial_bounds.east + 1),
step=resolution,
)
data = np.zeros((len(lon_coords), len(lat_coords), len(time_coords))) + test_value

ds = xr.Dataset(
data_vars={ERA5_LOOKUP[varname][1]: (("longitude", "latitude", "time"), data)},
coords={
"longitude": lon_coords,
"latitude": lat_coords,
"time": time_coords,
},
)
ds[ERA5_LOOKUP[varname][1]].attrs["units"] = ERA5_LOOKUP[varname][0]
ds["latitude"].attrs["units"] = "degrees_north"
ds["longitude"].attrs["units"] = "degrees_east"

return ds


ERA5_LOOKUP = { # name: (unit, fname)
"10m_u_component_of_wind": ("m s**-1", "u10"),
"10m_v_component_of_wind": ("m s**-1", "v10"),
"surface_pressure": ("Pa", "sp"),
}


def generate_era5_files(
directory: Path,
variables: list[str],
spatial_bounds: SpatialBounds,
time_bounds: TimeBounds,
) -> None:
data_dir_era5 = directory / "era5"
data_dir_era5.mkdir()

for var in variables:
ds = generate_era5_file(
varname=var,
time_bounds=time_bounds,
spatial_bounds=spatial_bounds,
test_value=1.0,
resolution=0.25,
)
ds.to_netcdf(path=data_dir_era5 / f"era5_{var}.nc")
16 changes: 16 additions & 0 deletions tests/test_recipes/recipes/era5_recipe.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: "era5_recipe"

download:
years: [2020, 2020]
bbox: [51, 4, 50, 3] # NESW

datasets:
era5:
variables:
- 10m_v_component_of_wind
- surface_pressure

convert:
convention: ALMA
frequency: 1H # outputs at 1 hour frequency. Pandas-like freq-keyword.
resolution: 0.5 # output resolution in degrees.
44 changes: 44 additions & 0 deletions tests/test_recipes/test_simple_recipe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Testing a simple recipe."""
from pathlib import Path
from unittest.mock import patch
import generate_test_data
import numpy as np
import xarray as xr
from zampy.datasets import DATASETS
from zampy.datasets.dataset_protocol import SpatialBounds
from zampy.datasets.dataset_protocol import TimeBounds
from zampy.datasets.dataset_protocol import write_properties_file
from zampy.recipe import RecipeManager


RECIPE_FILE = Path(__file__).parent / "recipes" / "era5_recipe.yml"


def test_recipe(tmp_path: Path, mocker):
with (patch.object(DATASETS["era5"], "download"),):
mocker.patch(
"zampy.recipe.config_loader",
return_value={"working_directory": str(tmp_path.absolute())},
)
rm = RecipeManager(str(RECIPE_FILE.absolute()))

spatial_bounds = SpatialBounds(51, 4, 50, 3)
time_bounds = TimeBounds(
np.datetime64("2020-01-01T00:00"), np.datetime64("2020-12-31T23:59")
)
variables = ["10m_v_component_of_wind", "surface_pressure"]

generate_test_data.generate_era5_files(
directory=tmp_path / "download",
variables=variables,
spatial_bounds=spatial_bounds,
time_bounds=time_bounds,
)
write_properties_file(
tmp_path / "download" / "era5", spatial_bounds, time_bounds, variables
)

rm.run()

ds = xr.open_mfdataset(str(tmp_path / "output" / "era5_recipe" / "*.nc"))
assert all(var in ds.data_vars for var in ["Psurf", "Wind_N"])

0 comments on commit 37dd2e2

Please sign in to comment.