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

41 

42 Object.__init__(self, **kwargs) 

43 

44 def contains(self, constraint): 

45 ''' 

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

47 

48 :param constraint: 

49 Other constraint. 

50 :type constraint: 

51 :py:class:`Constraint` 

52 ''' 

53 

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 

60 

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 

67 

68 return b1 and b2 

69 

70 def expand(self, constraint): 

71 ''' 

72 Widen constraint to include another given constraint. 

73 

74 :param constraint: 

75 Other constraint. 

76 :type constraint: 

77 :py:class:`Constraint` 

78 

79 Update is done in-place. 

80 ''' 

81 

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) 

86 

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) 

91 

92 

93class Source(Object): 

94 

95 ''' 

96 Base class for Squirrel data-sources. 

97 

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

102 

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

104 bookkeeping of the accessed data. 

105 ''' 

106 

107 def update_channel_inventory(self, squirrel, constraint): 

108 ''' 

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

110 ''' 

111 

112 pass 

113 

114 def update_event_inventory(self, squirrel, constraint): 

115 ''' 

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

117 ''' 

118 

119 pass 

120 

121 def update_waveform_promises(self, squirrel, constraint): 

122 ''' 

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

124 ''' 

125 

126 pass 

127 

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

129 ''' 

130 Remove waveform promises from live selection or global database. 

131 

132 :param from_database: 

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

134 ``'global'``. 

135 ''' 

136 

137 pass 

138 

139 

140__all__ = [ 

141 'Source', 

142 'Constraint', 

143]