コード例 #1
0
def optimization(*params, **args):
    counter = params[1]
    Config = params[2]
    Wdf = params[3]
    FilterMeta = params[4]
    mint = params[5]
    maxt = params[6]
    TTTGridMap = params[7]
    Folder = params[8]
    Origin = params[9]
    ntimes = params[10]
    switch = params[11]
    ev = params[12]
    arrayfolder = params[13]
    syn_in = params[14]
    data = params[15]
    evpath = params[16]
    XDict = params[17]
    RefDict = params[18]
    workdepth = params[19]
    filterindex = params[20]
    Wdfs = params[21]

    networks = Config['networks'].split(',')
    params = num.asarray(params)
    parameter = num.ndarray.tolist(params)
    ASL_syn = []


    C  = config.Config (evpath)
    Config = C.parseConfig ('config')
    cfg = ConfigObj (dict=Config)
    if cfg.pyrocko_download() == True:
        Meta = C.readpyrockostations()#

    elif cfg.colesseo_input() == True:
        scenario = guts.load(filename=cfg.colosseo_scenario_yml())
        scenario_path = cfg.colosseo_scenario_yml()[:-12]
        Meta = C.readcolosseostations(scenario_path)
    else:
        Meta = C.readMetaInfoFile()
    l = 0
    for i in networks:

        arrayname = i
        arrayfolder = os.path.join (Folder['semb'],arrayname)

        network = Config[i].split('|')

        FilterMeta = ttt.filterStations (Meta,Config,Origin,network)

        if len(FilterMeta)  < 3: continue

        W = XDict[i]
        refshift = RefDict[i]

        FilterMeta = cmpFilterMetavsXCORR (W, FilterMeta)

        Logfile.add ('BOUNDING BOX DIMX: %s  DIMY: %s  GRIDSPACING: %s \n'
                 % (Config['dimx'],Config['dimy'],Config['gridspacing']))

        f = open('../tttgrid/tttgrid_%s_%s_%s.pkl' % (ev.time, arrayname, workdepth), 'rb')
        TTTGridMap,mint,maxt = pickle.load(f)
        f.close()


        switch = filterindex

        tw  = times.calculateTimeWindows (mint,maxt,Config,ev, switch)
        Wdf = Wdfs[l]
        semb_syn = doCalc_syn (counter,Config,Wdf,FilterMeta,mint,maxt,TTTGridMap,
                                     Folder,Origin,ntimes,switch, ev,arrayfolder, syn_in,
                                      parameter[0])
        ASL_syn.append(semb_syn)
        counter += 1
        l += 1

    sembmax_syn = sembCalc.collectSemb(ASL_syn,Config,Origin,Folder,ntimes,len(networks),switch)

    misfit_list = []  # init a list for a all the singular misfits
    norm_list = []  # init a list for a all the singular normalizations
    taper = trace.CosFader(xfade=2.0)  # Cosine taper with fade in and out of 2s.
    bw_filter = trace.ButterworthResponse(corner=0.000055,  # in Hz
                                      order=4,
                                      type='high')  # "low"pass or "high"pass
    setup = trace.MisfitSetup(description='Misfit Setup',
                              norm=2,  # L1 or L2 norm
                              taper=taper,
                              filter=bw_filter,
                              domain='time_domain')
    nsamples = len(data)
    tmin = util.str_to_time('2010-02-20 15:15:30.100')
    tr = trace.Trace(station='TEST', channel='Z',
                     deltat=0.5, tmin=tmin, ydata=data)
    syn = trace.Trace(station='TEST', channel='Z',
                     deltat=0.5, tmin=tmin, ydata=sembmax_syn)
    misfit, norm = tr.misfit(candidate=syn, setup=setup) # calculate the misfit of a single observed trace with its synthetics
    # with the setup from above
    misfit_list.append(misfit), norm_list.append(norm)  # append the misfit into a list
    global_misfit_normed = num.sqrt(num.nansum((num.asarray(misfit_list))**2) / # sum all the misfits and normalize to get a single minimizable value
                                    num.nansum((num.asarray(norm_list))**2))
    return global_misfit_normed
コード例 #2
0
def optimization(*params, **args):
    counter = params[1]
    Config = params[2]
    Wdf = params[3]
    FilterMeta = params[4]
    mint = params[5]
    maxt = params[6]
    TTTGridMap = params[7]
    Folder = params[8]
    Origin = params[9]
    ntimes = params[10]
    switch = params[11]
    ev = params[12]
    arrayfolder = params[13]
    syn_in = params[14]
    data = params[15]

    params = num.asarray(params)
    parameter = num.ndarray.tolist(params)
    #    parameter = [val for sublist in parameter for val in sublist]

    semb_syn = doCalc_syn(counter, Config, Wdf, FilterMeta, mint, maxt,
                          TTTGridMap, Folder, Origin, ntimes, switch, ev,
                          arrayfolder, syn_in, parameter[0])
    misfit_list = []  # init a list for a all the singular misfits
    norm_list = []  # init a list for a all the singular normalizations
    taper = trace.CosFader(
        xfade=2.0)  # Cosine taper with fade in and out of 2s.
    bw_filter = trace.ButterworthResponse(
        corner=0.000055,  # in Hz
        order=4,
        type='high')  # "low"pass or "high"pass
    setup = trace.MisfitSetup(
        description='Misfit Setup',
        norm=2,  # L1 or L2 norm
        taper=taper,
        filter=bw_filter,
        domain='time_domain')
    for t_data, t_syn in zip(data, semb_syn):
        nsamples = len(t_data)
        tmin = util.str_to_time('2010-02-20 15:15:30.100')
        tr = trace.Trace(station='TEST',
                         channel='Z',
                         deltat=0.5,
                         tmin=tmin,
                         ydata=t_data)
        syn = trace.Trace(station='TEST',
                          channel='Z',
                          deltat=0.5,
                          tmin=tmin,
                          ydata=t_syn)
        misfit, norm = tr.misfit(
            candidate=syn, setup=setup
        )  # calculate the misfit of a single observed trace with its synthetics
        # with the setup from above
        misfit_list.append(misfit), norm_list.append(
            norm)  # append the misfit into a list
    global_misfit_normed = num.sqrt(
        num.nansum((num.asarray(misfit_list))**2)
        /  # sum all the misfits and normalize to get a single minimizable value
        num.nansum((num.asarray(norm_list))**2))
    print global_misfit_normed
    return global_misfit_normed
コード例 #3
0
ファイル: misfit.py プロジェクト: shineusn/pyrocko
ydata2 = num.random.random(1000)
rt = trace.Trace(station='REF', ydata=ydata1)
candidate1 = trace.Trace(station='TT1', ydata=ydata1)
candidate2 = trace.Trace(station='TT2', ydata=ydata2)

# Define a fader to apply before fft.
taper = trace.CosFader(xfade=5)

# Define a frequency response to apply before performing the inverse fft.
# This can be basically any funtion, as long as it contains a function called
# *evaluate*, which evaluates the frequency response function at a given list
# of frequencies.
# Please refer to the :py:class:`FrequencyResponse` class or its subclasses for
# examples.
# However, we are going to use a butterworth low-pass filter in this example.
bw_filter = trace.ButterworthResponse(corner=2, order=4, type='low')

# Combine all information in one misfit setup:
setup = trace.MisfitSetup(description='An Example Setup',
                          norm=2,
                          taper=taper,
                          filter=bw_filter,
                          domain='time_domain')

# Calculate misfits of each candidate against the reference trace:
for candidate in [candidate1, candidate2]:
    misfit = rt.misfit(candidate=candidate, setup=setup)
    print 'misfit: %s, normalization: %s' % misfit

# Finally, dump the misfit setup that has been used as a yaml file for later
# re-use:
コード例 #4
0
ファイル: waveform.py プロジェクト: simondaout/GSTI
    def __init__(self,
                 network,
                 reduction,
                 wdir,
                 event,
                 weight=1.,
                 phase='P',
                 component='Z',
                 filter_corner=0.055,
                 filter_order=4,
                 filter_type='low',
                 misfit_norm=2,
                 taper_fade=2.0,
                 base=0,
                 sig_base=0,
                 extension='',
                 dist='Unif'):

        self.network = network
        self.reduction = reduction
        self.wdir = wdir
        self.event = event
        self.phase = phase
        self.component = component
        self.filter_corner = filter_corner
        self.filter_order = filter_order
        self.filter_type = filter_type
        self.misfit_norm = misfit_norm
        self.taper_fade = taper_fade
        self.sigmad = 1. / weight

        self.base = base
        self.sig_base = sig_base
        self.Mbase = 1

        self.extension = extension
        self.dist = dist

        self.taper = trace.CosFader(
            xfade=self.taper_fade)  # Cosine taper with fade in and out of 2s.
        self.bw_filter = trace.ButterworthResponse(
            corner=self.filter_corner,  # in Hz
            order=self.filter_order,
            type=self.filter_type)  # "low"pass or "high"pass

        self.setup = trace.MisfitSetup(
            description='Misfit Setup',
            norm=2,  # L1 or L2 norm
            taper=self.taper,
            filter=self.bw_filter,
            domain='time_domain')  # Possible domains are:
        # time_domain, cc_max_norm (correlation)
        # and frequency_domain
        self.events = []
        self.events.extend(model.load_events(filename=wdir + self.event))
        origin = gf.Source(lat=np.float(self.events[0].lat),
                           lon=np.float(self.events[0].lon))
        # print util.time_to_str(events[0].time)

        self.base_source = gf.MTSource.from_pyrocko_event(self.events[0])
        self.base_source.set_origin(origin.lat, origin.lon)
        # print util.time_to_str(self.base_source.time), events[0].lon, events[0].lat
        # sys.exit()
        self.type = 'Waveform'
コード例 #5
0
    def call(self):
        self.cleanup()
        viewer = self.get_viewer()
        events = [m.get_event() for m in self.get_selected_event_markers()]
        for iev, ev in enumerate(events):
            ev.name = '%05i' % iev


        show_arrivals = False

        filters = []
        for ident in ['high', 'low']:
            val = getattr(self, ident)
            if val != None:
                filters.append(trace.ButterworthResponse(corner=float(val),
                                                         order=4,
                                                         type=ident))

        stations = self.get_stations()
        traces = list(self.chopper_selected_traces(fallback=True, trace_selector=
                                                   viewer.trace_selector,
                                                   load_data=False))
        traces = [tr for trs in traces for tr in trs ]
        visible_nslcs = [tr.nslc_id for tr in traces]
        stations = [x for x in stations if util.match_nslcs(
            "%s.%s.%s.*" % x.nsl(), visible_nslcs)]

        # TODO option to choose other models
        mod = cake.load_model()
        nevents = len(events)
        pile = self.get_pile()
        targets = make_targets(pile, stations)
        if len(targets)==0:
            self.fail("No station available")
        ntargets = len(targets)
        self.cc = num.zeros((ntargets, nevents, nevents), dtype=num.float)
        self.similarity_matrix = SimilarityMatrix(targets=targets,
                                                  events=events,
                                                  filters=filters,
                                                  padding=float(self.tpad),
                                                  windowing_method=self.time_window_choice,
                                                  vmax=float(self.vmax),
                                                  vmin=float(self.vmin))
        similarities = []
        if self.save_traces :
            figure_dir = self.input_directory(caption='Select directory to store images')
        for itarget, target in enumerate(targets):
            print((itarget+1.)/float(ntargets))
            ok_filtered = []
            markers = []
            for iev, ev in enumerate(events):
                dist = target.distance_to(ev)
                if self.time_window_choice=='vmin/vmax':
                    tmin = ev.time + dist / self.vmax - self.tpad
                    tmax = ev.time + dist / self.vmin + self.tpad
                elif self.time_window_choice=='P-phase':
                    d = dist*cake.m2d
                    z = ev.depth
                    t = self.phase_cache.get((mod, d, z), False)
                    if not t:
                        rays = mod.arrivals(
                            phases=[cake.PhaseDef(x) for x in 'p P'.split()],
                            distances=[d],
                            zstart=z)
                        t = rays[0].t
                        self.phase_cache[(mod, d, z)] = t
                    tmin = ev.time + t - self.tpad * 0.1
                    tmax = ev.time + t + self.tpad * 0.9
                trs = pile.chopper(tmin=tmin,
                                   tmax=tmax,
                                   trace_selector=viewer.trace_selector,
                                   want_incomplete=False)
                tr = [t for trss  in trs for t in trss if t.nslc_id==target.codes]
                if len(tr)==0:
                    continue
                elif len(tr)==1:
                    tr = tr[0]
                else:
                    self.fail('Something went wrong')
                if self.dt_wanted:
                    tr.downsample_to(self.dt_wanted)

                tr2 = tr.copy()
                for f in filters:
                    tr2 = tr2.transfer(transfer_function=f)
                tr2.chop(tmin, tmax)
                tr2.set_codes(location=ev.name+'f')

                tr.chop(tmin, tmax)
                tr.set_codes(location=ev.name+'r')

                ok_filtered.append((iev, ev, tr2))

            ok = ok_filtered
            while ok:
                (ia, a_ev, a_tr) = ok.pop()
                for (ib, b_ev, b_tr) in ok:
                    relamp = 0.0
                    if a_tr is not None and b_tr is not None:
                        c_tr = trace.correlate(a_tr, b_tr, mode='full', normalization='normal')
                        t_center = c_tr.tmin+(c_tr.tmax-c_tr.tmin)/2.
                        c_tr_chopped = c_tr.chop(t_center-self.tdist, t_center+self.tdist, inplace=False)
                        t_mini, v_mini = c_tr_chopped.min()
                        t_maxi, v_maxi = c_tr_chopped.max()
                        b_tr_shifted = b_tr.copy()

                        if abs(v_mini) > abs(v_maxi):
                            v_cc = v_mini
                            time_lag = -t_mini
                        else:
                            time_lag = -t_maxi
                            v_cc = v_maxi

                        self.cc[itarget, ia, ib] = v_cc
                        b_tr_shifted.shift(time_lag)

                        if self.cc[itarget, ia, ib] != 0.0:
                            tmin = max(a_tr.tmin, b_tr_shifted.tmin)
                            tmax = min(a_tr.tmax, b_tr_shifted.tmax)
                            try:
                                a_tr_chopped = a_tr.chop(tmin, tmax, inplace=False)
                                b_tr_chopped = b_tr_shifted.chop(tmin, tmax)
                            except trace.NoData:
                                logger.warn('NoData %s'%a_tr_chopped)
                                continue

                            ya = a_tr_chopped.ydata
                            yb = b_tr_chopped.ydata
                            relamp = num.sum(ya*yb) / num.sum(ya**2)

                        if self.save_traces:
                            fig, axes = plt.subplots(3,1)
                            fig.suptitle('.'.join(target.codes))
                            axes[0].plot(a_tr_chopped.get_xdata(), a_tr_chopped.get_ydata())
                            axes[0].text(0, 1, "id: %s, time: %s" %(a_ev.name, util.time_to_str(a_ev.time)),
                                         transform=axes[0].transAxes,
                                         verticalalignment='top', horizontalalignment='left')
                            axes[1].plot(b_tr_chopped.get_xdata(), b_tr_chopped.get_ydata())
                            axes[1].text(0, 1, "id: %s, time: %s" %(b_ev.name, util.time_to_str(b_ev.time)),
                                         transform=axes[1].transAxes,
                                         verticalalignment='top', horizontalalignment='left')
                            axes[2].plot(c_tr.get_xdata(), c_tr.get_ydata())
                            axes[2].text(0, 1, 'cc_max: %1.4f' % v_cc,
                                         transform=axes[2].transAxes,
                                         verticalalignment='top', horizontalalignment='left')
                            fn = op.join(figure_dir, 'cc_T%s.E%s.E%s.png' % (itarget, ia, ib))
                            fig.savefig(fn, pad_inches=0.1, bbox_inches='tight', tight_layout=True)

                        sim = Similarity(
                            ievent=ia,
                            jevent=ib,
                            itarget=itarget,
                            cross_correlation=float(self.cc[itarget, ia, ib]),
                            relative_amplitude=float(relamp),
                            time_lag=float(-time_lag))

                        similarities.append(sim)

        if self.show_results:
            for itarget, target in enumerate(targets):
                if not num.any(self.cc[itarget]):
                    continue
                fig = self.pylab(get='figure')
                fig.suptitle('.'.join(target.codes))
                axes = fig.add_subplot(111)
                axes.set_xlabel('Event number')
                axes.set_ylabel('Event number')
                mesh = axes.pcolormesh(self.cc[itarget,:,:], cmap='RdBu', vmin=-1.0, vmax=1.0)
                cb = fig.colorbar(mesh, ax=axes)
                cb.set_label('Max correlation coefficient')
                fig.canvas.draw()

        self.similarity_matrix.similarities = similarities
        self.similarity_matrix.validate()
コード例 #6
0
ファイル: waveform.py プロジェクト: braunfuss/silvertine
def plot_waveforms(traces, event, stations, savedir, picks, show=True):
    fig = plt.figure(figsize=plot.mpl_papersize('a4', 'landscape'))
    tap_color_annot = (0.35, 0.35, 0.25)
    tap_color_edge = (0.85, 0.85, 0.80)
    waveform_color = scolor('aluminium5')
    misfit_color = scolor('scarletred1')
    ncomps = 3
    k = 0
    nstations = len(stations)
    ntraces = nstations * ncomps
    i = 0
    for st in stations:
        for comp in st.channels:
            for tr in traces:
                if tr.station == st.station:
                    if comp.name == tr.channel:
                        #    tr.downsample_to(0.05)
                        #    tr.highpass(4, 0.01)
                        #    tr.lowpass(4, 0.2)
                        dtrace = tr
                        i = i + 1
            target = st

            tmin_fit = dtrace.tmin
            tmax_fit = dtrace.tmax

            tfade_taper = 1. / 0.2

            taper = trace.CosTaper(tmin_fit - 20, tmin_fit, tmax_fit,
                                   tmax_fit + 30)
            k = k + 1
            axes2 = fig.add_subplot(nstations / 3, nstations / 3, k)
            space = 0.5
            space_factor = 1.0 + space
            axes2.set_axis_off()
            axes2.set_ylim(-1.05 * space_factor, 1.05)

            axes = axes2.twinx()
            axes.set_axis_off()

            bw_filter = trace.ButterworthResponse(corner=2,
                                                  order=4,
                                                  type='low')

            setup = trace.MisfitSetup(description='setup',
                                      norm=2,
                                      taper=taper,
                                      filter=bw_filter,
                                      domain='time_domain')

            abs_tr = dtrace.copy()
            abs_tr.set_ydata(abs(dtrace.get_ydata()))

            plot_cc(axes2,
                    abs_tr,
                    space,
                    0.,
                    num.max(abs_tr.get_ydata()),
                    fc=light(misfit_color, 0.3),
                    ec=misfit_color,
                    zorder=4)

            plot_trace(axes, dtrace, color=waveform_color, lw=0.5, zorder=5)

            tmarks = [dtrace.tmin, dtrace.tmax]

            for tmark in tmarks:
                axes2.plot([tmark, tmark], [-0.9, 0.1], color=tap_color_annot)

            for tmark, text, ha, va in [
                (tmarks[0], '$\,$ ' + str_duration(tmarks[0]), 'left',
                 'bottom'),
                (tmarks[1], '$\Delta$ ' + str_duration(tmarks[1] - tmarks[0]),
                 'right', 'bottom')
            ]:
                axes2.annotate(text,
                               xy=(tmark, -0.9),
                               xycoords='data',
                               xytext=(fontsize * 0.4 * [-1, 1][ha == 'left'],
                                       fontsize * 0.2),
                               textcoords='offset points',
                               ha=ha,
                               va=va,
                               color=tap_color_annot,
                               fontsize=fontsize,
                               zorder=10)

            if picks is not None:
                for stp in picks["phases"]:
                    phases_station = []
                    picks_station = []
                    if st.station == stp["station"]:
                        phases_station.append(str(stp["phase"]))
                        picks_station.append(event.time + float(stp["pick"]))
                        picks_station.append(event.time)

                    tmarks = picks_station

                    for tmark in tmarks:
                        axes2.plot([tmark, tmark], [-1, 1.], color="blue")

                    for tmark, text, ha, va in [(tmarks, phases_station,
                                                 'left', 'bottom')]:
                        try:
                            axes2.annotate(
                                text[0],
                                xy=(tmark[0], -1.2),
                                xycoords='data',
                                xytext=(8 * 0.4 * [-1, 1][ha == 'left'],
                                        8 * 0.2),
                                textcoords='offset points',
                                ha=ha,
                                va=va,
                                color=tap_color_annot,
                                fontsize=8,
                                zorder=10)
                        except:
                            pass
            infos = []

            infos.append(target.network + "." + target.station + "." +
                         dtrace.channel)
            dist = event.distance_to(target)
            azi = event.azibazi_to(target)[0]
            infos.append(str_dist(dist))
            infos.append(u'%.0f\u00B0' % azi)

            axes2.annotate('\n'.join(infos),
                           xy=(0., 1.),
                           xycoords='axes fraction',
                           xytext=(2., 2.),
                           textcoords='offset points',
                           ha='left',
                           va='top',
                           fontsize=fontsize,
                           fontstyle='normal')
            if i / nstations == 1 or i / nstations == 2 or i / nstations == 3:
                fig.savefig(savedir +
                            "waveforms_%s.png" % str(int(i / nstations)),
                            dpi=100)

                if show is True:
                    plt.show()
                else:
                    plt.close()
                fig = plt.figure(figsize=plot.mpl_papersize('a4', 'landscape'))
                k = 0