From c994b5b734e05202198d5abff265a5242acff3fc Mon Sep 17 00:00:00 2001 From: Zebedee Nicholls Date: Mon, 17 Sep 2018 12:27:44 +0200 Subject: [PATCH] Add a test --- lib/iris/cube.py | 11 ++++++++--- lib/iris/tests/unit/cube/test_Cube.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/iris/cube.py b/lib/iris/cube.py index bd9475b221..562e6ef693 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -3210,8 +3210,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 @@ -3227,7 +3232,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 ae0a1abe3b..f78f4493cb 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.