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

Commit

Permalink
#393 opening datasets errors handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof (Chris) Bernat committed Oct 2, 2017
1 parent d800351 commit 92f4249
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
17 changes: 7 additions & 10 deletions cate/core/ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
import glob
from abc import ABCMeta, abstractmethod
from math import ceil, sqrt
import sys
from typing import Sequence, Optional, Union, Tuple, Any

import xarray as xr
Expand Down Expand Up @@ -463,16 +462,14 @@ class DataAccessError(Exception):
"""
Exceptions produced by Cate's data stores and data sources instances, used to report any problems handling data.
"""
def __init__(self, cause, *args, **kwargs):
if isinstance(cause, Exception):
super(DataAccessError, self).__init__(str(cause), *args, **kwargs)
_, _, traceback = sys.exc_info()
self.with_traceback(traceback)
elif isinstance(cause, str):
super(DataAccessError, self).__init__(cause, *args, **kwargs)
def __init__(self, source, cause, *args, **kwargs):
if isinstance(source, DataSource) or isinstance(source, DataStore):
if isinstance(cause, Exception):
super(DataAccessError, self).__init__("{}: {}".format(source, str(cause)), *args, **kwargs)
elif isinstance(cause, str):
super(DataAccessError, self).__init__("{}: {}".format(source, cause), *args, **kwargs)
else:
super(DataAccessError, self).__init__(*args, **kwargs)
self._cause = cause
super(DataAccessError, self).__init__(self, cause, *args, **kwargs)

@property
def cause(self):
Expand Down
17 changes: 11 additions & 6 deletions cate/ds/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ def open_dataset(self,
except OSError as e:
raise IOError("Files: {} caused:\nOSError({}): {}".format(paths, e.errno, e.strerror))
else:
return None
if time_range:
raise DataAccessError(self, "No data sets available for specified time range {}".format(
TimeRangeLike.format(time_range)), paths)
else:
raise DataAccessError(self, "No data sets available")

@staticmethod
def _get_harmonized_coordinate_value(attrs: dict, attr_name: str):
Expand Down Expand Up @@ -402,12 +406,14 @@ def update_local(self,

time_range = TimeRangeLike.convert(time_range) if time_range else None

local_store = DATA_STORE_REGISTRY.get_data_store('local')
local_store_name = 'local'

local_store = DATA_STORE_REGISTRY.get_data_store(local_store_name)
if not local_store:
add_to_data_store_registry()
local_store = DATA_STORE_REGISTRY.get_data_store('local')
local_store = DATA_STORE_REGISTRY.get_data_store(local_store_name)
if not local_store:
raise ValueError('Cannot initialize `local` DataStore')
raise ValueError('Cannot initialize `{}` DataStore'.format(local_store_name))

data_sources = local_store.query(ds_id=local_id) # type: Sequence['DataSource']
data_source = next((ds for ds in data_sources if isinstance(ds, LocalDataSource) and
Expand Down Expand Up @@ -752,8 +758,7 @@ def create_data_source(self, data_source_id: str, region: PolygonLike.TYPE = Non
data_source = ds
data_source.set_completed(False)
break
raise DataAccessError("Local data store '{}' already contains a data source named '{}'".
format(self.id, data_source_id))
raise DataAccessError(self, "Data source '{}' already exists.". format(data_source_id))
if not data_source:
data_source = LocalDataSource(data_source_id, files=[], data_store=self, spatial_coverage=region,
variables=var_names, temporal_coverage=time_range, meta_info=meta_info)
Expand Down
18 changes: 11 additions & 7 deletions test/ds/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import unittest.mock
import datetime
import shutil
from cate.core.ds import DATA_STORE_REGISTRY
from cate.core.ds import DATA_STORE_REGISTRY, DataAccessError
from cate.core.types import PolygonLike, TimeRangeLike, VarNamesLike
from cate.ds.local import LocalDataStore, LocalDataSource
from cate.ds.esa_cci_odp import EsaCciOdpDataStore
Expand Down Expand Up @@ -47,10 +47,10 @@ def test_add_pattern(self):
data_sources = self.data_store.query()
self.assertEqual(len(data_sources), 3)

with self.assertRaises(ValueError) as cm:
with self.assertRaises(DataAccessError) as cm:
self.data_store.add_pattern("a_name", "a_pat2")
self.assertEqual("Local data store 'test' already contains a data source named 'test.{}'".format(new_ds_name),
str(cm.exception))
self.assertEqual("{}: Data source '{}.{}' already exists.".format(
self.data_store, self.data_store.id, new_ds_name), str(cm.exception))

data_sources = self.data_store.query()
self.assertEqual(len(data_sources), 3)
Expand Down Expand Up @@ -304,9 +304,9 @@ def test_open_dataset(self):
self.assertIsNotNone(xr)
self.assertEqual(xr.coords.dims.get('time'), 3)

xr = ds.open_dataset(time_range=(datetime.datetime(1978, 11, 14),
datetime.datetime(1978, 11, 15)))
self.assertIsNone(xr)
with self.assertRaises(DataAccessError):
ds.open_dataset(time_range=(datetime.datetime(1978, 11, 14),
datetime.datetime(1978, 11, 15)))

ds = self._local_data_store.query('local_w_temporal')[0]

Expand All @@ -319,6 +319,10 @@ def test_open_dataset(self):
self.assertIsNotNone(xr)
self.assertEqual(xr.coords.dims.get('time'), 1)

with self.assertRaises(DataAccessError):
ds.open_dataset(time_range=(datetime.datetime(1978, 11, 20),
datetime.datetime(1978, 11, 21)))

def test_make_local(self):
data_source = self._local_data_store.query('local_w_temporal')[0]

Expand Down

0 comments on commit 92f4249

Please sign in to comment.