diff --git a/src/zampy/datasets/converter.py b/src/zampy/datasets/converter.py index 5fdf4b1..c57a6d0 100644 --- a/src/zampy/datasets/converter.py +++ b/src/zampy/datasets/converter.py @@ -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") diff --git a/tests/test_converter.py b/tests/test_converter.py new file mode 100644 index 0000000..64b662a --- /dev/null +++ b/tests/test_converter.py @@ -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, + ) diff --git a/tests/test_datasets/test_eth_canopy_height.py b/tests/test_datasets/test_eth_canopy_height.py index 7ecf464..29b6bb7 100644 --- a/tests/test_datasets/test_eth_canopy_height.py +++ b/tests/test_datasets/test_eth_canopy_height.py @@ -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) @@ -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():