-
-
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
ENH: Support using opened netCDF4.Dataset (Fixes #1459) #1508
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -187,35 +187,56 @@ def _open_netcdf4_group(filename, mode, group=None, **kwargs): | |
with close_on_error(ds): | ||
ds = _nc4_group(ds, group, mode) | ||
|
||
_disable_mask_and_scale(ds) | ||
|
||
return ds | ||
|
||
|
||
def _disable_mask_and_scale(ds): | ||
for var in ds.variables.values(): | ||
# we handle masking and scaling ourselves | ||
var.set_auto_maskandscale(False) | ||
return ds | ||
|
||
|
||
class NetCDF4DataStore(WritableCFDataStore, DataStorePickleMixin): | ||
"""Store for reading and writing data via the Python-NetCDF4 library. | ||
|
||
This store supports NetCDF3, NetCDF4 and OpenDAP datasets. | ||
""" | ||
def __init__(self, filename, mode='r', format='NETCDF4', group=None, | ||
writer=None, clobber=True, diskless=False, persist=False, | ||
def __init__(self, netcdf4_dataset, mode='r', writer=None, opener=None, | ||
autoclose=False): | ||
|
||
if autoclose and opener is None: | ||
raise ValueError('autoclose requires an opener') | ||
|
||
_disable_mask_and_scale(netcdf4_dataset) | ||
|
||
self.ds = netcdf4_dataset | ||
self._autoclose = autoclose | ||
self._isopen = True | ||
self.format = self.ds.data_model | ||
self._filename = self.ds.filepath() | ||
self.is_remote = is_remote_uri(self._filename) | ||
self._mode = mode = 'a' if mode == 'w' else mode | ||
if opener: | ||
self._opener = functools.partial(opener, mode=self._mode) | ||
else: | ||
self._opener = opener | ||
super(NetCDF4DataStore, self).__init__(writer) | ||
|
||
@classmethod | ||
def open(cls, filename, mode='r', format='NETCDF4', group=None, | ||
writer=None, clobber=True, diskless=False, persist=False, | ||
autoclose=False): | ||
if format is None: | ||
format = 'NETCDF4' | ||
opener = functools.partial(_open_netcdf4_group, filename, mode=mode, | ||
group=group, clobber=clobber, | ||
diskless=diskless, persist=persist, | ||
format=format) | ||
self.ds = opener() | ||
self._autoclose = autoclose | ||
self._isopen = True | ||
self.format = format | ||
self.is_remote = is_remote_uri(filename) | ||
self._filename = filename | ||
self._mode = 'a' if mode == 'w' else mode | ||
self._opener = functools.partial(opener, mode=self._mode) | ||
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 logic on these two lines with 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. Done, although calling |
||
super(NetCDF4DataStore, self).__init__(writer) | ||
ds = opener() | ||
return cls(ds, mode=mode, writer=writer, opener=opener, | ||
autoclose=autoclose) | ||
|
||
def open_store_variable(self, name, var): | ||
with self.ensure_open(autoclose=False): | ||
|
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.
What do you think about moving the current constructor logic into a classmethod
NetCDF4DataStore.open(filename, mode, format, group, writer, clobber, diskless, persist, autoclose)
And adjusting the constructor:
NetCDF4DataStore.__init__(self, netcdf4_dataset, opener=None, writer=None, autoclose=None)
.Right now, I don't think anyone is using the
NetCDF4DataStore
constructor directly -- there's literally no good reason for that. This also gives us a pattern we could use for other constructors (e.g., pydap) where passing in an existing object is desirable.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.
I'm happy to refactor like that--I just didn't think it was on the table.