Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/shadow_pile.py: 0%

88 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-10-11 13:29 +0000

1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6''' 

7Lazy block-wise processing based on :py:mod:`pyrocko.pile` (*deprecated*). 

8''' 

9 

10import math 

11import logging 

12 

13from . import pile, util, io 

14 

15logger = logging.getLogger('pyrocko.shadow_pile') 

16 

17 

18class NoBasePileSet(Exception): 

19 pass 

20 

21 

22class ShadowBlock(object): 

23 def __init__(self): 

24 self.mtime = None 

25 self.files = [] 

26 

27 

28class ShadowPile(pile.Pile): 

29 

30 def __init__(self, basepile=None, tinc=360., tpad=0., storepath=None): 

31 pile.Pile.__init__(self) 

32 

33 self._tinc = tinc 

34 self._tpad = tpad 

35 self._storepath = storepath 

36 self._blocks = {} 

37 

38 if basepile is None: 

39 basepile = pile.Pile() 

40 

41 self.set_basepile(basepile) 

42 

43 def clear(self): 

44 for iblock in self._blocks.keys(): 

45 self._clearblock() 

46 self._blocks = {} 

47 

48 def set_basepile(self, basepile): 

49 self.clear() 

50 self._base = basepile 

51 

52 def get_basepile(self): 

53 return self._base 

54 

55 def set_chopsize(self, tinc, tpad=0.): 

56 self.clear() 

57 self._tinc = tinc 

58 self._tpad = tpad 

59 

60 def set_store(self, storepath=None): 

61 self.clear() 

62 self._storepath = storepath 

63 

64 def chopper( 

65 self, tmin=None, tmax=None, tinc=None, tpad=0., *args, **kwargs): 

66 

67 if tmin is None: 

68 tmin = self.base.tmin+tpad 

69 

70 if tmax is None: 

71 tmax = self.base.tmax-tpad 

72 

73 self._update_range(tmin, tmax) 

74 

75 return pile.Pile.chopper(self, tmin, tmax, tinc, tpad, *args, **kwargs) 

76 

77 def process(self, iblock, tmin, tmax, traces): 

78 return traces 

79 

80 def _update_range(self, tmin, tmax): 

81 imin = int(math.floor(tmin / self._tinc)) 

82 imax = int(math.floor(tmax / self._tinc)+1) 

83 

84 todo = [] 

85 for i in range(imin, imax): 

86 wmin = i * self._tinc 

87 wmax = (i+1) * self._tinc 

88 mtime = util.gmctime(self._base.get_newest_mtime(wmin, wmax)) 

89 if i not in self._blocks or self._blocks[i].mtime != mtime: 

90 if i not in self._blocks: 

91 self._blocks[i] = ShadowBlock() 

92 

93 todo.append(i) 

94 self._blocks[i].mtime = mtime 

95 else: 

96 if todo: 

97 self._process_blocks(todo[0], todo[-1]+1) 

98 todo = [] 

99 if todo: 

100 self._process_blocks(todo[0], todo[-1]+1) 

101 

102 def _process_blocks(self, imin, imax): 

103 pmin = imin * self._tinc 

104 pmax = imax * self._tinc 

105 

106 iblock = imin 

107 for traces in self._base.chopper(pmin, pmax, self._tinc, self._tpad): 

108 tmin = iblock*self._tinc 

109 tmax = (iblock+1)*self._tinc 

110 traces = self.process(iblock, tmin, tmax, traces) 

111 if self._tpad != 0.0: 

112 for trace in traces: 

113 trace.chop(tmin, tmax, inplace=True) 

114 self._clearblock(iblock) 

115 self._insert(iblock, traces) 

116 iblock += 1 

117 

118 def _insert(self, iblock, traces): 

119 if traces: 

120 if self._storepath is not None: 

121 fns = io.save( 

122 traces, self._storepath, 

123 format='mseed', 

124 additional={'iblock': iblock}) 

125 

126 self.load_files(fns, fileformat='mseed', show_progress=False) 

127 else: 

128 file = pile.MemTracesFile(None, traces) 

129 self.add_file(file) 

130 

131 def _clearblock(self, iblock): 

132 for file in self._blocks[iblock].files: 

133 self.remove_file(file)