1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6import os.path as op 

7import logging 

8 

9from pyrocko.guts import List, load, StringPattern 

10 

11from ..has_paths import HasPaths 

12from .client.base import Source 

13from .error import SquirrelError 

14from .selection import re_persistent_name 

15from .operators.base import Operator 

16 

17guts_prefix = 'squirrel' 

18 

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

20 

21 

22class PersistentID(StringPattern): 

23 pattern = re_persistent_name 

24 

25 

26class Dataset(HasPaths): 

27 ''' 

28 Dataset description. 

29 ''' 

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

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

32 

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

34 for source in self.sources: 

35 squirrel.add_source( 

36 source, check=check) 

37 

38 for operator in self.operators: 

39 squirrel.add_operator(operator) 

40 

41 squirrel.update_operator_mappings() 

42 

43 

44def read_dataset(path): 

45 ''' 

46 Read dataset description file. 

47 ''' 

48 try: 

49 dataset = load(filename=path) 

50 except OSError: 

51 raise SquirrelError( 

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

53 

54 if not isinstance(dataset, Dataset): 

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

56 

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

58 return dataset 

59 

60 

61__all__ = [ 

62 'PersistentID', 

63 'Dataset', 

64 'read_dataset', 

65]