Skip to content

Commit f952caa

Browse files
committed
review changes
1 parent b862cd7 commit f952caa

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

lib/iris/analysis/__init__.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,8 +1841,7 @@ def __init__(self, groupby_coords, shared_coords=None):
18411841
18421842
Kwargs:
18431843
1844-
* shared_coords (list of tuples containing :class:`iris.coords.Coord`
1845-
instances with `int`):
1844+
* shared_coords (list of (:class:`iris.coords.Coord`, `int`) pairs):
18461845
One or more coordinates (including multidimensional coordinates)
18471846
that share the same group-by coordinate axis. The `int` identifies
18481847
which dimension of the coord is on the group-by coordinate axis.
@@ -2022,13 +2021,10 @@ def _compute_shared_coords(self):
20222021
# of interest to trailing dim and flatten the others.
20232022
work_arr = np.moveaxis(coord.points, dim, -1)
20242023
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]
2024+
work_shape = (-1, shape[-1])
2025+
new_shape = (len(self),)
2026+
if coord.ndim > 1:
2027+
new_shape += shape[:-1]
20322028
work_arr = work_arr.reshape(work_shape)
20332029

20342030
for key_slice in six.itervalues(self._slices_by_key):

lib/iris/cube.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3387,13 +3387,16 @@ def aggregated_by(self, coords, aggregator, **kwargs):
33873387
lambda coord_: coord_ not in groupby_coords,
33883388
self.coords(contains_dimension=dimension_to_groupby)))
33893389

3390-
match_dims = [
3391-
index for coord_ in shared_coords for (index, dim) in
3392-
enumerate(self.coord_dims(coord_)) if dim == dimension_to_groupby]
3390+
# Determine which of each shared coord's dimensions will be aggregated.
3391+
shared_coords_and_dims = [
3392+
(coord_, index)
3393+
for coord_ in shared_coords
3394+
for (index, dim) in enumerate(self.coord_dims(coord_))
3395+
if dim == dimension_to_groupby]
33933396

33943397
# Create the aggregation group-by instance.
33953398
groupby = iris.analysis._Groupby(groupby_coords,
3396-
zip(shared_coords, match_dims))
3399+
shared_coords_and_dims)
33973400

33983401
# Create the resulting aggregate-by cube and remove the original
33993402
# coordinates that are going to be groupedby.

lib/iris/tests/unit/cube/test_Cube.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,9 @@ def test_agg_by_label(self):
503503
long_name='label', units='no_unit')
504504
self.assertEqual(res_cube.coord('val'), val_coord)
505505
self.assertEqual(res_cube.coord('label'), label_coord)
506+
507+
def test_2d_agg_by_label(self):
508+
res_cube = self.cube.aggregated_by('label', self.mock_agg)
506509
# For 2d coord, slices of aggregated coord should be the same as
507510
# aggregated slices.
508511
for res_slice, cube_slice in zip(res_cube.slices('val'),
@@ -524,6 +527,9 @@ def test_agg_by_val(self):
524527
long_name='label', units='no_unit')
525528
self.assertEqual(res_cube.coord('val'), val_coord)
526529
self.assertEqual(res_cube.coord('label'), label_coord)
530+
531+
def test_2d_agg_by_val(self):
532+
res_cube = self.cube.aggregated_by('val', self.mock_agg)
527533
# For 2d coord, slices of aggregated coord should be the same as
528534
# aggregated slices.
529535
for res_slice, cube_slice in zip(res_cube.slices('val'),

0 commit comments

Comments
 (0)