This repository was archived by the owner on Aug 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatacube.py
More file actions
282 lines (219 loc) · 8.42 KB
/
datacube.py
File metadata and controls
282 lines (219 loc) · 8.42 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""OpenEO Python UDF interface"""
import numpy
import xarray
from typing import Dict, List
__license__ = "Apache License, Version 2.0"
__author__ = "Soeren Gebbert"
__copyright__ = "Copyright 2018, Soeren Gebbert"
__maintainer__ = "Soeren Gebbert"
__email__ = "[email protected]"
class DataCube:
"""This class is a hypercube representation of multi-dimensional data
that stores an xarray and provides methods to convert the xarray into
the HyperCube JSON representation
>>> array = xarray.DataArray(numpy.zeros(shape=(2, 3)), coords={'x': [1, 2], 'y': [1, 2, 3]}, dims=('x', 'y'))
>>> array.attrs["description"] = "This is an xarray with two dimensions"
>>> array.name = "testdata"
>>> h = DataCube(array=array)
>>> d = h.to_dict()
>>> d["id"]
'testdata'
>>> d["data"]
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
>>> d["dimensions"]
[{'name': 'x', 'coordinates': [1, 2]}, {'name': 'y', 'coordinates': [1, 2, 3]}]
>>> d["description"]
'This is an xarray with two dimensions'
>>> new_h = DataCube.from_dict(d)
>>> d = new_h.to_dict()
>>> d["id"]
'testdata'
>>> d["data"]
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
>>> d["dimensions"]
[{'name': 'x', 'coordinates': [1, 2]}, {'name': 'y', 'coordinates': [1, 2, 3]}]
>>> d["description"]
'This is an xarray with two dimensions'
>>> array = xarray.DataArray(numpy.zeros(shape=(2, 3)), coords={'x': [1, 2], 'y': [1, 2, 3]}, dims=('x', 'y'))
>>> h = DataCube(array=array)
>>> d = h.to_dict()
>>> d["id"]
>>> d["data"]
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
>>> d["dimensions"]
[{'name': 'x', 'coordinates': [1, 2]}, {'name': 'y', 'coordinates': [1, 2, 3]}]
>>> "description" not in d
True
>>> new_h = DataCube.from_dict(d)
>>> d = new_h.to_dict()
>>> d["id"]
>>> d["data"]
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
>>> d["dimensions"]
[{'name': 'x', 'coordinates': [1, 2]}, {'name': 'y', 'coordinates': [1, 2, 3]}]
>>> "description" not in d
True
>>> array = xarray.DataArray(numpy.zeros(shape=(2, 3)))
>>> h = DataCube(array=array)
>>> d = h.to_dict()
>>> d["id"]
>>> d["data"]
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
>>> d["dimensions"]
[]
>>> "description" not in d
True
>>> new_h = DataCube.from_dict(d)
>>> d = new_h.to_dict()
>>> d["id"]
>>> d["data"]
[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
>>> d["dimensions"]
[]
>>> "description" not in d
True
"""
def __init__(self, array: xarray.DataArray):
self.set_array(array)
def __str__(self):
return "id: %(id)s\n" \
"data: %(data)s"%{"id":self.id, "data":self.array}
def get_array(self) -> xarray.DataArray:
"""Return the xarray.DataArray that contains the data and dimension definition
Returns:
xarray.DataArray: that contains the data and dimension definition
"""
return self._array
def set_array(self, array: xarray.DataArray):
"""Set the xarray.DataArray that contains the data and dimension definition
This function will check if the provided data is a geopandas.GeoDataFrame and raises
an Exception
Args:
array: xarray.DataArray that contains the data and dimension definition
"""
if isinstance(array, xarray.DataArray) is False:
raise Exception("Argument data must be of type xarray.DataArray")
self._array = array
@property
def id(self):
return self._array.name
array = property(fget=get_array, fset=set_array)
def to_dict(self) -> Dict:
"""Convert this hypercube into a dictionary that can be converted into
a valid JSON representation
Returns:
dict:
HyperCube as a dictionary
>>> example = {
... "id": "test_data",
... "data": [
... [
... [0.0, 0.1],
... [0.2, 0.3]
... ],
... [
... [0.0, 0.1],
... [0.2, 0.3]
... ]
... ],
... "dimension": [{"name": "time", "unit": "ISO:8601", "coordinates":["2001-01-01", "2001-01-02"]},
... {"name": "X", "unit": "degree", "coordinates":[50.0, 60.0]},
... {"name": "Y", "unit": "degree"},
... ]
... }
"""
d = {"id":"", "data": "", "dimensions":[]}
if self._array is not None:
xd = self._array.to_dict()
if "name" in xd:
d["id"] = xd["name"]
if "data" in xd:
d["data"] = xd["data"]
if "attrs" in xd:
if "description" in xd["attrs"]:
d["description"] = xd["attrs"]["description"]
if "dims" in xd and "coords" in xd:
for dim in xd["dims"]:
if dim in xd["coords"]:
if "data" in xd["coords"][dim]:
d["dimensions"].append({"name": dim, "coordinates": xd["coords"][dim]["data"]})
else:
d["dimensions"].append({"name": dim})
return d
@staticmethod
def from_dict(hc_dict: Dict) -> "DataCube":
"""Create a hypercube from a python dictionary that was created from
the JSON definition of the HyperCube
Args:
hc_dict (dict): The dictionary that contains the hypercube definition
Returns:
HyperCube
"""
if "id" not in hc_dict:
raise Exception("Missing id in dictionary")
if "data" not in hc_dict:
raise Exception("Missing data in dictionary")
coords = {}
dims = list()
if "dimensions" in hc_dict:
for dim in hc_dict["dimensions"]:
dims.append(dim["name"])
if "coordinates" in dim:
coords[dim["name"]] = dim["coordinates"]
if dims and coords:
data = xarray.DataArray(numpy.asarray(hc_dict["data"]), coords=coords, dims=dims)
elif dims:
data = xarray.DataArray(numpy.asarray(hc_dict["data"]), dims=dims)
else:
data = xarray.DataArray(numpy.asarray(hc_dict["data"]))
if "id" in hc_dict:
data.name = hc_dict["id"]
if "description" in hc_dict:
data.attrs["description"] = hc_dict["description"]
hc = DataCube(array=data)
return hc
def to_data_collection(self):
pass
@staticmethod
def from_data_collection(data_collection: 'openeo_udf.server.data_model.data_collection_schema.DataCollectionModel') -> List['DataCube']:
"""Create data cubes from a data collection
Args:
data_collection:
Returns:
A list of data cubes
"""
dc_list = []
data_cubes = data_collection.object_collections.data_cubes
variables_collections = data_collection.variables_collections
for cube in data_cubes:
variable_collection = variables_collections[cube.variable_collection]
# Read the one dimensional array and reshape it
coords = {}
for key in cube.dimensions.keys():
d = cube.dimensions[key]
if d.values:
coords[key] = d.values
else:
l = d.number_of_cells
if l != 0 and d.extent:
stepsize = (d.extent[1] - d.extent[0])/l
values = []
predecessor = d.extent[0]
for i in range(l):
value = predecessor + stepsize/2.0
values.append(value)
predecessor = predecessor + stepsize
coords[key] = values
for variable in variable_collection.variables:
array = numpy.asarray(variable.values)
array = array.reshape(variable_collection.size)
data = xarray.DataArray(array, dims=cube.dim, coords=coords)
data.name = variable.name
dc = DataCube(array=data)
dc_list.append(dc)
return dc_list
if __name__ == "__main__":
import doctest
doctest.testmod()