def filterWaveform(self, Waveform, cfg):

        Logfile.red('Filter Waveform: ')

        new_frequence = (cfg.config_filter.newFrequency)

        st = Stream()
        for i in Waveform:
                Logfile.red('Downsampling to %s: from %d' % (new_frequence,
                            i.stats.sampling_rate))
                j = i.resample(new_frequence)
                switch = cfg.config_filter.filterswitch

                Logfile.add('bandpass filtered \
                stream for station %s ' % (i))

                j.filter('bandpass',
                         freqmin=cfg.config_filter.flo[switch-1],
                         freqmax=cfg.config_filter.fhi[switch-1],
                         corners=cfg.config_filter.ns[switch-1],
                         zerophase=False)

                st.append(j)

        return st
Beispiel #2
0
def readURL(url, tmpFile=None):

    lines = []

    try:
        datasource = urlopen(url)

        while True:
            line = datasource.readline()

            if line == '':
                break
            lines.append(line)

        if len(lines) == 0:
            Logfile.error('Cannot read from URL:', url)
            lines = []

        Logfile.add(str(len(lines)) + ' lines read from URL:', url)

        if tmpFile:
            writeTextFile(tmpFile, lines)

    except:
        Logfile.exception('readURL')

    return lines
def filterBestSolution(solution):

    evp  = os.path.join('/',*solution.path.split('/')[:-2])
    C= Config(evp)
    Conf = C.parseConfig('config')
    cfg  = ConfigObj(dict=Conf)

    SL   = []
    M= []
    fobj = open(os.path.join(solution.path, 'event.stations'),'r')

    for s in fobj:
       try:
           line = s.split()
           net,sta,loc,comp = line[0].split('.')

           slat= line[1]
           slon= line[2]
           smember = line[3]

           M.append(smember)
           SL.append(Station(net,sta,loc,comp,lat=slat,lon=slon,member=smember))

       except:
           Logfile.exception('filterBestSolution', '<' + s + '>')
           continue

    fobj.close()

    M = list(set(M))

    Logfile.add('number of clusters ' + str(len(M)),
                 'number of stations ' + str(len(SL)))

    kd = obs_kilometer2degrees(cfg.Distance('intraclusterdistance'))
    Logfile.add('icd ' + str(kd))

    maxdist = -1

    for i in SL:
        counter = 0

        for k in SL:
            if i.member == '8' and k.member == '8':
               if i.getName() != k.getName():
                  delta = loc2degrees(i, k)

                  if delta > maxdist:
                      maxdist = delta

                  if delta < kd:
                      counter +=1

        print(i, 'less then allowd ', counter)

    print('masxdist ', maxdist)
Beispiel #4
0
def execsemblance(nostat, nsamp, i, nstep, dimX,dimY, mint, new_freq, minSampleCount) :

    f = [nostat, nsamp, i, nstep, dimX,dimY, mint, new_freq, minSampleCount]
    args  = Basic.floatToString(f, delim= ',')
    prog  = sys.executable + ' ' + __file__
    cmd   = prog  + ' ' + args

    Logfile.add('--------------------------------------------', cmd)
    result = Basic.systemCmd(cmd)
    Logfile.addLines(result)
    backSemb = Basic.readVector(semb_txt)
    return backSemb
Beispiel #5
0
def removeTempFiles():

    dir = '/tmp'
    names = os.listdir(dir)
    user = getpass.getuser()
    cnt = 0

    for s in names:
        if s.startswith('obspy-'):
            os.remove(dir + '/' + s)
            cnt += 1

    if cnt != 0:
        Logfile.add('Remove ' + str(cnt) + ' temp files obspy-*.*')

    return cnt
Beispiel #6
0
def init(configFileName = None):

    global EventDir, ProtFileDir, ConfigDict, isDebug

    ProtFileDir = os.path.join(EventDir(), 'tmp1')

    if True:   # not isClient:
       ConfigDict  = ConfigFile.readGlobalConf(configFileName)

       if ConfigDict == None:  return False
    key = 'DEBUG_FLAG'

    if not isClient:
       if isDebug: Logfile.add('Debugging is on')

    return True
Beispiel #7
0
def writeWaveform(Folder, station, Stream, flag, network):

    if flag == 'U':
        s1 = 'unfiltered'
    elif flag == 'F':
        s1 = 'filtered'
    elif flag == 'R':
        s1 = 'resampled'

    else:
        Logfile.abort('writeWaveform: Illegal flag <' + flag + '>')

    fname = ('%s-%s-%s.mseed') % (network, station, flag)
    name = os.path.join(Folder['mseed'], fname)
    Stream.write(name, format='MSEED')

    Logfile.add('%s stream for station %s written ' % (s1, station))
Beispiel #8
0
def compareclusterCentre(oldcluster, newcluster, Config):

    counter = 0
    for i in range(int(Config['maxcluster'])):
        if newcluster[i].rank == -1:
            continue

        delta = loc2degrees(oldcluster[i], newcluster[i])

        msg = str(i) + ' OLD: ' + str(oldcluster[i].lat) + ' ' + str(
            oldcluster[i].lon)
        msg += ' NEW: ' + str(newcluster[i].lat) + ' ' + str(
            newcluster[i].lon) + ' DELTA: ' + str(delta)
        Logfile.add(msg)

        if delta < float(Config['centroidmindistance']):
            counter += 1
    return counter
Beispiel #9
0
def readWaveforms(stationList, tw, EventPath, Origin):

    t2 = UTCDateTime(Origin.time)
    sdspath = os.path.join(EventPath, 'data', str(t2.year))

    Wdict = OrderedDict()

    for i in stationList:
        streamData = i.getName() + '.D.' + str(t2.year)\
                     + '.' + str("%03d" % t2.julday)
        entry = os.path.join(sdspath, i.net, i.sta, i.comp + '.D', streamData)
        tdiff = tw['end'] - tw['start']

        try:
            st = read(entry,
                      format="MSEED",
                      starttime=tw['start'],
                      endtime=tw['end'],
                      nearest_sample=True)

        except Exception:
            Logfile.error('readWaveforms: File not found', entry)
            pass

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

        if len(st) > 0:
            trdiff = st[0].stats.endtime - st[0].stats.starttime

            totaldiff = abs(trdiff - tdiff)

            if totaldiff < 1:
                Wdict[i.getName()] = st
                Logfile.add(i.getName() + ' added to StreamList ')
        else:
            print(' OUT ', streamData)

    Logfile.red('%d Streams added with available Data' % len(Wdict))
    return Wdict
Beispiel #10
0
def readConf(fileName):

    if not Basic.checkFileExists(fileName):
        return None

    cDict = {}
    parser = SafeConfigParser()
    parser.read(fileName)

    isClient = Globals.isClient

    if not isClient:
        Logfile.setVisible(False)
        Logfile.add(' ', fileName, ': ')

    Logfile.setErrorLog(True)

    for section_name in parser.sections():
        for name, value in parser.items(section_name):
            cDict[name] = value

            if not isClient:
                if name != 'mail' and name != 'pwd':
                    Logfile.add(name + ' = ' + value)

    if not isClient:
        Logfile.add(' ')
        Logfile.setVisible(True)

    Logfile.setErrorLog(False)
    return cDict
Beispiel #11
0
def kmean(Config, inputCentroid, FilterMeta, counter, Folder, Origin, flag):

    counter += 1
    Logfile.add('COUNTER ' + str(counter) + ' CUTOFF ' + Config['cutoff'])

    cfg = ConfigObj(dict=Config)

    scl = stationBelongTocluster(Config, inputCentroid, FilterMeta)

    acounter = 1
    for a in inputCentroid:
        for i in scl:
            if acounter == i.member:
                delta = loc2degrees(i, a)
                if delta > cfg.Float('initialstationdistance'):
                    i.member = -1

        acounter += 1

    if counter == cfg.UInt('cutoff'):
        endcheck(inputCentroid, FilterMeta, Config, Folder, Origin, flag)
        sys.exit()

    nsc = calculateclusterCentre(Config, scl)
    t = compareclusterCentre(inputCentroid, nsc, Config)

    Logfile.add('ITERATIONSTEP: ---> ' + str(counter) +
                ' <-----------------------------')

    while t < cfg.UInt('maxcluster'):
        Logfile.add('number of arrays in KMEAN: ' + str(t))
        kmean(Config, nsc, FilterMeta, counter, Folder, Origin, flag)

    endcheck(inputCentroid, FilterMeta, Config, Folder, Origin, flag)
    sys.exit()
Beispiel #12
0
def printBestSolution(solution):

    maxline = -100
    L = []
    M = []
    print(solution.path)
    solution.path = solution.path[1:]
    Logfile.add('eventpath: ', os.path.join(solution.path, 'event.stations'))
    fobj = open(os.path.join(solution.path, 'event.stations'), 'r')
    for line in fobj:
        line = line.split()
        M.append(float(line[3]))
        L.append(
            BestSolution(line[0], float(line[3]), float(line[1]),
                         float(line[2])))

    fobj.close()

    maxline = max(M)
    C = {}
    fobjarrays = open(
        os.path.join(os.path.join(*solution.path.split('/')[:-2]),
                     'arraycluster.dat'), 'w')
    for i in range(int(maxline) + 1):
        array = ''

        for j in L:
            if int(j.cluster) == i:
                array += j.station + '|'

        ar = 'r' + str(i) + '=' + array[:-1] + '\n'
        C[i] = array
        print(ar, len(ar))
        fobjarrays.write(ar)

    fobjarrays.close()

    return C
Beispiel #13
0
def readMetaInfoFile(EventPath):

    Logfile.red('Parsing MetaInfoFile')

    try:
        for i in os.listdir(EventPath):
            if fnmatch.fnmatch(i, 'metainfo-*.meta'):
                evfile = os.path.join(EventPath, i)

        MetaL = []

        Logfile.add(evfile)
        fobj = open(evfile, 'r')

        for i in fobj:
            line = i.split()

            net = line[0]
            sta = line[1]
            loc = line[2]
            comp = line[3]
            lat = line[4]
            lon = line[5]
            ele = line[6]
            dip = line[7]
            azi = line[8]
            gain = line[9]

            if fnmatch.fnmatch(comp, 'Z'):
                MetaL.append(
                    Station(net, sta, loc, comp, lat, lon, ele, dip, azi,
                            gain))

        Logfile.red('%d ENTRIES IN METAFILE FOUND' % (len(MetaL)))
    except Exception:
        Logfile.red('METAFILE NOT READABLE')

    return MetaL
Beispiel #14
0
    def filterWaveform(self, Waveform):

        Logfile.red('Filter Waveform: ')
        cfg = FilterCfg(self.Config)

        new_frequence = (cfg.newFrequency())

        st = Stream()
        for i in Waveform:
            Logfile.red('Downsampling to %s: from %d' %
                        (new_frequence, i.stats.sampling_rate))
            j = i.resample(new_frequence)
            switch = cfg.filterswitch()

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

                j.filter('bandpass',
                         freqmin=cfg.flo(),
                         freqmax=cfg.fhi(),
                         corners=cfg.ns(),
                         zerophase=bool(self.Config['zph']))

            elif switch == 2:
                Logfile.add('bandpass filtered \
                        stream for station %s ' % (i))

                j.filter('bandpass',
                         freqmin=cfg.flo2(),
                         freqmax=cfg.fhi2(),
                         corners=cfg.ns2(),
                         zerophase=bool(self.Config['zph']))
            st.append(j)

        return st
    def calculateTimeWindows(self, mint):

        tw = {}
        st = str(self.Origin.time)[:-1]

        tw['start'] = UTCDateTime(UTCDateTime(st) + (mint - 100))
        tw['end'] = tw['start'] + 200

        tw['xcorrstart'] = UTCDateTime(UTCDateTime(st)
                                       + (mint - self.mintforerun))
        tw['xcorrend'] = tw['xcorrstart'] + 40

        Logfile.add(' ORIGIN TIME %s' % UTCDateTime(self.Origin.time))
        Logfile.add(' OVERALL TIME WINDOW: %s - %s' % (tw['start'], tw['end']))
        Logfile.add(' XCROSS TIME WINDOW: %s - %s' % (tw['xcorrstart'],
                                                      tw['xcorrend']))

        return tw
Beispiel #16
0
    def doXcorr(self, phase, traces):
        StreamDict, SNRDict = self.traveltimes(phase, traces)
        t = self.f6(SNRDict)
        Logfile.add('doXcorr: REFERENCE: ' + t)

        for i in SNRDict.keys():
            Logfile.add('doXcorr: STREAM: ' + i + ' SNR: ' + str(SNRDict[i]))

        alternativeref = os.path.join(
            *self.AF.split(os.sep)[-1:]) + 'refstation'

        if self.Config[alternativeref] == '':
            t = t
        else:
            t = self.Config[alternativeref]

        corrDict = {}
        try:
            ref = StreamDict[t][0].data
        except Exception:
            ref = StreamDict[t].data
        Logfile.red('Reference Station of %s for Xcorr Procedure %s' %
                    (os.path.basename(self.AF), t))
        Logfile.red('Enter Xcorr Procedure ')
        for stream in StreamDict.keys():
            a, b = obspy.signal.cross_correlation.xcorr(
                ref, StreamDict[stream][0], 0)
            shift = a / StreamDict[stream][0].stats.sampling_rate
            corrDict[stream] = Corr(shift, b, a)
            corrDict[stream].value = abs(corrDict[stream].value)
            msg = 'Index: ' + str(a) + ' Value: ' + str(b) + ' ----> '
            msg += (str(stream) +
                    str(StreamDict[stream][0].stats.sampling_rate) +
                    ' SHIFT IN TIME: ' + str(shift))
            Logfile.add(msg)

        Logfile.red('Finish Xcorr Procedure ')
        return corrDict, StreamDict[t], StreamDict
Beispiel #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
Beispiel #18
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)
    def resampleWaveform(self, Waveform, end_frequence):

        Logfile.add('enter resampling in crosscorrelation')
        print('sampling_rate = ', Waveform.stats.sampling_rate)
        return resampleWaveform_2(Waveform, end_frequence)
Beispiel #20
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)
Beispiel #21
0
def processWaveforms_obspy(WaveformDict, Config, Folder, network, MetaDict,
                           Event, switch, Xcorr):

    Logfile.red('Start Processing')

    cfg = FilterCfg(Config)
    new_frequence = cfg.newFrequency()

    for index, i in enumerate(WaveformDict):
        Logfile.add('%s:%s ---------------------' % (index, i))

        if Config['export_unfiltered'].capitalize() is 'True':
            writeWaveform(Folder, i, WaveformDict[i], 'U', network)

        station = getStation(i, MetaDict)

        if cfg.Int('fm') == 1:
            azi = ttt.bearing(Event, station)
            bazi = ttt.backazi(station, Event)

            msg = 'Takeoff ' + str(station.takeoff) + ' Azi ' + str(azi) +\
                  'Bazi ' + str(bazi)

            Logfile.add(msg)

        gain = float(station.gain)

        if gain == 0.0 or gain == -1.0:
            gain = 1

        WaveformDict[i][0].data * (1.0 / gain)

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

            WaveformDict[i].filter('bandpass',
                                   freqmin=cfg.flo(),
                                   freqmax=cfg.fhi(),
                                   corners=cfg.ns(),
                                   zerophase=bool(Config['zph']))

        elif switch is 1:
            Logfile.add('bandpass filtered stream for station %s ' % (i))

            WaveformDict[i].filter('bandpass',
                                   freqmin=cfg.flo2(),
                                   freqmax=cfg.fhi2(),
                                   corners=cfg.ns2(),
                                   zerophase=bool(Config['zph2']))

        else:
            Logfile.add('no filter set for station %s ' % (i))

        if Config['export_filtered'].capitalize() is 'True':
            writeWaveform(Folder, i, WaveformDict[i], 'F', network)

        j = resampleWaveform(WaveformDict[i][0], new_frequence)
        WaveformDict[i] = j

        if Config['export_resampled'].capitalize() is 'True':
            writeWaveform(Folder, i, WaveformDict[i], 'R', network)

    return WaveformDict
Beispiel #22
0
def semblance_py(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,
                 flag_rpe=False,
                 output=False,
                 boot_shifts=None):

    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 = calcStreamMap[tr][0]
            trs_orgs.append(tr_org)

        for tr in calcStreamMap:
            tr_org = calcStreamMap[tr][0]
            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 = calcStreamMap[tr]
        trs_orgs.append(tr_org)
    traveltime = []
    traveltime = toMatrix(traveltime_1, dimX * dimY)
    latv = latv_1.tolist()
    lonv = lonv_1.tolist()
    index_begins = OrderedDict()
    index_steps = []
    index_window = []
    for j in range(dimX * dimY):
        markers = []

        for k in range(nostat):
            if cfg.config_weight.bootstrap_array_weights is True:
                relstart = traveltime[k][j] + boot_shifts[
                    k] + num.random.uniform(
                        -1 * cfg.config_weight.shift_max / 10.,
                        cfg.config_weight.shift_max / 10.)
            else:
                relstart = traveltime[k][j]
            tr = trs_orgs[k]
            try:
                tmin = time + relstart - mint + refshifts[k]
                tmax = time + relstart - mint + nsamp + refshifts[k]
            except:
                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)
            # for debug:
    #    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 = calcStreamMap[tr]
        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 = tr_org.ydata / np.sqrt(np.mean(np.square(tr_org.ydata)))
            #        tr_org.ydata = abs(tr_org.ydata)

            #        tr_org.ydata = num.ediff1d(tr_org.ydata)
            if max(index_steps) % 2 == 1:
                tr_org.ydata = abs(tr_org.ydata)
    #            tr_org.ydata = num.ediff1d(tr_org.ydata)
    #    if cfg.config_weight.shift_by_phase_pws is True:
    #                cfx = num.fft.fft(tr_org.ydata)
    #                sums_schimmel = sums_schimmel + (cfx/(abs(cfx)) * num.exp(1j*2*num.pi*cfx)))
    #                print('calculate pws')
        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]
                if cfg.config_weight.shift_by_phase_pws is True:
                    cfx = num.fft.fft(data)
                    sums_schimmel = sums_schimmel + (
                        cfx / (abs(cfx)) * num.exp(1j * 2 * num.pi * cfx))
                try:
                    if do_bs_weights is True and combine is True:
                        sums += data * bs_weights[k]
                    else:
                        sums = sums + data
                except ValueError:
                    try:
                        if num.shape(data) < num.shape(sums):
                            data = tr.ydata[ibeg:iend + 1]
                        else:
                            data = tr.ydata[ibeg:iend - 1]
                        sums = sums + data
                    except:
                        sums = sums

                relstarts -= relstart
            sums_schimmel = num.real(sums_schimmel)**2.
            sums_copy = sums
            #    data = sums_schimmel
            #    t1 = trace.Trace(
            #        station='TEST', channel='Z', deltat=0.5, tmin=0., ydata=data)

            if cfg.config_weight.shift_by_phase_pws is True:
                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_copy = data.copy()
                    cfx = num.fft.fft(data) * sums_schimmel
                    data = num.fft.ifft(cfx)
                    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
            data = sums_copy
            basetime = util.str_to_time("2016-11-25 14:24:30.000")
            data = num.real(sums)
            sum = abs(num.sum(sums))
            if combine is True:
                sum = (1. / nostat) * ((abs(num.sum(
                    (sums)))**2) / num.sum(sums))
            semb = sum

            backSemb[i][j] = sum
            if semb > sembmax:
                sembmax = semb
                sembmaxX = latv[j]
                sembmaxY = lonv[j]
                #backSemb[i][:] = backSemb[i][:]/num.max(backSemb[i][:])
        if output is True:
            Logfile.add('max semblance: ' + str(sembmax) + ' at lat/lon: ' +
                        str(sembmaxX) + ',' + str(sembmaxY))
    if flag_rpe is False:
        backSemb = backSemb / num.max(num.max(backSemb))

    return backSemb
Beispiel #23
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
Beispiel #24
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
Beispiel #25
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
Beispiel #26
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
Beispiel #27
0
def collectSembweighted(SembList,Config,Origin,Folder,ntimes,arrays,switch, weights):
    '''
    method to collect semblance matrizes from all processes and write them to file for each timestep
    '''
    Logfile.add ('start collect in collectSemb')

    cfg= ConfigObj (dict=Config)
    origin = ConfigObj (dict=Origin)

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

    latv= []
    lonv= []

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

    z=0

    for i in xrange(dimX):
         oLatul = o_lat - ((dimX-1)/2) * gridspacing + i*gridspacing

         if z == 0 and i == 0 :
             Latul = oLatul
         o=0

         for j in xrange (dimY):
               oLonul = o_lon - ((dimY-1)/2) * gridspacing + j*gridspacing

               if o==0 and j==0:
                    Lonul = oLonul

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


    tmp=1
    for a, w in zip(SembList, weights):
        tmp *= a
    #sys.exit()

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

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

    folder  = Folder['semb']
    fobjsembmax = open (os.path.join (folder,'sembmax_%s.txt' % (switch)),'w')

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


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

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


        sembmax  = 0
        sembmaxX = 0
        sembmaxY = 0

        origin = DataTypes.dictToLocation (Origin)
        uncert = num.std(i) #maybe not std?
        for j in range(migpoints):
            x= latv[j]
            y= lonv[j]
            semb = i[j]

            fobj.write ('%.2f %.2f %.20f\n' % (x,y,semb))

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

        delta = loc2degrees (Location (sembmaxX, sembmaxY), origin)
        azi   = toAzimuth (float(Origin['lat']), float(Origin['lon']),float(sembmaxX), float(sembmaxY))

        sembmaxvaluev[a] = sembmax
        sembmaxlatv[a]   = sembmaxX
        sembmaxlonv[a]   = sembmaxY

        fobjsembmax.write ('%d %.2f %.2f %.20f %.20f %d %03f %f %03f\n' % (a*step,sembmaxX,sembmaxY,sembmax,uncert,usedarrays,delta,float(azi),delta*119.19))
        fobj.close()


    fobjsembmax.close()

    durationpath  = os.path.join (folder, "duration.txt")
    trigger.writeSembMaxValue (sembmaxvaluev,sembmaxlatv,sembmaxlonv,ntimes,Config,Folder)
    trigger.semblancestalta (sembmaxvaluev,sembmaxlatv,sembmaxlonv)
Beispiel #28
0
def optimization(*params, **args):
    counter = params[1]
    Config = params[2]
    Wdf = params[3]
    FilterMeta = params[4]
    mint = params[5]
    maxt = params[6]
    TTTGridMap = params[7]
    Folder = params[8]
    Origin = params[9]
    ntimes = params[10]
    switch = params[11]
    ev = params[12]
    arrayfolder = params[13]
    syn_in = params[14]
    data = params[15]
    evpath = params[16]
    XDict = params[17]
    RefDict = params[18]
    workdepth = params[19]
    filterindex = params[20]
    Wdfs = params[21]

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


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

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

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

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

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

        if len(FilterMeta)  < 3: continue

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

        FilterMeta = cmpFilterMetavsXCORR (W, FilterMeta)

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

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


        switch = filterindex

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

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

    misfit_list = []  # init a list for a all the singular misfits
    norm_list = []  # init a list for a all the singular normalizations
    taper = trace.CosFader(xfade=2.0)  # Cosine taper with fade in and out of 2s.
    bw_filter = trace.ButterworthResponse(corner=0.000055,  # in Hz
                                      order=4,
                                      type='high')  # "low"pass or "high"pass
    setup = trace.MisfitSetup(description='Misfit Setup',
                              norm=2,  # L1 or L2 norm
                              taper=taper,
                              filter=bw_filter,
                              domain='time_domain')
    nsamples = len(data)
    tmin = util.str_to_time('2010-02-20 15:15:30.100')
    tr = trace.Trace(station='TEST', channel='Z',
                     deltat=0.5, tmin=tmin, ydata=data)
    syn = trace.Trace(station='TEST', channel='Z',
                     deltat=0.5, tmin=tmin, ydata=sembmax_syn)
    misfit, norm = tr.misfit(candidate=syn, setup=setup) # calculate the misfit of a single observed trace with its synthetics
    # with the setup from above
    misfit_list.append(misfit), norm_list.append(norm)  # append the misfit into a list
    global_misfit_normed = num.sqrt(num.nansum((num.asarray(misfit_list))**2) / # sum all the misfits and normalize to get a single minimizable value
                                    num.nansum((num.asarray(norm_list))**2))
    return global_misfit_normed
Beispiel #29
0
def calcTTTAdvTauP(Config,
                   station,
                   Origin,
                   flag,
                   Xcorrshift=None,
                   Refshift=None,
                   flag_rpe=False):

    cfg = ConfigObj(dict=Config)
    if flag_rpe is False:
        dimX = cfg.Int('dimx')
        dimY = cfg.Int('dimy')
    else:
        dimX = cfg.Int('dimx_emp')
        dimY = cfg.Int('dimy_emp')
    gridspacing = cfg.config_geometry.gridspacing
    print('done this')
    o_lat = float(Origin['lat'])
    o_lon = float(Origin['lon'])
    o_depth = float(Origin['depth'])

    oLator = o_lat + dimX / 2
    oLonor = o_lon + dimY / 2
    oLatul = 0
    oLonul = 0

    TTTGridMap = {}
    LMINMAX = []
    GridArray = {}
    locStation = Location(station.lat, station.lon)

    sdelta = loc2degrees(Location(o_lat, o_lon), locStation)
    Logfile.add('TTT PROCESS %d STATION: %s --> DELTA: %f' %
                (flag, station.getName(), sdelta))

    inputpath = str(flag) + '-' + station.getName() + ".input"
    outputpath = str(flag) + '-' + station.getName() + ".output"
    errorpath = str(flag) + '-' + station.getName() + '.error'

    fobjinput = open(inputpath, 'w')

    fobjinput.write('s\n')
    fobjinput.write(('%s %s\n') % (station.lat, station.lon))
    fobjinput.write('h\n')
    fobjinput.write(('%s\n') % (o_depth))

    for i in xrange(dimX):
        oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing

        for j in xrange(dimY):
            oLonul = o_lon - ((dimY - 1) / 2) * gridspacing + j * gridspacing

            fobjinput.write('e\n')
            fobjinput.write(('%s %s\n') % (oLatul, oLonul))
    # endfor

    fobjinput.close()

    cmd = ('taup_time -ph P -mod ak135 -time -o %s < %s > %s') % (
        outputpath, inputpath, errorpath)
    p = subprocess.Popen(cmd,
                         shell=True,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.STDOUT)
    p.wait()

    L = []
    output = open(outputpath, 'r')
    'OUTPUT: ', outputpath

    for k in output:
        k = k.split()

        if len(k) == 1:
            tt = k[0].replace('\n', '')
            tt = float(tt) - float(Xcorrshift[station.getName()].shift)
            L.append(tt)

    output.close()

    z = 0

    for i in xrange(dimX):
        oLatul = o_lat - ((dimX - 1) / 2) * gridspacing + i * gridspacing

        if z == 0 and i == 0:
            Latul = oLatul
        o = 0

        for j in xrange(dimY):
            oLonul = o_lon - ((dimY - 1) / 2) * gridspacing + j * gridspacing

            if o == 0 and j == 0:
                Lonul = oLonul

            de = loc2degrees(Location(oLatul, oLonul), locStation)
            time = L[i * dimX + j]

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

    mint = float(min(LMINMAX))
    maxt = float(max(LMINMAX))
    k = MinTMaxT(mint, maxt)

    TTTGridMap[station.getName()] = TTTGrid(o_depth, mint, maxt, Latul, Lonul,
                                            oLator, oLonor, GridArray)

    #tttname = str(flag)+'-ttt.pkl'
    #Basic.dumpToFile(tttname, TTTGridMap)
    #Basic.dumpToFile('minmax-'+str(flag)+'.pkl', k)
    if flag_rpe is True:
        Basic.dumpToFile(str(flag) + '-ttt_emp.pkl', TTTGridMap)
        Basic.dumpToFile('minmax-emp' + str(flag) + '.pkl', k)
        Basic.dumpToFile('station-emp' + str(flag) + '.pkl', station)
    else:
        Basic.dumpToFile(str(flag) + '-ttt.pkl', TTTGridMap)
        Basic.dumpToFile('minmax-' + str(flag) + '.pkl', k)
        Basic.dumpToFile('station-' + str(flag) + '.pkl', station)
    try:
        os.remove(inputpath)
        os.remove(outputpath)
        os.remove(errorpath)

    except:
        Logfile.exception('cannot delete files')