Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/squirrel/dataset.py: 63%

30 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-10-06 15:01 +0000

1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6''' 

7Portable dataset description. 

8 

9The :py:class:`Dataset` class defines sets of local and remote data-sources to 

10be used in combination in Squirrel-based programs. By convention, 

11Squirrel-based programs accept the ``--dataset`` option to read such dataset 

12descriptions from file. To add a dataset programmatically, to a 

13:py:class:`~pyrocko.squirrel.base.Squirrel` instance, use 

14:py:meth:`~pyrocko.squirrel.base.Squirrel.add_dataset`. 

15''' 

16 

17import os.path as op 

18import logging 

19 

20from pyrocko.guts import List, load, StringPattern 

21 

22from ..has_paths import HasPaths 

23from .client.base import Source 

24from .error import SquirrelError 

25from .selection import re_persistent_name 

26from .operators.base import Operator 

27 

28guts_prefix = 'squirrel' 

29 

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

31 

32 

33class PersistentID(StringPattern): 

34 pattern = re_persistent_name 

35 

36 

37class Dataset(HasPaths): 

38 ''' 

39 Dataset description. 

40 ''' 

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

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

43 

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

45 for source in self.sources: 

46 squirrel.add_source( 

47 source, check=check) 

48 

49 for operator in self.operators: 

50 squirrel.add_operator(operator) 

51 

52 squirrel.update_operator_mappings() 

53 

54 

55def read_dataset(path): 

56 ''' 

57 Read dataset description file. 

58 ''' 

59 try: 

60 dataset = load(filename=path) 

61 except OSError: 

62 raise SquirrelError( 

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

64 

65 if not isinstance(dataset, Dataset): 

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

67 

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

69 return dataset 

70 

71 

72__all__ = [ 

73 'PersistentID', 

74 'Dataset', 

75 'read_dataset', 

76]