-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #147 from martindurant/intake2
- Loading branch information
Showing
22 changed files
with
172 additions
and
1,293 deletions.
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
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
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
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 |
---|---|---|
@@ -1,74 +1,22 @@ | ||
from . import __version__ | ||
from intake.source.base import DataSource, Schema | ||
class IntakeXarraySourceAdapter: | ||
container = "xarray" | ||
name = "xarray" | ||
version = "" | ||
|
||
def to_dask(self): | ||
if "chunks" not in self.reader.kwargs: | ||
return self.reader(chunks={}).read() | ||
else: | ||
return self.reader.read() | ||
|
||
class DataSourceMixin(DataSource): | ||
"""Common behaviours for plugins in this repo""" | ||
version = __version__ | ||
container = 'xarray' | ||
partition_access = True | ||
|
||
def _get_schema(self): | ||
"""Make schema object, which embeds xarray object and some details""" | ||
from .xarray_container import serialize_zarr_ds | ||
|
||
self.urlpath = self._get_cache(self.urlpath)[0] | ||
|
||
if self._ds is None: | ||
self._open_dataset() | ||
def __call__(self, *args, **kwargs): | ||
return self | ||
|
||
metadata = { | ||
'dims': dict(self._ds.dims), | ||
'data_vars': {k: list(self._ds[k].coords) | ||
for k in self._ds.data_vars.keys()}, | ||
'coords': tuple(self._ds.coords.keys()), | ||
} | ||
if getattr(self, 'on_server', False): | ||
metadata['internal'] = serialize_zarr_ds(self._ds) | ||
metadata.update(self._ds.attrs) | ||
self._schema = Schema( | ||
datashape=None, | ||
dtype=None, | ||
shape=None, | ||
npartitions=None, | ||
extra_metadata=metadata) | ||
return self._schema | ||
get = __call__ | ||
|
||
def read(self): | ||
"""Return a version of the xarray with all the data in memory""" | ||
self._load_metadata() | ||
return self._ds.load() | ||
return self.reader(chunks=None).read() | ||
|
||
def read_chunked(self): | ||
"""Return xarray object (which will have chunks)""" | ||
self._load_metadata() | ||
return self._ds | ||
|
||
def read_partition(self, i): | ||
"""Fetch one chunk of data at tuple index i | ||
""" | ||
import numpy as np | ||
self._load_metadata() | ||
if not isinstance(i, (tuple, list)): | ||
raise TypeError('For Xarray sources, must specify partition as ' | ||
'tuple') | ||
if isinstance(i, list): | ||
i = tuple(i) | ||
if hasattr(self._ds, 'variables') or i[0] in self._ds.coords: | ||
arr = self._ds[i[0]].data | ||
i = i[1:] | ||
else: | ||
arr = self._ds.data | ||
if isinstance(arr, np.ndarray): | ||
return arr | ||
# dask array | ||
return arr.blocks[i].compute() | ||
|
||
def to_dask(self): | ||
"""Return xarray object where variables are dask arrays""" | ||
return self.read_chunked() | ||
discover = read | ||
|
||
def close(self): | ||
"""Delete open file from memory""" | ||
self._ds = None | ||
self._schema = None | ||
read_chunked = to_dask |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.