-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#75 Add function to open a DEMS file
- Loading branch information
1 parent
07ee2f8
commit 4255acd
Showing
3 changed files
with
61 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |