Skip to content

Commit

Permalink
fix temp dir for windows by using mktemp
Browse files Browse the repository at this point in the history
  • Loading branch information
Yang committed Jul 24, 2023
1 parent 81a5f01 commit aaa0c48
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 72 deletions.
4 changes: 2 additions & 2 deletions src/zampy/datasets/dataset_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ def __post_init__(self) -> None:
"""Validate the initialized SpatialBounds class."""
if self.south > self.north:
raise ValueError(
"Value of southern bound is greater than norther bound."
"Value of southern bound is greater than northern bound."
"\nPlease check the spatial bounds input."
)
if self.west > self.east:
raise ValueError(
"Value of western bound is greater than east bound."
"Value of western bound is greater than eastern bound."
"\nPlease check the spatial bounds input."
)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_dataset_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def test_invalid_spatial_bounds_north_south():


def test_invalid_spatial_bounds_east_west():
with pytest.raises(ValueError, match="greater than east bound"):
with pytest.raises(ValueError, match="greater than eastern bound"):
SpatialBounds(54, 6, 51, 20)


Expand Down
139 changes: 70 additions & 69 deletions tests/test_datasets/test_eth_canopy_height.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,54 @@

import json
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch
import numpy as np
import pytest
import xarray as xr
from zampy.datasets import eth_canopy_height
from zampy.datasets.dataset_protocol import SpatialBounds
from zampy.datasets.dataset_protocol import TimeBounds
from . import data_folder


@pytest.fixture(scope="function")
def dummy_dir(tmp_path_factory):
"""Create a dummpy directory for testing."""
return tmp_path_factory.mktemp("data")


class TestEthCanopyHeight:
"""Test the EthCanopyHeight class."""

@patch("urllib.request.urlretrieve")
def test_download(self, mock_urlretrieve):
def test_download(self, mock_urlretrieve, dummy_dir):
"""Test download functionality.
Here we mock the downloading and save property file to a fake path.
"""
with TemporaryDirectory() as temp_dir:
times = TimeBounds(np.datetime64("2020-01-01"), np.datetime64("2020-12-31"))
bbox = SpatialBounds(54, 6, 51, 3)
variable = ["height_of_vegetation"]
download_dir = Path(temp_dir, "download")

canopy_height_dataset = eth_canopy_height.EthCanopyHeight()
canopy_height_dataset.download(
download_dir=download_dir,
time_bounds=times,
spatial_bounds=bbox,
variable_names=variable,
)
times = TimeBounds(np.datetime64("2020-01-01"), np.datetime64("2020-12-31"))
bbox = SpatialBounds(54, 6, 51, 3)
variable = ["height_of_vegetation"]
download_dir = Path(dummy_dir, "download")

canopy_height_dataset = eth_canopy_height.EthCanopyHeight()
canopy_height_dataset.download(
download_dir=download_dir,
time_bounds=times,
spatial_bounds=bbox,
variable_names=variable,
)

# make sure that the download is called
assert mock_urlretrieve.called
# make sure that the download is called
assert mock_urlretrieve.called

# check property file
with (download_dir / "eth-canopy-height" / "properties.json").open(
mode="r", encoding="utf-8"
) as file:
json_dict = json.load(file)
# check property
assert json_dict["variable_names"] == variable
# check property file
with (download_dir / "eth-canopy-height" / "properties.json").open(
mode="r", encoding="utf-8"
) as file:
json_dict = json.load(file)
# check property
assert json_dict["variable_names"] == variable

def ingest_dummy_data(self, temp_dir):
"""Ingest dummy tif data to nc for other tests."""
Expand All @@ -62,44 +67,41 @@ def ingest_dummy_data(self, temp_dir):

return ds, canopy_height_dataset

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

assert type(ds) == xr.Dataset
assert type(ds) == xr.Dataset

def test_load(self):
def test_load(self, dummy_dir):
"""Test load function."""
with TemporaryDirectory() as temp_dir:
_, canopy_height_dataset = self.ingest_dummy_data(temp_dir)

times = TimeBounds(np.datetime64("2020-01-01"), np.datetime64("2020-12-31"))
bbox = SpatialBounds(54, 6, 51, 3)
variable = ["height_of_vegetation"]

ds = canopy_height_dataset.load(
ingest_dir=Path(temp_dir),
time_bounds=times,
spatial_bounds=bbox,
variable_names=variable,
resolution=1.0,
regrid_method="flox",
)
_, canopy_height_dataset = self.ingest_dummy_data(dummy_dir)

times = TimeBounds(np.datetime64("2020-01-01"), np.datetime64("2020-12-31"))
bbox = SpatialBounds(54, 6, 51, 3)
variable = ["height_of_vegetation"]

ds = canopy_height_dataset.load(
ingest_dir=Path(dummy_dir),
time_bounds=times,
spatial_bounds=bbox,
variable_names=variable,
resolution=1.0,
regrid_method="flox",
)

# 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]
# 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)
np.testing.assert_allclose(ds.latitude.values, expected_lat)
np.testing.assert_allclose(ds.longitude.values, expected_lon)

def test_convert(self):
def test_convert(self, dummy_dir):
"""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.
_, canopy_height_dataset = self.ingest_dummy_data(dummy_dir)
canopy_height_dataset.convert(ingest_dir=Path(dummy_dir), convention="ALMA")
# TODO: finish this test when the function is complete.


def test_get_filenames():
Expand Down Expand Up @@ -155,20 +157,19 @@ def test_parse_tiff_file():
assert type(dummy_ds) == xr.Dataset


def test_convert_tiff_to_netcdf():
def test_convert_tiff_to_netcdf(dummy_dir):
"""Test tiff to netcdf conversion function."""
with TemporaryDirectory() as temp_dir:
dummy_data = Path(
data_folder,
"eth-canopy-height",
"ETH_GlobalCanopyHeight_10m_2020_N51E003_Map.tif",
)
eth_canopy_height.convert_tiff_to_netcdf(
ingest_folder=Path(temp_dir),
file=dummy_data,
)
dummy_data = Path(
data_folder,
"eth-canopy-height",
"ETH_GlobalCanopyHeight_10m_2020_N51E003_Map.tif",
)
eth_canopy_height.convert_tiff_to_netcdf(
ingest_folder=Path(dummy_dir),
file=dummy_data,
)

ds = xr.load_dataset(
Path(temp_dir, "ETH_GlobalCanopyHeight_10m_2020_N51E003_Map.nc")
)
assert type(ds) == xr.Dataset
ds = xr.load_dataset(
Path(dummy_dir, "ETH_GlobalCanopyHeight_10m_2020_N51E003_Map.nc")
)
assert type(ds) == xr.Dataset

0 comments on commit aaa0c48

Please sign in to comment.