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 2024-03-07 11:54 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2024-03-07 11:54 +0000
1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6'''
7Lazy block-wise processing based on :py:mod:`pyrocko.pile` (*deprecated*).
8'''
10import math
11import logging
13from . import pile, util, io
15logger = logging.getLogger('pyrocko.shadow_pile')
18class NoBasePileSet(Exception):
19 pass
22class ShadowBlock(object):
23 def __init__(self):
24 self.mtime = None
25 self.files = []
28class ShadowPile(pile.Pile):
30 def __init__(self, basepile=None, tinc=360., tpad=0., storepath=None):
31 pile.Pile.__init__(self)
33 self._tinc = tinc
34 self._tpad = tpad
35 self._storepath = storepath
36 self._blocks = {}
38 if basepile is None:
39 basepile = pile.Pile()
41 self.set_basepile(basepile)
43 def clear(self):
44 for iblock in self._blocks.keys():
45 self._clearblock()
46 self._blocks = {}
48 def set_basepile(self, basepile):
49 self.clear()
50 self._base = basepile
52 def get_basepile(self):
53 return self._base
55 def set_chopsize(self, tinc, tpad=0.):
56 self.clear()
57 self._tinc = tinc
58 self._tpad = tpad
60 def set_store(self, storepath=None):
61 self.clear()
62 self._storepath = storepath
64 def chopper(
65 self, tmin=None, tmax=None, tinc=None, tpad=0., *args, **kwargs):
67 if tmin is None:
68 tmin = self.base.tmin+tpad
70 if tmax is None:
71 tmax = self.base.tmax-tpad
73 self._update_range(tmin, tmax)
75 return pile.Pile.chopper(self, tmin, tmax, tinc, tpad, *args, **kwargs)
77 def process(self, iblock, tmin, tmax, traces):
78 return traces
80 def _update_range(self, tmin, tmax):
81 imin = int(math.floor(tmin / self._tinc))
82 imax = int(math.floor(tmax / self._tinc)+1)
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()
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)
102 def _process_blocks(self, imin, imax):
103 pmin = imin * self._tinc
104 pmax = imax * self._tinc
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
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})
126 self.load_files(fns, fileformat='mseed', show_progress=False)
127 else:
128 file = pile.MemTracesFile(None, traces)
129 self.add_file(file)
131 def _clearblock(self, iblock):
132 for file in self._blocks[iblock].files:
133 self.remove_file(file)