snuffling.py 4.2 KB
Newer Older
1
2
3
4
5
6
7
import copy
import numpy as num
from pyrocko import util, gui_util, model, orthodrome, pile, snuffler
from pyrocko.snuffling import Snuffling, Param, Choice, Switch


class LassieSnuffling(Snuffling):
Marius Kriegerowski's avatar
Marius Kriegerowski committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

    @property
    def __doc__(self):
        s = '''
        <html>
        <head>
        <style type="text/css">
            body { margin-left:10px };
        </style>
        </head>
        <body>
        <h2 style="test-align:center"> Scrutinize Lassie Detections and
        Re-Detect</h2>
        <p>
        Adjust the detector <i>threshold</i>, press <i>run</i>. From every 
        instant, where the signal rises above <i>threshold</i>, a time length 
        of <i>tsearch</i> seconds is searched for a maximum. Detections are
        added as event markers to the viewer.  </p>

        <p>
        If you want to save the updated detections, it might be helpful to use the marker table 
        (see <a href="http://pyrocko.org/v0.3/snuffler_tutorial.html"> Snuffler
        Tutorial</a> at the bottom) to sort all markers by their kind.
        </p>
        </body>
        </html>

        '''
        return s

38
39
40
41
42
43
44
45
46
47
    def __init__(self):
        Snuffling.__init__(self)
        self.config = None

    def setup(self):
        if self.config:
            detector_default = self.config.detector_threshold
        else:
            detector_default = 100.
        self.set_name('Lassie investigate')
Marius Kriegerowski's avatar
Marius Kriegerowski committed
48
        self.add_parameter(Param('Tsearch', 'tsearch', 1., 0.01, 100))
49
        self.add_parameter(Param(
Marius Kriegerowski's avatar
Marius Kriegerowski committed
50
            'Threshold', 'detector_threshold', detector_default, 100., 10000.))
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
        self.add_parameter(Switch('Level Trace', 'level_trace', False))
        self.add_parameter(Choice(
            'new marker kind', 'marker_kind', '1 (green)',
            ['1 (green)', '2 (blue)', '3 (orange)', '4 (purple)', '5 (brown)',
             '0 (red)']))
        self.set_live_update(False)

    def call(self):
        self.cleanup()
        self.detections = []
        for traces in self.chopper_selected_traces(
                trace_selector=lambda x: x.station == "SMAX",
                fallback=True):
            for tr_stackmax in traces:
                tpeaks, apeaks = tr_stackmax.peaks(
                    self.detector_threshold, self.tsearch)
                if self.level_trace:
                    ltrace = tr_stackmax.copy(data=False)
                    ltrace.set_ydata(
                        num.ones(
                            tr_stackmax.data_len()) * self.detector_threshold)
                    self.add_trace(ltrace)
                for t, a in zip(tpeaks, apeaks):
                    e = model.Event(time=t, name=str(a))
                    self.detections.append(
                        gui_util.EventMarker(
                            event=e, kind=int(self.marker_kind[0])))

        self.add_markers(self.detections)


def __snufflings__():
    return [LassieSnuffling()]


def snuffle(config):
    global _lassie_config
    _lassie_config = copy.deepcopy(config)

    def load_snuffling(win):
        s = LassieSnuffling()
        s.config = _lassie_config
        s.setup()
        win.pile_viewer.viewer.add_snuffling(s)

    receivers = config.get_receivers()
    stations = set()
    for r in receivers:
        n, s, l = r.codes[:3]
        stations.add(model.Station(lat=r.lat, lon=r.lon,
                                   network=n, station=s, location=l))

    markers = []
    with open(config.detections_path, 'r') as f:
        for line in f.readlines():
            data = line.split()
            i, t_d, t_t, apeak, latpeak, lonpeak, xpeak, ypeak, zpeak = data
            lat, lon = orthodrome.ne_to_latlon(
                float(latpeak), float(lonpeak), float(ypeak), float(xpeak))
            t = util.str_to_time("%s %s" % (t_d, t_t))
            label = "%s-%s" % (i, apeak)
            e = model.Event(lat=lat, lon=lon, depth=float(zpeak)*1000.,
                            name=label, time=t)
            m = gui_util.EventMarker(e)
            markers.append(m)

    paths = config.data_paths
    paths.append('stackmax')
    p = pile.make_pile(paths=paths, fileformat='detect')
    snuffler.snuffle(p, markers=markers, stations=stations,
                     launch_hook=load_snuffling)


__all__ = [
    'snuffle']