plot.py 8.41 KB
Newer Older
Sebastian Heimann's avatar
Sebastian Heimann committed
1
2
3
import time
import numpy as num
from pyrocko import automap
4
from lassie import grid as gridmod, geo
Sebastian Heimann's avatar
Sebastian Heimann committed
5
6
7
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52


def map_gmt(
        receivers, grid, center_lat, center_lon, radius, output_path,
        width=25., height=25.,
        show_station_labels=False):

    station_lats = num.array([r.lat for r in receivers])
    station_lons = num.array([r.lon for r in receivers])

    map = automap.Map(
        width=width,
        height=height,
        lat=center_lat,
        lon=center_lon,
        radius=radius,
        show_rivers=False,
        show_topo=False,
        illuminate_factor_land=0.35,
        color_dry=(240, 240, 235),
        topo_cpt_wet='white_sea_land',
        topo_cpt_dry='white_sea_land')

    map.gmt.psxy(
        in_columns=(station_lons, station_lats),
        S='t8p',
        G='black',
        *map.jxyr)

    if grid:
        map.gmt.psxy(
            in_columns=(grid.lons, grid.lats),
            S='c1p',
            G='black',
            *map.jxyr)

    if show_station_labels:
        for r in receivers:
            map.add_label(r.lat, r.lon, '%s' % r.station)

    map.save(output_path)


def map_geometry(config, output_path):
    receivers = config.get_receivers()
    grid = config.get_grid()

    lat0, lon0, north, east, depth = geo.bounding_box_square(
53
        *geo.points_coords(receivers),
Sebastian Heimann's avatar
Sebastian Heimann committed
54
55
56
57
58
59
60
61
62
63
64
        scale=config.autogrid_radius_factor)

    radius = max((north[1] - north[0]), (east[1] - east[0]))

    radius *= config.autogrid_radius_factor * 1.5

    map_gmt(receivers, grid, lat0, lon0, radius, output_path,
            show_station_labels=False)


def plot_receivers(axes, receivers, system='latlon'):
65
    x, y = geo.points_coords(receivers, system=system)
Sebastian Heimann's avatar
Sebastian Heimann committed
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
    axes.plot(y, x, '^', color='black', ms=10.)


def plot_geometry_carthesian(grid, receivers):

    from matplotlib import pyplot as plt
    from pyrocko.cake_plot import mpl_init, labelspace

    mpl_init()

    plt.figure(figsize=(9, 9))
    axes = plt.subplot2grid((1, 1), (0, 0), aspect=1.0)
    labelspace(axes)

    grid.plot_points(axes, system=('ne', grid.lat, grid.lon))
    plot_receivers(axes, receivers, system=('ne', grid.lat, grid.lon))

    distances = grid.distances(receivers)
    delta_grid = max(grid.dx, grid.dy, grid.dz)
    norm_map = gridmod.geometrical_normalization(distances, delta_grid)
    grid.plot(axes, norm_map, system=('ne', grid.lat, grid.lon))

    plt.show()


def plot_detection(
        grid, receivers, frames, tmin_frames, deltat_cf, imax, iframe,
        fsmooth_min, xpeak, ypeak, zpeak, tr_stackmax, tpeaks, apeaks,
        detector_threshold, pdata, trs_raw, fmin, fmax, idetection,
        grid_station_shift_max,
        movie=False):

    from matplotlib import pyplot as plt
    from matplotlib import cm
    from pyrocko.cake_plot import mpl_init, labelspace, colors, \
        str_to_mpl_color as scolor

    distances = grid.distances(receivers)

    mpl_init()

    fig = plt.figure(figsize=(16, 9))

    axes = plt.subplot2grid((2, 3), (0, 2), aspect=1.0)
    labelspace(axes)

    axes2 = plt.subplot2grid((2, 3), (1, 2))
    labelspace(axes2)

    axes3 = plt.subplot2grid((2, 3), (0, 1), rowspan=2)
    axes4 = plt.subplot2grid((2, 3), (0, 0), rowspan=2)

    axes.set_xlabel('X [m]')
    axes.set_ylabel('Y [m]')
    axes.locator_params(nbins=6, tight=True)

    axes2.set_xlabel('Time [s]')
    axes2.set_ylabel('Detector level')

    axes3.set_xlabel('Time [s]')
    for el in axes3.get_yticklabels():
        el.set_visible(False)

    axes4.set_xlabel('Time [s]')
    axes4.set_ylabel('Distance [m]')

    tpeak_current = tmin_frames + deltat_cf * iframe
    t0 = tpeak_current
    tduration = 2.0*grid_station_shift_max + 1./fsmooth_min

    for tpeak, apeak in zip(tpeaks, apeaks):
        axes2.axvline(
            tpeak-t0,
            color=scolor('aluminium3'),
            lw=2.)

    axes2.axvspan(
        tpeak_current-0.5*tduration - t0,
        tpeak_current+0.5*tduration - t0,
        color=scolor('aluminium2'),
        lw=2.)

    axes2.axhline(
        detector_threshold,
        color=scolor('aluminium6'),
        lw=2.)

    t = tr_stackmax.get_xdata()
    amp = tr_stackmax.get_ydata()
    axes2.plot(t - t0, amp, color=scolor('scarletred2'), lw=2.)

    for tpeak, apeak in zip(tpeaks, apeaks):
        axes2.plot(
            tpeak-t0, apeak, '*',
            ms=20.,
            mfc='white',
            mec='black')

    station_index = dict(
        (rec.codes, i) for (i, rec) in enumerate(receivers))

    dists = []
    amps = []
    shifts = []
    pdata2 = []
    for trs, shift_table, shifter in pdata:
        trs = [tr.copy() for tr in trs]
        for tr in trs:
            istation = station_index[tr.nslc_id[:3]]
            shift = shift_table[imax, istation]
            tr2 = tr.chop(
                tpeak_current - 0.5*tduration + shift,
                tpeak_current + 0.5*tduration + shift,
                inplace=False)

            dists.append(distances[imax, istation])
            amp = tr2.get_ydata() * shifter.weight
            amps.append(num.max(num.abs(amp)))
            shifts.append(shift)

        pdata2.append((trs, shift_table, shifter))

    dist_min = min(dists)
    dist_max = max(dists)

    shift_min = min(shifts)
    shift_max = max(shifts)

    amp_max = max(amps)

    scalefactor = (dist_max - dist_min) / len(trs) * 0.5

    axes3.set_xlim(-0.5*tduration + shift_min, 0.5*tduration + shift_max)
    axes3.set_ylim(dist_min - scalefactor, dist_max + scalefactor)

    axes4.set_xlim(-0.5*tduration + shift_min, 0.5*tduration + shift_max)
    axes4.set_ylim(dist_min - scalefactor, dist_max + scalefactor)

    axes3.axvline(
        0.,
        color=scolor('aluminium3'),
        lw=2.)

    for ishifter, (trs, shift_table, shifter) in enumerate(pdata2):
        color = colors[ishifter % len(colors)]

        for tr, dist in zip(trs, dists):
            tr = tr.chop(
                tpeak_current - 0.5*tduration + shift_min,
                tpeak_current + 0.5*tduration + shift_max, inplace=False)

            istation = station_index[tr.nslc_id[:3]]
            shift = shift_table[imax, istation]
            print ishifter, shift
            axes3.plot(shift, dist, '|', mew=2, mec=color, ms=10)
            t = tr.get_xdata()
            amp = tr.get_ydata() * shifter.weight
            amp /= amp_max
            axes3.plot(
                t-t0,
                dist + scalefactor*amp + ishifter*scalefactor*0.1,
                color=color)

    for tr in trs_raw:
        istation = station_index[tr.nslc_id[:3]]
        dist = distances[imax, istation]
        shift = shift_table[imax, istation]

        tr = tr.copy()

        tr.highpass(4, fmin, demean=True)
        tr.lowpass(4, fmax, demean=False)

        tr.chop(
            tpeak_current - 0.5*tduration + shift_min,
            tpeak_current + 0.5*tduration + shift_max)

        t = tr.get_xdata()
        amp = tr.get_ydata().astype(num.float)
        amp = amp / num.max(num.abs(amp))
        axes4.plot(t-t0, dist + scalefactor*amp, color='black')
        axes4.plot(shift, dist, '|', mew=2, mec=color, ms=10)

    nframes = frames.shape[1]

    iframe_min = max(0, int(round(iframe - 0.5*tduration/deltat_cf)))
    iframe_max = min(nframes-1, int(round(iframe + 0.5*tduration/deltat_cf)))

    amax = frames[imax, iframe]

256
257
258
    axes.set_xlim(grid.ymin, grid.ymax)
    axes.set_ylim(grid.xmin, grid.xmax)

Sebastian Heimann's avatar
Sebastian Heimann committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
    cmap = cm.YlOrBr
    system = ('ne', grid.lat, grid.lon)
    if movie:
        plt.ion()
        plt.show()

        for iframe in xrange(iframe_min, iframe_max+1):
            frame = frames[:, iframe]
            axes.cla()
            grid.plot(
                axes, frame,
                amin=0.0,
                amax=amax,
                cmap=cmap,
                system=system)

            plot_receivers(axes, receivers, system=system)
            axes.plot(
                ypeak, xpeak, '*',
                ms=20.,
                mfc='white',
                mec='black')

            plt.draw()
            time.sleep(0.05)

        plt.ioff()

    else:
        frame = num.max(frames[:, iframe_min:iframe_max+1], axis=1)
        grid.plot(axes, frame, amin=0.0, amax=amax, cmap=cmap, system=system)
290

Sebastian Heimann's avatar
Sebastian Heimann committed
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
        plot_receivers(axes, receivers, system=system)
        axes.plot(
            ypeak, xpeak, '*',
            ms=20.,
            mec='black',
            mfc='white')

        fig.tight_layout()

        plt.show()


__all__ = [
    'map_geometry',
    'map_gmt',
    'plot_detection',
    'plot_geometry_carthesian',
    'plot_receivers',
]