Skip to content

Commit

Permalink
add tests for converter
Browse files Browse the repository at this point in the history
  • Loading branch information
Yang committed Jul 6, 2023
1 parent 970bcb8 commit b887efc
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/zampy/datasets/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ def check_convention(convention: Union[str, Path]) -> None:
def convert(
data: xr.Dataset, dataset: Dataset, convention: Union[str, Path]
) -> xr.Dataset:
"""Convert a loaded dataset to the specified convention."""
"""Convert a loaded dataset to the specified convention.
Args:
data: Input xarray data.
dataset: Zampy dataset instance.
convention: Input data exchange convention.
Return:
Input xarray with converted variables following given convention.
"""
converted = False
if isinstance(convention, str):
convention_file = Path(CONVENTIONS[convention]).open(mode="r", encoding="UTF8")
Expand Down
49 changes: 49 additions & 0 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""Unit test for regridding."""

from pathlib import Path
import numpy as np
import pytest
from zampy.datasets import EthCanopyHeight
from zampy.datasets import converter
from zampy.datasets.eth_canopy_height import parse_tiff_file


path_dummy_data = Path(__file__).resolve().parent / "test_data" / "eth-canopy-height"

# ruff: noqa: B018 (protected-access)


def test_check_convention_not_support():
convention = "fake_convention"
with pytest.raises(ValueError, match="not supported"):
converter.check_convention(convention)


def test_check_convention_not_exist():
convention = Path("fake_path")
with pytest.raises(FileNotFoundError, match="could not be found"):
converter.check_convention(convention)


def test_convert():
ds = parse_tiff_file(
path_dummy_data / "ETH_GlobalCanopyHeight_10m_2020_N51E003_Map.tif",
)
ds_convert = converter.convert(
data=ds, dataset=EthCanopyHeight(), convention="ALMA"
)

assert list(ds_convert.data_vars)[0] == "Hveg"


def test_convert_var():
ds = parse_tiff_file(
path_dummy_data / "ETH_GlobalCanopyHeight_10m_2020_N51E003_Map.tif",
)
ds_convert = converter._convert_var(ds, "height_of_vegetation", "decimeter")

assert np.allclose(
ds_convert["height_of_vegetation"].values,
ds["height_of_vegetation"].values * 10.0,
equal_nan=True,
)
16 changes: 14 additions & 2 deletions tests/test_datasets/test_eth_canopy_height.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,17 @@ def ingest_dummy_data(self, temp_dir):
return ds, canopy_height_dataset

def test_ingest(self):
"""Test ingest function."""
with TemporaryDirectory() as temp_dir:
ds, _ = self.ingest_dummy_data(temp_dir)

assert type(ds) == xr.Dataset

def test_load(self):
"""Test load function."""
with TemporaryDirectory() as temp_dir:
_, canopy_height_dataset = self.ingest_dummy_data(temp_dir)
ds, canopy_height_dataset = self.ingest_dummy_data(temp_dir)
ds.close() # make windows happy, otherwise gives PermissionError on CI/CD

times = TimeBounds(np.datetime64("2020-01-01"), np.datetime64("2020-12-31"))
bbox = SpatialBounds(54, 6, 51, 3)
Expand All @@ -87,11 +90,20 @@ def test_load(self):

# we assert the regridded coordinates
expected_lat = [51.0, 52.0, 53.0, 54.0]
expected_lon = [3.0, 4.0, 5.0, 6.0]

np.testing.assert_allclose(ds.latitude.values, expected_lat)
np.testing.assert_allclose(ds.longitude.values, expected_lon)

def test_convert(self):
pass
"""Test convert function."""
with TemporaryDirectory() as temp_dir:
_, canopy_height_dataset = self.ingest_dummy_data(temp_dir)
canopy_height_dataset.convert(
ingest_dir=Path(temp_dir),
convention="ALMA"
)
# TODO: finish this test when the function is complete.


def test_get_filenames():
Expand Down

0 comments on commit b887efc

Please sign in to comment.