Skip to content

Commit

Permalink
Add a test
Browse files Browse the repository at this point in the history
  • Loading branch information
znicholls committed Oct 22, 2019
1 parent aedcfdb commit c994b5b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions lib/iris/tests/unit/cube/test_Cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c994b5b

Please sign in to comment.