Beispiel #1
0
    def testMisfitBox(self):

        ydata = num.zeros(9)
        ydata[3:6] = 1.0

        deltat = 0.5

        tr = trace.Trace(ydata=ydata, deltat=0.5, tmin=0.0)

        tshiftmin = -(tr.tmax - tr.tmin) * 2
        tshiftmax = (tr.tmax - tr.tmin) * 2

        tshifts = num.linspace(
            tshiftmin, tshiftmax,
            int(round((tshiftmax - tshiftmin) / deltat)) + 1)

        candidates = []
        for tshift in tshifts:
            trc = tr.copy()
            trc.shift(tshift)
            candidates.append(trc)

        trace.MisfitSetup(norm=2,
                          taper=trace.CosTaper(tr.tmin, tr.tmin, tr.tmax,
                                               tr.tmax),
                          domain='time_domain')
Beispiel #2
0
    def testMisfitOfSameTracesZero(self):
        y = num.random.random(10000)
        y -= max(y) * 0.5
        t1 = trace.Trace(tmin=0, ydata=y, deltat=0.01)
        t2 = trace.Trace(tmin=0, ydata=y, deltat=0.01)
        # ttraces = [t2]
        fresponse = trace.FrequencyResponse()
        taper = trace.CosFader(xfade=2.)
        norms = [1, 2]
        domains = ['time_domain', 'frequency_domain', 'envelope', 'absolute']
        setups = [
            trace.MisfitSetup(norm=n,
                              taper=taper,
                              domain=domain,
                              filter=fresponse) for domain in domains
            for n in norms
        ]

        for setup in setups:
            m, n, tr, tc = t1.misfit(candidate=t2, setup=setup, debug=True)
            if isinstance(tr, trace.Trace):
                self.assertEqual(tr.tmin, tc.tmin)
                self.assertTrue(all(tr.get_ydata() == tc.get_ydata()))
            else:
                self.assertTrue(all(tc == tr))

            self.assertEqual(m, 0., 'misfit\'s m of equal traces is != 0')
Beispiel #3
0
    def testMisfitOfSameTracesDtDifferentShifted(self):
        """
        Tests:
            Different length
            Different delta t
            Shifted
            L2-Norm
            L1-Norm
            time- and frequency-domain 
        """
        test_file = os.path.join(os.path.dirname(__file__),
                                 '../examples/1989.072.evt.mseed')
        p = pile.make_pile(test_file, show_progress=False)
        rt = p.all()[0]
        tt = rt.copy()

        # make downsampled, chopped copies:
        deltats = [0.5, 1.0, 2.0]
        tt.chop(tmin=rt.tmin + 10, tmax=rt.tmax - 15)
        tts = [tt.copy() for i in range(len(deltats))]
        [t.downsample_to(deltats[i]) for i, t in enumerate(tts)]

        # shift traces:
        t_shifts = [1.0, 0.49999, 0.5]
        for ts in t_shifts:
            tts_shifted = [t.copy() for t in tts]
            map(lambda x: x.shift(ts), tts_shifted)
            tts.extend(tts_shifted)

        a = rt.tmin
        d = rt.tmax
        b = a + (d - a) / 10
        c = d - (d - a) / 10

        taper = trace.CosTaper(a, b, c, d)
        fresponse = trace.FrequencyResponse()
        norms = [1, 2]
        domains = ['time_domain', 'frequency_domain', 'envelope', 'absolute']
        setups = [
            trace.MisfitSetup(norm=n,
                              taper=taper,
                              domain=domain,
                              filter=fresponse) for domain in domains
            for n in norms
        ]

        for cand in tts:
            for setup in setups:
                m, n = rt.misfit(candidate=cand, setup=setup)
                self.assertNotEqual(m, None, 'misfit\'s m is None')
Beispiel #4
0
    def __init__(self, markers, stations=None):
        # Targets================================================
        store_id = 'castor'

        if store_id=='local1':
            phase_ids_start = 'p|P|Pv20p|Pv35p'
            phase_ids_end =   's|S|Sv20s|Sv35s'

        if store_id=='very_local':
            phase_ids_start = 'p|P|Pv3p|Pv8p|Pv20p|Pv35p'
            phase_ids_end =   's|S|Sv3s|Sv8s|Sv20s|Sv35s'

        if store_id=='very_local_20Hz':
            phase_ids_start = 'begin_fallback|p|P|Pv1p|Pv3p|Pv8p|Pv20p|Pv35p'
            phase_ids_end =   's|S|Sv1s|Sv3s|Sv8s|Sv20s|Sv35s'

        if store_id=='very_local_20Hz':
            phase_ids_start = 'begin_fallback|p|P|Pv1p|Pv3p|Pv8p|Pv20p|Pv35p'
            phase_ids_end =   's|S|Sv1s|Sv3s|Sv8s|Sv20s|Sv35s'

        if store_id=='castor':
            # bug?! bei Pv1.5p gibt's bei nahen Entfernungen ein index ot of
            # bounds
            phase_ids_start = 'p|P|Pv12.5p|Pv2.5p|Pv18.5p|Pv20p|Pv35p'
            phase_ids_end= 's|S|Sv12.5s|Sv2.5s|Sv18.5s|Sv20s|Sv35s'

        # Event==================================================
        event = filter(lambda x: isinstance(x, gui_util.EventMarker), markers)
        assert len(event) == 1
        event = event[0].get_event()
        event.magnitude = 4.3
        event.moment_tensor = moment_tensor.MomentTensor(
                                        m=num.array([[0.0, 0.0, 1.0],
                                                     [0.0, 0.0, 0.0],
                                                     [0.0, 0.0, 0.0]]))
    

        # generate stations from olat, olon:
        if not stations:
            print 'Generating station distribution.'
            stations = du.station_distribution((event.lat,event.lon),
                                           [[10000., 4], [130000., 8]], 
                                           rotate={3000.:45, 130000.:0})

        targets = stations2targets(stations, store_id)

        derec_home = os.environ["DEREC_HOME"]
        store_dirs = [derec_home + '/fomostos']

        engine = LocalEngine(store_superdirs=store_dirs)
        model = get_earthmodel_from_engine(engine, store_id) 

        #TESTSOURCES===============================================
        
        offset = 3*km
        zoffset= 1000.
        ref_source = event2source(event, 'DC', strike=37.3, dip=30, rake=-3)
        center_lat = ref_source.lat
        center_lon = ref_source.lon

        negative_lat_offset, negative_lon_offset = du.lat_lon_relative_shift(
                center_lat, center_lon, -offset, -offset)

        positive_lat_offset, positive_lon_offset = du.lat_lon_relative_shift(
                center_lat, center_lon, offset, offset)

        lats=num.linspace(negative_lat_offset, positive_lat_offset, 3) 
        #lats = [ref_source.lat]

        lons=num.linspace(negative_lon_offset, positive_lon_offset, 3)
        #lons = [ref_source.lon]

        depths=num.linspace(ref_source.depth-zoffset, ref_source.depth+zoffset, 3)
        depths = [ref_source.depth]

        #strikes = num.linspace(ref_source.strike-90, ref_source.strike+90, 25)
        strikes = [ref_source.strike]

        #dips = num.linspace(ref_source.dip-45, ref_source.dip+45, 25)
        dips = [ref_source.dip]

        #rakes = num.linspace(ref_source.rake-180, ref_source.rake+180, 25)
        rakes = [ref_source.rake]

        print lats, '<- lats'
        print lons, '<- lons'
        print depths, '<- depths'
        print ref_source.lat, '<- event lat'
        print ref_source.lon, '<- event lon'
        print ref_source.depth, '<- event depth'
        print ref_source.strike, ref_source.dip, ref_source.rake, '<- event S D R'
        location_test_sources = [DCSource(lat=lat,
                               lon=lon,
                               depth=depth,
                               time=event.time,
                               strike=strike,
                               dip=dip,
                               rake=rake,
                               magnitude=event.magnitude) for strike in strikes 
                                                        for dip in dips 
                                                        for rake in rakes 
                                                        for lat in lats 
                                                        for lon in lons 
                                                        for depth in depths]

        for s in location_test_sources:
            s.regularize()
        #==========================================================

        test_case = TestCase(location_test_sources, 
                             targets, 
                             engine, 
                             store_id, 
                             test_parameters={'lat':lats, 
                                              'lon':lons, 
                                              'depth':depths})
        test_case.ref_source = ref_source

        test_case.request_data()

        print 'source location: ', test_case.ref_source
        reference_seismograms = make_reference_trace(test_case.ref_source,
                                                     test_case.targets, 
                                                     engine)
        
        extended_ref_marker = du.chop_ranges(test_case.ref_source, 
                                             test_case.targets, 
                                             test_case.store,
                                             phase_ids_start,
                                             phase_ids_end)

        print('test data marker....')
        extended_test_marker = du.chop_ranges(test_case.sources,
                                              test_case.targets,
                                              test_case.store,
                                              phase_ids_start, 
                                              phase_ids_end)
        
        test_case.test_markers = extended_test_marker
        test_case.ref_markers = extended_ref_marker

        print('chopping ref....')
        test_case.references = du.chop_using_markers(
                reference_seismograms.iter_results(), extended_ref_marker, 
                                                        t_shift_frac=0.1)

        print('chopping cand....')
        test_case.seismograms = du.chop_using_markers(
                test_case.response.iter_results(), extended_test_marker, 
                                                        t_shift_frac=0.1)

        norm = 2.
        #taper = trace.CosFader(xfade=4) # Seconds or samples?
        taper = trace.CosFader(xfrac=0.1) 
        
        z, p, k = butter(4, (2.*num.pi*2. ,0.4*num.pi*2.) , 
                           'bandpass', 
                           analog=True, 
                           output='zpk')

        z = num.array(z, dtype=complex)
        p = num.array(p, dtype=complex)
        k = num.complex(k)
        fresponse = trace.PoleZeroResponse(z,p,k)
        fresponse.regularize()
        setup = trace.MisfitSetup(norm=norm,
                                  taper=taper,
                                  domain='envelope',
                                  filter=fresponse)

        test_case.set_misfit_setup(setup)
        du.calculate_misfit(test_case)
        #test_case.yaml_dump()

        # Display results===================================================
        #test_case.plot1d(order, event.lon)
        #test_case.contourf(xkey='lon', ykey='lat')

        test_case.check_plot({'lat':ref_source.lat, 'depth':ref_source.depth})

        optics = OpticBase(test_case)
        #optics.plot_1d(fix_parameters={'lat':event.lat, 'lon':event.lon})
        optics.gmt_map(stations=True, events=True)
Beispiel #5
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
Beispiel #6
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
Beispiel #7
0
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:
setup.dump(filename='my_misfit_setup.txt')
Beispiel #8
0
    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'
Beispiel #9
0
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