1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6from pyrocko.guts import Object, Timestamp, List 

7 

8from ..model import CodesNSLCE 

9 

10 

11guts_prefix = 'squirrel' 

12 

13 

14class Constraint(Object): 

15 

16 ''' 

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

18 

19 Squirrel data-sources typically try to mirror only a subset of the remotely 

20 available data. This subset may need to be grown or updated when data from 

21 other time intervals or from locations outside the initially requested 

22 region is requested. This class helps in the involved bookeeping. 

23 

24 The current implementation only supports a time interval selection with a 

25 single time span but more sophisticated constraints, including e.g. 

26 location boxes could be thought of. 

27 ''' 

28 

29 tmin = Timestamp.T(optional=True) 

30 tmax = Timestamp.T(optional=True) 

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

32 

33 def __init__(self, **kwargs): 

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

35 

36 if codes is None: 

37 pass 

38 elif not isinstance(codes, list): 

39 codes = [CodesNSLCE(codes)] 

40 else: 

41 codes = [CodesNSLCE(sc) for sc in codes] 

42 

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

44 

45 def contains(self, constraint): 

46 ''' 

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

48 

49 :param constraint: 

50 Other constraint. 

51 :type constraint: 

52 :py:class:`Constraint` 

53 ''' 

54 

55 if self.tmin is not None and constraint.tmin is not None: 

56 b1 = self.tmin <= constraint.tmin 

57 elif self.tmin is None: 

58 b1 = True 

59 else: 

60 b1 = False 

61 

62 if self.tmax is not None and constraint.tmax is not None: 

63 b2 = constraint.tmax <= self.tmax 

64 elif self.tmax is None: 

65 b2 = True 

66 else: 

67 b2 = False 

68 

69 return b1 and b2 

70 

71 def expand(self, constraint): 

72 ''' 

73 Widen constraint to include another given constraint. 

74 

75 :param constraint: 

76 Other constraint. 

77 :type constraint: 

78 :py:class:`Constraint` 

79 

80 Update is done in-place. 

81 ''' 

82 

83 if constraint.tmin is None or self.tmin is None: 

84 self.tmin = None 

85 else: 

86 self.tmin = min(constraint.tmin, self.tmin) 

87 

88 if constraint.tmax is None or self.tmax is None: 

89 self.tmax = None 

90 else: 

91 self.tmax = max(constraint.tmax, self.tmax) 

92 

93 

94class Source(Object): 

95 

96 ''' 

97 Base class for Squirrel data-sources. 

98 

99 Data-sources can be attached to a Squirrel instance to allow transparent 

100 access to remote (or otherwise generated) resources, e.g. through FDSN web 

101 services (:py:class:`~pyrocko.squirrel.client.fdsn.FDSNSource`) or online 

102 event catalogs (:py:class:`~pyrocko.client.catalog.CatalogSource`). 

103 

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

105 bookkeeping of the accessed data. 

106 ''' 

107 

108 def update_channel_inventory(self, squirrel, constraint): 

109 ''' 

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

111 ''' 

112 

113 pass 

114 

115 def update_event_inventory(self, squirrel, constraint): 

116 ''' 

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

118 ''' 

119 

120 pass 

121 

122 def update_waveform_promises(self, squirrel, constraint): 

123 ''' 

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

125 ''' 

126 

127 pass 

128 

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

130 ''' 

131 Remove waveform promises from live selection or global database. 

132 

133 :param from_database: 

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

135 ``'global'``. 

136 ''' 

137 

138 pass 

139 

140 def update_response_inventory(self, squirrel, constraint): 

141 ''' 

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

143 ''' 

144 

145 pass 

146 

147 

148__all__ = [ 

149 'Source', 

150 'Constraint', 

151]