Skip to content

Commit

Permalink
#75 Add function to open a DEMS file
Browse files Browse the repository at this point in the history
  • Loading branch information
astropenguin committed Oct 21, 2023
1 parent 07ee2f8 commit 4255acd
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 40 additions & 0 deletions decode/io.py
Original file line number Diff line number Diff line change
@@ -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.")
18 changes: 18 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 4255acd

Please sign in to comment.