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 

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 

32 def contains(self, constraint): 

33 ''' 

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

35 

36 :param constraint: 

37 Other constraint. 

38 :type constraint: 

39 :py:class:`Constraint` 

40 ''' 

41 

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

43 b1 = self.tmin <= constraint.tmin 

44 elif self.tmin is None: 

45 b1 = True 

46 else: 

47 b1 = False 

48 

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

50 b2 = constraint.tmax <= self.tmax 

51 elif self.tmax is None: 

52 b2 = True 

53 else: 

54 b2 = False 

55 

56 return b1 and b2 

57 

58 def expand(self, constraint): 

59 ''' 

60 Widen constraint to include another given constraint. 

61 

62 :param constraint: 

63 Other constraint. 

64 :type constraint: 

65 :py:class:`Constraint` 

66 

67 Update is done in-place. 

68 ''' 

69 

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

71 self.tmin = None 

72 else: 

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

74 

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

76 self.tmax = None 

77 else: 

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

79 

80 

81class Source(Object): 

82 

83 ''' 

84 Base class for Squirrel data-sources. 

85 

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

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

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

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

90 

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

92 bookkeeping of the accessed data. 

93 ''' 

94 

95 def update_channel_inventory(self, squirrel, constraint): 

96 ''' 

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

98 ''' 

99 

100 pass 

101 

102 def update_event_inventory(self, squirrel, constraint): 

103 ''' 

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

105 ''' 

106 

107 pass 

108 

109 def update_waveform_promises(self, squirrel, constraint): 

110 ''' 

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

112 ''' 

113 

114 pass 

115 

116 

117__all__ = [ 

118 'Source', 

119 'Constraint', 

120]