1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6from __future__ import absolute_import, print_function 

7 

8from pyrocko.guts import Object, Timestamp, List 

9 

10from ..model import CodesNSLCE 

11 

12 

13guts_prefix = 'squirrel' 

14 

15 

16class Constraint(Object): 

17 

18 ''' 

19 Used by some data-sources to grow or join locally mirrored data selections. 

20 

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. 

25 

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 ''' 

30 

31 tmin = Timestamp.T(optional=True) 

32 tmax = Timestamp.T(optional=True) 

33 codes = List.T(CodesNSLCE.T(), optional=True) 

34 

35 def __init__(self, **kwargs): 

36 codes = kwargs.pop('codes', None) 

37 

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] 

44 

45 Object.__init__(self, codes=codes, **kwargs) 

46 

47 def contains(self, constraint): 

48 ''' 

49 Check if the constraint completely includes a more restrictive one. 

50 

51 :param constraint: 

52 Other constraint. 

53 :type constraint: 

54 :py:class:`Constraint` 

55 ''' 

56 

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 

63 

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 

70 

71 return b1 and b2 

72 

73 def expand(self, constraint): 

74 ''' 

75 Widen constraint to include another given constraint. 

76 

77 :param constraint: 

78 Other constraint. 

79 :type constraint: 

80 :py:class:`Constraint` 

81 

82 Update is done in-place. 

83 ''' 

84 

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) 

89 

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) 

94 

95 

96class Source(Object): 

97 

98 ''' 

99 Base class for Squirrel data-sources. 

100 

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`). 

105 

106 Derived classes implement the details of querying, caching, updating and 

107 bookkeeping of the accessed data. 

108 ''' 

109 

110 def update_channel_inventory(self, squirrel, constraint): 

111 ''' 

112 Let local inventory be up-to-date with remote for a given constraint. 

113 ''' 

114 

115 pass 

116 

117 def update_event_inventory(self, squirrel, constraint): 

118 ''' 

119 Let local inventory be up-to-date with remote for a given constraint. 

120 ''' 

121 

122 pass 

123 

124 def update_waveform_promises(self, squirrel, constraint): 

125 ''' 

126 Let local inventory be up-to-date with remote for a given constraint. 

127 ''' 

128 

129 pass 

130 

131 def remove_waveform_promises(self, squirrel, from_database='selection'): 

132 ''' 

133 Remove waveform promises from live selection or global database. 

134 

135 :param from_database: 

136 Remove from live selection ``'selection'`` or global database 

137 ``'global'``. 

138 ''' 

139 

140 pass 

141 

142 def update_response_inventory(self, squirrel, constraint): 

143 ''' 

144 Let local inventory be up-to-date with remote for a given constraint. 

145 ''' 

146 

147 pass 

148 

149 

150__all__ = [ 

151 'Source', 

152 'Constraint', 

153]