-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Zarr-Python 3 #1082
Merged
Merged
Support Zarr-Python 3 #1082
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ba2b1b5
Support Zarr-Python 3
maxrjones a37907a
Remove test-zarr-v2 deps
maxrjones 8e353f4
Update src/titiler/xarray/titiler/xarray/io.py
maxrjones e081d0f
Update io.py
maxrjones b29267b
Merge branch 'main' of https://github.com/developmentseed/titiler int…
vincentsarago 4961ef2
update changelog
vincentsarago File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
from morecantile import TileMatrixSet | ||
from rio_tiler.constants import WEB_MERCATOR_TMS | ||
from rio_tiler.io.xarray import XarrayReader | ||
from xarray.namedarray.utils import module_available | ||
|
||
|
||
def xarray_open_dataset( # noqa: C901 | ||
|
@@ -29,21 +30,6 @@ def xarray_open_dataset( # noqa: C901 | |
""" | ||
import fsspec # noqa | ||
|
||
try: | ||
import gcsfs | ||
except ImportError: # pragma: nocover | ||
gcsfs = None # type: ignore | ||
|
||
try: | ||
import s3fs | ||
except ImportError: # pragma: nocover | ||
s3fs = None # type: ignore | ||
|
||
try: | ||
import aiohttp | ||
except ImportError: # pragma: nocover | ||
aiohttp = None # type: ignore | ||
|
||
try: | ||
import h5netcdf | ||
except ImportError: # pragma: nocover | ||
|
@@ -66,55 +52,8 @@ def xarray_open_dataset( # noqa: C901 | |
|
||
else: | ||
assert zarr is not None, "'zarr' must be installed to read Zarr dataset" | ||
|
||
xr_engine = "zarr" | ||
|
||
if protocol in ["", "file"]: | ||
filesystem = fsspec.filesystem(protocol) # type: ignore | ||
file_handler = ( | ||
filesystem.open(src_path) | ||
if xr_engine == "h5netcdf" | ||
else filesystem.get_mapper(src_path) | ||
) | ||
|
||
elif protocol == "s3": | ||
assert ( | ||
s3fs is not None | ||
), "'aiohttp' must be installed to read dataset stored online" | ||
|
||
s3_filesystem = s3fs.S3FileSystem() | ||
file_handler = ( | ||
s3_filesystem.open(src_path) | ||
if xr_engine == "h5netcdf" | ||
else s3fs.S3Map(root=src_path, s3=s3_filesystem) | ||
) | ||
|
||
elif protocol == "gs": | ||
assert ( | ||
gcsfs is not None | ||
), "'gcsfs' must be installed to read dataset stored in Google Cloud Storage" | ||
|
||
gcs_filesystem = gcsfs.GCSFileSystem() | ||
file_handler = ( | ||
gcs_filesystem.open(src_path) | ||
if xr_engine == "h5netcdf" | ||
else gcs_filesystem.get_mapper(root=src_path) | ||
) | ||
|
||
elif protocol in ["http", "https"]: | ||
assert ( | ||
aiohttp is not None | ||
), "'aiohttp' must be installed to read dataset stored online" | ||
|
||
filesystem = fsspec.filesystem(protocol) # type: ignore | ||
file_handler = ( | ||
filesystem.open(src_path) | ||
if xr_engine == "h5netcdf" | ||
else filesystem.get_mapper(src_path) | ||
) | ||
|
||
else: | ||
raise ValueError(f"Unsupported protocol: {protocol}, for {src_path}") | ||
maxrjones marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_zarr_v3 = module_available("zarr", minversion="3.0") | ||
maxrjones marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Arguments for xarray.open_dataset | ||
# Default args | ||
|
@@ -135,13 +74,21 @@ def xarray_open_dataset( # noqa: C901 | |
"lock": False, | ||
} | ||
) | ||
|
||
ds = xarray.open_dataset(file_handler, **xr_open_args) | ||
fs = fsspec.filesystem(protocol) | ||
ds = xarray.open_dataset(fs.open(src_path), **xr_open_args) | ||
|
||
# Fallback to Zarr | ||
else: | ||
ds = xarray.open_zarr(file_handler, **xr_open_args) | ||
|
||
if _zarr_v3: | ||
if protocol == "file": | ||
store = zarr.storage.LocalStore(parsed.path, read_only=True) | ||
else: | ||
fs = fsspec.filesystem(protocol, storage_options={"asynchronous": True}) | ||
store = zarr.storage.FsspecStore(fs, path=src_path, read_only=True) | ||
ds = xarray.open_zarr(store, **xr_open_args) | ||
else: | ||
fs = fsspec.filesystem(protocol) | ||
ds = xarray.open_zarr(fs.get_mapper(src_path), **xr_open_args) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the bulk of the changes to account for Zarr no longer accepting fsspec's mutable mapping wrappers and expecting asynchronous file systems.
maxrjones marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return ds | ||
|
||
|
||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will this work for zarr<3 ? Should that be specified?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I accounted for both the new and old syntax in
titiler/src/titiler/xarray/titiler/xarray/io.py
Lines 81 to 89 in e081d0f