1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6from __future__ import absolute_import, print_function
8from pyrocko.guts import Object, Timestamp, List
10from ..model import CodesNSLCE
13guts_prefix = 'squirrel'
16class Constraint(Object):
18 '''
19 Used by some data-sources to grow or join locally mirrored data selections.
21 Squirrel data-sources typically try to mirror only a subset of the remotely
22 available data. This subset may need to be grown or updated when data from
23 other time intervals or from locations outside the initially requested
24 region is requested. This class helps in the involved bookeeping.
26 The current implementation only supports a time interval selection with a
27 single time span but more sophisticated constraints, including e.g.
28 location boxes could be thought of.
29 '''
31 tmin = Timestamp.T(optional=True)
32 tmax = Timestamp.T(optional=True)
33 codes = List.T(CodesNSLCE.T(), optional=True)
35 def __init__(self, **kwargs):
36 codes = kwargs.pop('codes', None)
38 if codes is None:
39 pass
40 elif not isinstance(codes, list):
41 codes = [CodesNSLCE(codes)]
42 else:
43 codes = [CodesNSLCE(sc) for sc in codes]
45 Object.__init__(self, codes=codes, **kwargs)
47 def contains(self, constraint):
48 '''
49 Check if the constraint completely includes a more restrictive one.
51 :param constraint:
52 Other constraint.
53 :type constraint:
54 :py:class:`Constraint`
55 '''
57 if self.tmin is not None and constraint.tmin is not None:
58 b1 = self.tmin <= constraint.tmin
59 elif self.tmin is None:
60 b1 = True
61 else:
62 b1 = False
64 if self.tmax is not None and constraint.tmax is not None:
65 b2 = constraint.tmax <= self.tmax
66 elif self.tmax is None:
67 b2 = True
68 else:
69 b2 = False
71 return b1 and b2
73 def expand(self, constraint):
74 '''
75 Widen constraint to include another given constraint.
77 :param constraint:
78 Other constraint.
79 :type constraint:
80 :py:class:`Constraint`
82 Update is done in-place.
83 '''
85 if constraint.tmin is None or self.tmin is None:
86 self.tmin = None
87 else:
88 self.tmin = min(constraint.tmin, self.tmin)
90 if constraint.tmax is None or self.tmax is None:
91 self.tmax = None
92 else:
93 self.tmax = max(constraint.tmax, self.tmax)
96class Source(Object):
98 '''
99 Base class for Squirrel data-sources.
101 Data-sources can be attached to a Squirrel instance to allow transparent
102 access to remote (or otherwise generated) resources, e.g. through FDSN web
103 services (:py:class:`~pyrocko.squirrel.client.fdsn.FDSNSource`) or online
104 event catalogs (:py:class:`~pyrocko.client.catalog.CatalogSource`).
106 Derived classes implement the details of querying, caching, updating and
107 bookkeeping of the accessed data.
108 '''
110 def update_channel_inventory(self, squirrel, constraint):
111 '''
112 Let local inventory be up-to-date with remote for a given constraint.
113 '''
115 pass
117 def update_event_inventory(self, squirrel, constraint):
118 '''
119 Let local inventory be up-to-date with remote for a given constraint.
120 '''
122 pass
124 def update_waveform_promises(self, squirrel, constraint):
125 '''
126 Let local inventory be up-to-date with remote for a given constraint.
127 '''
129 pass
131 def remove_waveform_promises(self, squirrel, from_database='selection'):
132 '''
133 Remove waveform promises from live selection or global database.
135 :param from_database:
136 Remove from live selection ``'selection'`` or global database
137 ``'global'``.
138 '''
140 pass
143__all__ = [
144 'Source',
145 'Constraint',
146]