diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index bc3d9a8..874ff08 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -22,10 +22,12 @@ jobs: python: ["3.9", "3.10", "3.11", "3.12"] steps: - uses: actions/checkout@v3 + with: + submodules: true - uses: actions/setup-python@v4 with: python-version: ${{ matrix.python }} - run: pip install poetry && poetry install - run: black --check docs tests decode - - run: pytest -v test + - run: pytest -v tests - run: docs/build diff --git a/decode/io.py b/decode/io.py index e69de29..28d7283 100644 --- a/decode/io.py +++ b/decode/io.py @@ -0,0 +1,40 @@ +# standard library +from pathlib import Path +from typing import Any, Union + + +# dependencies +import xarray as xr + + +# constants +NETCDF_SUFFIX = ".nc" +NETCDF_ENGINE = "scipy" +ZARR_SUFFIX = ".zarr" +ZARR_ENGINE = "zarr" + + +# type hints +PathLike = Union[Path, str] + + +def open_dems(dems: PathLike, **kwargs: Any) -> xr.DataArray: + """Open a DEMS file as a DataArray. + + Args: + dems: Path of the DEMS file. + kwargs: Arguments to be passed to ``xarray.open_dataarray``. + + Return: + A DataArray of the opened DEMS file. + + """ + suffixes = Path(dems).suffixes + + if NETCDF_SUFFIX in suffixes: + return xr.open_dataarray(dems, engine=NETCDF_ENGINE, **kwargs) + + if ZARR_SUFFIX in suffixes: + return xr.open_dataarray(dems, engine=ZARR_ENGINE, **kwargs) + + raise ValueError(f"Extension of {dems} is not supported.") diff --git a/tests/test_io.py b/tests/test_io.py index e69de29..3468e9c 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -0,0 +1,18 @@ +# standard library +from pathlib import Path + + +# dependencies +from decode import io +from pytest import mark + + +# constants +DEMS_DIR = Path(__file__).parents[1] / "data" / "dems" +DEMS_ALL = map(Path, DEMS_DIR.glob("*.nc.gz")) + + +# test functions +@mark.parametrize("dems", DEMS_ALL) +def test_open_dems(dems: Path) -> None: + io.open_dems(dems)