diff --git a/lib/iris/cube.py b/lib/iris/cube.py index 939d2b308b4..a09590009d0 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -3166,8 +3166,13 @@ def collapsed(self, coords, aggregator, **kwargs): if (isinstance(aggregator, iris.analysis.WeightedAggregator) and not aggregator.uses_weighting(**kwargs)): - - if "cell_area" in [c.standard_name for c in self.cell_measures()]: + cell_area = [ + c for c in self.cell_measures() + if c.measure == 'area' + ] + assert len(cell_area) < 2, "more than one cell area measure" + cell_area = cell_area[0] if cell_area else None + if cell_area is not None: lon, lat = iris.analysis.cartography._get_lon_lat_coords(self) # would be ideal to resuse the below from @@ -3183,7 +3188,7 @@ def collapsed(self, coords, aggregator, **kwargs): lon_dim = self.coord_dims(lon) lon_dim = lon_dim[0] if lon_dim else None - ll_weights = self.cell_measure('cell_area').data + ll_weights = cell_area.data # Now we create an array of weights for each cell. This # process will handle adding the required extra dimensions and diff --git a/lib/iris/tests/unit/cube/test_Cube.py b/lib/iris/tests/unit/cube/test_Cube.py index 5633417c80c..7c1bad246e5 100644 --- a/lib/iris/tests/unit/cube/test_Cube.py +++ b/lib/iris/tests/unit/cube/test_Cube.py @@ -393,6 +393,25 @@ def test_lat_lon_weighted_aggregator_alt(self): coords = [coord for coord in coords if 'latitude' in coord] self._assert_warn_collapse_without_weight(coords, warn) + def test_lat_lon_cell_area_no_weights(self): + # Collapse grid_latitude coordinate with weighted aggregator without + # providing weights. Tests coordinate matching logic. + cell_areas = CellMeasure(data=np.arange(4).reshape(2, 2), + long_name='area', + measure='area') + + self.cube.add_cell_measure(cell_areas, [0, 1],) + self.cube.remove_coord('grid_latitude') + self.cube.remove_coord('grid_longitude') + + coords = ['latitude', 'longitude'] + + with mock.patch('warnings.warn') as warn: + self.cube.collapsed(coords, iris.analysis.MEAN) + + coords = [coord for coord in coords if 'latitude' in coord] + self._assert_nowarn_collapse_without_weight(coords, warn) + def test_no_lat_weighted_aggregator_mixed(self): # Collapse grid_latitude and an unmatched coordinate (not lat/lon) # with weighted aggregator without providing weights.