Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Merge pull request #611 from CCI-Tools/jg-393-validation-error
Browse files Browse the repository at this point in the history
ValueError -> ValidationError
  • Loading branch information
forman authored Apr 17, 2018
2 parents 19a78af + 6546fee commit a65c716
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 111 deletions.
38 changes: 19 additions & 19 deletions cate/ops/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from cate.core.op import op, op_input, op_return
from cate.ops.select import select_var
from cate.util.monitor import Monitor
from cate.core.types import VarNamesLike, DatasetLike
from cate.core.types import VarNamesLike, DatasetLike, ValidationError

from cate.ops.normalize import adjust_temporal_attrs

Expand Down Expand Up @@ -65,21 +65,21 @@ def long_term_average(ds: DatasetLike.TYPE,
ds = DatasetLike.convert(ds)
# Check if time dtype is what we want
if 'datetime64[ns]' != ds.time.dtype:
raise ValueError('Long term average operation expects a dataset with the'
' time coordinate of type datetime64[ns], but received'
' {}. Running the normalize operation on this'
' dataset may help'.format(ds.time.dtype))
raise ValidationError('Long term average operation expects a dataset with the'
' time coordinate of type datetime64[ns], but received'
' {}. Running the normalize operation on this'
' dataset may help'.format(ds.time.dtype))

# Check if we have a monthly dataset
try:
if ds.attrs['time_coverage_resolution'] != 'P1M':
raise ValueError('Long term average operation expects a monthly dataset'
' running temporal aggregation on this dataset'
' beforehand may help.')
raise ValidationError('Long term average operation expects a monthly dataset'
' running temporal aggregation on this dataset'
' beforehand may help.')
except KeyError:
raise ValueError('Could not determine temporal resolution. Running'
' the adjust_temporal_attrs operation beforehand may'
' help.')
raise ValidationError('Could not determine temporal resolution. Running'
' the adjust_temporal_attrs operation beforehand may'
' help.')

var = VarNamesLike.convert(var)
# Shallow
Expand Down Expand Up @@ -154,19 +154,19 @@ def temporal_aggregation(ds: DatasetLike.TYPE,
ds = DatasetLike.convert(ds)
# Check if time dtype is what we want
if 'datetime64[ns]' != ds.time.dtype:
raise ValueError('Temporal aggregation operation expects a dataset with the'
' time coordinate of type datetime64[ns], but received'
' {}. Running the normalize operation on this'
' dataset may help'.format(ds.time.dtype))
raise ValidationError('Temporal aggregation operation expects a dataset with the'
' time coordinate of type datetime64[ns], but received'
' {}. Running the normalize operation on this'
' dataset may help'.format(ds.time.dtype))

# Check if we have a daily dataset
try:
if ds.attrs['time_coverage_resolution'] != 'P1D':
raise ValueError('Temporal aggregation operation expects a daily dataset')
raise ValidationError('Temporal aggregation operation expects a daily dataset')
except KeyError:
raise ValueError('Could not determine temporal resolution. Running'
' the adjust_temporal_attrs operation beforehand may'
' help.')
raise ValidationError('Could not determine temporal resolution. Running'
' the adjust_temporal_attrs operation beforehand may'
' help.')

with monitor.observing("resample dataset"):
retset = ds.resample(freq='MS', dim='time', keep_attrs=True, how=method)
Expand Down
12 changes: 6 additions & 6 deletions cate/ops/animate.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
import numpy as np

from cate.core.op import op, op_input
from cate.core.types import VarName, DictLike, PolygonLike, HTML
from cate.core.types import VarName, DictLike, PolygonLike, HTML, ValidationError
from cate.util.monitor import Monitor

from cate.ops.plot_helpers import (get_var_data,
Expand Down Expand Up @@ -157,7 +157,7 @@ def animate_map(ds: xr.Dataset,
try:
var = ds[var_name]
except KeyError:
raise ValueError('Provided variable name "{}" does not exist in the given dataset'.format(var_name))
raise ValidationError('Provided variable name "{}" does not exist in the given dataset'.format(var_name))

indexers = DictLike.convert(indexers) or {}
properties = DictLike.convert(plot_properties) or {}
Expand All @@ -172,8 +172,8 @@ def animate_map(ds: xr.Dataset,
if len(ds.lat) < 2 or len(ds.lon) < 2:
# Matplotlib can not plot datasets with less than these dimensions with
# contourf and pcolormesh methods
raise ValueError('The minimum dataset spatial dimensions to create a map'
' plot are (2,2)')
raise ValidationError('The minimum dataset spatial dimensions to create a map'
' plot are (2,2)')

# See http://scitools.org.uk/cartopy/docs/v0.15/crs/projections.html#
if projection == 'PlateCarree':
Expand All @@ -197,7 +197,7 @@ def animate_map(ds: xr.Dataset,
elif projection == 'SouthPolarStereo':
proj = ccrs.SouthPolarStereo(central_longitude=central_lon)
else:
raise ValueError('illegal projection: "%s"' % projection)
raise ValidationError('illegal projection: "%s"' % projection)

figure = plt.figure(figsize=(8, 4))
ax = plt.axes(projection=proj)
Expand Down Expand Up @@ -283,7 +283,7 @@ def _get_min_max(data, monitor=None):
data_min = data.min()
if np.isnan(data_min):
# Handle all-NaN dataset
raise ValueError('Can not create an animation of a dataset containing only NaN values.')
raise ValidationError('Can not create an animation of a dataset containing only NaN values.')
else:
with monitor.child(1).observing("find maximum"):
data_max = data.max()
Expand Down
14 changes: 7 additions & 7 deletions cate/ops/anomaly.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from cate.util.monitor import Monitor
from cate.ops.subset import subset_spatial, subset_temporal
from cate.ops.arithmetics import diff, ds_arithmetics
from cate.core.types import TimeRangeLike, PolygonLike
from cate.core.types import TimeRangeLike, PolygonLike, ValidationError


_ALL_FILE_FILTER = dict(name='All Files', extensions=['*'])
Expand Down Expand Up @@ -71,13 +71,13 @@ def anomaly_external(ds: xr.Dataset,
# Check if the time coordinate is of dtype datetime
try:
if ds.time.dtype != 'datetime64[ns]':
raise ValueError('The dataset provided for anomaly calculation'
' is required to have a time coordinate of'
' dtype datetime64[ns]. Running the normalize'
' operation on this dataset might help.')
raise ValidationError('The dataset provided for anomaly calculation'
' is required to have a time coordinate of'
' dtype datetime64[ns]. Running the normalize'
' operation on this dataset might help.')
except AttributeError:
raise ValueError('The dataset provided for anomaly calculation'
' is required to have a time coordinate.')
raise ValidationError('The dataset provided for anomaly calculation'
' is required to have a time coordinate.')

clim = xr.open_dataset(file)
ret = ds.copy()
Expand Down
6 changes: 3 additions & 3 deletions cate/ops/arithmetics.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from xarray import ufuncs as xu

from cate.core.op import op, op_input, op_return
from cate.core.types import DatasetLike
from cate.core.types import DatasetLike, ValidationError
from cate.util.monitor import Monitor
from cate.util.safe import safe_exec

Expand Down Expand Up @@ -95,8 +95,8 @@ def ds_arithmetics(ds: DatasetLike.TYPE,
elif item[:] == 'exp':
retset = xu.exp(retset)
else:
raise ValueError('Arithmetic operation {} not'
' implemented.'.format(item[0]))
raise ValidationError('Arithmetic operation {} not'
' implemented.'.format(item[0]))

return retset

Expand Down
73 changes: 37 additions & 36 deletions cate/ops/coregistration.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import xarray as xr

from cate.core.op import op_input, op, op_return
from cate.core.types import ValidationError
from cate.util.monitor import Monitor

from cate.ops import resampling
Expand Down Expand Up @@ -90,46 +91,46 @@ def coregister(ds_master: xr.Dataset,
('master', ds_master['lat'].values, -90),
('master', ds_master['lon'].values, -180))
except KeyError:
raise ValueError('Coregistration requires that both datasets are'
' spatial datasets with lon and lat dimensions. The'
' dimensionality of the provided master dataset is: {},'
' the dimensionality of the provided slave dataset is:'
' {}. Running the normalize operation might help in'
' case spatial dimensions have different'
' names'.format(ds_master.dims, ds_slave.dims))
raise ValidationError('Coregistration requires that both datasets are'
' spatial datasets with lon and lat dimensions. The'
' dimensionality of the provided master dataset is: {},'
' the dimensionality of the provided slave dataset is:'
' {}. Running the normalize operation might help in'
' case spatial dimensions have different'
' names'.format(ds_master.dims, ds_slave.dims))

# Check if all arrays of the slave dataset have the required dimensionality
for key in ds_slave.data_vars:
if not _is_valid_array(ds_slave[key]):
raise ValueError('{} data array of slave dataset is not valid for'
' coregistration. The data array is expected to'
' have lat and lon dimensions. The data array has'
' the following dimensions: {}. Consider running'
' select_var operation to exclude this'
' data array'.format(key, ds_slave[key].dims))
raise ValidationError('{} data array of slave dataset is not valid for'
' coregistration. The data array is expected to'
' have lat and lon dimensions. The data array has'
' the following dimensions: {}. Consider running'
' select_var operation to exclude this'
' data array'.format(key, ds_slave[key].dims))

# Check if the grids of the provided datasets are equidistant and pixel
# registered
for array in grids:
if not _within_bounds(array[1], array[2]):
raise ValueError('The {} dataset grid does not fall into required'
' boundaries. Required boundaries are ({}, {}),'
' dataset boundaries are ({}, {}). Running the'
' normalize operation'
' may help.'.format(array[0],
array[2],
abs(array[2]),
array[1][0],
array[1][-1]))
raise ValidationError('The {} dataset grid does not fall into required'
' boundaries. Required boundaries are ({}, {}),'
' dataset boundaries are ({}, {}). Running the'
' normalize operation'
' may help.'.format(array[0],
array[2],
abs(array[2]),
array[1][0],
array[1][-1]))
if not _is_equidistant(array[1]):
raise ValueError('The {} dataset grid is not'
' equidistant, can not perform'
' coregistration'.format(array[0]))
raise ValidationError('The {} dataset grid is not'
' equidistant, can not perform'
' coregistration'.format(array[0]))

if not _is_pixel_registered(array[1], array[2]):
raise ValueError('The {} dataset grid is not'
' pixel-registered, can not perform'
' coregistration'.format(array[0]))
raise ValidationError('The {} dataset grid is not'
' pixel-registered, can not perform'
' coregistration'.format(array[0]))

# Co-register
methods_us = {'nearest': 10, 'linear': 11}
Expand Down Expand Up @@ -334,8 +335,8 @@ def _find_intersection(first: np.ndarray,

delta = maximum - minimum
if delta < max(first_px_size, second_px_size):
raise ValueError('Could not find a valid intersection to perform'
' coregistration on')
raise ValidationError('Could not find a valid intersection to perform'
' coregistration on')

# Make sure min/max fall on pixel boundaries for both grids
# Because there exists a number N denoting how many smaller pixels fall
Expand All @@ -348,24 +349,24 @@ def _find_intersection(first: np.ndarray,
while (0 != (minimum - global_bounds[0]) % first_px_size and
0 != (minimum - global_bounds[0]) % second_px_size):
if i == safety:
raise ValueError('Could not find a valid intersection to perform'
' coregistration on')
raise ValidationError('Could not find a valid intersection to perform'
' coregistration on')
minimum = minimum + finer
i = i + 1

i = 0
while (0 != (global_bounds[1] - maximum) % first_px_size and
0 != (global_bounds[1] - maximum) % second_px_size):
if i == safety:
raise ValueError('Could not find a valid intersection to perform'
' coregistration on')
raise ValidationError('Could not find a valid intersection to perform'
' coregistration on')
maximum = maximum - finer
i = i + 1

# This is possible in some cases when mis-aligned grid arrays are presented
if maximum <= minimum:
raise ValueError('Could not find a valid intersection to perform'
' coregistration on')
raise ValidationError('Could not find a valid intersection to perform'
' coregistration on')

return (minimum, maximum)

Expand Down
Loading

0 comments on commit a65c716

Please sign in to comment.