-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add support for remote string paths to h5netcdf
engine
#8424
Open
jrbourbeau
wants to merge
9
commits into
pydata:main
Choose a base branch
from
jrbourbeau:h5betcdf-storage-options
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
13866e0
Add support for remote string paths to h5netcdf engine
jrbourbeau 8090116
Fixup
jrbourbeau 35df213
Merge branch 'main' of https://github.com/pydata/xarray into h5betcdf…
jrbourbeau 124b14c
Test fixup
jrbourbeau 30fcdca
Merge branch 'main' of https://github.com/pydata/xarray into h5betcdf…
jrbourbeau 9e58c58
Merge branch 'main' into h5betcdf-storage-options
kmuehlbauer 4e248dc
init fsspec before init CachingFileManager
kmuehlbauer 38b9001
Update xarray/tests/test_backends.py
kmuehlbauer 977dab7
Merge branch 'main' into h5betcdf-storage-options
kmuehlbauer 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 |
---|---|---|
|
@@ -88,6 +88,20 @@ def _h5netcdf_create_group(dataset, name): | |
return dataset.create_group(name) | ||
|
||
|
||
def _h5netcdf_opener(filename, mode, storage_options=None, **kwargs): | ||
import h5netcdf | ||
|
||
if isinstance(filename, str) and is_remote_uri(filename): | ||
import fsspec | ||
|
||
mode_ = "rb" if mode == "r" else mode | ||
fs, _, _ = fsspec.get_fs_token_paths( | ||
filename, mode=mode_, storage_options=storage_options | ||
) | ||
filename = fs.open(filename, mode=mode_) | ||
return h5netcdf.File(filename, mode=mode, **kwargs) | ||
|
||
|
||
class H5NetCDFStore(WritableCFDataStore): | ||
"""Store for reading and writing data via h5netcdf""" | ||
|
||
|
@@ -140,9 +154,8 @@ def open( | |
invalid_netcdf=None, | ||
phony_dims=None, | ||
decode_vlen_strings=True, | ||
storage_options=None, | ||
): | ||
import h5netcdf | ||
|
||
if isinstance(filename, bytes): | ||
raise ValueError( | ||
"can't open netCDF4/HDF5 as bytes " | ||
|
@@ -161,6 +174,7 @@ def open( | |
kwargs = { | ||
"invalid_netcdf": invalid_netcdf, | ||
"decode_vlen_strings": decode_vlen_strings, | ||
"storage_options": storage_options, | ||
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. If we can obtain the # get open fsspec-handle first
if storage_options is not None:
filename = _find_absolute_paths(filename, engine="h5netcdf", backend_kwargs=dict(storage_options=storage_options))
# other code
manager = CachingFileManager(
h5netcdf.File, filename, mode=mode, kwargs=kwargs
)
|
||
} | ||
if phony_dims is not None: | ||
kwargs["phony_dims"] = phony_dims | ||
|
@@ -171,7 +185,9 @@ def open( | |
else: | ||
lock = combine_locks([HDF5_LOCK, get_write_lock(filename)]) | ||
|
||
manager = CachingFileManager(h5netcdf.File, filename, mode=mode, kwargs=kwargs) | ||
manager = CachingFileManager( | ||
_h5netcdf_opener, filename, mode=mode, kwargs=kwargs | ||
) | ||
return cls(manager, group=group, mode=mode, lock=lock, autoclose=autoclose) | ||
|
||
def _acquire(self, needs_lock=True): | ||
|
@@ -397,6 +413,7 @@ def open_dataset( # type: ignore[override] # allow LSP violation, not supporti | |
invalid_netcdf=None, | ||
phony_dims=None, | ||
decode_vlen_strings=True, | ||
storage_options=None, | ||
) -> Dataset: | ||
filename_or_obj = _normalize_path(filename_or_obj) | ||
store = H5NetCDFStore.open( | ||
|
@@ -407,6 +424,7 @@ def open_dataset( # type: ignore[override] # allow LSP violation, not supporti | |
invalid_netcdf=invalid_netcdf, | ||
phony_dims=phony_dims, | ||
decode_vlen_strings=decode_vlen_strings, | ||
storage_options=storage_options, | ||
) | ||
|
||
store_entrypoint = StoreBackendEntrypoint() | ||
|
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
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.
Not an expert in this bit but there's a
_find_absolute_path
inbackends/common.py
that shares a lot of code with the first three lines here. It includes a nice error message iffsspec
is not installed.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.
Do we really want to unconditionally open remote urls with
fsspec
? This contradicts the usage of native implementations (via "driver"-kwarg, see #8360) in h5py/hdf5.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.
My guess is yes by default. That's what other pydata libraries like pandas, Dask, Zarr, etc. have converged on for file handling, so it would be familiar to many users. That said, if
driver=
offers some benefits overfsspec
(again, I'm not familiar with the newdriver
functionality) it'd be easy for these two approaches to live alongside each other:fsspec
by default is nodriver
is specifieddriver
is specified use that insteaddriver=
andstorage_options=
) then raise an informative error.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.
The algorithm shown here looks good to me. I also think fsspec is more used by users although keeping the ros3 alternative is desirable too (see this).
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.
These changes looks indeed very similar to
_find_absolute_paths
, can we instead get that one working for this case as well?xarray/xarray/backends/common.py
Lines 60 to 115 in 49bd63a