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-04 09:52 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-04 09:52 +0000
1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6'''
7Portable dataset description.
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'''
17import os.path as op
18import logging
20from pyrocko.guts import List, load, StringPattern
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
28guts_prefix = 'squirrel'
30logger = logging.getLogger('psq.dataset')
33class PersistentID(StringPattern):
34 pattern = re_persistent_name
37class Dataset(HasPaths):
38 '''
39 Dataset description.
40 '''
41 sources = List.T(Source.T())
42 operators = List.T(Operator.T())
44 def setup(self, squirrel, check=True):
45 for source in self.sources:
46 squirrel.add_source(
47 source, check=check)
49 for operator in self.operators:
50 squirrel.add_operator(operator)
52 squirrel.update_operator_mappings()
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)
65 if not isinstance(dataset, Dataset):
66 raise SquirrelError('Invalid dataset file "%s".' % path)
68 dataset.set_basepath(op.dirname(path) or '.')
69 return dataset
72__all__ = [
73 'PersistentID',
74 'Dataset',
75 'read_dataset',
76]