Skip to content

Commit

Permalink
rename from_zarr to from_ds
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerkuou committed Oct 11, 2024
1 parent 702a783 commit 6bfd60a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
4 changes: 2 additions & 2 deletions sarxarray/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from sarxarray import stack
from sarxarray._io import from_binary, from_zarr
from sarxarray._io import from_binary, from_ds
from sarxarray.utils import complex_coherence, multi_look

__all__ = ("stack", "from_binary", "from_zarr", "multi_look", "complex_coherence")
__all__ = ("stack", "from_binary", "from_ds", "multi_look", "complex_coherence")
29 changes: 14 additions & 15 deletions sarxarray/_io.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import math
from pathlib import Path

import dask
import dask.array as da
Expand All @@ -14,37 +13,37 @@
# Example: https://docs.dask.org/en/stable/array-creation.html#memory-mapping


def from_zarr(files_zarr: str | Path) -> xr.Dataset:
"""Read a SLC stack or related variables from zarr files.
def from_ds(ds: xr.Dataset) -> xr.Dataset:
"""Create a SLC stack or from an Xarray Dataset.
Note that this function only works for the SLC stack zarr files.
This function create tasks graph converting the two data variables of complex data:
`real` and `imag`, to three variables: `complex`, `amplitude`, and `phase`.
The purpose of this function is to create task graph converting `real`/`imag` to
`complex`, `amplitude`, and `phase` variables.
The function is intented for an SLC stack in `xr.Dataset` loaded from a Zarr file.
For other datasets, such as lat, lon, etc., please use `xr.open_zarr` directly.
Parameters
----------
files_zarr : str | Path
Paths to the zarr files.
ds : xr.Dataset
SLC stack loaded from a Zarr file.
Must have three dimensions: `(azimuth, range, time)`.
Must have two variables: `real` and `imag`.
Returns
-------
xr.Dataset
Loaded SLC stack.
An xarray.Dataset with three dimensions: (azimuth, range, time), and
three variables: ("complex", "amplitude", "phase").
Converted SLC stack.
An xarray.Dataset with three dimensions: `(azimuth, range, time)`, and
three variables: `complex`, `amplitude`, `phase`.
Raises
------
ValueError
The input dataset should have three dimensions: (azimuth, range, time).
The input dataset should have three dimensions: `(azimuth, range, time)`.
ValueError
The input dataset should have the following variables: ('real', 'imag').
The input dataset should have the following variables: `('real', 'imag')`.
"""
ds = xr.open_zarr(files_zarr)

# Check ds should have the following dimensions: (azimuth, range, time)
if any(dim not in ds.dims for dim in ["azimuth", "range", "time"]):
raise ValueError(
Expand Down
28 changes: 16 additions & 12 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import numpy as np
import pytest
import xarray as xr

import sarxarray
from sarxarray._io import _calc_chunksize, _unpack_complex
Expand All @@ -17,30 +18,33 @@ def test_slcs():
]


class TestFromZarr:
"""from_zarr in _io.py"""
class TestFromDS:
"""from_ds in _io.py"""

def test_from_zarr_normal(self):
slcs = sarxarray.from_zarr(
def test_from_ds_normal(self):
test_ds = xr.open_zarr(
f"{os.path.dirname(__file__)}/data/zarrs/slcs_example.zarr"
)
slcs = sarxarray.from_ds(test_ds)
assert all(dim in slcs.dims for dim in ["azimuth", "range", "time"])
assert all(var not in slcs.variables.keys() for var in ["real", "imag"])
assert all(
var in slcs.variables.keys() for var in ["complex", "amplitude", "phase"]
)

def test_from_zarr_broken_dim(self):
def test_from_ds_broken_dim(self):
test_ds_broken_dim = xr.open_zarr(
f"{os.path.dirname(__file__)}/data/zarrs/slcs_example_broken_dim.zarr"
)
with pytest.raises(ValueError):
sarxarray.from_zarr(
f"{os.path.dirname(__file__)}/data/zarrs/slcs_example_broken_dim.zarr"
)
sarxarray.from_ds(test_ds_broken_dim)

def test_from_zarr_broken_vars(self):
def test_from_ds_broken_vars(self):
test_ds_broken_vars = xr.open_zarr(
f"{os.path.dirname(__file__)}/data/zarrs/slcs_example_broken_vars.zarr"
)
with pytest.raises(ValueError):
sarxarray.from_zarr(
f"{os.path.dirname(__file__)}/data/zarrs/slcs_example_broken_vars.zarr"
)
sarxarray.from_ds(test_ds_broken_vars)


class TestFromBinary:
Expand Down

0 comments on commit 6bfd60a

Please sign in to comment.