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
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 = CodesNSLCE.T(optional=True)
35 def __init__(self, **kwargs):
36 if 'codes' in kwargs:
37 kwargs['codes'] = CodesNSLCE(kwargs['codes'])
39 Object.__init__(self, **kwargs)
41 def contains(self, constraint):
42 '''
43 Check if the constraint completely includes a more restrictive one.
45 :param constraint:
46 Other constraint.
47 :type constraint:
48 :py:class:`Constraint`
49 '''
51 if self.tmin is not None and constraint.tmin is not None:
52 b1 = self.tmin <= constraint.tmin
53 elif self.tmin is None:
54 b1 = True
55 else:
56 b1 = False
58 if self.tmax is not None and constraint.tmax is not None:
59 b2 = constraint.tmax <= self.tmax
60 elif self.tmax is None:
61 b2 = True
62 else:
63 b2 = False
65 return b1 and b2
67 def expand(self, constraint):
68 '''
69 Widen constraint to include another given constraint.
71 :param constraint:
72 Other constraint.
73 :type constraint:
74 :py:class:`Constraint`
76 Update is done in-place.
77 '''
79 if constraint.tmin is None or self.tmin is None:
80 self.tmin = None
81 else:
82 self.tmin = min(constraint.tmin, self.tmin)
84 if constraint.tmax is None or self.tmax is None:
85 self.tmax = None
86 else:
87 self.tmax = max(constraint.tmax, self.tmax)
90class Source(Object):
92 '''
93 Base class for Squirrel data-sources.
95 Data-sources can be attached to a Squirrel instance to allow transparent
96 access to remote (or otherwise generated) resources, e.g. through FDSN web
97 services (:py:class:`~pyrocko.squirrel.client.fdsn.FDSNSource`) or online
98 event catalogs (:py:class:`~pyrocko.client.catalog.CatalogSource`).
100 Derived classes implement the details of querying, caching, updating and
101 bookkeeping of the accessed data.
102 '''
104 def update_channel_inventory(self, squirrel, constraint):
105 '''
106 Let local inventory be up-to-date with remote for a given constraint.
107 '''
109 pass
111 def update_event_inventory(self, squirrel, constraint):
112 '''
113 Let local inventory be up-to-date with remote for a given constraint.
114 '''
116 pass
118 def update_waveform_promises(self, squirrel, constraint):
119 '''
120 Let local inventory be up-to-date with remote for a given constraint.
121 '''
123 pass
125 def remove_waveform_promises(self, squirrel, from_database='selection'):
126 '''
127 Remove waveform promises from live selection or global database.
129 :param from_database:
130 Remove from live selection ``'selection'`` or global database
131 ``'global'``.
132 '''
134 pass
137__all__ = [
138 'Source',
139 'Constraint',
140]