Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Straw man: alternative solution for non-cubes in cubelists #3510

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@
__all__ = ['Cube', 'CubeList', 'CubeMetadata']


def cube_attributes(func):
"""
Wrap functions that operate on CubeLists so they produce a more informative
error message if they find an element that is not a cube.
"""
def inner(*args, **kwargs):
try:
result = func(*args, **kwargs)
except AttributeError:
raise TypeError(
'Cubelist appears to contain objects that are not cubes')
else:
return result

return inner


class CubeMetadata(namedtuple('CubeMetadata',
['standard_name',
'long_name',
Expand Down Expand Up @@ -206,6 +223,7 @@ def __new__(cls, list_of_cubes=None):
'instances.')
return cube_list

@cube_attributes
def __str__(self):
"""Runs short :meth:`Cube.summary` on every cube."""
result = ['%s: %s' % (i, cube.summary(shorten=True)) for i, cube in
Expand All @@ -216,10 +234,12 @@ def __str__(self):
result = '< No cubes >'
return result

@cube_attributes
def __repr__(self):
"""Runs repr on every cube."""
return '[%s]' % ',\n'.join([repr(cube) for cube in self])

@cube_attributes
def _repr_html_(self):
from iris.experimental.representation import CubeListRepresentation
representer = CubeListRepresentation(self)
Expand Down Expand Up @@ -247,6 +267,7 @@ def __getslice__(self, start, stop):
result = CubeList(result)
return result

@cube_attributes
def xml(self, checksum=False, order=True, byteorder=True):
"""Return a string of the XML that this list of cubes represents."""
doc = Document()
Expand All @@ -263,6 +284,7 @@ def xml(self, checksum=False, order=True, byteorder=True):
# return our newly created XML string
return doc.toprettyxml(indent=" ")

@cube_attributes
def extract(self, constraints, strict=False):
"""
Filter each of the cubes which can be filtered by the given
Expand Down Expand Up @@ -351,6 +373,7 @@ def overlap_fn(cell):

return self.extract(iris.Constraint(coord_values=coord_values))

@cube_attributes
def merge_cube(self):
"""
Return the merged contents of the :class:`CubeList` as a single
Expand Down Expand Up @@ -388,6 +411,7 @@ def merge_cube(self):
merged_cube, = proto_cube.merge()
return merged_cube

@cube_attributes
def merge(self, unique=True):
"""
Returns the :class:`CubeList` resulting from merging this
Expand Down Expand Up @@ -476,6 +500,7 @@ def _none_sort(item):

return merged_cubes

@cube_attributes
def concatenate_cube(self, check_aux_coords=True):
"""
Return the concatenated contents of the :class:`CubeList` as a single
Expand Down Expand Up @@ -521,6 +546,7 @@ def concatenate_cube(self, check_aux_coords=True):
names[1]))
raise iris.exceptions.ConcatenateError(msgs)

@cube_attributes
def concatenate(self, check_aux_coords=True):
"""
Concatenate the cubes over their common dimensions.
Expand Down Expand Up @@ -599,6 +625,7 @@ def concatenate(self, check_aux_coords=True):
return iris._concatenate.concatenate(self,
check_aux_coords=check_aux_coords)

@cube_attributes
def realise_data(self):
"""
Fetch 'real' data for all cubes, in a shared calculation.
Expand Down
1 change: 1 addition & 0 deletions lib/iris/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ def find_saver(filespec):
return _savers[matches[0]] if matches else None


@iris.cube.cube_attributes
def save(source, target, saver=None, **kwargs):
"""
Save one or more Cubes to file (or other writable).
Expand Down