def traveltimes(self, phase):

        Logfile.red('Enter AUTOMATIC CROSSCORRELATION ')
        Logfile.red('\n\n+++++++++++++++++++++++++++++++++++++++++++++++\n ')
        T = []
        Wdict = OrderedDict()
        SNR = OrderedDict()
        Config = self.Config
        cfg = ConfigObj(dict=Config)
        for i in self.StationMeta:
            Logfile.red('read in %s ' % (i))
            de = loc2degrees(self.Origin, i)
            Phase = cake.PhaseDef(phase)
            traveltime_model = cfg.Str('traveltime_model')
            model = cake.load_model('../data/' + traveltime_model)
            if cfg.colesseo_input() is True:
                arrivals = model.arrivals([de, de],
                                          phases=Phase,
                                          zstart=self.Origin.depth,
                                          zstop=0.)
            else:
                arrivals = model.arrivals([de, de],
                                          phases=Phase,
                                          zstart=self.Origin.depth * km,
                                          zstop=0.)
            try:
                ptime = arrivals[0].t
            except Exception:
                try:
                    arrivals = model.arrivals([de, de],
                                              phases=Phase,
                                              zstart=self.Origin.depth * km -
                                              2.1)
                    ptime = arrivals[0].t
                except Exception:
                    ptime = 0
            T.append(ptime)
            if ptime == 0:
                Logfile.red('Available phases for station %s in\
                            range %f deegree' % (i, de))
                Logfile.red('you tried phase %s' % (phase))
                raise Exception("ILLEGAL: phase definition")
            else:
                tw = self.calculateTimeWindows(ptime)
                if cfg.pyrocko_download() is True:
                    w, snr = self.readWaveformsCross_pyrocko(i, tw, ptime)
                elif cfg.colesseo_input() is True:
                    w, snr = self.readWaveformsCross_colesseo(i, tw, ptime)
                else:
                    w, snr = self.readWaveformsCross(i, tw, ptime)
                Wdict[i.getName()] = w
                SNR[i.getName()] = snr

            Logfile.red('\n\n+++++++++++++++++++++++++++++++++++++++++++++++ ')

        Logfile.red('Exit AUTOMATIC FILTER ')
        return Wdict, SNR
Exemplo n.º 2
0
def processLoop():

    C = config.Config(evpath)
    Origin = C.parseConfig('origin')
    flag_rpe = False

    try:
        Syn_in = C.parseConfig('syn')
        syn_in = SynthCfg(Syn_in)
    except TypeError:
        pass
    Config = C.parseConfig('config')

    cfg = ConfigObj(dict=Config)
    phases = cfg.Str('ttphases')
    phases = phases.split(',')

    if cfg.pyrocko_download() is True:
        Meta = C.readpyrockostations()
    elif cfg.colesseo_input() is 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()
    Folder = C.createFolder()
    C.writeConfig(Config, Origin, Folder)

    filter = FilterCfg(Config)
    if cfg.UInt('forerun') > 0:
        ntimes = int(
            (cfg.UInt('forerun') + cfg.UInt('duration')) / cfg.UInt('step'))
    else:
        ntimes = int((cfg.UInt('duration')) / cfg.UInt('step'))
    origin = OriginCfg(Origin)

    if cfg.colesseo_input() is True:
        from pyrocko import util
        events = scenario.get_events()
        ev = events[0]
        origin.strike = str(ev.moment_tensor.strike1)
        origin.rake = str(ev.moment_tensor.rake1)
        origin.dip = str(ev.moment_tensor.dip1)
        strike = ev.moment_tensor.strike1
        origin.lat = str(ev.lat)
        origin.lon = str(ev.lon)
        origin.depth = str(ev.depth / 1000.)
        depth = ev.depth
        origin.time = util.time_to_str(ev.time)
        time_ev = util.time_to_str(ev.time)
        lat = ev.lat
        lon = ev.lon
        rake = ev.moment_tensor.rake1
        dip = ev.moment_tensor.dip1
        Origin['strike'] = str(ev.moment_tensor.strike1)
        Origin['rake'] = str(ev.moment_tensor.rake1)
        Origin['dip'] = str(ev.moment_tensor.dip1)
        Origin['lat'] = str(ev.lat)
        Origin['lon'] = str(ev.lon)
        Origin['time'] = util.time_to_str(ev.time)
        Origin['depth'] = str(ev.depth / 1000.)
        ev = Event(lat, lon, depth, time_ev, strike=strike, dip=dip, rake=rake)
    else:

        default = 0
        strike = origin.strike(default)
        dip = origin.dip(default)
        rake = origin.rake(default)

        ev = Event(origin.lat(),
                   origin.lon(),
                   origin.depth(),
                   origin.time(),
                   strike=strike,
                   dip=dip,
                   rake=rake)

    if cfg.Bool('correct_shifts_empirical') is True:

        Origin_emp = C.parseConfig('origin_emp')
        origin_emp = OriginCfg(Origin_emp)
        ev_emp = Event(origin_emp.lat(),
                       origin_emp.lon(),
                       origin_emp.depth(),
                       origin_emp.time(),
                       strike=strike,
                       dip=dip,
                       rake=rake)
    filtername = filter.filterName()
    Logfile.add('filtername = ' + filtername)

    XDict = OrderedDict()
    RefDict = OrderedDict()
    SL = OrderedDict()
    refshifts_global = []
    newFreq = str(filter.newFrequency())
    xcorrnetworks = cfg.String('networks').split(',')

    if cfg.Int('xcorr') is 1:

        fobjreferenceshiftname = newFreq + '_' + filtername + '.refpkl'
        rp = os.path.join(Folder['semb'], fobjreferenceshiftname)
        fobjreferenceshiftnameemp = newFreq + '_' + filtername + 'emp' + '.refpkl'
        rpe = os.path.join(Folder['semb'], fobjreferenceshiftnameemp)
        fobjpickleshiftname = newFreq + '_' + filtername + '.xcorrpkl'
        ps = os.path.join(Folder['semb'], fobjpickleshiftname)

        if (os.path.isfile(rp) and os.path.getsize(rp) != 0
                and os.path.isfile(ps) and os.path.getsize(ps) != 0):
            Logfile.add('xcorr/reference shift file exits : ' + rp)
            Logfile.add('loaded reference shift')

            if sys.version_info.major >= 3:
                f = open(rp, 'rb')
            else:
                f = open(rp)

            RefDict = pickle.load(f)
            if sys.version_info.major >= 3:
                x = open(ps, 'rb')
            else:
                x = open(ps)
            XDict = pickle.load(x)

            for i in xcorrnetworks:
                SL[i] = len(Config[i].split('|'))
        else:
            SL = {}
            for i in xcorrnetworks:
                W = {}
                network = cfg.String(i).split('|')
                FilterMeta = ttt.filterStations(Meta, Config, Origin, network)
                arrayfolder = os.path.join(Folder['semb'], i)

                if os.access(arrayfolder, os.F_OK) is False:
                    os.makedirs(arrayfolder)
                if cfg.pyrocko_download() is True:
                    # TODO check seperate xcoor nescessity
                    A = Xcorr(ev, FilterMeta, evpath, Config, Syn_in,
                              arrayfolder)
                print("run Xcorr")
                phase = phases[0]
                W, triggerobject = A.runXcorr(phase)
                XDict[i] = W
                RefDict[i] = triggerobject.tdiff
                SL[i] = len(network)
                for j in range(0, len(FilterMeta)):
                    refshifts_global.append(triggerobject.tdiff)

            if sys.version_info.major >= 3:
                fobjrefshift = open(rp, 'wb')
            else:
                fobjrefshift = open(rp, 'w')
            pickle.dump(RefDict, fobjrefshift)
            fobjrefshift.close()

            if sys.version_info.major >= 3:
                output = open(ps, 'wb')
            else:
                output = open(ps, 'w')
            pickle.dump(XDict, output)
            output.close()

    else:
        fobjreferenceshiftname = newFreq + '_' + filtername + '.refpkl'
        rp = os.path.join(Folder['semb'], fobjreferenceshiftname)
        fobjreferenceshiftnameemp = newFreq + '_' + filtername + 'emp' + '.refpkl'
        rpe = os.path.join(Folder['semb'], fobjreferenceshiftnameemp)
        fobjpickleshiftname = newFreq + '_' + filtername + '.xcorrpkl'
        ps = os.path.join(Folder['semb'], fobjpickleshiftname)
        refshift = 0
        if (os.path.isfile(rp) and os.path.getsize(rp) != 0
                and os.path.isfile(ps) and os.path.getsize(ps) != 0):
            Logfile.add('Temporay Memory file exits : ' + rp)
            if sys.version_info.major >= 3:
                f = open(rp, 'rb')
            else:
                f = open(rp)

            RefDict = pickle.load(f)
            if sys.version_info.major >= 3:
                x = open(ps, 'rb')
            else:
                x = open(ps)
            XDict = pickle.load(x)

            for i in xcorrnetworks:
                SL[i] = len(Config[j].split('|'))
                network = cfg.String(i).split('|')
                FilterMeta = ttt.filterStations(Meta, Config, Origin, network)
                RefDict[i] = refshift

                for j in range(0, len(FilterMeta)):
                    refshifts_global.append(refshift)
        else:
            SL = {}
            for i in xcorrnetworks:
                W = {}
                refshift = 0
                network = cfg.String(i).split('|')
                FilterMeta = ttt.filterStations(Meta, Config, Origin, network)
                arrayfolder = os.path.join(Folder['semb'], i)

                if os.access(arrayfolder, os.F_OK) is False:
                    os.makedirs(arrayfolder)
                if cfg.pyrocko_download() is True:
                    # TODO check seperate xcoor nescessity
                    A = Xcorr(ev, FilterMeta, evpath, Config, Syn_in,
                              arrayfolder)
                else:
                    A = Xcorr(ev, FilterMeta, evpath, Config, Syn_in,
                              arrayfolder)

                print("run Xcorr")
                phase = phases[0]
                W, triggerobject = A.runXcorr_dummy(phase)

                XDict[j] = W
                RefDict[j] = refshift
                SL[j] = len(network)
                for j in range(0, len(FilterMeta)):
                    refshifts_global.append(refshift)

            if sys.version_info.major >= 3:
                fobjrefshift = open(rp, 'wb')
            else:
                fobjrefshift = open(rp, 'w')
            pickle.dump(RefDict, fobjrefshift)
            fobjrefshift.close()

            if sys.version_info.major >= 3:
                output = open(ps, 'wb')
            else:
                output = open(ps, 'w')
            pickle.dump(XDict, output)
            output.close()

    if sys.version_info.major >= 3:
        for j in sorted(XDict.keys()):
            Logfile.red('Array %s has %3d of %3d Stations left' %
                        (j, len(XDict[j]), SL[j]))
    else:
        for j in sorted(XDict.keys()):
            Logfile.red('Array %s has %3d of %3d Stations left' %
                        (j, len(XDict[j]), SL[j]))
    while True:
        if sys.version_info.major >= 3:
            nnl = input("please enter your choice: ")
        else:
            nnl = raw_input("please enter your choice: ")

        if len(nnl) == 0:
            if not Basic.question('Process all networks ?'):
                continue

            Logfile.red('This networks will be used for processing: %s' %
                        (Config['networks']))
            break

        elif str(nnl) == 'quit':
            sys.exit()

        elif str(nnl) == 'rerun':
            event = os.path.join(*evpath.split('/')[-1:])

            try:
                os.remove(rp)
                os.remove(ps)

            except Exception:
                pass

            mainfolder = os.path.join(os.path.sep, *evpath.split('/')[:-2])
            os.chdir(mainfolder)

            cmd = ('%s arraytool.py process %s') % (sys.executable, event)
            Logfile.add('cmd = ' + cmd)
            os.system(cmd)
            sys.exit()

        else:

            names = nnl.split(',')
            isOk = True

            for array in names:
                arrayfolder = os.path.join(Folder['semb'], array)

                if not os.path.isdir(arrayfolder):
                    Logfile.error('Illegal network name ' + str(array))
                    isOk = False
                    break
            if not isOk:
                continue

            Logfile.add('This networks will be used for processing: %s' %
                        (nnl))
            Config['networks'] = nnl
            break

    for j in range(3, 0, -1):
        time.sleep(1)
        Logfile.red('Start processing in %d seconds ' % (j))

    wd = Origin['depth']
    start, stop, step = cfg.String('depths').split(',')

    start = int(start)
    stop = int(stop) + 1
    step_depth = int(step)
    filters = cfg.String('filters')
    filters = int(filters)
    Logfile.add('working on ' + Config['networks'])
    if cfg.Bool('correct_shifts_empirical') is True:
        emp_loop = True
    else:
        emp_loop = False


# ==================================loop over phases======================
    for phase in phases:
        if phase is 'P':
            desired = 'Z'
        if phase is 'S':
            desired = 'T'
        # ==================================loop over filter setups=====
        for filterindex in xrange(0, filters):
            # ==================================loop over depth=======
            for depthindex in xrange(start, stop, step_depth):

                workdepth = float(wd) + depthindex
                Origin['depth'] = workdepth
                ev = Event(Origin['lat'],
                           Origin['lon'],
                           Origin['depth'],
                           Origin['time'],
                           strike=strike,
                           dip=dip,
                           rake=rake)
                Logfile.add('WORKDEPTH: ' + str(Origin['depth']))
                networks = Config['networks'].split(',')

                ASL = []
                weights = []
                array_centers = []
                counter = 1
                stations_per_array = []
                Wdfs = []
                Wdfs_emp = []
                FilterMetas = []
                TTTgrids = OrderedDict()
                mints = []
                maxts = []
                refshifts = []
                for i in networks:
                    arrayname = i
                    arrayfolder = os.path.join(Folder['semb'], arrayname)

                    network = Config[i].split('|')
                    Logfile.add('network: ' + str(network))

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

                    W = XDict[i]
                    refshift = RefDict[i]
                    for j in range(0, len(FilterMeta)):
                        if cfg.correct_shifts() is False:
                            refshift = refshift * 0.
                        refshifts.append(refshift)

                    FilterMeta = cmpFilterMetavsXCORR(W, FilterMeta)

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

                    Logfile.red('Calculating Traveltime Grid')
                    t1 = time.time()

                    isParallel = False
                    TTTGridMap = []
                    mint = []
                    maxt = []
                    ttt_model = cfg.Str('traveltime_model')

                    try:
                        if cfg.Bool('correct_shifts_empirical') is True:
                            f = open(
                                '../tttgrid/tttgrid%s_%s_%s_%s_%s_emp.pkl' %
                                (phase, ttt_model, ev_emp.time, arrayname,
                                 workdepth), 'rb')
                            print(
                                "loading travel time grid%s_%s_%s_%s_%s_emp.pkl"
                                % (phase, ttt_model, ev_emp.time, arrayname,
                                   workdepth))
                            TTTGridMap_emp, mint_emp, maxt_emp = pickle.load(f)
                            f.close()
                        f = open(
                            '../tttgrid/tttgrid%s_%s_%s_%s_%s.pkl' %
                            (phase, ttt_model, ev.time, arrayname, workdepth),
                            'rb')
                        print(
                            "loading travel time grid%s_%s_%s_%s_%s.pkl" %
                            (phase, ttt_model, ev.time, arrayname, workdepth))
                        TTTGridMap, mint, maxt = pickle.load(f)
                        f.close()
                        print("loading of travel time grid sucessful")
                    except Exception:
                        print("loading of travel time grid unsucessful,\n \
                              will now calculate the grid:")
                        if isParallel:
                            maxp = 6
                            po = multiprocessing.Pool(maxp)

                            for i in xrange(len(FilterMeta)):
                                po.apply_async(ttt.calcTTTAdv,
                                               (Config, FilterMeta[i], Origin,
                                                i, arrayname, W, refshift))

                                po.close()
                                po.join()
                        else:
                            for i in xrange(len(FilterMeta)):
                                t1 = time.time()
                                ttt.calcTTTAdv(Config, FilterMeta[i], Origin,
                                               i, arrayname, W, refshift,
                                               phase)

                                Logfile.add('ttt.calcTTTAdv : ' +
                                            str(time.time() - t1) + ' sec.')
                        assert len(FilterMeta) > 0

                        TTTGridMap = deserializer.deserializeTTT(
                            len(FilterMeta))
                        mint, maxt = deserializer.deserializeMinTMaxT(
                            len(FilterMeta))
                        f = open(
                            '../tttgrid/tttgrid%s_%s_%s_%s_%s.pkl' %
                            (phase, ttt_model, ev.time, arrayname, workdepth),
                            'wb')
                        print("dumping the traveltime grid for this array")
                        pickle.dump([TTTGridMap, mint, maxt], f)
                        f.close()

                        if cfg.Bool('correct_shifts_empirical') is True:
                            ttt.calcTTTAdv(Config, FilterMeta[i], Origin_emp,
                                           i, arrayname, W, refshift, phase)

                            assert len(FilterMeta) > 0
                            TTTGridMap_emp = deserializer.deserializeTTT(
                                len(FilterMeta))
                            mint_emp, maxt_emp = deserializer.deserializeMinTMaxT(
                                len(FilterMeta))
                            f = open(
                                '../tttgrid/tttgrid%s_%s_%s_%s_%s_emp.pkl' %
                                (phase, ttt_model, ev_emp.time, arrayname,
                                 workdepth), 'wb')
                            print("dumping the traveltime grid for this array")
                            pickle.dump([TTTGridMap_emp, mint_emp, maxt_emp],
                                        f)
                            f.close()

                    t2 = time.time()
                    Logfile.red('%s took %0.3f s' % ('TTT', (t2 - t1)))

                    switch = filterindex
                    tw = times.calculateTimeWindows(mint, maxt, Config, ev,
                                                    switch)
                    if cfg.Bool('correct_shifts_empirical') is True:
                        tw_emp = times.calculateTimeWindows(
                            mint_emp, maxt_emp, Config, ev_emp, switch)

                        if cfg.pyrocko_download() is True:
                            if cfg.quantity() == 'displacement':
                                Wd_emp = waveform.readWaveformsPyrocko_restituted(
                                    FilterMeta, tw, evpath, ev_emp, desired)
                            elif cfg.Bool('synthetic_test') is True:
                                Wd_emp = waveform.readWaveformsPyrockodummy(
                                    FilterMeta, tw_emp, evpath_emp, ev_emp)
                            else:
                                Wd_emp = waveform.readWaveformsPyrocko(
                                    FilterMeta, tw_emp, evpath_emp, ev_emp,
                                    desired)
                        elif cfg.colesseo_input() is True:
                            Wd_emp = waveform.readWaveforms_colesseo(
                                FilterMeta, tw_emp, evpath_emp, ev_emp, C)
                        else:
                            Wd_emp = waveform.readWaveforms(
                                FilterMeta, tw_emp, evpath_emp, ev_emp)
                        if cfg.Bool('synthetic_test') is True\
                           or cfg.Bool('dynamic_filter') is True:
                            Wdf_emp = waveform.processdummyWaveforms(
                                Wd_emp, Config, Folder, arrayname, FilterMeta,
                                ev_emp, switch, W)
                            Wdfs_emp.extend(Wdf_emp)
                        else:
                            Wdf_emp = waveform.processWaveforms(
                                Wd_emp, Config, Folder, arrayname, FilterMeta,
                                ev_emp, switch, W)
                            Wdfs_emp.extend(Wdf_emp)
                    if cfg.pyrocko_download() is True:
                        if cfg.quantity() == 'displacement':
                            Wd = waveform.readWaveformsPyrocko_restituted(
                                FilterMeta, tw, evpath, ev, desired)
                        elif cfg.Bool('synthetic_test') is True:
                            Wd = waveform.readWaveformsPyrockodummy(
                                FilterMeta, tw, evpath, ev)
                        else:
                            Wd = waveform.readWaveformsPyrocko(
                                FilterMeta, tw, evpath, ev, desired)
                    elif cfg.colesseo_input() is True:
                        Wd = waveform.readWaveforms_colesseo(
                            FilterMeta, tw, evpath, ev, C)
                    else:
                        Wd = waveform.readWaveforms(FilterMeta, tw, evpath, ev)
                    if cfg.Bool('synthetic_test') is True\
                       or cfg.Bool('dynamic_filter') is True:
                        Wdf = waveform.processdummyWaveforms(
                            Wd, Config, Folder, arrayname, FilterMeta, ev,
                            switch, W)
                        Wdfs.extend(Wdf)
                    else:
                        Wdf = waveform.processWaveforms(
                            Wd, Config, Folder, arrayname, FilterMeta, ev,
                            switch, W)
                        Wdfs.extend(Wdf)

                    C.writeStationFile(FilterMeta, Folder, counter)
                    Logfile.red('%d Streams added for Processing' % (len(Wd)))

                    t1 = time.time()

                    f = open(
                        '../tttgrid/tttgrid%s_%s_%s_%s_%s.pkl' %
                        (phase, ttt_model, ev.time, arrayname, workdepth),
                        'rb')
                    TTTGridMap, mint, maxt = pickle.load(f)
                    f.close()
                    if switch == 0:
                        step = cfg.step()
                    if switch == 1:
                        step = cfg.step_f2()
                    if cfg.UInt('forerun') > 0:
                        ntimes = int(
                            (cfg.UInt('forerun') + cfg.UInt('duration')) /
                            step)
                    else:
                        ntimes = int((cfg.UInt('duration')) / step)
                    if cfg.Bool('combine_all') is False:

                        if cfg.optimize() is True:
                            optim.solve(counter, Config, Wdf, FilterMeta, mint,
                                        maxt, TTTGridMap, Folder, Origin,
                                        ntimes, switch, ev, arrayfolder,
                                        syn_in, refshifts, phase,
                                        rpe + str(arrayname), flag_rpe)
                        else:
                            if cfg.Bool('correct_shifts_empirical') is True:
                                if cfg.Bool('correct_shifts_empirical_run'
                                            ) is True:
                                    f = open(
                                        '../tttgrid/tttgrid%s_%s_%s_%s_%s_emp.pkl'
                                        % (phase, ttt_model, ev_emp.time,
                                           arrayname, workdepth), 'rb')
                                    TTTGridMap_emp, mint_emp, maxt_emp = pickle.load(
                                        f)
                                    f.close()
                                    flag_rpe = True
                                    arraySemb, weight, array_center = sembCalc.doCalc(
                                        counter, Config, Wdf_emp, FilterMeta,
                                        mint, maxt, TTTGridMap_emp, Folder,
                                        Origin, ntimes, switch, ev_emp,
                                        arrayfolder, syn_in, refshifts, phase,
                                        rpe + str(arrayname), flag_rpe)
                                    if sys.version_info.major >= 3:
                                        f = open(rpe + str(arrayname), 'rb')
                                    else:
                                        f = open(rpe + str(arrayname))
                                    RefDict_empirical = pickle.load(f)
                                    refshifts = RefDict_empirical
                                    for j in range(0, len(FilterMeta)):
                                        if cfg.correct_shifts() is False:
                                            refshifts[j] = refshifts[j] * 0.
                            flag_rpe = False
                            arraySemb, weight, array_center = sembCalc.doCalc(
                                counter, Config, Wdf, FilterMeta, mint, maxt,
                                TTTGridMap, Folder, Origin, ntimes, switch, ev,
                                arrayfolder, syn_in, refshifts, phase,
                                rpe + str(arrayname), flag_rpe)
                            weights.append(weight)
                            array_centers.append(array_center)
                            ASL.append(arraySemb)
                            sembCalc.writeSembMatricesSingleArray(
                                arraySemb, Config, Origin, arrayfolder, ntimes,
                                switch, phase)

                    fileName = os.path.join(arrayfolder, 'stations.txt')
                    Logfile.add('Write to file ' + fileName)

                    fobjarraynetwork = open(fileName, 'w')
                    for i in FilterMeta:
                        fobjarraynetwork.write(
                            ('%s %s %s\n') % (i.getName(), i.lat, i.lon))

                    fobjarraynetwork.close()
                    t2 = time.time()
                    Logfile.add('CALC took %0.3f sec' % (t2 - t1))
                    counter += 1
                    stations_per_array.append(len(FilterMeta))
                    TTTgrids.update(TTTGridMap)
                    mints.append(mint)
                    maxts.append(maxt)
                    FilterMetas[len(FilterMetas):] = FilterMeta
                    TTTGridMap = []

                if cfg.Bool('combine_all') is True:
                    if cfg.pyrocko_download() is True:
                        if cfg.Bool('synthetic_test') is True:
                            Wd = waveform.readWaveformsPyrockodummy(
                                FilterMetas, tw, evpath, ev)
                        else:
                            if cfg.quantity() == 'displacement':
                                Wd = waveform.readWaveformsPyrocko_restituted(
                                    FilterMetas, tw, evpath, ev, desired)
                            else:
                                Wd = waveform.readWaveformsPyrocko(
                                    FilterMetas, tw, evpath, ev, desired)
                    elif cfg.colesseo_input() is True:
                        Wd = waveform.readWaveforms_colesseo(
                            FilterMetas, tw, evpath, ev, C)
                    else:
                        Wd = waveform.readWaveforms(FilterMetas, tw, evpath,
                                                    ev)
                    if cfg.Bool('synthetic_test') is True:
                        Wdf = waveform.processdummyWaveforms(
                            Wd, Config, Folder, arrayname, FilterMetas, ev,
                            switch, W)
                    else:
                        Wdf = waveform.processWaveforms(
                            Wd, Config, Folder, arrayname, FilterMetas, ev,
                            switch, W)
                    mint = num.min(mints)
                    maxt = num.max(maxts)
                    flag_rpe = False
                    if cfg.Bool('bootstrap_array_weights') is False:

                        arraySemb, weight, array_center = sembCalc.doCalc(
                            counter, Config, Wdf, FilterMetas, mint, maxt,
                            TTTgrids, Folder, Origin, ntimes, switch, ev,
                            arrayfolder, syn_in, refshifts_global, phase,
                            rpe + str(arrayname), flag_rpe)
                        ASL.append(arraySemb)
                        weights.append(weight)
                        array_centers.append(array_center)
                        sembCalc.writeSembMatricesSingleArray(
                            arraySemb, Config, Origin, arrayfolder, ntimes,
                            switch, phase)
                    else:
                        nboot = cfg.Int('n_bootstrap')
                        tmp_general = 1
                        for ibootstrap in range(nboot):
                            f = rstate.uniform(0., 1., size=counter + 1)
                            f = num.sort(f)
                            g = f[1:] - f[:-1]
                            k = 0
                            ws = []

                            for wss in range(0, counter - 1):
                                for stats in range(0, stations_per_array[k]):
                                    ws.append(g[k])
                                k = +1
                            ws = num.asarray(ws)
                            arraySemb, weight, array_center = sembCalc.doCalc(
                                counter,
                                Config,
                                Wdf,
                                FilterMetas,
                                mint,
                                maxt,
                                TTTgrids,
                                Folder,
                                Origin,
                                ntimes,
                                switch,
                                ev,
                                arrayfolder,
                                syn_in,
                                refshifts_global,
                                phase,
                                rpe + str(arrayname),
                                flag_rpe,
                                bs_weights=ws)

                            ASL.append(arraySemb)
                            weights.append(weight)
                            array_centers.append(array_center)
                            sembCalc.writeSembMatricesSingleArray(
                                arraySemb,
                                Config,
                                Origin,
                                arrayfolder,
                                ntimes,
                                switch,
                                phase,
                                bootstrap=ibootstrap)

                            if ASL:
                                Logfile.red('collect semblance matrices from\
                                            all arrays')
                                sembmax, tmp = sembCalc.collectSemb(
                                    ASL,
                                    Config,
                                    Origin,
                                    Folder,
                                    ntimes,
                                    len(networks),
                                    switch,
                                    array_centers,
                                    phase,
                                    cboot=ibootstrap)
                                tmp_general *= tmp
                                ASL = []
                        sembmax, tmp = sembCalc.collectSemb(
                            ASL,
                            Config,
                            Origin,
                            Folder,
                            ntimes,
                            len(networks),
                            switch,
                            array_centers,
                            phase,
                            cboot=None,
                            temp_comb=tmp_general)
                if cfg.optimize_all() is True:
                    import optim_csemb
                    sembmax, tmp = sembCalc.collectSemb(
                        ASL, Config, Origin, Folder, ntimes, len(networks),
                        switch)
                    optim_csemb.solve(counter, Config, Wdf, FilterMeta, mint,
                                      maxt, TTTGridMap, Folder, Origin, ntimes,
                                      switch, ev, arrayfolder, syn_in, ASL,
                                      sembmax, evpath, XDict, RefDict,
                                      workdepth, filterindex, Wdfs)

                if ASL and cfg.Bool('bootstrap_array_weights') is False:
                    Logfile.red('collect semblance matrices from all arrays')
                    sembmax, tmp = sembCalc.collectSemb(
                        ASL, Config, Origin, Folder, ntimes, len(networks),
                        switch, array_centers, phase)
                    if cfg.Bool('weight_by_noise') is True:
                        sembCalc.collectSembweighted(ASL, Config, Origin,
                                                     Folder, ntimes,
                                                     len(networks), switch,
                                                     weights)

    else:
        Logfile.red('Nothing to do  -> Finish')
    print("last work depth:")
    print(workdepth)
Exemplo n.º 3
0
def calcTTTAdv(Config, station, Origin, flag, arrayname, Xcorrshift, Refshift,
               phase):

    cfg = ConfigObj(dict=Config)
    if cfg.Bool('correct_shifts_empirical') is True:
        dimX = cfg.Int('dimx_emp')
        dimY = cfg.Int('dimy_emp')
    else:
        dimX = cfg.Int('dimx')
        dimY = cfg.Int('dimy')
    gridspacing = cfg.Float('gridspacing')
    traveltime_model = cfg.Str('traveltime_model')

    o_lat = float(Origin['lat'])
    o_lon = float(Origin['lon'])
    o_depth = float(Origin['depth'])

    oLator = o_lat + dimX / 2
    oLonor = o_lon + dimY / 2
    oLatul = 0
    oLonul = 0
    o_dip = 80.
    plane = False

    TTTGridMap = {}
    LMINMAX = []
    GridArray = {}
    locStation = Location(station.lat, station.lon)
    sdelta = loc2degrees(Location(o_lat, o_lon), locStation)
    Phase = cake.PhaseDef(phase)
    model = cake.load_model('../data/' + traveltime_model)

    z = 0
    if plane is True:
        depth = np.linspace(0., 40., num=dimY)
        for i in xrange(70):
            oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing
            if z == 0 and i == 0:
                Latul = oLatul
            o = 0
            start_time = time.clock()

            for j in xrange(40):
                oLonul = o_lon - (
                    (dimY - 1) /
                    2) * gridspacing + j * gridspacing / np.cos(o_dip)
                if o == 0 and j == 0:
                    Lonul = oLonul
                de = loc2degrees(Location(oLatul, oLonul), locStation)
                arrivals = model.arrivals([de, de],
                                          phases=Phase,
                                          zstart=depth[j] * km,
                                          zstop=0.)
            try:
                ttime = arrivals[0].t
            except Exception:
                try:
                    arrivals = model.arrivals([de, de],
                                              phases=Phase,
                                              zstart=depth[j] * km - 2.5,
                                              zstop=depth[j] * km + 2.5,
                                              refine=True)
                    ttime = arrivals[0].t
                except Exception:
                    tt = obs_TravelTimes(de, o_depth)
                    for k in tt:
                        if k['phase_name'] == 'P' or k['phase_name'] == (
                                '%sdiff') % (Config[phasename]):
                            ttime = k['time']
                        print("Something wrong with phase arrival, too large\
                             distances choosen?")

                GridArray[(i, j)] = GridElem(oLatul, oLonul, depth[j], ttime,
                                             de)
                LMINMAX.append(ttime)

                GridArray[(i, j)] = GridElem(oLatul, oLonul, o_depth, ttime,
                                             de)
                LMINMAX.append(ttime)

                if ttime == 0:
                    raise Exception("\033[31mILLEGAL: phase definition\033[0m")
    else:
        for i in xrange(dimX):
            oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing

            if z == 0 and i == 0:
                Latul = oLatul
            o = 0
            for j in xrange(dimY):
                oLonul = o_lon - (
                    (dimY - 1) / 2) * gridspacing + j * gridspacing

                if o == 0 and j == 0:
                    Lonul = oLonul
                de = loc2degrees(Location(oLatul, oLonul), locStation)
                arrivals = model.arrivals([de, de],
                                          phases=Phase,
                                          zstart=o_depth * km)
                try:
                    ttime = arrivals[0].t
                except:
                    try:
                        arrivals = model.arrivals([de, de],
                                                  phases=Phase,
                                                  zstart=o_depth * km,
                                                  zstop=o_depth * km,
                                                  refine=True)
                        ttime = arrivals[0].t
                    except:
                        arrivals = model.arrivals([de, de],
                                                  phases=Phase,
                                                  zstart=o_depth * km - 2.5,
                                                  zstop=o_depth * km + 2.5,
                                                  refine=True)
                        ttime = arrivals[0].t

                GridArray[(i, j)] = GridElem(oLatul, oLonul, o_depth, ttime,
                                             de)
                LMINMAX.append(ttime)

                GridArray[(i, j)] = GridElem(oLatul, oLonul, o_depth, ttime,
                                             de)
                LMINMAX.append(ttime)
                if ttime == 0:
                    raise Exception("\033[31mILLEGAL: phase definition\033[0m")

    mint = min(LMINMAX)
    maxt = max(LMINMAX)
    TTTGridMap[station.getName()] = TTTGrid(o_depth, mint, maxt, Latul, Lonul,
                                            oLator, oLonor, GridArray)
    k = MinTMaxT(mint, maxt)

    Basic.dumpToFile(str(flag) + '-ttt.pkl', TTTGridMap)
    Basic.dumpToFile('minmax-' + str(flag) + '.pkl', k)
    Basic.dumpToFile('station-' + str(flag) + '.pkl', station)