コード例 #1
0
ファイル: main.py プロジェクト: HongjianFang/Palantiri
def processLoop():

    #==================================get meta info==========================================
    C = config.Config(evpath)
    Origin = C.parseConfig('origin')
    try:
        Syn_in = C.parseConfig('syn')
        syn_in = SynthCfg(Syn_in)
    except:
        pass
    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()
    #==================================get meta info==========================================

    #==================================do prerequiries========================================
    Folder = C.createFolder()

    #C.cpSkeleton(Folder,Config)
    C.writeConfig(Config, Origin, Folder)

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

    if cfg.colesseo_input() == 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)  # Origin.get ('strike', default)
        dip = origin.dip(default)  # Origin.get ('dip',    default)
        rake = origin.rake(default)  # Origin.get ('rake',   default)

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

    filtername = filter.filterName()
    Logfile.add('filtername = ' + filtername)

    #todo crosscorrelation for all arrays before processing

    XDict = {}
    RefDict = {}
    SL = {}
    if cfg.Int('xcorr') == 1:

        newFreq = str(filter.newFrequency())
        fobjreferenceshiftname = newFreq + '_' + filtername + '.refpkl'
        rp = os.path.join(Folder['semb'], fobjreferenceshiftname)
        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('file exits : ' + rp)
            Logfile.add('load refshifts')

            f = open(rp)
            RefDict = pickle.load(f)
            x = open(ps)
            XDict = pickle.load(x)
            xcorrnetworks = cfg.String('networks').split(',')

            for i in xcorrnetworks:
                SL[i] = len(Config[i].split('|'))
        else:
            SL = {}
            xcorrnetworks = cfg.String('networks').split(',')
            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) == False:
                    os.makedirs(arrayfolder)
                if cfg.pyrocko_download() == True:
                    A = Xcorr(ev, FilterMeta, evpath, Config, Syn_in,
                              arrayfolder)
                else:
                    A = Xcorr(ev, FilterMeta, evpath, Config, Syn_in,
                              arrayfolder)

                print "run Xcorr"
                W, triggerobject = A.runXcorr()

                XDict[i] = W
                RefDict[i] = triggerobject.tdiff
                SL[i] = len(network)
            #endfor

            fobjrefshift = open(rp, 'w')
            pickle.dump(RefDict, fobjrefshift)
            fobjrefshift.close()

            output = open(ps, 'w')
            pickle.dump(XDict, output)
            output.close()

        for i in sorted(XDict.iterkeys()):
            Logfile.red('Array %s has %3d of %3d Stations left' %
                        (i, len(XDict[i]), SL[i]))

        logger.info(
            '\033[31mFor proceeding without changes press enter or give new comma seperatet network list or quit for exit\033[0m'
        )

        while True:
            nnl = raw_input("please enter your choice: ")
            #Logfile.add ('Choise = ' + nnl)

            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:
                    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:
                # Check if selected array(s) exists

                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
                #endfor

                if not isOk: continue  # Illegal network : input again

                # use these networks

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

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

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

    start = int(start)
    stop = int(stop) + 1
    step = int(step)
    filters = cfg.String('filters')
    filters = int(filters)
    Logfile.add('working on ' + Config['networks'])

    #==================================loop over depth======================
    for filterindex in xrange(0, filters):
        for depthindex in xrange(start, stop, step):

            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']))

            #==================================do prerequiries===============

            #==================================loop over arrays================
            ASL = []
            weights = []
            array_centers = []

            networks = Config['networks'].split(',')
            counter = 1
            TriggerOnset = []
            Wdfs = []
            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)

                #if len(FilterMeta) < 3: continue              #hs : wieder rein
                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']))

                ##############=======================PARALLEL===========================================

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

                isParallel = False  #10.12.2015
                TTTGridMap = []
                mint = []
                maxt = []
                try:
                    f = open(
                        '../tttgrid/tttgrid_%s_%s_%s.pkl' %
                        (ev.time, arrayname, workdepth), 'rb')
                    print "loading travel time grid_%s_%s_%s.pkl" % (
                        ev.time, arrayname, workdepth)
                    TTTGridMap, mint, maxt = pickle.load(f)
                    f.close()
                    print "loading of travel time grid sucessful"
                except:

                    print "loading of travel time grid unsucessful, will now calculate the grid:"
                    if isParallel:  #hs
                        # maxp = int (Config['ncore'])
                        maxp = 6  #hs
                        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:  #hs+
                        for i in xrange(len(FilterMeta)):
                            t1 = time.time()
                            ttt.calcTTTAdv(Config, FilterMeta[i], Origin, i,
                                           arrayname, W, refshift)

                            Logfile.add('ttt.calcTTTAdv : ' +
                                        str(time.time() - t1) + ' sec.')
                            #endif                                                                           #hs-

                    assert len(FilterMeta) > 0

                    TTTGridMap = deserializer.deserializeTTT(len(FilterMeta))
                    mint, maxt = deserializer.deserializeMinTMaxT(
                        len(FilterMeta))
                    f = open(
                        '../tttgrid/tttgrid_%s_%s_%s.pkl' %
                        (ev.time, arrayname, workdepth), 'wb')
                    print "dumping the traveltime grid for this array"
                    pickle.dump([TTTGridMap, mint, maxt], 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.pyrocko_download() == True:
                    if cfg.quantity() == 'displacement':
                        Wd = waveform.readWaveformsPyrocko_restituted(
                            FilterMeta, tw, evpath, ev)
                    else:
                        Wd = waveform.readWaveformsPyrocko(
                            FilterMeta, tw, evpath, ev)
                #    Wdf = waveform.processpyrockoWaveforms(Wd, Config, Folder, arrayname, FilterMeta, ev, switch, W)
                elif cfg.colesseo_input() == 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:
                    Wdf = waveform.processdummyWaveforms(
                        Wd, Config, Folder, arrayname, FilterMeta, ev, switch,
                        W)
                    Wdfs.append(Wdf)
                else:
                    Wdf = waveform.processWaveforms(Wd, Config, Folder,
                                                    arrayname, FilterMeta, ev,
                                                    switch, W)
                    Wdfs.append(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.pkl' %
                    (ev.time, arrayname, workdepth), 'rb')
                print "loading travel time grid_%s_%s_%s.pkl" % (
                    ev.time, arrayname, workdepth)
                TTTGridMap, mint, maxt = pickle.load(f)
                f.close()
                if cfg.optimize() == True:
                    optim.solve(counter, Config, Wdf, FilterMeta, mint, maxt,
                                TTTGridMap, Folder, Origin, ntimes, switch, ev,
                                arrayfolder, syn_in)
                else:
                    arraySemb, weight, array_center = sembCalc.doCalc(
                        counter, Config, Wdf, FilterMeta, mint, maxt,
                        TTTGridMap, Folder, Origin, ntimes, switch, ev,
                        arrayfolder, syn_in)
                t2 = time.time()
                Logfile.add('CALC took %0.3f sec' % (t2 - t1))
                weights.append(weight)
                array_centers.append(array_center)
                ASL.append(arraySemb)
                counter += 1

                sembCalc.writeSembMatricesSingleArray(arraySemb, Config,
                                                      Origin, arrayfolder,
                                                      ntimes, switch)

                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()
                TTTGridMAP = []
            if cfg.optimize_all() == True:
                import optim_csemb
                from optim_csemb import solve

                sembmax = 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:
                Logfile.red('collect semblance matrices from all arrays')
                sembmax = sembCalc.collectSemb(ASL, Config, Origin, Folder,
                                               ntimes, len(networks), switch,
                                               array_centers)
                if cfg.Bool('weight_by_noise') == True:
                    sembCalc.collectSembweighted(ASL, Config, Origin, Folder,
                                                 ntimes, len(networks), switch,
                                                 weights)

    else:
        Logfile.red('Nothing to do  -> Finish')
    print "depth:"
    print workdepth
コード例 #2
0
def doCalc(flag, Config, WaveformDict, FilterMetaData, Gmint, Gmaxt,
           TTTGridMap, Folder, Origin, ntimes, switch, ev, arrayfolder,
           syn_in):
    '''
    method for calculating semblance of one station array
    '''
    Logfile.add('PROCESS %d %s' % (flag, ' Enters Semblance Calculation'))
    Logfile.add('MINT  : %f  MAXT: %f Traveltime' % (Gmint, Gmaxt))

    cfg = ConfigObj(dict=Config)

    dimX = cfg.dimX()  # ('dimx')
    dimY = cfg.dimY()  # ('dimy')
    winlen = cfg.winlen()  # ('winlen')
    step = cfg.step()  # ('step')

    new_frequence = cfg.newFrequency()  #('new_frequence')
    forerun = cfg.Int('forerun')
    duration = cfg.Int('duration')
    gridspacing = cfg.Float('gridspacing')

    nostat = len(WaveformDict)
    traveltimes = {}
    recordstarttime = ''
    minSampleCount = 999999999

    ntimes = int((forerun + duration) / step)
    nsamp = int(winlen * new_frequence)
    nstep = int(step * new_frequence)
    from pyrocko import obspy_compat
    from pyrocko import orthodrome, model
    obspy_compat.plant()

    ############################################################################
    calcStreamMap = WaveformDict

    stations = []
    py_trs = []
    for trace in calcStreamMap.iterkeys():
        py_tr = obspy_compat.to_pyrocko_trace(calcStreamMap[trace])
        py_trs.append(py_tr)
        for il in FilterMetaData:
            if str(il) == str(trace):
                szo = model.Station(lat=il.lat,
                                    lon=il.lon,
                                    station=il.sta,
                                    network=il.net,
                                    channels=py_tr.channel,
                                    elevation=il.ele,
                                    location=il.loc)
                stations.append(szo)  #right number of stations?

#==================================synthetic BeamForming=======================================

    if cfg.Bool('shift_by_phase_pws') == True:
        calcStreamMapshifted = calcStreamMap.copy()
        from obspy.core import stream
        stream = stream.Stream()
        for trace in calcStreamMapshifted.iterkeys():
            stream.append(calcStreamMapshifted[trace])
        pws_stack = PWS_stack([stream], weight=2, normalize=True)
        for tr in pws_stack:
            for trace in calcStreamMapshifted.iterkeys():
                calcStreamMapshifted[trace] = tr
        calcStreamMap = calcStreamMapshifted

    if cfg.Bool('shift_by_phase_onset') == True:
        pjoin = os.path.join
        timeev = util.str_to_time(ev.time)
        trs_orgs = []
        calcStreamMapshifted = calcStreamMap.copy()
        for trace in calcStreamMapshifted.iterkeys():
            tr_org = obspy_compat.to_pyrocko_trace(calcStreamMapshifted[trace])
            trs_orgs.append(tr_org)

        timing = CakeTiming(
            phase_selection='first(p|P|PP|P(cmb)P(icb)P(icb)p(cmb)p)-20',
            fallback_time=100.)
        traces = trs_orgs

        event = model.Event(lat=float(ev.lat),
                            lon=float(ev.lon),
                            depth=ev.depth * 1000.,
                            time=timeev)
        directory = arrayfolder
        bf = BeamForming(stations, traces, normalize=True)
        shifted_traces = bf.process(event=event,
                                    timing=timing,
                                    fn_dump_center=pjoin(
                                        directory, 'array_center.pf'),
                                    fn_beam=pjoin(directory, 'beam.mseed'))
        i = 0
        store_id = syn_in.store()
        engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()])
        for trace in calcStreamMapshifted.iterkeys():
            recordstarttime = calcStreamMapshifted[
                trace].stats.starttime.timestamp
            recordendtime = calcStreamMapshifted[trace].stats.endtime.timestamp
            mod = shifted_traces[i]
            extracted = mod.chop(recordstarttime, recordendtime, inplace=False)
            shifted_obs_tr = obspy_compat.to_obspy_trace(extracted)
            calcStreamMapshifted[trace] = shifted_obs_tr
            i = i + 1

        calcStreamMap = calcStreamMapshifted

    weight = 0.
    if cfg.Bool('weight_by_noise') == True:
        from noise_analyser import analyse
        pjoin = os.path.join
        timeev = util.str_to_time(ev.time)
        trs_orgs = []
        calcStreamMapshifted = calcStreamMap.copy()
        for trace in calcStreamMapshifted.iterkeys():
            tr_org = obspy_compat.to_pyrocko_trace(calcStreamMapshifted[trace])
            trs_orgs.append(tr_org)

        timing = CakeTiming(
            phase_selection='first(p|P|PP|P(cmb)P(icb)P(icb)p(cmb)p)-20',
            fallback_time=100.)
        traces = trs_orgs
        event = model.Event(lat=float(ev.lat),
                            lon=float(ev.lon),
                            depth=ev.depth * 1000.,
                            time=timeev)
        directory = arrayfolder
        bf = BeamForming(stations, traces, normalize=True)
        shifted_traces = bf.process(event=event,
                                    timing=timing,
                                    fn_dump_center=pjoin(
                                        directory, 'array_center.pf'),
                                    fn_beam=pjoin(directory, 'beam.mseed'))
        i = 0
        store_id = syn_in.store()
        engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()])
        weight = analyse(shifted_traces,
                         engine,
                         event,
                         stations,
                         100.,
                         store_id,
                         nwindows=1,
                         check_events=True,
                         phase_def='P')

    for trace in calcStreamMap.iterkeys():
        recordstarttime = calcStreamMap[trace].stats.starttime
        d = calcStreamMap[trace].stats.starttime
        d = d.timestamp

        if calcStreamMap[trace].stats.npts < minSampleCount:
            minSampleCount = calcStreamMap[trace].stats.npts

    ############################################################################
    traces = num.ndarray(shape=(len(calcStreamMap), minSampleCount),
                         dtype=float)
    traveltime = num.ndarray(shape=(len(calcStreamMap), dimX * dimY),
                             dtype=float)
    latv = num.ndarray(dimX * dimY, dtype=float)
    lonv = num.ndarray(dimX * dimY, dtype=float)
    ############################################################################

    c = 0
    streamCounter = 0

    for key in calcStreamMap.iterkeys():
        streamID = key
        c2 = 0

        for o in calcStreamMap[key]:
            if c2 < minSampleCount:
                traces[c][c2] = o

                c2 += 1

        for key in TTTGridMap.iterkeys():

            if streamID == key:
                traveltimes[streamCounter] = TTTGridMap[key]
            else:
                "NEIN", streamID, key

        if not streamCounter in traveltimes:
            continue  #hs : thread crashed before

        g = traveltimes[streamCounter]
        dimZ = g.dimZ
        mint = g.mint
        maxt = g.maxt
        Latul = g.Latul
        Lonul = g.Lonul
        Lator = g.Lator
        Lonor = g.Lonor

        gridElem = g.GridArray

        for x in range(dimX):
            for y in range(dimY):
                elem = gridElem[x, y]

                traveltime[c][x * dimY + y] = elem.tt
                latv[x * dimY + y] = elem.lat
                lonv[x * dimY + y] = elem.lon
        #endfor

        c += 1
        streamCounter += 1

    #endfor

    ############################## CALCULATE PARAMETER FOR SEMBLANCE CALCULATION ##################
    nsamp = winlen * new_frequence

    nstep = int(step * new_frequence)
    migpoints = dimX * dimY

    dimZ = 0
    new_frequence = cfg.newFrequency()  # ['new_frequence']
    maxp = int(Config['ncore'])

    Logfile.add('PROCESS %d  NTIMES: %d' % (flag, ntimes))

    if False:
        print('nostat ', nostat, type(nostat))
        print('nsamp ', nsamp, type(nsamp))
        print('ntimes ', ntimes, type(ntimes))
        print('nstep ', nstep, type(nstep))
        print('dimX ', dimX, type(dimX))
        print('dimY ', dimY, type(dimY))
        print('mint ', Gmint, type(mint))
        print('new_freq ', new_frequence, type(new_frequence))
        print('minSampleCount ', minSampleCount, type(minSampleCount))
        print('latv ', latv, type(latv))
        print('traces', traces, type(traces))
        print('traveltime', traveltime, type(traveltime))

    t1 = time.time()
    traces_org = traces.reshape(1, nostat * minSampleCount)
    traveltime_org = traveltime.reshape(1, nostat * dimX * dimY)
    USE_C_CODE = True
    try:
        if USE_C_CODE:
            import Cm
            import CTrig
            start_time = time.time()
            k = Cm.otest(maxp, nostat, nsamp, ntimes, nstep, dimX, dimY, Gmint,
                         new_frequence, minSampleCount, latv, lonv,
                         traveltime_org, traces_org)
            print("--- %s seconds ---" % (time.time() - start_time))
        else:
            start_time = time.time()
            k = otest(maxp, nostat, nsamp, ntimes, nstep, dimX, dimY, Gmint,
                      new_frequence, minSampleCount, latv, lonv,
                      traveltime_org, traces_org)  #hs
            print("--- %s seconds ---" % (time.time() - start_time))
    except:
        print "loaded tttgrid has probably wrong dimensions or stations, delete\
                ttgrid or exchange"

    t2 = time.time()

    Logfile.add('%s took %0.3f s' % ('CALC:', (t2 - t1)))

    partSemb = k

    partSemb_data = partSemb.reshape(ntimes, migpoints)

    return partSemb_data
コード例 #3
0
def doCalc_syn(flag, Config, WaveformDict, FilterMetaData, Gmint, Gmaxt,
               TTTGridMap, Folder, Origin, ntimes, switch, ev, arrayfolder,
               syn_in, parameter):
    '''
    method for calculating semblance of one station array
    '''
    Logfile.add('PROCESS %d %s' % (flag, ' Enters Semblance Calculation'))
    Logfile.add('MINT  : %f  MAXT: %f Traveltime' % (Gmint, Gmaxt))

    cfg = ConfigObj(dict=Config)

    dimX = cfg.dimX()  # ('dimx')
    dimY = cfg.dimY()  # ('dimy')
    winlen = cfg.winlen()  # ('winlen')
    step = cfg.step()  # ('step')

    new_frequence = cfg.newFrequency()  #('new_frequence')
    forerun = cfg.Int('forerun')
    duration = cfg.Int('duration')
    gridspacing = cfg.Float('gridspacing')

    nostat = len(WaveformDict)
    traveltimes = {}
    recordstarttime = ''
    minSampleCount = 999999999

    ntimes = int((forerun + duration) / step)
    nsamp = int(winlen * new_frequence)
    nstep = int(step * new_frequence)
    from pyrocko import obspy_compat
    from pyrocko import orthodrome, model
    obspy_compat.plant()

    ############################################################################
    calcStreamMap = WaveformDict

    stations = []
    py_trs = []
    for trace in calcStreamMap.iterkeys():
        py_tr = obspy_compat.to_pyrocko_trace(calcStreamMap[trace])
        py_trs.append(py_tr)
        for il in FilterMetaData:
            if str(il) == str(trace):
                szo = model.Station(lat=il.lat,
                                    lon=il.lon,
                                    station=il.sta,
                                    network=il.net,
                                    channels=py_tr.channel,
                                    elevation=il.ele,
                                    location=il.loc)
                stations.append(szo)  #right number of stations?

    store_id = syn_in.store()
    engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()])

    targets = []
    for st in stations:
        target = Target(lat=st.lat,
                        lon=st.lon,
                        store_id=store_id,
                        codes=(st.network, st.station, st.location, 'BHZ'),
                        tmin=-1900,
                        tmax=3900,
                        interpolation='multilinear',
                        quantity=cfg.quantity())
        targets.append(target)

        if syn_in.nsources() == 1:
            if syn_in.use_specific_stf() is True:
                stf = syn_in.stf()
                exec(stf)
            else:
                stf = STF()
            if syn_in.source() == 'RectangularSource':
                source = RectangularSource(
                    lat=float(syn_in.lat_0()),
                    lon=float(syn_in.lon_0()),
                    depth=syn_in.depth_syn_0() * 1000.,
                    strike=syn_in.strike_0(),
                    dip=syn_in.dip_0(),
                    rake=syn_in.rake_0(),
                    width=syn_in.width_0() * 1000.,
                    length=syn_in.length_0() * 1000.,
                    nucleation_x=syn_in.nucleation_x_0(),
                    slip=syn_in.slip_0(),
                    nucleation_y=syn_in.nucleation_y_0(),
                    stf=stf,
                    time=util.str_to_time(syn_in.time_0()))
            if syn_in.source() == 'DCSource':
                source = DCSource(lat=float(syn_in.lat_0()),
                                  lon=float(syn_in.lon_0()),
                                  depth=syn_in.depth_syn_0() * 1000.,
                                  strike=syn_in.strike_0(),
                                  dip=syn_in.dip_0(),
                                  rake=syn_in.rake_0(),
                                  stf=stf,
                                  time=util.str_to_time(syn_in.time_0()),
                                  magnitude=syn_in.magnitude_0())

        else:
            sources = []
            for i in range(syn_in.nsources()):
                if syn_in.use_specific_stf() is True:
                    stf = syn_in.stf()
                    exec(stf)

                else:
                    stf = STF()
                if syn_in.source() == 'RectangularSource':
                    sources.append(
                        RectangularSource(
                            lat=float(syn_in.lat_1(i)),
                            lon=float(syn_in.lon_1(i)),
                            depth=syn_in.depth_syn_1(i) * 1000.,
                            strike=syn_in.strike_1(i),
                            dip=syn_in.dip_1(i),
                            rake=syn_in.rake_1(i),
                            width=syn_in.width_1(i) * 1000.,
                            length=syn_in.length_1(i) * 1000.,
                            nucleation_x=syn_in.nucleation_x_1(i),
                            slip=syn_in.slip_1(i),
                            nucleation_y=syn_in.nucleation_y_1(i),
                            stf=stf,
                            time=util.str_to_time(syn_in.time_1(i))))

                if syn_in.source() == 'DCSource':
                    sources.append(
                        DCSource(lat=float(syn_in.lat_1(i)),
                                 lon=float(syn_in.lon_1(i)),
                                 depth=syn_in.depth_1(i) * 1000.,
                                 strike=syn_in.strike_1(i),
                                 dip=syn_in.dip_1(i),
                                 rake=syn_in.rake_1(i),
                                 stf=stf,
                                 time=util.str_to_time(syn_in.time_1(i)),
                                 magnitude=syn_in.magnitude_1(i)))
            source = CombiSource(subsources=sources)
        response = engine.process(source, targets)

        synthetic_traces = response.pyrocko_traces()
        if cfg.Bool('synthetic_test_add_noise') is True:
            from noise_addition import add_noise
            trs_orgs = []
            calcStreamMapsyn = calcStreamMap.copy()
            #from pyrocko import trace
            for tracex in calcStreamMapsyn.iterkeys():
                for trl in synthetic_traces:
                    if str(trl.name()[4:12]) == str(tracex[4:]):
                        tr_org = obspy_compat.to_pyrocko_trace(
                            calcStreamMapsyn[tracex])
                        tr_org.downsample_to(2.0)
                        trs_orgs.append(tr_org)
            store_id = syn_in.store()
            engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()])
            synthetic_traces = add_noise(trs_orgs,
                                         engine,
                                         source.pyrocko_event(),
                                         stations,
                                         store_id,
                                         phase_def='P')
        trs_org = []
        trs_orgs = []
        fobj = os.path.join(arrayfolder, 'shift.dat')
        xy = num.loadtxt(fobj, usecols=1, delimiter=',')
        calcStreamMapsyn = calcStreamMap.copy()
        #from pyrocko import trace
        for tracex in calcStreamMapsyn.iterkeys():
            for trl in synthetic_traces:
                if str(trl.name()[4:12]) == str(tracex[4:]):
                    mod = trl

                    recordstarttime = calcStreamMapsyn[
                        tracex].stats.starttime.timestamp
                    recordendtime = calcStreamMapsyn[
                        tracex].stats.endtime.timestamp
                    tr_org = obspy_compat.to_pyrocko_trace(
                        calcStreamMapsyn[tracex])
                    trs_orgs.append(tr_org)

                    tr_org_add = mod.chop(recordstarttime,
                                          recordendtime,
                                          inplace=False)
                    synthetic_obs_tr = obspy_compat.to_obspy_trace(tr_org_add)
                    calcStreamMapsyn[tracex] = synthetic_obs_tr
                    trs_org.append(tr_org_add)
        calcStreamMap = calcStreamMapsyn

    if cfg.Bool('shift_by_phase_pws') == True:
        calcStreamMapshifted = calcStreamMap.copy()
        from obspy.core import stream
        stream = stream.Stream()
        for trace in calcStreamMapshifted.iterkeys():
            stream.append(calcStreamMapshifted[trace])
        pws_stack = PWS_stack([stream], weight=2, normalize=True)
        for tr in pws_stack:
            for trace in calcStreamMapshifted.iterkeys():
                calcStreamMapshifted[trace] = tr
        calcStreamMap = calcStreamMapshifted

    if cfg.Bool('shift_by_phase_onset') == True:
        pjoin = os.path.join
        timeev = util.str_to_time(ev.time)
        trs_orgs = []
        calcStreamMapshifted = calcStreamMap.copy()
        for trace in calcStreamMapshifted.iterkeys():
            tr_org = obspy_compat.to_pyrocko_trace(calcStreamMapshifted[trace])
            trs_orgs.append(tr_org)

        timing = CakeTiming(
            phase_selection='first(p|P|PP|P(cmb)P(icb)P(icb)p(cmb)p)-20',
            fallback_time=100.)
        traces = trs_orgs

        event = model.Event(lat=float(ev.lat),
                            lon=float(ev.lon),
                            depth=ev.depth * 1000.,
                            time=timeev)
        directory = arrayfolder
        bf = BeamForming(stations, traces, normalize=True)
        shifted_traces = bf.process(event=event,
                                    timing=timing,
                                    fn_dump_center=pjoin(
                                        directory, 'array_center.pf'),
                                    fn_beam=pjoin(directory, 'beam.mseed'))
        i = 0
        store_id = syn_in.store()
        engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()])
        for trace in calcStreamMapshifted.iterkeys():
            recordstarttime = calcStreamMapshifted[
                trace].stats.starttime.timestamp
            recordendtime = calcStreamMapshifted[trace].stats.endtime.timestamp
            mod = shifted_traces[i]
            extracted = mod.chop(recordstarttime, recordendtime, inplace=False)
            shifted_obs_tr = obspy_compat.to_obspy_trace(extracted)
            calcStreamMapshifted[trace] = shifted_obs_tr
            i = i + 1

        calcStreamMap = calcStreamMapshifted

    weight = 0.
    if cfg.Bool('weight_by_noise') == True:
        from noise_analyser import analyse
        pjoin = os.path.join
        timeev = util.str_to_time(ev.time)
        trs_orgs = []
        calcStreamMapshifted = calcStreamMap.copy()
        for trace in calcStreamMapshifted.iterkeys():
            tr_org = obspy_compat.to_pyrocko_trace(calcStreamMapshifted[trace])
            trs_orgs.append(tr_org)

        timing = CakeTiming(
            phase_selection='first(p|P|PP|P(cmb)P(icb)P(icb)p(cmb)p)-20',
            fallback_time=100.)
        traces = trs_orgs
        event = model.Event(lat=float(ev.lat),
                            lon=float(ev.lon),
                            depth=ev.depth * 1000.,
                            time=timeev)
        directory = arrayfolder
        bf = BeamForming(stations, traces, normalize=True)
        shifted_traces = bf.process(event=event,
                                    timing=timing,
                                    fn_dump_center=pjoin(
                                        directory, 'array_center.pf'),
                                    fn_beam=pjoin(directory, 'beam.mseed'))
        i = 0
        store_id = syn_in.store()
        engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()])
        weight = analyse(shifted_traces,
                         engine,
                         event,
                         stations,
                         100.,
                         store_id,
                         nwindows=1,
                         check_events=True,
                         phase_def='P')

    for trace in calcStreamMap.iterkeys():
        recordstarttime = calcStreamMap[trace].stats.starttime
        d = calcStreamMap[trace].stats.starttime
        d = d.timestamp

        if calcStreamMap[trace].stats.npts < minSampleCount:
            minSampleCount = calcStreamMap[trace].stats.npts

    ############################################################################
    traces = num.ndarray(shape=(len(calcStreamMap), minSampleCount),
                         dtype=float)
    traveltime = num.ndarray(shape=(len(calcStreamMap), dimX * dimY),
                             dtype=float)
    latv = num.ndarray(dimX * dimY, dtype=float)
    lonv = num.ndarray(dimX * dimY, dtype=float)
    ############################################################################

    c = 0
    streamCounter = 0

    for key in calcStreamMap.iterkeys():
        streamID = key
        c2 = 0

        for o in calcStreamMap[key]:
            if c2 < minSampleCount:
                traces[c][c2] = o

                c2 += 1

        for key in TTTGridMap.iterkeys():

            if streamID == key:
                traveltimes[streamCounter] = TTTGridMap[key]
            else:
                "NEIN", streamID, key

        if not streamCounter in traveltimes:
            continue  #hs : thread crashed before

        g = traveltimes[streamCounter]
        dimZ = g.dimZ
        mint = g.mint
        maxt = g.maxt
        Latul = g.Latul
        Lonul = g.Lonul
        Lator = g.Lator
        Lonor = g.Lonor

        gridElem = g.GridArray

        for x in range(dimX):
            for y in range(dimY):
                elem = gridElem[x, y]

                traveltime[c][x * dimY + y] = elem.tt
                latv[x * dimY + y] = elem.lat
                lonv[x * dimY + y] = elem.lon
        #endfor

        c += 1
        streamCounter += 1

    #endfor

    ############################## CALCULATE PARAMETER FOR SEMBLANCE CALCULATION ##################
    nsamp = winlen * new_frequence

    nstep = int(step * new_frequence)
    migpoints = dimX * dimY

    dimZ = 0
    new_frequence = cfg.newFrequency()  # ['new_frequence']
    maxp = int(Config['ncore'])

    Logfile.add('PROCESS %d  NTIMES: %d' % (flag, ntimes))

    if False:
        print('nostat ', nostat, type(nostat))
        print('nsamp ', nsamp, type(nsamp))
        print('ntimes ', ntimes, type(ntimes))
        print('nstep ', nstep, type(nstep))
        print('dimX ', dimX, type(dimX))
        print('dimY ', dimY, type(dimY))
        print('mint ', Gmint, type(mint))
        print('new_freq ', new_frequence, type(new_frequence))
        print('minSampleCount ', minSampleCount, type(minSampleCount))
        print('latv ', latv, type(latv))
        print('traces', traces, type(traces))
        print('traveltime', traveltime, type(traveltime))


#==================================semblance calculation========================================

    t1 = time.time()
    traces = traces.reshape(1, nostat * minSampleCount)
    traveltime = traveltime.reshape(1, nostat * dimX * dimY)
    USE_C_CODE = True
    try:
        if USE_C_CODE:
            import Cm
            import CTrig
            start_time = time.time()
            k = Cm.otest(maxp, nostat, nsamp, ntimes, nstep, dimX, dimY, Gmint,
                         new_frequence, minSampleCount, latv, lonv, traveltime,
                         traces)
            print("--- %s seconds ---" % (time.time() - start_time))
        else:
            start_time = time.time()
            k = otest(maxp, nostat, nsamp, ntimes, nstep, dimX, dimY, Gmint,
                      new_frequence, minSampleCount, latv, lonv, traveltime,
                      traces)  #hs
            print("--- %s seconds ---" % (time.time() - start_time))
    except:
        print "loaded tttgrid has probably wrong dimensions or stations, delete\
                ttgrid or exchange"

    t2 = time.time()

    partSemb = k

    partSemb_syn = partSemb.reshape(ntimes, migpoints)

    return partSemb_syn
コード例 #4
0
ファイル: sembCalc.py プロジェクト: HongjianFang/Palantiri
def doCalc(flag, Config, WaveformDict, FilterMetaData, Gmint, Gmaxt,
           TTTGridMap, Folder, Origin, ntimes, switch, ev, arrayfolder,
           syn_in):
    '''
    method for calculating semblance of one station array
    '''
    Logfile.add('PROCESS %d %s' % (flag, ' Enters Semblance Calculation'))
    Logfile.add('MINT  : %f  MAXT: %f Traveltime' % (Gmint, Gmaxt))

    cfg = ConfigObj(dict=Config)
    cfg_f = FilterCfg(Config)

    timeev = util.str_to_time(ev.time)
    dimX = cfg.dimX()  #('dimx')
    dimY = cfg.dimY()  #('dimy')
    winlen = cfg.winlen()  #('winlen')
    step = cfg.step()  #('step')

    new_frequence = cfg.newFrequency()  #('new_frequence')
    forerun = cfg.Int('forerun')
    duration = cfg.Int('duration')

    nostat = len(WaveformDict)
    traveltimes = {}
    recordstarttime = ''
    minSampleCount = 999999999

    ntimes = int((forerun + duration) / step)
    nsamp = int(winlen * new_frequence)
    nstep = int(step * new_frequence)
    from pyrocko import obspy_compat
    from pyrocko import model
    obspy_compat.plant()

    ############################################################################
    calcStreamMap = WaveformDict

    stations = []
    py_trs = []
    lats = []
    lons = []
    for trace in calcStreamMap.iterkeys():
        py_tr = obspy_compat.to_pyrocko_trace(calcStreamMap[trace])
        py_trs.append(py_tr)
        for il in FilterMetaData:
            if str(il) == str(trace):
                szo = model.Station(lat=float(il.lat),
                                    lon=float(il.lon),
                                    station=il.sta,
                                    network=il.net,
                                    channels=py_tr.channel,
                                    elevation=il.ele,
                                    location=il.loc)
                stations.append(szo)
                lats.append(float(il.lat))
                lons.append(float(il.lon))
    array_center = [num.mean(lats), num.mean(lons)]

    #==================================synthetic BeamForming======================

    if cfg.Bool('synthetic_test') is True:
        store_id = syn_in.store()
        engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()])
        recordstarttimes = []
        for tracex in calcStreamMap.iterkeys():
            recordstarttimes.append(
                calcStreamMap[tracex].stats.starttime.timestamp)
            tr_org = obspy_compat.to_pyrocko_trace(calcStreamMap[tracex])
            tmin = tr_org.tmin

        #tmin= num.min(recordstarttimes)
        targets = []
        sources = []
        for st in stations:
            target = Target(lat=st.lat,
                            lon=st.lon,
                            store_id=store_id,
                            codes=(st.network, st.station, st.location, 'BHZ'),
                            tmin=-6900,
                            tmax=6900,
                            interpolation='multilinear',
                            quantity=cfg.quantity())
            targets.append(target)

        if syn_in.nsources() == 1:
            if syn_in.use_specific_stf() is True:
                stf = syn_in.stf()
                exec(stf)
            else:
                stf = STF()
            if syn_in.source() == 'RectangularSource':
                sources.append(
                    RectangularSource(
                        lat=float(syn_in.lat_0()),
                        lon=float(syn_in.lon_0()),
                        east_shift=float(syn_in.east_shift_0()) * 1000.,
                        north_shift=float(syn_in.north_shift_0()) * 1000.,
                        depth=syn_in.depth_syn_0() * 1000.,
                        strike=syn_in.strike_0(),
                        dip=syn_in.dip_0(),
                        rake=syn_in.rake_0(),
                        width=syn_in.width_0() * 1000.,
                        length=syn_in.length_0() * 1000.,
                        nucleation_x=syn_in.nucleation_x_0(),
                        slip=syn_in.slip_0(),
                        nucleation_y=syn_in.nucleation_y_0(),
                        stf=stf,
                        time=util.str_to_time(syn_in.time_0())))
            if syn_in.source() == 'DCSource':
                sources.append(
                    DCSource(lat=float(syn_in.lat_0()),
                             lon=float(syn_in.lon_0()),
                             east_shift=float(syn_in.east_shift_0()) * 1000.,
                             north_shift=float(syn_in.north_shift_0()) * 1000.,
                             depth=syn_in.depth_syn_0() * 1000.,
                             strike=syn_in.strike_0(),
                             dip=syn_in.dip_0(),
                             rake=syn_in.rake_0(),
                             stf=stf,
                             time=util.str_to_time(syn_in.time_0()),
                             magnitude=syn_in.magnitude_0()))

        else:
            for i in range(syn_in.nsources()):
                if syn_in.use_specific_stf() is True:
                    stf = syn_in.stf()
                    exec(stf)

                else:
                    stf = STF()
                if syn_in.source() == 'RectangularSource':
                    sources.append(
                        RectangularSource(
                            lat=float(syn_in.lat_1(i)),
                            lon=float(syn_in.lon_1(i)),
                            east_shift=float(syn_in.east_shift_1(i)) * 1000.,
                            north_shift=float(syn_in.north_shift_1(i)) * 1000.,
                            depth=syn_in.depth_syn_1(i) * 1000.,
                            strike=syn_in.strike_1(i),
                            dip=syn_in.dip_1(i),
                            rake=syn_in.rake_1(i),
                            width=syn_in.width_1(i) * 1000.,
                            length=syn_in.length_1(i) * 1000.,
                            nucleation_x=syn_in.nucleation_x_1(i),
                            slip=syn_in.slip_1(i),
                            nucleation_y=syn_in.nucleation_y_1(i),
                            stf=stf,
                            time=util.str_to_time(syn_in.time_1(i))))

                if syn_in.source() == 'DCSource':
                    sources.append(
                        DCSource(
                            lat=float(syn_in.lat_1(i)),
                            lon=float(syn_in.lon_1(i)),
                            east_shift=float(syn_in.east_shift_1(i)) * 1000.,
                            north_shift=float(syn_in.north_shift_1(i)) * 1000.,
                            depth=syn_in.depth_syn_1(i) * 1000.,
                            strike=syn_in.strike_1(i),
                            dip=syn_in.dip_1(i),
                            rake=syn_in.rake_1(i),
                            stf=stf,
                            time=util.str_to_time(syn_in.time_1(i)),
                            magnitude=syn_in.magnitude_1(i)))
            #source = CombiSource(subsources=sources)
        synthetic_traces = []
        for source in sources:
            response = engine.process(source, targets)
            synthetic_traces_source = response.pyrocko_traces()
            if not synthetic_traces:
                synthetic_traces = synthetic_traces_source
            else:
                for trsource, tr in zip(synthetic_traces_source,
                                        synthetic_traces):
                    tr.add(trsource)
            from pyrocko import trace as trld
            #trld.snuffle(synthetic_traces)
        timeev = util.str_to_time(syn_in.time_0())
        if cfg.Bool('synthetic_test_add_noise') is True:
            from noise_addition import add_noise
            trs_orgs = []
            calcStreamMapsyn = calcStreamMap.copy()
            #from pyrocko import trace
            for tracex in calcStreamMapsyn.iterkeys():
                for trl in synthetic_traces:
                    if str(trl.name()[4:12]) == str(tracex[4:]) or str(
                            trl.name()[3:13]) == str(tracex[3:]) or str(
                                trl.name()[3:11]) == str(tracex[3:]) or str(
                                    trl.name()[3:14]) == str(tracex[3:]):
                        tr_org = obspy_compat.to_pyrocko_trace(
                            calcStreamMapsyn[tracex])
                        tr_org.downsample_to(2.0)
                        trs_orgs.append(tr_org)
            store_id = syn_in.store()
            engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()])
            synthetic_traces = add_noise(trs_orgs,
                                         engine,
                                         source.pyrocko_event(),
                                         stations,
                                         store_id,
                                         phase_def='P')
        trs_org = []
        trs_orgs = []
        from pyrocko import trace
        fobj = os.path.join(arrayfolder, 'shift.dat')
        calcStreamMapsyn = calcStreamMap.copy()
        for tracex in calcStreamMapsyn.iterkeys():
            for trl in synthetic_traces:
                if str(trl.name()[4:12]) == str(tracex[4:]) or str(
                        trl.name()[3:13]) == str(tracex[3:]) or str(
                            trl.name()[3:11]) == str(tracex[3:]) or str(
                                trl.name()[3:14]) == str(tracex[3:]):
                    mod = trl
                    recordstarttime = calcStreamMapsyn[
                        tracex].stats.starttime.timestamp
                    recordendtime = calcStreamMapsyn[
                        tracex].stats.endtime.timestamp
                    tr_org = obspy_compat.to_pyrocko_trace(
                        calcStreamMapsyn[tracex])
                    if switch == 0:
                        tr_org.bandpass(4, cfg_f.flo(), cfg_f.fhi())
                    elif switch == 1:
                        tr_org.bandpass(4, cfg_f.flo2(), cfg_f.fhi2())
                    trs_orgs.append(tr_org)
                    tr_org_add = mod.chop(recordstarttime,
                                          recordendtime,
                                          inplace=False)
                    synthetic_obs_tr = obspy_compat.to_obspy_trace(tr_org_add)
                    calcStreamMapsyn[tracex] = synthetic_obs_tr
                    trs_org.append(tr_org_add)
        calcStreamMap = calcStreamMapsyn

    if cfg.Bool('shift_by_phase_pws') == True:
        calcStreamMapshifted = calcStreamMap.copy()
        from obspy.core import stream
        stream = stream.Stream()
        for trace in calcStreamMapshifted.iterkeys():
            stream.append(calcStreamMapshifted[trace])
        pws_stack = PWS_stack([stream], weight=2, normalize=True)
        for tr in pws_stack:
            for trace in calcStreamMapshifted.iterkeys():
                calcStreamMapshifted[trace] = tr
        calcStreamMap = calcStreamMapshifted

    if cfg.Bool('shift_by_phase_cc') is True:
        from stacking import align_traces
        calcStreamMapshifted = calcStreamMap.copy()
        list_tr = []
        for trace in calcStreamMapshifted.iterkeys():
            tr_org = calcStreamMapshifted[trace]
            list_tr.append(tr_org)
        shifts, ccs = align_traces(list_tr, 10, master=False)
        for shift in shifts:
            for trace in calcStreamMapshifted.iterkeys():
                tr_org = obspy_compat.to_pyrocko_trace(
                    calcStreamMapshifted[trace])
                tr_org.shift(shift)
                shifted = obspy_compat.to_obspy_trace(tr_org)
                calcStreamMapshifted[trace] = shifted
        calcStreamMap = calcStreamMapshifted

    if cfg.Bool('shift_by_phase_onset') is True:
        pjoin = os.path.join
        timeev = util.str_to_time(ev.time)
        trs_orgs = []
        calcStreamMapshifted = calcStreamMap.copy()
        for trace in calcStreamMapshifted.iterkeys():
            tr_org = obspy_compat.to_pyrocko_trace(calcStreamMapshifted[trace])
            trs_orgs.append(tr_org)

        timing = CakeTiming(
            phase_selection='first(p|P|PP|P(cmb)P(icb)P(icb)p(cmb)p)-20',
            fallback_time=100.)
        traces = trs_orgs

        event = model.Event(lat=float(ev.lat),
                            lon=float(ev.lon),
                            depth=ev.depth * 1000.,
                            time=timeev)
        directory = arrayfolder
        bf = BeamForming(stations, traces, normalize=True)
        shifted_traces = bf.process(event=event,
                                    timing=timing,
                                    fn_dump_center=pjoin(
                                        directory, 'array_center.pf'),
                                    fn_beam=pjoin(directory, 'beam.mseed'))
        i = 0
        store_id = syn_in.store()
        engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()])
        for tracex in calcStreamMapshifted.iterkeys():
            for trl in shifted_traces:
                if str(trl.name()[4:12]) == str(tracex[4:]) or str(
                        trl.name()[3:13]) == str(tracex[3:]) or str(
                            trl.name()[3:11]) == str(tracex[3:]) or str(
                                trl.name()[3:14]) == str(tracex[3:]):
                    mod = trl
                    recordstarttime = calcStreamMapshifted[
                        tracex].stats.starttime.timestamp
                    recordendtime = calcStreamMapshifted[
                        tracex].stats.endtime.timestamp
                    tr_org = obspy_compat.to_pyrocko_trace(
                        calcStreamMapshifted[tracex])
                    tr_org_add = mod.chop(recordstarttime,
                                          recordendtime,
                                          inplace=False)
                    shifted_obs_tr = obspy_compat.to_obspy_trace(tr_org_add)
                    calcStreamMapshifted[tracex] = shifted_obs_tr
        calcStreamMap = calcStreamMapshifted

    weight = 1.
    if cfg.Bool('weight_by_noise') is True:
        from noise_analyser import analyse
        pjoin = os.path.join
        timeev = util.str_to_time(ev.time)
        trs_orgs = []
        calcStreamMapshifted = calcStreamMap.copy()
        for trace in calcStreamMapshifted.iterkeys():
            tr_org = obspy_compat.to_pyrocko_trace(calcStreamMapshifted[trace])
            trs_orgs.append(tr_org)

        timing = CakeTiming(
            phase_selection='first(p|P|PP|P(cmb)P(icb)P(icb)p(cmb)p)-20',
            fallback_time=100.)
        traces = trs_orgs
        event = model.Event(lat=float(ev.lat),
                            lon=float(ev.lon),
                            depth=ev.depth * 1000.,
                            time=timeev)
        directory = arrayfolder
        bf = BeamForming(stations, traces, normalize=True)
        shifted_traces = bf.process(event=event,
                                    timing=timing,
                                    fn_dump_center=pjoin(
                                        directory, 'array_center.pf'),
                                    fn_beam=pjoin(directory, 'beam.mseed'))
        i = 0
        store_id = syn_in.store()
        engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()])
        weight = analyse(shifted_traces,
                         engine,
                         event,
                         stations,
                         100.,
                         store_id,
                         nwindows=1,
                         check_events=True,
                         phase_def='P')

    if cfg.Bool('array_response') is True:
        from obspy.signal import array_analysis
        from obspy.core import stream
        ntimesr = int((forerun + duration) / step)
        nsampr = int(winlen)
        nstepr = int(step)
        sll_x = -3.0
        slm_x = 3.0
        sll_y = -3.0
        slm_y = 3.0
        sl_s = 0.03,
        # sliding window properties

        # frequency properties
        frqlow = 1.0,
        frqhigh = 8.0
        prewhiten = 0
        # restrict output
        semb_thres = -1e9
        vel_thres = -1e9
        stime = stime
        etime = etime
        stream_arr = stream.Stream()
        for trace in calcStreamMapshifted.iterkeys():
            stream_arr.append(calcStreamMapshifted[trace])
        results = array_analysis.array_processing(stream_arr, nsamp, nstep,\
                                                  sll_x, slm_x, sll_y, slm_y,\
                                                   sl_s, semb_thres, vel_thres, \
                                                   frqlow, frqhigh, stime, \
                                                   etime, prewhiten)
        timestemp = results[0]
        relative_relpow = results[1]
        absolute_relpow = results[2]

    for trace in calcStreamMap.iterkeys():
        recordstarttime = calcStreamMap[trace].stats.starttime
        d = calcStreamMap[trace].stats.starttime
        d = d.timestamp

        if calcStreamMap[trace].stats.npts < minSampleCount:
            minSampleCount = calcStreamMap[trace].stats.npts

    ###########################################################################

    traces = num.ndarray(shape=(len(calcStreamMap), minSampleCount),
                         dtype=float)
    traveltime = num.ndarray(shape=(len(calcStreamMap), dimX * dimY),
                             dtype=float)

    latv = num.ndarray(dimX * dimY, dtype=float)
    lonv = num.ndarray(dimX * dimY, dtype=float)
    ###########################################################################

    c = 0
    streamCounter = 0

    for key in calcStreamMap.iterkeys():
        streamID = key
        c2 = 0

        for o in calcStreamMap[key]:
            if c2 < minSampleCount:
                traces[c][c2] = o

                c2 += 1

        for key in TTTGridMap.iterkeys():

            if streamID == key:
                traveltimes[streamCounter] = TTTGridMap[key]
            else:
                "NEIN", streamID, key

        if not streamCounter in traveltimes:
            continue  #hs : thread crashed before

        g = traveltimes[streamCounter]
        dimZ = g.dimZ
        mint = g.mint
        gridElem = g.GridArray

        for x in range(dimX):
            for y in range(dimY):
                elem = gridElem[x, y]

                traveltime[c][x * dimY + y] = elem.tt
                latv[x * dimY + y] = elem.lat
                lonv[x * dimY + y] = elem.lon
        #endfor

        c += 1
        streamCounter += 1

    #endfor

    ################ CALCULATE PARAMETER FOR SEMBLANCE CALCULATION ########
    nsamp = winlen * new_frequence

    nstep = step * new_frequence
    migpoints = dimX * dimY

    dimZ = 0
    maxp = int(Config['ncore'])

    Logfile.add('PROCESS %d  NTIMES: %d' % (flag, ntimes))

    if False:
        print('nostat ', nostat, type(nostat))
        print('nsamp ', nsamp, type(nsamp))
        print('ntimes ', ntimes, type(ntimes))
        print('nstep ', nstep, type(nstep))
        print('dimX ', dimX, type(dimX))
        print('dimY ', dimY, type(dimY))
        print('mint ', Gmint, type(mint))
        print('new_freq ', new_frequence, type(new_frequence))
        print('minSampleCount ', minSampleCount, type(minSampleCount))
        print('latv ', latv, type(latv))
        print('traces', traces, type(traces))

#===================compressed sensing=================================
    try:
        cs = cfg.cs()
    except:
        cs = 0
    if cs == 1:
        csmaxvaluev = num.ndarray(ntimes, dtype=float)
        csmaxlatv = num.ndarray(ntimes, dtype=float)
        csmaxlonv = num.ndarray(ntimes, dtype=float)
        folder = Folder['semb']
        fobjcsmax = open(os.path.join(folder, 'csmax_%s.txt' % (switch)), 'w')
        traveltimes = traveltime.reshape(1, nostat * dimX * dimY)
        traveltime2 = toMatrix(traveltimes, dimX * dimY)  # for relstart
        traveltime = traveltime.reshape(dimX * dimY, nostat)
        import matplotlib as mpl
        import scipy.optimize as spopt
        import scipy.fftpack as spfft
        import scipy.ndimage as spimg
        import cvxpy as cvx
        import matplotlib.pyplot as plt
        A = spfft.idct(traveltime, norm='ortho', axis=0)
        n = (nostat * dimX * dimY)
        vx = cvx.Variable(dimX * dimY)
        res = cvx.Variable(1)
        objective = cvx.Minimize(cvx.norm(res, 1))
        back2 = num.zeros([dimX, dimY])
        l = int(nsamp)
        fobj = open(
            os.path.join(folder,
                         '%s-%s_%03d.cs' % (switch, Origin['depth'], l)), 'w')
        for i in range(ntimes):
            ydata = []
            try:
                for tr in traces:
                    relstart = int((dimX * dimY - mint) * new_frequence +
                                   0.5) + i * nstep
                    tr = spfft.idct(tr[relstart + i:relstart + i +
                                       dimX * dimY],
                                    norm='ortho',
                                    axis=0)

                    ydata.append(tr)
                    ydata = num.asarray(ydata)
                    ydata = ydata.reshape(dimX * dimY, nostat)

                    constraints = [
                        res == cvx.sum_entries(0 + num.sum([
                            ydata[:, x] - A[:, x] * vx for x in range(nostat)
                        ]))
                    ]

                    prob = cvx.Problem(objective, constraints)
                    result = prob.solve(verbose=False, max_iters=200)

                    x = num.array(vx.value)
                    x = num.squeeze(x)
                    back1 = x.reshape(dimX, dimY)
                    sig = spfft.idct(x, norm='ortho', axis=0)
                    back2 = back2 + back1
                    xs = num.array(res.value)
                    xs = num.squeeze(xs)
                    max_cs = num.max(back1)
                    idx = num.where(back1 == back1.max())
                    csmaxvaluev[i] = max_cs
                    csmaxlatv[i] = latv[idx[0]]
                    csmaxlonv[i] = lonv[idx[1]]
                    fobj.write('%.5f %.5f %.20f\n' %
                               (latv[idx[0]], lonv[idx[1]], max_cs))
                    fobjcsmax.write('%.5f %.5f %.20f\n' %
                                    (latv[idx[0]], lonv[idx[1]], max_cs))
                fobj.close()
                fobjcsmax.close()

            except:
                pass

#==================================semblance calculation========================================

    t1 = time.time()
    traces = traces.reshape(1, nostat * minSampleCount)

    traveltimes = traveltime.reshape(1, nostat * dimX * dimY)
    USE_C_CODE = False
    #try:
    if USE_C_CODE:
        import Cm
        import CTrig
        start_time = time.time()
        k = Cm.otest(maxp, nostat, nsamp, ntimes, nstep, dimX, dimY, Gmint,
                     new_frequence, minSampleCount, latv, lonv, traveltimes,
                     traces)
        print("--- %s seconds ---" % (time.time() - start_time))
    else:
        start_time = time.time()
        ntimes = int((forerun + duration) / step)
        nsamp = int(winlen)
        nstep = int(step)
        Gmint = cfg.Int('forerun')
        k = otest(maxp, nostat, nsamp, ntimes, nstep, dimX, dimY, Gmint,
                  new_frequence, minSampleCount, latv, lonv, traveltimes,
                  traces, calcStreamMap, timeev)
        print("--- %s seconds ---" % (time.time() - start_time))
    #except ValueError:
    #        k  = Cm.otest(maxp,nostat,nsamp,ntimes,nstep,dimX,dimY,Gmint,new_frequence,
    #                      minSampleCount,latv,lonv,traveltimes,traces)
    #    print "loaded tttgrid has probably wrong dimensions or stations,\
    #                delete ttgrid or exchange is recommended"

    t2 = time.time()

    Logfile.add('%s took %0.3f s' % ('CALC:', (t2 - t1)))

    partSemb = k
    partSemb = partSemb.reshape(ntimes, migpoints)

    return partSemb, weight, array_center
コード例 #5
0
ファイル: sembCalc.py プロジェクト: HongjianFang/Palantiri
def collectSemb(SembList, Config, Origin, Folder, ntimes, arrays, switch,
                array_centers):
    '''
    method to collect semblance matrizes from all processes and write them to file for each timestep
    '''
    Logfile.add('start collect in collectSemb')
    cfg = ConfigObj(dict=Config)
    origin = ConfigObj(dict=Origin)

    dimX = cfg.dimX()  #('dimx')
    dimY = cfg.dimY()  #('dimy')
    if switch == 0:
        winlen = cfg.winlen()  #('winlen')
        step = cfg.step()  #('step')
    if switch == 1:
        winlen = cfg.winlen_f2()  #('winlen')
        step = cfg.step_f2()  #('step')

    latv = []
    lonv = []

    gridspacing = cfg.Float('gridspacing')
    migpoints = dimX * dimY
    o_lat = origin.lat()  # float(Origin['lat'])
    o_lon = origin.lon()  # float(Origin['lon'])
    oLatul = 0
    oLonul = 0

    z = 0

    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

            latv.append(oLatul)
            lonv.append(oLonul)

    tmp = 1
    origin = DataTypes.dictToLocation(Origin)
    i = 0

    #for a in SembList:
    #    tmp = num.zeros(num.shape(a))
    azis = []
    for a in SembList:
        x = array_centers[i][0]
        y = array_centers[i][1]
        delta = orthodrome.distance_accurate50m_numpy(x, y, origin.lat,
                                                      origin.lon)
        #a = a*((1./delta**2)*1.e+15)
        tmp *= a

        #azis.append(toAzimuth(float(Origin['lat']), float(Origin['lon']),x, y))
        i = i + 1

    #min_coor = num.zeros([i,2])
    #i = 0
    #for a in SembList:
    #    deltas = []
#        x = array_centers[i][0]
#        y = array_centers[i][1]
#        for k in range(0,len(latv)):
#            delta = orthodrome.distance_accurate50m_numpy(x, y, latv[k], lonv[k])
#            deltas.append(orthodrome.distance_accurate50m_numpy(x, y, latv[k], lonv[k]))
#            if delta <= num.min(deltas):
#                min_coor[i]= [latv[k], lonv[k]]
#        i = i+1
#    array_overlap = num.average(min_coor, axis=0)
#    delta_center = orthodrome.distance_accurate50m_numpy(array_overlap[0], array_overlap[1], origin.lat, origin.lon)

#    print(array_overlap)

#    print(delta_center)
#    diff_center_lat = origin.lat-array_overlap[0]
#    diff_center_lon = origin.lon-array_overlap[1]
#    print(diff_center_lat)
#    print(diff_center_lon)
#for a in SembList:
#if num.mean(a)>0:
#        tmp *= a

    sembmaxvaluev = num.ndarray(ntimes, dtype=float)
    sembmaxlatv = num.ndarray(ntimes, dtype=float)
    sembmaxlonv = num.ndarray(ntimes, dtype=float)

    rc = UTCDateTime(Origin['time'])
    rcs = '%s-%s-%s_%02d:%02d:%02d' % (rc.day, rc.month, rc.year, rc.hour,
                                       rc.minute, rc.second)
    d = rc.timestamp

    usedarrays = arrays
    folder = Folder['semb']
    fobjsembmax = open(os.path.join(folder, 'sembmax_%s.txt' % (switch)), 'w')
    norm = num.max(num.max(tmp, axis=1))
    max_p = 0.
    sum_i = 0.
    for a, i in enumerate(tmp):
        if a < 1:
            sum_i *= i
    for a, i in enumerate(tmp):
        if a < 1:
            max = num.max(sum_i[:])
            for j in range(migpoints):
                if i[j] > num.max(i[:]) * 0.9 and i[j] > max_p:
                    latvmax = latv[j]
                    lonvmax = lonv[j]
                    max_p = i[j]

#    delta_lat = origin.lat-latvmax
#    delta_lon = origin.lon-lonvmax

#for a, i in enumerate(tmp):
#    max_pos = [l for l, k in enumerate(i) if k == i.max()][0]
#        delta_lat = origin.lat-latv[max_pos]
#        delta_lon = origin.lon-lonv[max_pos]
    for j in range(migpoints):
        latv[j] = latv[j]  #+delta_lat
        lonv[j] = lonv[j]  #+delta_lon
    #        latv.append(latv[j]-delta_lat)
    #        lonv.append(lonv[j]-delta_lon)

    #nix = []
    #for a, i in enumerate(tmp):
    #    for j in range(migpoints):
    #            if i[j]/norm > num.max(sum_i/norm)*0.4:
    #                if j in nix:
    #                    pass
    #                else:
    #                    latv[j] = latv[j]+delta_lat
    #                    lonv[j] = lonv[j]+delta_lon
    #                    nix.append(j)
    #if i[j]/norm > num.max(sum_i/norm)*0.4:
    #    print('yes')
    #    delta_lat = origin.lat-latv[j]
    #    delta_lon = origin.lon-lonv[j]
    #    print delta_lat, delta_lon, latvmax, lonvmax
    #    print latv[j], lonv[j], origin.lat, origin.lon
    #    ix = num.where(latv[j]+delta_lat)[0][0]
    #    iy = num.where(lonv[j]+delta_lon)[0][0]
    #    lat = latv[j].copy()
    #    lon = lonv[j].copy()
    #    latv[j] = latv[ix]
    ##    lonv[j] =  lonv[iy]
    #    lonv[iy]
    #    #latv[j] = latv[j]+delta_lat
    #lonv[j] = lonv[j]+delta_lon
    #    print latv[j], lonv[j]


#

    for a, i in enumerate(tmp):
        logger.info('timestep %d' % a)
        print(a)

        fobj = open(
            os.path.join(folder,
                         '%s-%s_%03d.ASC' % (switch, Origin['depth'], a)), 'w')

        fobj.write('# %s , %s\n' % (d, rcs))
        fobj.write('# step %ds| ntimes %d| winlen: %ds\n' %
                   (step, ntimes, winlen))
        fobj.write('# \n')
        fobj.write('# southwestlat: %.2f dlat: %f nlat: %f \n' %
                   (Latul, gridspacing, dimX))
        fobj.write('# southwestlon: %.2f dlon: %f nlon: %f \n' %
                   (Lonul, gridspacing, dimY))
        fobj.write('# ddepth: 0 ndepth: 1 \n')

        sembmax = 0
        sembmaxX = 0
        sembmaxY = 0

        uncert = num.std(i)  #maybe not std?
        for j in range(migpoints):

            x = latv[j]  #+delta_lat
            y = lonv[j]  #+delta_lon
            #    if i[j]/norm > num.max(i[:]/norm)*0.1:
            #            delta_lat = origin.lat-latv[max_pos]
            #            delta_lon = origin.lon-lonv[max_pos]
            #            print delta_lat, delta_lon, latv[max_pos], lonv[max_pos]
            #            print latv[j], lonv[j], origin.lat, origin.lon
            #        x = latv[j]+delta_lat
            #            y = lonv[j]+delta_lon
            #            print x, y
            semb = i[j] / norm
            fobj.write('%.2f %.2f %.20f\n' % (x, y, semb))
            #    xd= latv[j]-delta_lat
            #        yd= lonv[j]-delta_lon
            #            sembd = 0.
            #            fobj.write('%.2f %.2f %.20f\n' %(xd,yd,sembd))

            if semb > sembmax:
                sembmax = semb
                # search for maximum and position of maximum on semblance grid for given time step
                sembmaxX = x
                sembmaxY = y

        delta = orthodrome.distance_accurate50m_numpy(x, y, origin.lat,
                                                      origin.lon)
        azi = toAzimuth(float(Origin['lat']), float(Origin['lon']),
                        float(sembmaxX), float(sembmaxY))

        sembmaxvaluev[a] = sembmax
        sembmaxlatv[a] = sembmaxX
        sembmaxlonv[a] = sembmaxY
        fobjsembmax.write('%d %.3f %.3f %.30f %.30f %d %03f %f %03f\n' %
                          (a * step, sembmaxX, sembmaxY, sembmax, uncert,
                           usedarrays, delta, float(azi), delta * 119.19))
        fobj.close()

    fobjsembmax.close()

    trigger.writeSembMaxValue(sembmaxvaluev, sembmaxlatv, sembmaxlonv, ntimes,
                              Config, Folder)
    inspect_semb = cfg.Bool('inspect_semb')
    if inspect_semb is True:
        trigger.semblancestalta(sembmaxvaluev, sembmaxlatv, sembmaxlonv)
    return sembmaxvaluev
コード例 #6
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)
コード例 #7
0
ファイル: ttt.py プロジェクト: Luojiahong/Palantiri
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)
コード例 #8
0
ファイル: optim_csemb.py プロジェクト: HongjianFang/Palantiri
def  doCalc_syn (flag,Config,WaveformDict,FilterMetaData,Gmint,Gmaxt,TTTGridMap,
                Folder,Origin, ntimes, switch, ev,arrayfolder, syn_in, parameter):
    '''
    method for calculating semblance of one station array
    '''
    Logfile.add ('PROCESS %d %s' % (flag,' Enters Semblance Calculation') )
    Logfile.add ('MINT  : %f  MAXT: %f Traveltime' % (Gmint,Gmaxt))

    cfg = ConfigObj (dict=Config)

    dimX   = cfg.dimX()         # ('dimx')
    dimY   = cfg.dimY()         # ('dimy')
    winlen = cfg.winlen ()      # ('winlen')
    step   = cfg.step()         # ('step')

    new_frequence   = cfg.newFrequency()          #('new_frequence')
    forerun= cfg.Int('forerun')
    duration= cfg.Int('duration')
    gridspacing = cfg.Float('gridspacing')

    nostat = len (WaveformDict)
    traveltimes = {}
    recordstarttime = ''
    minSampleCount  = 999999999

    ntimes = int ((forerun + duration)/step)
    nsamp  = int (winlen * new_frequence)
    nstep  = int (step   * new_frequence)
    from pyrocko import obspy_compat
    from pyrocko import orthodrome, model
    obspy_compat.plant()

    ############################################################################
    calcStreamMap = WaveformDict

    stations = []
    py_trs = []
    for trace in calcStreamMap.iterkeys():
        py_tr = obspy_compat.to_pyrocko_trace(calcStreamMap[trace])
        py_trs.append(py_tr)
        for il in FilterMetaData:
            if str(il) == str(trace):
                        szo = model.Station(lat=il.lat, lon=il.lon,
                                            station=il.sta, network=il.net,
                                            channels=py_tr.channel,
                                            elevation=il.ele, location=il.loc)
			stations.append(szo) #right number of stations?


    store_id = syn_in.store()
    engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()])

        targets = []
        for st in stations:
            target = Target(
                    lat=st.lat,
                    lon=st.lon,
                    store_id=store_id,
                    codes=(st.network, st.station, st.location, 'BHZ'),
                    tmin=-1900,
                    tmax=3900,
                    interpolation='multilinear',
                    quantity=cfg.quantity())
            targets.append(target)

        if syn_in.nsources() == 1:
            if syn_in.use_specific_stf() is True:
                stf = syn_in.stf()
                exec(stf)
            else:
                stf = STF()
            if syn_in.source() == 'RectangularSource':
                    source = RectangularSource(
                        lat=float(syn_in.lat_0()),
                        lon=float(syn_in.lon_0()),
                        depth=syn_in.depth_syn_0()*1000.,
                        strike=syn_in.strike_0(),
                        dip=syn_in.dip_0(),
                        rake=syn_in.rake_0(),
                        width=syn_in.width_0()*1000.,
                        length=syn_in.length_0()*1000.,
                        nucleation_x=syn_in.nucleation_x_0(),
                        slip=syn_in.slip_0(),
                        nucleation_y=syn_in.nucleation_y_0(),
                        stf=stf,
                        time=util.str_to_time(syn_in.time_0()))
            if syn_in.source() == 'DCSource':
                    source = DCSource(
                        lat=float(syn_in.lat_0()),
                        lon=float(syn_in.lon_0()),
                        depth=syn_in.depth_syn_0()*1000.,
                        strike=syn_in.strike_0(),
                        dip=syn_in.dip_0(),
                        rake=syn_in.rake_0(),
                        stf=stf,
                        time=util.str_to_time(syn_in.time_0()),
                        magnitude=syn_in.magnitude_0())

        else:
            sources = []
            for i in range(syn_in.nsources()):
                if syn_in.use_specific_stf() is True:
                    stf = syn_in.stf()
                    exec(stf)

                else:
                    stf = STF()
                if syn_in.source() == 'RectangularSource':
                        sources.append(RectangularSource(
                            lat=float(syn_in.lat_1(i)),
                            lon=float(syn_in.lon_1(i)),
                            depth=syn_in.depth_syn_1(i)*1000.,
                            strike=syn_in.strike_1(i),
                            dip=syn_in.dip_1(i),
                            rake=syn_in.rake_1(i),
                            width=syn_in.width_1(i)*1000.,
                            length=syn_in.length_1(i)*1000.,
                            nucleation_x=syn_in.nucleation_x_1(i),
                            slip=syn_in.slip_1(i),
                            nucleation_y=syn_in.nucleation_y_1(i),
                            stf=stf,
                            time=util.str_to_time(syn_in.time_1(i))))

                if syn_in.source() == 'DCSource':
                        sources.append(DCSource(
                            lat=float(syn_in.lat_1(i)),
                            lon=float(syn_in.lon_1(i)),
                            depth=syn_in.depth_1(i)*1000.,
                            strike=syn_in.strike_1(i),
                            dip=syn_in.dip_1(i),
                            rake=syn_in.rake_1(i),
                            stf=stf,
                            time=util.str_to_time(syn_in.time_1(i)),
                            magnitude=syn_in.magnitude_1(i)))
            source = CombiSource(subsources=sources)
        response = engine.process(source, targets)

        synthetic_traces = response.pyrocko_traces()
        if cfg.Bool('synthetic_test_add_noise') is True:
            from noise_addition import add_noise
            trs_orgs = []
            calcStreamMapsyn = calcStreamMap.copy()
            #from pyrocko import trace
            for tracex in calcStreamMapsyn.iterkeys():
                    for trl in synthetic_traces:
                        if str(trl.name()[4:12])== str(tracex[4:]):
                            tr_org = obspy_compat.to_pyrocko_trace(calcStreamMapsyn[tracex])
                            tr_org.downsample_to(2.0)
                            trs_orgs.append(tr_org)
            store_id = syn_in.store()
            engine = LocalEngine(store_superdirs=[syn_in.store_superdirs()])
            synthetic_traces = add_noise(trs_orgs, engine, source.pyrocko_event(),
                                         stations,
                                         store_id, phase_def='P')
        trs_org = []
        trs_orgs = []
        fobj = os.path.join(arrayfolder, 'shift.dat')
        xy = num.loadtxt(fobj, usecols=1, delimiter=',')
        calcStreamMapsyn = calcStreamMap.copy()
        #from pyrocko import trace
        for tracex in calcStreamMapsyn.iterkeys():
                for trl in synthetic_traces:
                    if str(trl.name()[4:12])== str(tracex[4:]):
                        mod = trl

                        recordstarttime = calcStreamMapsyn[tracex].stats.starttime.timestamp
                        recordendtime = calcStreamMapsyn[tracex].stats.endtime.timestamp
                        tr_org = obspy_compat.to_pyrocko_trace(calcStreamMapsyn[tracex])
                        trs_orgs.append(tr_org)

                        tr_org_add = mod.chop(recordstarttime, recordendtime, inplace=False)
                        synthetic_obs_tr = obspy_compat.to_obspy_trace(tr_org_add)
                        calcStreamMapsyn[tracex] = synthetic_obs_tr
                        trs_org.append(tr_org_add)
        calcStreamMap = calcStreamMapsyn