1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

4# ---|P------/S----------~Lg---------- 

5 

6from __future__ import absolute_import, print_function 

7 

8import os.path as op 

9import logging 

10 

11from pyrocko.guts import List, load, StringPattern 

12 

13from ..has_paths import HasPaths 

14from .client.base import Source 

15from .error import SquirrelError 

16from .selection import re_persistent_name 

17from .operators.base import Operator 

18 

19guts_prefix = 'squirrel' 

20 

21logger = logging.getLogger('psq.dataset') 

22 

23 

24class PersistentID(StringPattern): 

25 pattern = re_persistent_name 

26 

27 

28class Dataset(HasPaths): 

29 ''' 

30 Dataset description. 

31 ''' 

32 sources = List.T(Source.T()) 

33 operators = List.T(Operator.T()) 

34 

35 def setup(self, squirrel, check=True): 

36 for source in self.sources: 

37 squirrel.add_source( 

38 source, check=check) 

39 

40 for operator in self.operators: 

41 squirrel.add_operator(operator) 

42 

43 squirrel.update_operator_mappings() 

44 

45 

46def read_dataset(path): 

47 ''' 

48 Read dataset description file. 

49 ''' 

50 try: 

51 dataset = load(filename=path) 

52 except OSError: 

53 raise SquirrelError( 

54 'Cannot read dataset file: %s' % path) 

55 

56 if not isinstance(dataset, Dataset): 

57 raise SquirrelError('Invalid dataset file "%s".' % path) 

58 

59 dataset.set_basepath(op.dirname(path) or '.') 

60 return dataset 

61 

62 

63__all__ = [ 

64 'PersistentID', 

65 'Dataset', 

66 'read_dataset', 

67]