Esempio n. 1
0
    def filterpyrockoWaveform(self, Waveform):
        obspy_compat.plant()
        Logfile.red('Filter Waveform: ')
        cfg = FilterCfg(self.Config)

        new_frequence = (cfg.newFrequency())
        st = Stream()
        for i in Waveform:
            tr = obspy_compat.to_pyrocko_trace(i)
            tr.downsample_to(new_frequence)

            Logfile.red('Downsampling to %d: from %d' %
                        (new_frequence, i.stats.sampling_rate))

            if switch == 0:
                Logfile.add('bandpass filtered stream for station %s ' % (i))

                tr.bandpass(4, cfg.flo, cfg.fhi)
            elif switch == 1:
                Logfile.add('bandpass filtered stream for station %s ' % (i))
            j = obspy_compat.to_obspy_trace(tr)

            st.append(j)

        return st
    def readWaveformsPicker_pyrocko(self, station, tw, Origin, ttime, cfg_yaml):

        obspy_compat.plant()
        cfg = ConfigObj(dict=self.Config)
        if cfg_yaml.config_data.quantity == 'displacement':
            try:
                traces = io.load(self.EventPath+'/data/traces_rotated.mseed')
            except:
                traces = io.load(self.EventPath+'/data/traces_restituted.mseed')
        else:
            traces = io.load(self.EventPath+'/data/traces_velocity.mseed')
        for tr in traces:
            tr_name = str(tr.network+'.'+tr.station+'.'+tr.location+'.'
                                    + tr.channel[:3])
            if tr_name == str(station)[:-2] or tr_name == str(station)[:]:
                traces_station = tr
                es = obspy_compat.to_obspy_trace(traces_station)

                st = obspy.Stream()
                st.extend([es])
                stream = ''

                if station.loc == '--':
                    station.loc = ''

                if len(st.get_gaps()) > 0:
                    st.merge(method=0, fill_value='interpolate',
                             interpolation_samples=0)
                stream = self.filterWaveform(st, cfg_yaml)

                stream.trim(tw['xcorrstart'], tw['xcorrend'])
                return stream

        else:
            pass
Esempio n. 3
0
def readWaveformsPyrocko_restituted(stationlist, w, EventPath, Origin,
                                    desired):
    Wdict = OrderedDict()
    if desired is 'Z':
        try:
            traces = io.load(EventPath + '/data/traces_rotated.mseed')
        except Exception:
            traces = io.load(EventPath + '/data/traces_restituted.mseed')
    else:
        traces = io.load(EventPath + '/data/traces_rotated.mseed')

    obspy_compat.plant()
    traces_dict = []
    for tr in traces:
        for il in stationlist:
            tr_name = str(tr.network + '.' + tr.station + '.' + tr.location +
                          '.')
            if (tr_name == str(il) and tr.channel[-1] == desired) or (
                    tr_name == str(il)[:-3] and tr.channel[-1] == desired):
                st = obspy.Stream()
                es = obspy_compat.to_obspy_trace(tr)
                st.extend([es])
                traces_dict.append(tr)
                Wdict[il.getName()] = st

    return Wdict
Esempio n. 4
0
def processpyrockoWaveforms(WaveformDict, Config, Folder, network, MetaDict,
                            Event, switch, Xcorr):
    WaveformDict_obs = []
    obspy_compat.plant()
    Logfile.red('Start Processing')
    cfg = FilterCfg(Config)
    new_frequence = cfg.newFrequency()  #  ['new_frequence']

    traces = []
    for tr in WaveformDict:
        Logfile.add(
            '%s:%s -------------------------------------------------------------'
            % (index, i))

        if Config['export_unfiltered'].capitalize() == 'True':
            tr = obspy_compat.to_obspy_trace(tr)
            writeWaveform(Folder, i, tr, 'U', network)

        if switch == 0:
            Logfile.add('bandpass filtered stream for station %s ' % (i))

            tr.bandpass(4, cfg.flo, cfg.fhi)
        elif switch == 1:
            Logfile.add('bandpass filtered stream for station %s ' % (i))

            tr.bandpass(4, cfg.flo2, cfg.fhi2)
        tr.downsample_to(new_frequence)
        tr = obspy_compat.to_obspy_trace(tr)
        traces.append(tr)
        stream = obspy.Stream()
        Wdict = stream.extend([tr])
        WaveformDict_obs.append(Wdict)

    return WaveformDict_obs
    def readWaveformsCross_pyrocko(self, station, tw, ttime):
        obspy_compat.plant()
        cfg = ConfigObj(dict=self.Config)

        t2 = UTCDateTime(self.Origin.time)
        if cfg.quantity() == 'displacement':
            try:
                traces = io.load(self.EventPath + '/data/traces_rotated.mseed')
            except Exception:
                traces = io.load(self.EventPath +
                                 '/data/traces_restituted.mseed')
        else:
            traces = io.load(self.EventPath + '/data/traces_velocity.mseed')

        found = False
        while found is False:
            for tr in traces:
                tr_name = str(tr.network + '.' + tr.station + '.' +
                              tr.location + '.' + tr.channel[:3])

                if tr_name == str(station)[:-2] or tr_name == str(station)[:]:
                    traces_station = tr
                    es = obspy_compat.to_obspy_trace(traces_station)
                    streamData = station.net + '.' + station.sta + '.'\
                                             + station.loc + '.'\
                                             + station.comp\
                                             + '.D.'\
                                             + str(t2.year) + '.'\
                                             + str("%03d" % t2.julday)

                    st = obspy.Stream()
                    st.extend([es])
                    stream = ''
                    snr = ''
                    if station.loc == '--':
                        station.loc = ''

                    if len(st.get_gaps()) > 0:
                        st.merge(method=0,
                                 fill_value='interpolate',
                                 interpolation_samples=0)
                    snr_trace = traces_station.chop(tmin=traces_station.tmin,
                                                    tmax=traces_station.tmin +
                                                    ttime - 20.,
                                                    inplace=False)
                    snr = num.var(snr_trace.ydata)
                    stream = self.filterWaveform(st)

                    xname = os.path.join(self.AF, (streamData + '_all.mseed'))
                    stream.write(xname, format='MSEED')
                    stream.trim(tw['xcorrstart'], tw['xcorrend'])
                    found = True
                    return stream, snr

        if found is False:
            print('Waveform missing!', tr_name, str(station))
Esempio n. 6
0
def load_obspy_data(datadir):
    """
    Load data from the directory through obspy and convert to pyrocko objects.

    Parameters
    ----------
    datadir : string
        absolute path to the data directory

    Returns
    -------
    data_traces, stations
    """

    import obspy
    from pyrocko import obspy_compat

    obspy_compat.plant()

    filenames = set(glob(datadir + '/*'))
    remaining_f = copy.deepcopy(filenames)
    print(filenames)
    stations = []
    for f in filenames:
        print(f)
        try:
            inv = obspy.read_inventory(f)
            stations.extend(inv.to_pyrocko_stations())
            remaining_f.discard(f)
        except TypeError:
            logger.debug('File %s not an inventory.' % f)

    filenames = copy.deepcopy(remaining_f)
    print(filenames)
    data_traces = []
    for f in filenames:
        print(f)
        try:
            stream = obspy.read(f)
            pyrocko_traces = stream.to_pyrocko_traces()
            for tr in pyrocko_traces:
                data_traces.append(heart.SeismicDataset.from_pyrocko_trace(tr))

            remaining_f.discard(f)

        except TypeError:
            logger.debug('File %s not waveforms' % f)

    print(remaining_f)
    if len(remaining_f) > 0:
        logger.warning('Could not import these files %s' %
                       utility.list2string(list(filenames)))

    logger.info('Imported %i data_traces and %i stations' %
                (len(stations), len(data_traces)))
    return stations, data_traces
Esempio n. 7
0
    def readWaveformsPicker_colos(self, station, tw, Origin, ttime):

        obspy_compat.plant()
        pjoin = os.path.join
        Config = self.Config
        cfg = ConfigObj(dict=Config)
        Syn_in = self.Syn_in
        syn_in = SynthCfg(Syn_in)

        t2 = UTCDateTime(self.Origin.time)
        sdspath = os.path.join(self.EventPath, 'data')
        traces = io.load(cfg.colosseo_scenario_yml()[:-12] + 'scenario.mseed')

        for tr in traces:
            tr_name = str(tr.network + '.' + tr.station + '.' + tr.location +
                          '.' + tr.channel[:3])
            if tr_name == str(station):
                traces_station = tr

                es = obspy_compat.to_obspy_trace(traces_station)
                streamData = station.net + '.' + station.sta + '.' + station.loc + '.' + station.comp + '.D.' + str(
                    t2.year) + '.' + str("%03d" % t2.julday)

                entry = os.path.join(sdspath, station.net, station.sta,
                                     station.comp + '.D', streamData)

                #stl = es.trim(starttime=tw['start'], endtime=tw['end'])
                st = obspy.Stream()
                st.extend([es])
                stream = ''
                snr = ''

                if station.loc == '--':
                    station.loc = ''

                if len(st.get_gaps()) > 0:
                    st.merge(method=0,
                             fill_value='interpolate',
                             interpolation_samples=0)
                #snr  = self.signoise(st[0], ttime, entry)
                snr_trace = traces_station.chop(tmin=traces_station.tmin,
                                                tmax=traces_station.tmin +
                                                ttime - 20.,
                                                inplace=False)
                snr = num.var(snr_trace.ydata)
                stream = self.filterWaveform(st)

                xname = os.path.join(self.AF, (streamData + '_all.mseed'))
                stream.write(xname, format='MSEED')
                stream.trim(tw['xcorrstart'], tw['xcorrend'])
                return stream

            else:
                pass
Esempio n. 8
0
def readWaveformsPyrocko_restituted(stationlist, w, EventPath, Origin):
    Wdict = {}
    traces = io.load(EventPath + '/data/traces_restituted.mseed')
    obspy_compat.plant()

    traces_dict = []
    for tr in traces:
        for il in stationlist:
            tr_name = str(tr.network + '.' + tr.station + '.' + tr.location +
                          '.' + tr.channel[:3])
            if tr_name == str(il):
                st = obspy.Stream()
                es = obspy_compat.to_obspy_trace(tr)
                st.extend([es])
                traces_dict.append(tr)
                Wdict[il.getName()] = st
    return Wdict
    def readWaveformsCross_colesseo(self, station, tw, ttime, cfg_yaml):
        obspy_compat.plant()
        Config = self.Config
        cfg = ConfigObj(dict=Config)
        t2 = UTCDateTime(self.Origin.time)

        traces = io.load(cfg.colosseo_scenario_yml()[:-12]+'scenario.mseed')

        for tr in traces:
            tr_name = str(tr.network+'.'+tr.station+'.'+tr.location+'.'
                                    + tr.channel[:3])
            if tr_name == str(station):
                    traces_station = tr

                    es = obspy_compat.to_obspy_trace(traces_station)
                    streamData = station.net + '.' + station.sta + '.'\
                                             + station.loc + '.'\
                                             + station.comp + '.D.'\
                                             + str(t2.year) + '.'\
                                             + str("%03d" % t2.julday)

                    st = obspy.Stream()
                    st.extend([es])
                    stream = ''
                    snr = ''

                    if station.loc == '--':
                        station.loc = ''

                    if len(st.get_gaps()) > 0:
                        st.merge(method=0, fill_value='interpolate',
                                 interpolation_samples=0)
                    snr_trace = traces_station.chop(tmin=traces_station.tmin,
                                                    tmax=traces_station.tmin +
                                                    ttime-20.,
                                                    inplace=False)
                    snr = num.var(snr_trace.ydata)
                    stream = self.filterWaveform(st, cfg_yaml)

                    xname = os.path.join(self.AF, (streamData+'_all.mseed'))
                    stream.write(xname, format='MSEED')
                    stream.trim(tw['xcorrstart'], tw['xcorrend'])
                    return stream, snr

            else:
                pass
Esempio n. 10
0
    def readWaveformsPicker_pyrocko(self, station, tw, Origin, ttime):

        obspy_compat.plant()
        cfg = ConfigObj(dict=self.Config)
        t2 = UTCDateTime(self.Origin.time)
        sdspath = os.path.join(self.EventPath, 'data')
        if cfg.quantity() == 'displacement':
            traces = io.load(self.EventPath + '/data/traces_restituted.mseed')
        else:
            traces = io.load(self.EventPath + '/data/traces.mseed')
        for tr in traces:
            tr_name = str(tr.network + '.' + tr.station + '.' + tr.location +
                          '.' + tr.channel[:3])
            if tr_name == str(station):
                traces_station = tr

                es = obspy_compat.to_obspy_trace(traces_station)
                streamData = station.net + '.' + station.sta + '.' + station.loc + '.' + station.comp + '.D.' + str(
                    t2.year) + '.' + str("%03d" % t2.julday)

                entry = os.path.join(sdspath, station.net, station.sta,
                                     station.comp + '.D', streamData)

                #stl = es.trim(starttime=tw['start'], endtime=tw['end'])
                st = obspy.Stream()
                st.extend([es])
                stream = ''
                snr = ''

                if station.loc == '--':
                    station.loc = ''

                if len(st.get_gaps()) > 0:
                    st.merge(method=0,
                             fill_value='interpolate',
                             interpolation_samples=0)
                #snr  = self.signoise(st[0], ttime, entry)
                stream = self.filterWaveform(st)

                xname = os.path.join(self.AF, (streamData + '_all.mseed'))
                stream.trim(tw['xcorrstart'], tw['xcorrend'])
                return stream

            else:
                pass
Esempio n. 11
0
    def readWaveformsPicker_colos(self, station, tw, Origin, ttime):

        obspy_compat.plant()
        Config = self.Config
        cfg = ConfigObj(dict=Config)

        traces = io.load(cfg.colosseo_scenario_yml()[:-12] + 'scenario.mseed')

        for tr in traces:
            tr_name = str(tr.network + '.' + tr.station + '.' + tr.location +
                          '.' + tr.channel[:3])
        for tr in traces:
            tr_name = str(tr.network + '.' + tr.station + '.' + tr.location +
                          '.' + tr.channel[:3])
            if tr_name == str(station):
                traces_station = tr

                es = obspy_compat.to_obspy_trace(traces_station)

                st = obspy.Stream()
                st.extend([es])
                stream = ''

                if station.loc == '--':
                    station.loc = ''

                if len(st.get_gaps()) > 0:
                    st.merge(method=0,
                             fill_value='interpolate',
                             interpolation_samples=0)
                stream = self.filterWaveform(st)

                stream.trim(tw['xcorrstart'], tw['xcorrend'])
                return stream

        else:
            pass
Esempio n. 12
0
def semblance_py_cube(ncpus,
                      nostat,
                      nsamp,
                      ntimes,
                      nstep,
                      dimX,
                      dimY,
                      mint,
                      new_frequence,
                      minSampleCount,
                      latv_1,
                      lonv_1,
                      traveltime_1,
                      trace_1,
                      calcStreamMap,
                      time,
                      cfg,
                      refshifts,
                      bs_weights=None):
    obspy_compat.plant()
    trs_orgs = []
    snap = (round, round)
    if cfg.config_weight.combine_all is True:
        combine = True
    else:
        combine = False
    if cfg.config_weight.bootstrap_array_weights is True:
        do_bs_weights = True
    else:
        do_bs_weights = False
    for tr in sorted(calcStreamMap):
        tr_org = obspy_compat.to_pyrocko_trace(calcStreamMap[tr])
        tr_org.ydata = tr_org.ydata / np.sqrt(np.mean(np.square(tr_org.ydata)))
        if combine is True:
            # some trickery to make all waveforms have same polarity, while still
            # considering constructive/destructive interferences. This is needed
            # when combing all waveforms/arrays from the world at once(only then)
            # for a single array with polarity issues we recommend fixing polarity.
            # advantage of the following is that nothing needs to be known about the
            # mechanism.
            tr_org.ydata = abs(tr_org.ydata)
            tr_org.ydata = num.diff(tr_org.ydata)
        trs_orgs.append(tr_org)

    traveltime = []
    traveltime = toMatrix(traveltime_1, dimX * dimY * cfg.Int('dimz'))
    latv = latv_1.tolist()
    lonv = lonv_1.tolist()
    '''
    Basic.writeMatrix(trace_txt,  trace, nostat, minSampleCount, '%e')
    Basic.writeMatrix(travel_txt, traveltime, nostat, dimX * dimY, '%e')
    Basic.writeVector(latv_txt,   latv, '%e')
    Basic.writeVector(lonv_txt,   lonv, '%e')
    '''
    if nsamp == 0:
        nsamp = 1
    backSemb = np.ndarray(shape=(ntimes, dimX * dimY), dtype=float)
    for i in range(ntimes):
        sembmax = 0
        sembmaxX = 0
        sembmaxY = 0
        for j in range(dimX * dimY):
            semb = 0
            nomin = 0
            denom = 0
            if combine is True:
                sums = 0
            else:
                sums = 0
            relstart = []
            relstarts = nostat

            for k in range(nostat):
                relstart = traveltime[k][j]
                tr = trs_orgs[k]
                try:
                    tmin = time + relstart + (i * nstep) - mint - refshifts[k]
                    tmax = time + relstart + (
                        i * nstep) - mint + nsamp - refshifts[k]
                except IndexError:
                    tmin = time + relstart + (i * nstep) - mint
                    tmax = time + relstart + (i * nstep) - mint + nsamp
                try:
                    ibeg = max(0, t2ind_fast(tmin - tr.tmin, tr.deltat,
                                             snap[0]))
                    iend = min(tr.data_len(),
                               t2ind_fast(tmax - tr.tmin, tr.deltat, snap[1]))
                except Exception:
                    print('Loaded traveltime grid wrong!')

                data = tr.ydata[ibeg:iend]

                if combine is True:
                    if do_bs_weights is True:
                        sums += (data) * bs_weights[k]
                    else:
                        sums += (data)

                else:
                    sums += (data)

                relstarts -= relstart

            sum = abs(num.sum(((sums))))
            semb = sum

            backSemb[i][j] = sum
            if semb > sembmax:
                sembmax = semb  # search for maximum and position of maximum on semblance
                # grid for given time step
                sembmaxX = latv[j]
                sembmaxY = lonv[j]
        Logfile.add('max semblance: ' + str(sembmax) + ' at lat/lon: ' +
                    str(sembmaxX) + ',' + str(sembmaxY))

    backSemb = backSemb
    return abs(backSemb)
Esempio n. 13
0
from __future__ import division, print_function, absolute_import
import unittest
from . import common

import pyrocko.trace
from pyrocko import util, io, model, pile

if common.have_obspy():
    import obspy
    from pyrocko import obspy_compat
    obspy_compat.plant()


def close_win(win):
    win.close()


@common.require_obspy
class ObsPyCompatTestCase(unittest.TestCase):
    @common.require_gui
    def test_obspy_snuffle(self):
        fn = common.test_data_file('test1.mseed')

        stream = obspy.read(fn)
        stream.snuffle(launch_hook=close_win)

        trace = stream[0]
        trace.snuffle(launch_hook=close_win)

    @common.require_gui
    def test_obspy_fiddle(self):
Esempio n. 14
0
def otest_py(ncpus, nostat, nsamp, ntimes, nstep, dimX, dimY, mint,
             new_frequence, minSampleCount, latv_1, lonv_1, traveltime_1,
             trace_1, calcStreamMap, time):
    from pyrocko import obspy_compat
    obspy_compat.plant()
    trs_orgs = []
    for tr in calcStreamMap:
        tr_org = obspy_compat.to_pyrocko_trace(calcStreamMap[tr])
        trs_orgs.append(tr_org)
    trace = toMatrix(trace_1, minSampleCount)
    traveltime = []
    traveltime = toMatrix(traveltime_1, dimX * dimY)

    latv = latv_1.tolist()
    lonv = lonv_1.tolist()
    '''
    Basic.writeMatrix (trace_txt,  trace, nostat, minSampleCount, '%e')
    Basic.writeMatrix (travel_txt, traveltime, nostat, dimX * dimY, '%e')
    Basic.writeVector (latv_txt,   latv, '%e')
    Basic.writeVector (lonv_txt,   lonv, '%e')
    '''
    snap = (round, round)

    backSemb = np.ndarray(shape=(ntimes, dimX * dimY), dtype=float)
    for i in range(ntimes):
        #  loop over grid points
        sembmax = 0
        sembmaxX = 0
        sembmaxY = 0

        for j in range(dimX * dimY):
            semb = 0
            nomin = 0
            denom = 0
            sums_cc = 0
            sums = 0
            shifted = []
            relstart = []
            relstarts = nostat
            cc_data = []
            for k in range(nostat):
                relstart = traveltime[k][j]
                tr = trs_orgs[k]
                tmin = time + relstart + (i * nstep) - mint
                tmax = time + relstart + (i * nstep) - mint + nsamp
                try:
                    ibeg = max(0, t2ind_fast(tmin - tr.tmin, tr.deltat,
                                             snap[0]))
                    iend = min(tr.data_len(),
                               t2ind_fast(tmax - tr.tmin, tr.deltat, snap[1]))
                except:
                    print('Loaded traveltime grid wrong!')

                data = tr.ydata[ibeg:iend]
                try:
                    sums += (data)  ##put gradient on top
                except:
                    pass
                relstarts -= (relstart)

            #for dat in cc_data:
        #        sums_cc +=xcorr(cc_data[0],dat,0)[1]
            sum = abs(num.sum(((sums))))
            #    sum = sums_cc
            denom = sum**2
            nomin = sum
            semb = sum

            backSemb[i][j] = sum
            if semb > sembmax:

                sembmax = semb  # search for maximum and position of maximum on semblance
                # grid for given time step
                sembmaxX = latv[j]
                sembmaxY = lonv[j]

        Logfile.add('max semblance: ' + str(sembmax) + ' at lat/lon: ' +
                    str(sembmaxX) + ',' + str(sembmaxY))

    backSemb = backSemb / num.max(num.max(backSemb))

    return abs(backSemb)
Esempio n. 15
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

    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') )
    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.keys():
        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.keys():
                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.keys():
            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.keys():
            stream.append(calcStreamMapshifted[trace])
        pws_stack = PWS_stack([stream], weight=2, normalize=True)
        for tr in pws_stack:
            for trace in calcStreamMapshifted.keys():
                    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.keys():
                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.keys():
            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.keys():
                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.keys():
        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.keys():
        streamID = key
        c2   = 0

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

                c2 += 1


        for key in TTTGridMap.keys():

            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
Esempio n. 16
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

    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') )
    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.keys():
        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.keys():
            stream.append(calcStreamMapshifted[trace])
        pws_stack = PWS_stack([stream], weight=2, normalize=True)
        for tr in pws_stack:
            for trace in calcStreamMapshifted.keys():
                    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.keys():
                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.keys():
            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.keys():
                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.keys():
        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.keys():
        streamID = key
        c2   = 0

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

                c2 += 1


        for key in TTTGridMap.keys():

            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


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

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

    traveltimes = traveltime.reshape(1, nostat*dimX*dimY)
    TTTGrid = True
    manual_shift = False

    if manual_shift:

        pjoin = os.path.join
        timeev = util.str_to_time(ev.time)
        trs_orgs = []
        calcStreamMapshifted = calcStreamMap.copy()
        for trace in calcStreamMapshifted.keys():
                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
        backSemb = num.ndarray(shape=(ntimes, dimX*dimY), dtype=float)
        bf = BeamForming(stations, traces, normalize=True)

        for i in range(ntimes):
            sembmax = 0
            sembmaxX = 0
            sembmaxY = 0
            for j in range(dimX * dimY):
                event = model.Event(lat=float(latv[j]), lon=float(lonv[j]),
                                    depth=ev.depth*1000., time=timeev)
                directory = arrayfolder
                shifted_traces, stack = bf.process(event=event,
                                                   timing=timing,
                                                   fn_dump_center=pjoin(
                                                                directory,
                                                         'array_center.pf'),
                                                   fn_beam=pjoin(directory,
                                                                 'beam.mseed'))
                tmin = stack.tmin+(i*nstep)+20
                tmax = stack.tmin+(i*nstep)+60
                stack.chop(tmin, tmax)
                backSemb[i][j] = abs(sum(stack.ydata))

        k = backSemb
        TTTGrid = False

    if TTTGrid:
        start_time = time.time()
        if cfg.UInt('forerun') > 0:
            ntimes = int((cfg.UInt('forerun') + cfg.UInt('duration'))/step)
        else:
            ntimes = int((cfg.UInt('duration')) / step)
        nsamp = int(winlen)
        nstep = int(step)
        Gmint = cfg.Int('forerun')

        k = semblance(maxp, nostat, nsamp, ntimes, nstep, dimX, dimY, Gmint,
                      new_frequence, minSampleCount, latv, lonv, traveltimes,
                      traces, calcStreamMap, timeev, Config, Origin)
        print("--- %s seconds ---" % (time.time() - start_time))

    t2 = time.time()

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

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

    return partSemb
Esempio n. 17
0
def semblance_py_freq(ncpus,
                      nostat,
                      nsamp,
                      ntimes,
                      nstep,
                      dimX,
                      dimY,
                      mint,
                      new_frequence,
                      minSampleCount,
                      latv_1,
                      lonv_1,
                      traveltime_1,
                      trace_1,
                      calcStreamMap,
                      time,
                      cfg,
                      refshifts,
                      nstats,
                      bs_weights=None):
    obspy_compat.plant()
    trs_orgs = []
    snap = (round, round)
    if cfg.config_weight.combine_all is True:
        combine = True
    else:
        combine = False
    if cfg.config_weight.bootstrap_array_weights is True:
        do_bs_weights = True
    else:
        do_bs_weights = False
    trs_data = []

    for tr in sorted(calcStreamMap):
        tr_org = obspy_compat.to_pyrocko_trace(calcStreamMap[tr])
        trs_orgs.append(tr_org)

    traveltime = []
    traveltime = toMatrix(traveltime_1, dimX * dimY)
    latv = latv_1.tolist()
    lonv = lonv_1.tolist()

    from collections import OrderedDict
    index_begins = OrderedDict()

    index_steps = []
    index_window = []

    for j in range(dimX * dimY):
        for k in range(nostat):
            relstart = traveltime[k][j]
            tr = trs_orgs[k]

            try:
                tmin = time + relstart - mint - refshifts[k]
                tmax = time + relstart - mint + nsamp - refshifts[k]
            except IndexError:
                tmin = time + relstart - mint
                tmax = time + relstart - mint + nsamp

            ibeg = max(0, t2ind_fast(tmin - tr.tmin, tr.deltat, snap[0]))
            index_begins[str(j) + str(k)] = [ibeg, tmin]

            iend = min(tr.data_len(),
                       t2ind_fast(tmax - tr.tmin, tr.deltat, snap[1]))

            iend_step = min(
                tr.data_len(),
                t2ind_fast(tmax - tr.tmin + nstep, tr.deltat, snap[1]))
            index_steps.append(iend_step - iend)
            index_window.append(iend - ibeg)
    '''
    Basic.writeMatrix(trace_txt,  trace, nostat, minSampleCount, '%e')
    Basic.writeMatrix(travel_txt, traveltime, nostat, dimX * dimY, '%e')
    Basic.writeVector(latv_txt,   latv, '%e')
    Basic.writeVector(lonv_txt,   lonv, '%e')
    '''
    if nsamp == 0:
        nsamp = 1
    backSemb = np.ndarray(shape=(ntimes, dimX * dimY), dtype=float)
    for i in range(ntimes):
        sembmax = 0
        sembmaxX = 0
        sembmaxY = 0
        for j in range(dimX * dimY):
            semb = 0.
            nomin = 0
            denom = 0
            sums = 1.
            relstart = []
            relstarts = nostat
            ws = []
            # or normalize group

            #for k in range(nostat):
            #    relstart = traveltime[k][j]
            #    tr = trs_orgs[k]
            #    ibeg = index_begins[str(j)+str(k)][0]+i*index_steps[j+k]
            #    iend = index_begins[str(j)+str(k)][0]+index_window[j+k]+i*index_steps[j+k]
            #    data_tr = tr.ydata[ibeg:iend]
            #    w = 1. / np.sqrt(np.mean(np.square(tr_org.ydata)))
            #    ws.append(w)

            for k in range(nostat):
                relstart = traveltime[k][j]
                tr = trs_orgs[k]
                ibeg = index_begins[str(j) +
                                    str(k)][0] + i * index_steps[j + k]
                iend = index_begins[str(j) + str(k)][0] + index_window[
                    j + k] + i * index_steps[j + k]
                data_tr = tr.ydata[ibeg:iend]
                fydata = num.fft.rfft(data_tr, data_tr.size)
                df = 1. / (tr.deltat)
                fxdata = num.arange(len(fydata)) * df
                w = 1. / np.sqrt(np.mean(np.square(tr_org.ydata)))
                data = fydata * (num.exp(relstart) *
                                 num.imag(2 * math.pi * fxdata)) * w

                if do_bs_weights is True:
                    sums *= data
                else:
                    sums *= data

            backSemb[i][j] = num.sum(sums)

            if semb > sembmax:
                sembmax = semb  # search for maximum and position of maximum on semblance
                # grid for given time step
                sembmaxX = latv[j]
                sembmaxY = lonv[j]
        Logfile.add('max semblance: ' + str(sembmax) + ' at lat/lon: ' +
                    str(sembmaxX) + ',' + str(sembmaxY))

    return backSemb
Esempio n. 18
0
def semblance_py_coherence(ncpus,
                           nostat,
                           nsamp,
                           ntimes,
                           nstep,
                           dimX,
                           dimY,
                           mint,
                           new_frequence,
                           minSampleCount,
                           latv_1,
                           lonv_1,
                           traveltime_1,
                           trace_1,
                           calcStreamMap,
                           time,
                           cfg,
                           refshifts,
                           nstats,
                           bs_weights=None):
    obspy_compat.plant()
    trs_orgs = []
    snap = (round, round)
    if cfg.config_weight.combine_all is True:
        combine = True
    else:
        combine = False
    if cfg.config_weight.bootstrap_array_weights is True:
        do_bs_weights = True
    else:
        do_bs_weights = False

    do_weight_by_array = True
    if do_weight_by_array:
        tr_bases = []
        tr_bases_data = []
        k = 0
        ks = 0
        s_index = 0
        for tr in calcStreamMap:
            tr_org = obspy_compat.to_pyrocko_trace(calcStreamMap[tr])
            #    tr_org.ydata = abs(tr_org.ydata)

            #    tr_org.ydata = num.ediff1d(tr_org.ydata, to_end=tr_org.ydata[-1])
            if k == 1:
                tr_bases.append(tr_org)

            tr_org.set_location('%s' % s_index)
            if k == nstats[s_index]:
                s_index = s_index + 1
                k = 0
            if k < nstats[s_index]:
                k = k + 1
            calcStreamMap[tr] = obspy_compat.to_obspy_trace(tr_org)
            ks = ks + 1
    trs_orgs = []
    for tr in sorted(calcStreamMap):
        tr_org = obspy_compat.to_pyrocko_trace(calcStreamMap[tr])
        trs_orgs.append(tr_org)
    traveltime = []
    traveltime = toMatrix(traveltime_1, dimX * dimY)
    latv = latv_1.tolist()
    lonv = lonv_1.tolist()
    from collections import OrderedDict
    index_begins = OrderedDict()

    index_steps = []
    index_window = []
    for j in range(dimX * dimY):
        markers = []

        for k in range(nostat):
            relstart = traveltime[k][j]
            tr = trs_orgs[k]

            try:
                tmin = time + relstart - mint - refshifts[k]
                tmax = time + relstart - mint + nsamp - refshifts[k]
            except IndexError:
                tmin = time + relstart - mint
                tmax = time + relstart - mint + nsamp

                m = PhaseMarker(tmin=tmin,
                                tmax=tmax,
                                phasename='P',
                                nslc_ids=(tr.nslc_id, ))
                markers.append(m)

            ibeg = max(0, t2ind_fast(tmin - tr.tmin, tr.deltat, snap[0]))
            index_begins[str(j) + str(k)] = [ibeg, tmin]

            iend = min(tr.data_len(),
                       t2ind_fast(tmax - tr.tmin, tr.deltat, snap[1]))

            iend_step = min(
                tr.data_len(),
                t2ind_fast(tmax - tr.tmin + nstep, tr.deltat, snap[1]))
            index_steps.append(iend_step - iend)

            index_window.append(iend - ibeg)
    #    trld.snuffle(trs_orgs, markers=markers)
    '''
    Basic.writeMatrix(trace_txt,  trace, nostat, minSampleCount, '%e')
    Basic.writeMatrix(travel_txt, traveltime, nostat, dimX * dimY, '%e')
    Basic.writeVector(latv_txt,   latv, '%e')
    Basic.writeVector(lonv_txt,   lonv, '%e')
    '''
    trs_orgs = []
    do_trad = False
    for tr in sorted(calcStreamMap):
        tr_org = obspy_compat.to_pyrocko_trace(calcStreamMap[tr])
        #    if combine is True:
        #        tr_org.ydata = abs(tr_org.ydata)

        #        tr_org.ydata = num.ediff1d(tr_org.ydata, to_end=tr_org.ydata[-1])

        #            tr_org.ydata = tr_org.ydata/num.max(tr_org.ydata)
        #            tr_org.ydata = abs(tr_org.ydata)
        #                tr_org.ydata = tr_org.ydata / np.sqrt(np.mean(np.square(tr_org.ydata)))

        trs_orgs.append(tr_org)

    if nsamp == 0:
        nsamp = 1
    backSemb = np.ndarray(shape=(ntimes, dimX * dimY), dtype=float)
    for i in range(ntimes):
        sembmax = 0
        sembmaxX = 0
        sembmaxY = 0
        for j in range(dimX * dimY):
            semb = 0.
            nomin = 0
            denom = 0
            sums = num.zeros(max(index_steps))
            sums = 0.
            relstart = []
            relstarts = nostat
            data_comp = False
            for k in range(nostat):
                relstart = traveltime[k][j]
                tr = trs_orgs[k]
                ibeg = index_begins[str(j) +
                                    str(k)][0] + i * index_steps[j + k]
                iend = index_begins[str(j) + str(k)][0] + index_window[
                    j + k] + i * index_steps[j + k]
                data = tr.ydata[ibeg:iend]
                data = data / np.sqrt(np.mean(np.square(data)))
                if combine is True:
                    ind = int(tr_org.location)
                    data_comp = tr_bases[ind].ydata[ibeg:iend]
                #        data_comp = data

                else:
                    if data_comp is False:
                        data_comp = data
                cfx = num.fft.fft(data)
                cfy = num.fft.fft(data_comp)

                # Get cross spectrum
                cross = cfx.conj() * cfy
                #        cross = num.correlate(data, data_comp)
                #f, coh = coherence(data, data_comp, fs=tr.deltat)
                sums = sums + num.sum(abs(cross))

                relstarts -= relstart
            sum = abs(num.sum(sums))

            semb = sum

            backSemb[i][j] = sum
            if semb > sembmax:
                sembmax = semb  # search for maximum and position of maximum on semblance
                # grid for given time step
                sembmaxX = latv[j]
                sembmaxY = lonv[j]
        Logfile.add('max semblance: ' + str(sembmax) + ' at lat/lon: ' +
                    str(sembmaxX) + ',' + str(sembmaxY))

    backSemb = backSemb / num.max(num.max(backSemb))
    return backSemb
Esempio n. 19
0
def music_wrapper(ncpus,
                  nostat,
                  nsamp,
                  ntimes,
                  nstep,
                  dimX,
                  dimY,
                  mint,
                  new_frequence,
                  minSampleCount,
                  latv_1,
                  lonv_1,
                  traveltime_1,
                  trace_1,
                  calcStreamMap,
                  time,
                  cfg,
                  refshifts,
                  nstats,
                  bs_weights=None):
    obspy_compat.plant()
    trs_orgs = []
    snap = (round, round)
    if cfg.config_weight.combine_all is True:
        combine = True
    else:
        combine = False
    if cfg.config_weight.bootstrap_array_weights is True:
        do_bs_weights = True
    else:
        do_bs_weights = False

    do_weight_by_array = False
    if do_weight_by_array:
        k = 0
        stats_done = 0
        for k in range(0, len(nstats)):
            for stats in range(0, nstats[k]):
                list(calcStreamMap.keys())[stats].data = list(
                    calcStreamMap.keys())[stats].data / np.max(
                        list(calcStreamMap.keys())[0].data)
                stats_done = stats_done + nstats[k]

        k = 0
        s_index = 0
        for tr in calcStreamMap:
            tr_org = obspy_compat.to_pyrocko_trace(calcStreamMap[tr])
            trs_orgs.append(tr_org)

        for tr in calcStreamMap:
            tr_org = obspy_compat.to_pyrocko_trace(calcStreamMap[tr])
            datas = trs_orgs[0:s_index].ydata
            if k <= nstats[s_index]:
                k = k + 1
                tr_org.ydata = tr_org.ydata / np.sqrt(np.mean(
                    np.square(datas)))

            if k == nstats[s_index]:
                s_index = s_index + 1
            calcStreamMap[tr] = obspy_compat.to_obspy_trace(tr_org)

            stats_done = stats_done + nstats[k]
    trs_orgs = []
    for tr in sorted(calcStreamMap):
        tr_org = obspy_compat.to_pyrocko_trace(calcStreamMap[tr])
        trs_orgs.append(tr_org)
        array_lats, array_lons = calcStreamMap[tr].lat, calcStreamMap[tr].lon
    traveltime = []
    traveltime = toMatrix(traveltime_1, dimX * dimY)
    latv = latv_1.tolist()
    lonv = lonv_1.tolist()
    from collections import OrderedDict
    index_begins = OrderedDict()

    index_steps = []
    index_window = []
    for j in range(dimX * dimY):
        markers = []

        for k in range(nostat):
            relstart = traveltime[k][j]
            tr = trs_orgs[k]

            try:
                tmin = time + relstart - mint - refshifts[k]
                tmax = time + relstart - mint + nsamp - refshifts[k]
            except IndexError:
                tmin = time + relstart - mint
                tmax = time + relstart - mint + nsamp

                m = PhaseMarker(tmin=tmin,
                                tmax=tmax,
                                phasename='P',
                                nslc_ids=(tr.nslc_id, ))
                markers.append(m)

            ibeg = max(0, t2ind_fast(tmin - tr.tmin, tr.deltat, snap[0]))
            index_begins[str(j) + str(k)] = [ibeg, tmin]

            iend = min(tr.data_len(),
                       t2ind_fast(tmax - tr.tmin, tr.deltat, snap[1]))

            iend_step = min(
                tr.data_len(),
                t2ind_fast(tmax - tr.tmin + nstep, tr.deltat, snap[1]))
            index_steps.append(iend_step - iend)

            index_window.append(iend - ibeg)
    #    trld.snuffle(trs_orgs, markers=markers)
    '''
    Basic.writeMatrix(trace_txt,  trace, nostat, minSampleCount, '%e')
    Basic.writeMatrix(travel_txt, traveltime, nostat, dimX * dimY, '%e')
    Basic.writeVector(latv_txt,   latv, '%e')
    Basic.writeVector(lonv_txt,   lonv, '%e')
    '''
    trs_orgs = []
    k = 0
    for tr in sorted(calcStreamMap):
        tr_org = obspy_compat.to_pyrocko_trace(calcStreamMap[tr])
        trs_orgs.append(tr_org)

    if nsamp == 0:
        nsamp = 1
    backSemb = np.ndarray(shape=(ntimes, dimX * dimY), dtype=float)
    for i in range(ntimes):
        sembmax = 0
        sembmaxX = 0
        sembmaxY = 0
        for j in range(dimX * dimY):
            semb = 0.
            nomin = 0
            denom = 0
            sums = num.zeros(max(index_steps))
            sums = 0.
            relstart = []
            relstarts = nostat
            sums_schimmel = 0

            for k in range(nostat):
                relstart = traveltime[k][j]
                tr = trs_orgs[k]
                ibeg = index_begins[str(j) +
                                    str(k)][0] + i * index_steps[j + k]
                iend = index_begins[str(j) + str(k)][0] + index_window[
                    j + k] + i * index_steps[j + k]
                data = tr.ydata[ibeg:iend]
                # normalize:
                #data = data / np.sqrt(np.mean(np.square(data)))
                try:
                    if do_bs_weights is True:
                        sums += data * bs_weights[k]
                    else:
                        sums = sums + data
                except ValueError:
                    if num.shape(data) < num.shape(sums):
                        data = tr.ydata[ibeg:iend + 1]
                    else:
                        data = tr.ydata[ibeg:iend - 1]
                    sums = sums + data

                relstarts -= relstart
            sums_schimmel = abs(sums_schimmel)**2.

            sum = abs(num.sum(sums))

            semb = sum

            backSemb[i][j] = sum
            if semb > sembmax:
                sembmax = semb
                sembmaxX = latv[j]
                sembmaxY = lonv[j]
        Logfile.add('max semblance: ' + str(sembmax) + ' at lat/lon: ' +
                    str(sembmaxX) + ',' + str(sembmaxY))
    return backSemb
Esempio n. 20
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
Esempio n. 21
0
from __future__ import division, print_function, absolute_import
import unittest
from . import common

import pyrocko.trace
from pyrocko import util, io, model, pile

if common.have_obspy():
    import obspy
    from pyrocko import obspy_compat
    obspy_compat.plant()


def close_win(win):
    win.close()


@common.require_obspy
class ObsPyCompatTestCase(unittest.TestCase):

    @common.require_gui
    def test_obspy_snuffle(self):
        fn = common.test_data_file('test1.mseed')

        stream = obspy.read(fn)
        stream.snuffle(launch_hook=close_win)

        trace = stream[0]
        trace.snuffle(launch_hook=close_win)

    @common.require_gui
Esempio n. 22
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)
    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
Esempio n. 23
0
def semblance_py_dynamic_cf(ncpus, nostat, nsamp, ntimes, nstep, dimX, dimY,
                            mint, new_frequence, minSampleCount, latv_1,
                            lonv_1, traveltime_1, trace_1, calcStreamMap, time,
                            origin, FilterCfg):

    obspy_compat.plant()
    trs_orgs = []
    for tr in calcStreamMap:
        tr_org = obspy_compat.to_pyrocko_trace(calcStreamMap[tr])
        tr_org.ydata = tr_org.ydata / np.sqrt(np.mean(np.square(tr_org.ydata)))
        trs_orgs.append(tr_org)
    trace = toMatrix(trace_1, minSampleCount)
    traveltime = []
    traveltime = toMatrix(traveltime_1, dimX * dimY)

    latv = latv_1.tolist()
    lonv = lonv_1.tolist()
    '''
    Basic.writeMatrix(trace_txt,  trace, nostat, minSampleCount, '%e')
    Basic.writeMatrix(travel_txt, traveltime, nostat, dimX * dimY, '%e')
    Basic.writeVector(latv_txt,   latv, '%e')
    Basic.writeVector(lonv_txt,   lonv, '%e')
    '''
    snap = (round, round)
    backSemb = np.ndarray(shape=(ntimes, dimX * dimY), dtype=float)
    for i in range(ntimes):
        #  loop over grid points
        sembmax = 0
        sembmaxX = 0
        sembmaxY = 0

        for j in range(dimX * dimY):
            semb = 0
            nomin = 0
            denom = 0
            sums_cc = 0
            sums = 0
            shifted = []
            relstart = []
            relstarts = nostat
            cc_data = []
            tt = []

            for k in range(nostat):
                relstart = traveltime[k][j]
                tr = trs_orgs[k]
                tmin = time + relstart + (i * nstep) - mint
                tmax = time + relstart + (i * nstep) - mint + nsamp
                try:
                    ibeg = max(0, t2ind_fast(tmin - tr.tmin, tr.deltat,
                                             snap[0]))
                    iend = min(tr.data_len(),
                               t2ind_fast(tmax - tr.tmin, tr.deltat, snap[1]))
                except:
                    print('Loaded traveltime grid wrong!')

                data = tr.ydata[ibeg:iend]
                try:
                    sums += num.gradient(abs(data))
                except:
                    pass
                relstarts -= (relstart)

            sum = abs(num.sum(((sums))))
            denom = sum**2
            nomin = sum
            semb = sum

            backSemb[i][j] = sum
            if semb > sembmax:

                sembmax = semb
                sembmaxX = latv[j]
                sembmaxY = lonv[j]

        Logfile.add('max semblance: ' + str(sembmax) + ' at lat/lon: ' +
                    str(sembmaxX) + ',' + str(sembmaxY))

    backSemb = backSemb / num.max(num.max(backSemb))
    return abs(backSemb)
Esempio n. 24
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()


    partSemb = k

    partSemb_data  = partSemb.reshape (ntimes,migpoints)

    return partSemb_data