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())
35 def __init__(self, **kwargs):
36 if 'codes' in kwargs:
37 if not isinstance(kwargs['codes'], list):
38 kwargs['codes'] = [CodesNSLCE(kwargs['codes'])]
39 else:
40 kwargs['codes'] = [CodesNSLCE(x) for x in kwargs['codes']]
42 Object.__init__(self, **kwargs)
44 def contains(self, constraint):
45 '''
46 Check if the constraint completely includes a more restrictive one.
48 :param constraint:
49 Other constraint.
50 :type constraint:
51 :py:class:`Constraint`
52 '''
54 if self.tmin is not None and constraint.tmin is not None:
55 b1 = self.tmin <= constraint.tmin
56 elif self.tmin is None:
57 b1 = True
58 else:
59 b1 = False
61 if self.tmax is not None and constraint.tmax is not None:
62 b2 = constraint.tmax <= self.tmax
63 elif self.tmax is None:
64 b2 = True
65 else:
66 b2 = False
68 return b1 and b2
70 def expand(self, constraint):
71 '''
72 Widen constraint to include another given constraint.
74 :param constraint:
75 Other constraint.
76 :type constraint:
77 :py:class:`Constraint`
79 Update is done in-place.
80 '''
82 if constraint.tmin is None or self.tmin is None:
83 self.tmin = None
84 else:
85 self.tmin = min(constraint.tmin, self.tmin)
87 if constraint.tmax is None or self.tmax is None:
88 self.tmax = None
89 else:
90 self.tmax = max(constraint.tmax, self.tmax)
93class Source(Object):
95 '''
96 Base class for Squirrel data-sources.
98 Data-sources can be attached to a Squirrel instance to allow transparent
99 access to remote (or otherwise generated) resources, e.g. through FDSN web
100 services (:py:class:`~pyrocko.squirrel.client.fdsn.FDSNSource`) or online
101 event catalogs (:py:class:`~pyrocko.client.catalog.CatalogSource`).
103 Derived classes implement the details of querying, caching, updating and
104 bookkeeping of the accessed data.
105 '''
107 def update_channel_inventory(self, squirrel, constraint):
108 '''
109 Let local inventory be up-to-date with remote for a given constraint.
110 '''
112 pass
114 def update_event_inventory(self, squirrel, constraint):
115 '''
116 Let local inventory be up-to-date with remote for a given constraint.
117 '''
119 pass
121 def update_waveform_promises(self, squirrel, constraint):
122 '''
123 Let local inventory be up-to-date with remote for a given constraint.
124 '''
126 pass
128 def remove_waveform_promises(self, squirrel, from_database='selection'):
129 '''
130 Remove waveform promises from live selection or global database.
132 :param from_database:
133 Remove from live selection ``'selection'`` or global database
134 ``'global'``.
135 '''
137 pass
140__all__ = [
141 'Source',
142 'Constraint',
143]