Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/titiler/xarray/titiler/xarray/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def xarray_open_dataset( # noqa: C901
except ImportError: # pragma: nocover
zarr = None # type: ignore

try:
import icechunk
except ImportError: # pragma: nocover
icechunk = None # type: ignore


parsed = urlparse(src_path)
protocol = parsed.scheme or "file"

Expand Down Expand Up @@ -77,12 +83,31 @@ def xarray_open_dataset( # noqa: C901

# Fallback to Zarr
else:
if module_available("zarr", minversion="3.0"):
store = zarr.storage.FsspecStore.from_url(
src_path, storage_options={"asynchronous": True}
)
else:
store = fsspec.filesystem(protocol).get_mapper(src_path)
# try icechunk first
try:
assert icechunk is not None, "'icechunk' must be installed to read icechunk dataset"
if protocol == "file":
storage = icechunk.local_filesystem_storage(src_path)
elif protocol == "s3":
storage = icechunk.s3_storage(
bucket=parsed.netloc,
prefix=parsed.path.lstrip('/'), # remove leading slash, this is an annoying mismatch between icechunk and urlparse
from_env=True,
)
else:
raise ValueError(f"Unsupported storage protocol {protocol} for icechunk in titiler.xarray")
repo = icechunk.Repository.open(storage=storage)
session = repo.readonly_session('main')
store = session.store
xr_open_args['consolidated'] = False
except icechunk.IcechunkError:
# try fsspec and zarr python
if module_available("zarr", minversion="3.0"):
store = zarr.storage.FsspecStore.from_url(
src_path, storage_options={"asynchronous": True}
)
else:
store = fsspec.filesystem(protocol).get_mapper(src_path)

ds = xarray.open_zarr(store, **xr_open_args)
return ds
Expand Down