Skip to content

Commit

Permalink
added in exceptions, not tested
Browse files Browse the repository at this point in the history
  • Loading branch information
ESadek-MO committed Dec 18, 2024
1 parent 088bb94 commit 21489d5
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ def _assert_is_cube(obj):
if not hasattr(obj, "add_aux_coord"):
msg = r"Object {obj} cannot be put in a cubelist, as it is not a Cube."
raise ValueError(msg)
elif obj.is_dataless():
raise iris.exceptions.DatalessError("CubeList")

def _repr_html_(self):
from iris.experimental.representation import CubeListRepresentation
Expand Down Expand Up @@ -1479,6 +1481,8 @@ def convert_units(self, unit: str | Unit) -> None:
"""
# If the cube has units convert the data.
if self.is_dataless():
raise iris.exceptions.DatalessError("convert_units")
if self.units.is_unknown():
raise iris.exceptions.UnitConversionError(
"Cannot convert from unknown units. "
Expand Down Expand Up @@ -3105,6 +3109,8 @@ def subset(self, coord: AuxCoord | DimCoord) -> Cube | None:
whole cube is returned. As such, the operation is not strict.
"""
if self.is_dataless():
raise iris.exceptions.DatalessError("subset")
if not isinstance(coord, iris.coords.Coord):
raise ValueError("coord_to_extract must be a valid Coord.")

Expand Down Expand Up @@ -3226,6 +3232,8 @@ def intersection(self, *args, **kwargs) -> Cube:
which intersects with the requested coordinate intervals.
"""
if self.is_dataless():
raise iris.exceptions.DatalessError("intersection")
result = self
ignore_bounds = kwargs.pop("ignore_bounds", False)
threshold = kwargs.pop("threshold", 0)
Expand Down Expand Up @@ -3750,6 +3758,9 @@ def slices(
dimension index.
""" # noqa: D214, D406, D407, D410, D411
if self.is_dataless():
raise iris.exceptions.DatalessError("slices")

if not isinstance(ordered, bool):
raise TypeError("'ordered' argument to slices must be boolean.")

Expand Down Expand Up @@ -3837,7 +3848,8 @@ def transpose(self, new_order: list[int] | None = None) -> None:

# Transpose the data payload.
dm = self._data_manager
data = dm.core_data().transpose(new_order)
if not self.is_dataless():
data = dm.core_data().transpose(new_order)
self._data_manager = DataManager(data)

dim_mapping = {src: dest for dest, src in enumerate(new_order)}
Expand Down Expand Up @@ -4325,6 +4337,8 @@ def collapsed(
cube.collapsed(['latitude', 'longitude'],
iris.analysis.VARIANCE)
"""
if self.is_dataless():
raise iris.exceptions.DatalessError("collapsed")
# Update weights kwargs (if necessary) to handle different types of
# weights
weights_info = None
Expand Down Expand Up @@ -4545,6 +4559,8 @@ def aggregated_by(
STASH m01s00i024
"""
if self.is_dataless():
raise iris.exceptions.DatalessError("aggregated_by")
# Update weights kwargs (if necessary) to handle different types of
# weights
weights_info = None
Expand Down Expand Up @@ -4844,6 +4860,8 @@ def rolling_window(
""" # noqa: D214, D406, D407, D410, D411
# Update weights kwargs (if necessary) to handle different types of
# weights
if self.is_dataless():
raise iris.exceptions.DatalessError("rolling_window")
weights_info = None
if kwargs.get("weights") is not None:
weights_info = _Weights(kwargs["weights"], self)
Expand Down Expand Up @@ -5049,6 +5067,8 @@ def interpolate(
True
"""
if self.is_dataless():
raise iris.exceptions.DatalessError("interoplate")
coords, points = zip(*sample_points)
interp = scheme.interpolator(self, coords) # type: ignore[arg-type]
return interp(points, collapse_scalar=collapse_scalar)
Expand Down Expand Up @@ -5094,6 +5114,8 @@ def regrid(self, grid: Cube, scheme: iris.analysis.RegriddingScheme) -> Cube:
this function is not applicable.
"""
if self.is_dataless():
raise iris.exceptions.DatalessError("regrid")
regridder = scheme.regridder(self, grid)
return regridder(self)

Expand Down
11 changes: 11 additions & 0 deletions lib/iris/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,14 @@ class CannotAddError(ValueError):
"""Raised when an object (e.g. coord) cannot be added to a :class:`~iris.cube.Cube`."""

pass


class DatalessError(ValueError):
"""Raised when an method cannot be performed on a dataless :class:`~iris.cube.Cube`."""

def __str__(self):
msg = (
"Dataless cubes are still early in implementation, and dataless {} "
"operations are not currently supported."
)
return msg.format(super().__str__())

0 comments on commit 21489d5

Please sign in to comment.