Skip to content

Commit b862cd7

Browse files
committed
Fix multidim string coords
1 parent 3cab9bc commit b862cd7

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

lib/iris/analysis/__init__.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,14 +2017,32 @@ def _compute_shared_coords(self):
20172017
if coord.bounds is None:
20182018
new_points = []
20192019
new_bounds = None
2020+
# np.apply_along_axis does not work with str.join, so we
2021+
# need to loop through the array directly. First move axis
2022+
# of interest to trailing dim and flatten the others.
2023+
work_arr = np.moveaxis(coord.points, dim, -1)
2024+
shape = work_arr.shape
2025+
if coord.ndim == 1:
2026+
work_shape = (1, ) + work_arr.shape
2027+
new_shape = (len(self),)
2028+
else:
2029+
work_shape = (np.product(work_arr.shape[:-1]),
2030+
work_arr.shape[-1])
2031+
new_shape = (len(self),) + shape[:-1]
2032+
work_arr = work_arr.reshape(work_shape)
2033+
20202034
for key_slice in six.itervalues(self._slices_by_key):
20212035
if isinstance(key_slice, slice):
20222036
indices = key_slice.indices(
20232037
coord.points.shape[dim])
20242038
key_slice = range(*indices)
2025-
new_pts = np.apply_along_axis(
2026-
'|'.join, dim, coord.points.take(key_slice, dim))
2027-
new_points.append(new_pts)
2039+
2040+
for arr in work_arr:
2041+
new_points.append('|'.join(arr.take(key_slice)))
2042+
2043+
# Reinstate flattened dimensions. Aggregated dim now leads.
2044+
new_points = np.array(new_points).reshape(new_shape)
2045+
20282046
# Move aggregated dimension back to position it started in.
20292047
new_points = np.moveaxis(new_points, 0, dim)
20302048
else:

0 commit comments

Comments
 (0)