Skip to content

Commit

Permalink
Include ancillary variables and cell_measures during intersect (#5804)
Browse files Browse the repository at this point in the history
* added rough fix, needs tests and refactoring

* consolidated

* added tests

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* review comments

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
ESadek-MO and pre-commit-ci[bot] authored Aug 23, 2024
1 parent b8f554f commit 773c55b
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 12 deletions.
49 changes: 37 additions & 12 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ def from_cubes(cubes, constraints=None):
constraints = iris._constraints.list_of_constraints(constraints)
pairs = [_CubeFilter(constraint) for constraint in constraints]
collection = _CubeFilterCollection(pairs)
for cube in cubes:
collection.add_cube(cube)
for c in cubes:
collection.add_cube(c)
return collection

def __init__(self, pairs):
Expand Down Expand Up @@ -132,8 +132,8 @@ def __init__(self, *args, **kwargs):
# Do whatever a list does, to initialise ourself "as a list"
super().__init__(*args, **kwargs)
# Check that all items in the list are cubes.
for cube in self:
self._assert_is_cube(cube)
for c in self:
self._assert_is_cube(c)

def __str__(self):
"""Run short :meth:`Cube.summary` on every cube."""
Expand Down Expand Up @@ -308,9 +308,9 @@ def _extract_and_merge(cubes, constraints, strict=False, return_single_cube=Fals
constraint_groups = dict(
[(constraint, CubeList()) for constraint in constraints]
)
for cube in cubes:
for c in cubes:
for constraint, cube_list in constraint_groups.items():
sub_cube = constraint.extract(cube)
sub_cube = constraint.extract(c)
if sub_cube is not None:
cube_list.append(sub_cube)

Expand Down Expand Up @@ -394,8 +394,8 @@ def merge_cube(self):

# Register each of our cubes with a single ProtoCube.
proto_cube = iris._merge.ProtoCube(self[0])
for cube in self[1:]:
proto_cube.register(cube, error_on_mismatch=True)
for c in self[1:]:
proto_cube.register(c, error_on_mismatch=True)

# Extract the merged cube from the ProtoCube.
(merged_cube,) = proto_cube.merge()
Expand Down Expand Up @@ -471,18 +471,18 @@ def merge(self, unique=True):
"""
# Register each of our cubes with its appropriate ProtoCube.
proto_cubes_by_name = {}
for cube in self:
name = cube.standard_name
for c in self:
name = c.standard_name
proto_cubes = proto_cubes_by_name.setdefault(name, [])
proto_cube = None

for target_proto_cube in proto_cubes:
if target_proto_cube.register(cube):
if target_proto_cube.register(c):
proto_cube = target_proto_cube
break

if proto_cube is None:
proto_cube = iris._merge.ProtoCube(cube)
proto_cube = iris._merge.ProtoCube(c)
proto_cubes.append(proto_cube)

# Emulate Python 2 behaviour.
Expand Down Expand Up @@ -3175,8 +3175,33 @@ def create_coords(src_coords, add_coord):
add_coord(result_coord, dims)
coord_mapping[id(src_coord)] = result_coord

def create_metadata(src_metadatas, add_metadata, get_metadata):
for src_metadata in src_metadatas:
dims = src_metadata.cube_dims(self)
if dim in dims:
dim_within_coord = dims.index(dim)
data = np.concatenate(
[
get_metadata(chunk, src_metadata.name()).core_data()
for chunk in chunks
],
dim_within_coord,
)
result_coord = src_metadata.copy(values=data)
else:
result_coord = src_metadata.copy()
add_metadata(result_coord, dims)

create_coords(self.dim_coords, result.add_dim_coord)
create_coords(self.aux_coords, result.add_aux_coord)
create_metadata(
self.cell_measures(), result.add_cell_measure, Cube.cell_measure
)
create_metadata(
self.ancillary_variables(),
result.add_ancillary_variable,
Cube.ancillary_variable,
)
for factory in self.aux_factories:
result.add_aux_factory(factory.updated(coord_mapping))
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,32 @@
</coord>
</coords>
<cellMethods/>
<cellMeasures>
<cell-measure datadims="[1, 2]">
<cellMeasure data="[[170, 171, 172, 173, 174, 175, 176, 177, 178,
179, 180, 181, 182, 183, 184, 185, 186, 187,
188, 189, 190],
[530, 531, 532, 533, 534, 535, 536, 537, 538,
539, 540, 541, 542, 543, 544, 545, 546, 547,
548, 549, 550],
[890, 891, 892, 893, 894, 895, 896, 897, 898,
899, 900, 901, 902, 903, 904, 905, 906, 907,
908, 909, 910]]" id="1f66f27e" measure="area" shape="(3, 21)" standard_name="cell_area" units="Unit('m2')" value_type="int64"/>
</cell-measure>
</cellMeasures>
<ancillaryVariables>
<ancillary-var datadims="[1, 2]">
<ancillaryVariable data="[[170, 171, 172, 173, 174, 175, 176, 177, 178,
179, 180, 181, 182, 183, 184, 185, 186, 187,
188, 189, 190],
[530, 531, 532, 533, 534, 535, 536, 537, 538,
539, 540, 541, 542, 543, 544, 545, 546, 547,
548, 549, 550],
[890, 891, 892, 893, 894, 895, 896, 897, 898,
899, 900, 901, 902, 903, 904, 905, 906, 907,
908, 909, 910]]" id="fc39454e" shape="(3, 21)" standard_name="land_area_fraction" units="Unit('%')" value_type="int64"/>
</ancillary-var>
</ancillaryVariables>
<data dtype="float32" shape="(4, 3, 21)" state="loaded"/>
</cube>
</cubes>
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,32 @@
</coord>
</coords>
<cellMethods/>
<cellMeasures>
<cell-measure datadims="[1, 2]">
<cellMeasure data="[[ 350, 351, 352, 353, 354, 355, 356, 357,
358, 359, 0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10],
[ 710, 711, 712, 713, 714, 715, 716, 717,
718, 719, 360, 361, 362, 363, 364, 365,
366, 367, 368, 369, 370],
[1070, 1071, 1072, 1073, 1074, 1075, 1076, 1077,
1078, 1079, 720, 721, 722, 723, 724, 725,
726, 727, 728, 729, 730]]" id="1f66f27e" measure="area" shape="(3, 21)" standard_name="cell_area" units="Unit('m2')" value_type="int64"/>
</cell-measure>
</cellMeasures>
<ancillaryVariables>
<ancillary-var datadims="[1, 2]">
<ancillaryVariable data="[[ 350, 351, 352, 353, 354, 355, 356, 357,
358, 359, 0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10],
[ 710, 711, 712, 713, 714, 715, 716, 717,
718, 719, 360, 361, 362, 363, 364, 365,
366, 367, 368, 369, 370],
[1070, 1071, 1072, 1073, 1074, 1075, 1076, 1077,
1078, 1079, 720, 721, 722, 723, 724, 725,
726, 727, 728, 729, 730]]" id="fc39454e" shape="(3, 21)" standard_name="land_area_fraction" units="Unit('%')" value_type="int64"/>
</ancillary-var>
</ancillaryVariables>
<data dtype="float32" shape="(4, 3, 21)" state="loaded"/>
</cube>
</cubes>
16 changes: 16 additions & 0 deletions lib/iris/tests/unit/cube/test_Cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,22 @@ def create_cube(lon_min, lon_max, bounds=False):
),
2,
)
cube.add_cell_measure(
iris.coords.CellMeasure(
np.arange(0, n_lons * 3).reshape(3, -1),
"cell_area",
units="m2",
),
data_dims=[1, 2],
)
cube.add_ancillary_variable(
iris.coords.AncillaryVariable(
np.arange(0, n_lons * 3).reshape(3, -1),
"land_area_fraction",
units="%",
),
data_dims=[1, 2],
)
if bounds:
cube.coord("longitude").guess_bounds()
cube.add_aux_coord(
Expand Down

0 comments on commit 773c55b

Please sign in to comment.