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 

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 = CodesNSLCE.T(optional=True) 

34 

35 def __init__(self, **kwargs): 

36 if 'codes' in kwargs: 

37 kwargs['codes'] = CodesNSLCE(kwargs['codes']) 

38 

39 Object.__init__(self, **kwargs) 

40 

41 def contains(self, constraint): 

42 ''' 

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

44 

45 :param constraint: 

46 Other constraint. 

47 :type constraint: 

48 :py:class:`Constraint` 

49 ''' 

50 

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 

57 

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 

64 

65 return b1 and b2 

66 

67 def expand(self, constraint): 

68 ''' 

69 Widen constraint to include another given constraint. 

70 

71 :param constraint: 

72 Other constraint. 

73 :type constraint: 

74 :py:class:`Constraint` 

75 

76 Update is done in-place. 

77 ''' 

78 

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) 

83 

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) 

88 

89 

90class Source(Object): 

91 

92 ''' 

93 Base class for Squirrel data-sources. 

94 

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

99 

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

101 bookkeeping of the accessed data. 

102 ''' 

103 

104 def update_channel_inventory(self, squirrel, constraint): 

105 ''' 

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

107 ''' 

108 

109 pass 

110 

111 def update_event_inventory(self, squirrel, constraint): 

112 ''' 

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

114 ''' 

115 

116 pass 

117 

118 def update_waveform_promises(self, squirrel, constraint): 

119 ''' 

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

121 ''' 

122 

123 pass 

124 

125 

126__all__ = [ 

127 'Source', 

128 'Constraint', 

129]