-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathderived.py
More file actions
64 lines (53 loc) · 1.8 KB
/
derived.py
File metadata and controls
64 lines (53 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from intake import Schema
from intake.source.derived import GenericTransform
class XArrayTransform(GenericTransform):
"""Transform where the input and output are both xarray objects.
You must supply ``transform`` and any ``transform_kwargs``.
"""
input_container = "xarray"
container = "xarray"
optional_params = {}
_ds = None
def to_dask(self):
if self._ds is None:
self._pick()
self._ds = self._transform(
self._source.to_dask(), **self._params["transform_kwargs"]
)
return self._ds
def _get_schema(self):
"""load metadata only if needed"""
self.to_dask()
return Schema(
datashape=None,
dtype=None,
shape=None,
npartitions=None,
extra_metadata=self._ds.extra_metadata,
)
def read(self):
return self.to_dask().compute()
class Sel(XArrayTransform):
"""Simple array transform to subsample an xarray object using
the sel method.
Note that you could use XArrayTransform directly, by writing a
function to choose the subsample instead of a method as here.
"""
input_container = "xarray"
container = "xarray"
required_params = ["indexers"]
def __init__(self, indexers, **kwargs):
"""
indexers: dict (stord as str) which is passed to xarray.Dataset.sel
"""
# this class wants required "indexers", but XArrayTransform
# uses "transform_kwargs", which we don't need since we use a method for the
# transform
kwargs.update(
transform=self.sel,
indexers=indexers,
transform_kwargs={},
)
super().__init__(**kwargs)
def sel(self, ds):
return ds.sel(eval(self._params["indexers"]))