Skip to content

Commit c8fa1b4

Browse files
committed
Initial WIP for dataless merges -- cannot yet merge datafull+dataless.
1 parent 7536010 commit c8fa1b4

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

lib/iris/_merge.py

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ class _CubeSignature(
320320
"data_type",
321321
"cell_measures_and_dims",
322322
"ancillary_variables_and_dims",
323+
"is_dataless",
323324
],
324325
)
325326
):
@@ -430,6 +431,9 @@ def match(self, other, error_on_mismatch):
430431
if self.data_shape != other.data_shape:
431432
msg = "cube.shape differs: {} != {}"
432433
msgs.append(msg.format(self.data_shape, other.data_shape))
434+
if self.is_dataless != other.is_dataless:
435+
msg = "cube.is_dataless differs: {} != {}"
436+
msgs.append(msg.format(self.is_dataless, other.is_dataless))
433437
if self.data_type != other.data_type:
434438
msg = "cube data dtype differs: {} != {}"
435439
msgs.append(msg.format(self.data_type, other.data_type))
@@ -1109,8 +1113,9 @@ def __init__(self, cube):
11091113
source-cube.
11101114
11111115
"""
1112-
if cube.is_dataless():
1113-
raise iris.exceptions.DatalessError("merge")
1116+
# if cube.is_dataless():
1117+
# raise iris.exceptions.DatalessError("merge")
1118+
11141119
# Default hint ordering for candidate dimension coordinates.
11151120
self._hints = [
11161121
"time",
@@ -1234,33 +1239,42 @@ def merge(self, unique=True):
12341239

12351240
# Generate group-depth merged cubes from the source-cubes.
12361241
for level in range(group_depth):
1237-
# Stack up all the data from all of the relevant source
1238-
# cubes in a single dask "stacked" array.
1239-
# If it turns out that all the source cubes already had
1240-
# their data loaded then at the end we convert the stack back
1241-
# into a plain numpy array.
1242-
stack = np.empty(self._stack_shape, "object")
1243-
all_have_data = True
1244-
for nd_index in nd_indexes:
1245-
# Get the data of the current existing or last known
1246-
# good source-cube
1247-
group = group_by_nd_index[nd_index]
1248-
offset = min(level, len(group) - 1)
1249-
data = self._skeletons[group[offset]].data
1250-
# Ensure the data is represented as a dask array and
1251-
# slot that array into the stack.
1252-
if is_lazy_data(data):
1253-
all_have_data = False
1254-
else:
1255-
data = as_lazy_data(data)
1256-
stack[nd_index] = data
1242+
if self._cube_signature.is_dataless:
1243+
merged_shape = self._cube_signature.data_shape
1244+
# ?WRONG? merged_shape = self._stack_shape
1245+
# ?WRONG? merged_shape = (len(nd_indexes),) + shape
1246+
merged_data = None
1247+
all_have_data = False
1248+
else:
1249+
# Stack up all the data from all of the relevant source
1250+
# cubes in a single dask "stacked" array.
1251+
# If it turns out that all the source cubes already had
1252+
# their data loaded then at the end we convert the stack back
1253+
# into a plain numpy array.
1254+
stack = np.empty(self._stack_shape, "object")
1255+
all_have_data = True
1256+
for nd_index in nd_indexes:
1257+
# Get the data of the current existing or last known
1258+
# good source-cube
1259+
group = group_by_nd_index[nd_index]
1260+
offset = min(level, len(group) - 1)
1261+
data = self._skeletons[group[offset]].data
1262+
# Ensure the data is represented as a dask array and
1263+
# slot that array into the stack.
1264+
if is_lazy_data(data):
1265+
all_have_data = False
1266+
else:
1267+
data = as_lazy_data(data)
1268+
stack[nd_index] = data
1269+
1270+
merged_data = multidim_lazy_stack(stack)
1271+
merged_shape = None
12571272

1258-
merged_data = multidim_lazy_stack(stack)
12591273
if all_have_data:
12601274
# All inputs were concrete, so turn the result back into a
12611275
# normal array.
12621276
merged_data = as_concrete_data(merged_data)
1263-
merged_cube = self._get_cube(merged_data)
1277+
merged_cube = self._get_cube(merged_data, shape=merged_shape)
12641278
merged_cubes.append(merged_cube)
12651279

12661280
return merged_cubes
@@ -1545,7 +1559,7 @@ def name_in_independents():
15451559
# deferred loading, this does NOT change the shape.
15461560
self._shape.extend(signature.data_shape)
15471561

1548-
def _get_cube(self, data):
1562+
def _get_cube(self, data, shape=None):
15491563
"""Generate fully constructed cube.
15501564
15511565
Return a fully constructed cube for the given data, containing
@@ -1573,6 +1587,7 @@ def _get_cube(self, data):
15731587
aux_coords_and_dims=aux_coords_and_dims,
15741588
cell_measures_and_dims=cms_and_dims,
15751589
ancillary_variables_and_dims=avs_and_dims,
1590+
shape=shape,
15761591
**kwargs,
15771592
)
15781593

@@ -1711,6 +1726,7 @@ def _build_signature(self, cube):
17111726
cube.dtype,
17121727
cube._cell_measures_and_dims,
17131728
cube._ancillary_variables_and_dims,
1729+
cube.is_dataless(),
17141730
)
17151731

17161732
def _add_cube(self, cube, coord_payload):

0 commit comments

Comments
 (0)