-
-
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
Enable subclassing the netCDF4 backend, changing the dataset class #8338
Closed
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -327,13 +327,11 @@ class NetCDF4DataStore(WritableCFDataStore): | |||||||||||
def __init__( | ||||||||||||
self, manager, group=None, mode=None, lock=NETCDF4_PYTHON_LOCK, autoclose=False | ||||||||||||
): | ||||||||||||
import netCDF4 | ||||||||||||
|
||||||||||||
if isinstance(manager, netCDF4.Dataset): | ||||||||||||
if isinstance(manager, self.DatasetClass()): | ||||||||||||
if group is None: | ||||||||||||
root, group = find_root_and_group(manager) | ||||||||||||
else: | ||||||||||||
if type(manager) is not netCDF4.Dataset: | ||||||||||||
if type(manager) is not self.DatasetClass(): | ||||||||||||
raise ValueError( | ||||||||||||
"must supply a root netCDF4.Dataset if the group " | ||||||||||||
"argument is provided" | ||||||||||||
|
@@ -350,6 +348,13 @@ def __init__( | |||||||||||
self.lock = ensure_lock(lock) | ||||||||||||
self.autoclose = autoclose | ||||||||||||
|
||||||||||||
@classmethod | ||||||||||||
def DatasetClass(self) -> type: | ||||||||||||
# This is to allow subclassing the datastore and retain lazy-importing | ||||||||||||
import netCDF4 | ||||||||||||
|
||||||||||||
return netCDF4.Dataset | ||||||||||||
|
||||||||||||
@classmethod | ||||||||||||
def open( | ||||||||||||
cls, | ||||||||||||
|
@@ -364,8 +369,6 @@ def open( | |||||||||||
lock_maker=None, | ||||||||||||
autoclose=False, | ||||||||||||
): | ||||||||||||
import netCDF4 | ||||||||||||
|
||||||||||||
if isinstance(filename, os.PathLike): | ||||||||||||
filename = os.fspath(filename) | ||||||||||||
|
||||||||||||
|
@@ -395,7 +398,7 @@ def open( | |||||||||||
clobber=clobber, diskless=diskless, persist=persist, format=format | ||||||||||||
) | ||||||||||||
manager = CachingFileManager( | ||||||||||||
netCDF4.Dataset, filename, mode=mode, kwargs=kwargs | ||||||||||||
cls.DatasetClass(), filename, mode=mode, kwargs=kwargs | ||||||||||||
) | ||||||||||||
return cls(manager, group=group, mode=mode, lock=lock, autoclose=autoclose) | ||||||||||||
|
||||||||||||
|
@@ -561,6 +564,8 @@ class NetCDF4BackendEntrypoint(BackendEntrypoint): | |||||||||||
"Open netCDF (.nc, .nc4 and .cdf) and most HDF5 files using netCDF4 in Xarray" | ||||||||||||
) | ||||||||||||
url = "https://docs.xarray.dev/en/stable/generated/xarray.backends.NetCDF4BackendEntrypoint.html" | ||||||||||||
DataStore = NetCDF4DataStore | ||||||||||||
# This is to allow subclassing this backend entrypoint | ||||||||||||
Comment on lines
+567
to
+568
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.
Suggested change
This is a nit but we usually comment above the code |
||||||||||||
|
||||||||||||
def guess_can_open( | ||||||||||||
self, | ||||||||||||
|
@@ -600,7 +605,7 @@ def open_dataset( # type: ignore[override] # allow LSP violation, not supporti | |||||||||||
autoclose=False, | ||||||||||||
) -> Dataset: | ||||||||||||
filename_or_obj = _normalize_path(filename_or_obj) | ||||||||||||
store = NetCDF4DataStore.open( | ||||||||||||
store = self.DataStore.open( | ||||||||||||
filename_or_obj, | ||||||||||||
mode=mode, | ||||||||||||
format=format, | ||||||||||||
|
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.
@classmethod
(because it does not return a new instance of theNetCDF4DataStore
class, also would take acls
as first argument). If then I think it would be a@staticmethod
.xr.Dataset
- maybeopener
(as in https://github.com/PlasmaFAIR/xarray/blob/18af8e63f98c9aa7c54ad6afe13cb583e92bacc9/xarray/backends/file_manager.py#L54) oropener_cls
orbackend_opener
or so.