-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration test for recipe runner
- Loading branch information
1 parent
e49d0de
commit 37dd2e2
Showing
5 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,7 @@ dev = [ | |
"types-PyYAML", | ||
"pytest", | ||
"pytest-cov", | ||
"pytest-mock", | ||
"pre-commit", | ||
] | ||
docs = [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]) |