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, String 

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

32 

33 def contains(self, constraint): 

34 ''' 

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

36 

37 :param constraint: 

38 Other constraint. 

39 :type constraint: 

40 :py:class:`Constraint` 

41 ''' 

42 

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

44 b1 = self.tmin <= constraint.tmin 

45 elif self.tmin is None: 

46 b1 = True 

47 else: 

48 b1 = False 

49 

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

51 b2 = constraint.tmax <= self.tmax 

52 elif self.tmax is None: 

53 b2 = True 

54 else: 

55 b2 = False 

56 

57 return b1 and b2 

58 

59 def expand(self, constraint): 

60 ''' 

61 Widen constraint to include another given constraint. 

62 

63 :param constraint: 

64 Other constraint. 

65 :type constraint: 

66 :py:class:`Constraint` 

67 

68 Update is done in-place. 

69 ''' 

70 

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

72 self.tmin = None 

73 else: 

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

75 

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

77 self.tmax = None 

78 else: 

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

80 

81 

82class Source(Object): 

83 

84 ''' 

85 Base class for Squirrel data-sources. 

86 

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

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

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

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

91 

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

93 bookkeeping of the accessed data. 

94 ''' 

95 

96 def update_channel_inventory(self, squirrel, constraint): 

97 ''' 

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

99 ''' 

100 

101 pass 

102 

103 def update_event_inventory(self, squirrel, constraint): 

104 ''' 

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

106 ''' 

107 

108 pass 

109 

110 def update_waveform_promises(self, squirrel, constraint): 

111 ''' 

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

113 ''' 

114 

115 pass 

116 

117 

118__all__ = [ 

119 'Source', 

120 'Constraint', 

121]