def corread(saclist, ipick, timewindow, taperwindow, tapertype): """ Read data within timewindow+taperwindow (same length for each trace) for cross-correlation. """ tw = timewindow[1] - timewindow[0] taperwidth = taperwindow / (taperwindow + tw) reftimes = array([sacdh.gethdr(ipick) for sacdh in saclist]) if -12345.0 in reftimes: print('Not all seismograms has ipick={0:s} set. Exit.'.format(ipick)) sys.exit() nstart, ntotal = windowIndex(saclist, reftimes, timewindow, taperwindow) windata = windowData(saclist, nstart, ntotal, taperwidth, tapertype) return windata, reftimes
def corread(saclist, ipick, timewindow, taperwindow, tapertype): """ Read data within timewindow+taperwindow (same length for each trace) for cross-correlation. """ tw = timewindow[1] - timewindow[0] taperwidth = taperwindow/(taperwindow+tw) reftimes = array([ sacdh.gethdr(ipick) for sacdh in saclist]) if -12345.0 in reftimes: print ('Not all seismograms has ipick={0:s} set. Exit.'.format(ipick)) sys.exit() nstart, ntotal = windowIndex(saclist, reftimes, timewindow, taperwindow) windata = windowData(saclist, nstart, ntotal, taperwidth, tapertype) return windata, reftimes
def plotStack(self): """ Calculate mean stack from all traces and plot it. No taper window is added in. """ saclist = self.saclist mm = self.bmax, self.emin twplot = axLimit(mm, -0.01) twp = twplot[1] - twplot[0] taperwindow = 0 reftimes = [sacdh.reftime for sacdh in saclist] nstart, ntotal = windowIndex(saclist, reftimes, twplot, taperwindow) datacut = windowData(saclist, nstart, ntotal, taperwindow / twp) datamean = mean(datacut, 0) # copy a sacdh object for stack stackdh = copy.copy(saclist[0]) stackdh.b = twplot[0] - taperwindow * 0.5 stackdh.npts = len(datamean) stackdh.data = datamean stackdh.thdrs = [ 0., ] * 10 stackdh.filename = 'meanstack.sac' self.sstack = SingleSeis(stackdh, self.opts, self.axss, self.stackbase, self.stackcolor, self.stacklinew) # plot 1-std range from mean stack if self.opts.zero_on and self.opts.std_on: datastd = std(datacut, 0) stda = copy.copy(stackdh) stdb = copy.copy(stackdh) stda.data = datamean + datastd stdb.data = datamean - datastd stda.thdrs = [ 0, ] * 10 stdb.thdrs = [ 0, ] * 10 stda.filename = 'stackstdplus.sac' stda.filename = 'stackstdnega.sac' self.sstda = SingleSeis(stda, self.opts, self.axss, self.stackbase, self.stackcolor, self.stacklinew / 2.) self.sstdb = SingleSeis(stdb, self.opts, self.axss, self.stackbase, self.stackcolor, self.stacklinew / 2.) self.stdfill = self.axss.fill_between(self.sstack.time, stda.data, stdb.data, color=self.stackcolor, alpha=.25)
def plotStack(self): """ Calculate mean stack from all traces and plot it. No taper window is added in. """ saclist = self.saclist mm = self.bmax, self.emin twplot = axLimit(mm, -0.01) twp = twplot[1] - twplot[0] taperwindow = 0 reftimes = [ sacdh.reftime for sacdh in saclist ] nstart, ntotal = windowIndex(saclist, reftimes, twplot, taperwindow) datacut = windowData(saclist, nstart, ntotal, taperwindow/twp) datamean = mean(datacut, 0) # copy a sacdh object for stack stackdh = copy.copy(saclist[0]) stackdh.b = twplot[0] - taperwindow*0.5 stackdh.npts = len(datamean) stackdh.data = datamean stackdh.thdrs = [0.,]*10 stackdh.filename = 'meanstack.sac' self.sstack = SingleSeis(stackdh, self.opts, self.axss, self.stackbase, self.stackcolor, self.stacklinew) # plot 1-std range from mean stack if self.opts.zero_on and self.opts.std_on: datastd = std(datacut, 0) stda = copy.copy(stackdh) stdb = copy.copy(stackdh) stda.data = datamean + datastd stdb.data = datamean - datastd stda.thdrs = [0,]*10 stdb.thdrs = [0,]*10 stda.filename = 'stackstdplus.sac' stda.filename = 'stackstdnega.sac' self.sstda = SingleSeis(stda, self.opts, self.axss, self.stackbase, self.stackcolor, self.stacklinew/2.) self.sstdb = SingleSeis(stdb, self.opts, self.axss, self.stackbase, self.stackcolor, self.stacklinew/2.) self.stdfill = self.axss.fill_between(self.sstack.time, stda.data, stdb.data, color=self.stackcolor, alpha=.25)
def ccWeightStack(saclist, opts): """ Align seismograms by the iterative cross-correlation and stack algorithm. Parameters ---------- opts.delta : sample time interval opts.ccpara : a class instance for ICCS parameters * qqhdrs : SAC headers to save quality factors: ccc, snr, coh * maxiter : maximum number of iteration * converg : convergence critrion * cchdrs : inputand output picks of cross-correlation * twcorr : time window for cross-correlation """ ccpara = opts.ccpara delta = opts.delta maxiter = ccpara.maxiter convtype = ccpara.convtype convepsi = ccpara.convepsi taperwidth = ccpara.taperwidth tapertype = ccpara.tapertype xcorr = ccpara.xcorr shift = ccpara.shift twhdrs = ccpara.twhdrs qqhdrs = ccpara.qheaders cchdrs = ccpara.cchdrs twcorr = ccpara.twcorr hdrccc, hdrsnr, hdrcoh, = qqhdrs[:3] cchdr0, cchdr1 = cchdrs if convtype == 'coef': convergence = coConverg elif convtype == 'resi': convergence = reConverg else: print('Unknown convergence criterion: {:s}. Exit.'.format(convtype)) sys.exit() out = '\n--> Run ICCS at window [{0:5.1f}, {1:5.1f}] wrt {2:s}. Write to header: {3:s}' print(out.format(twcorr[0], twcorr[1], cchdr0, cchdr1)) print(' Convergence criterion: {:s}'.format(convtype)) if ccpara.stackwgt == 'coef': wgtcoef = True else: wgtcoef = False taperwindow = taperWindow(twcorr, taperwidth) # get initial time picks tinis = array([sacdh.gethdr(cchdr0) for sacdh in saclist]) tfins = tinis.copy() nseis = len(saclist) ccc = zeros(nseis) snr = zeros(nseis) coh = zeros(nseis) wgts = ones(nseis) stkdata = [] for it in list(range(maxiter)): # recut data and update array stack nstart, ntotal = windowIndex(saclist, tfins, twcorr, taperwindow) windata = windowData(saclist, nstart, ntotal, taperwidth, tapertype) sdata = normWeightStack(windata, wgts, taperwidth, tapertype) stkdata.append(sdata) if it == 0: print('=== Iteration {0:d} : epsilon'.format(it)) else: conv = convergence(stkdata[it], stkdata[it - 1]) print('=== Iteration {0:d} : {1:8.6f}'.format(it, conv)) if conv <= convepsi: print('Array stack converged... Done. Mean corrcoef={0:.3f}'. format(mean(ccc))) break # Find time lag at peak correlation between each trace and the array stack. # Calculate cross correlation coefficient, signal/noise ratio and temporal coherence sdatanorm = sdata / LA.norm(sdata) for i in list(range(nseis)): datai = windata[i] delay, ccmax, ccpol = corrmax(sdata, datai, delta, xcorr, shift) tfins[i] += delay sacdh = saclist[i] sacdh.sethdr(cchdr1, tfins[i]) if wgtcoef: # update weight only when stackwgt == coef wgts[i] = ccpol * ccmax ccc[i] = ccmax sacdh.sethdr(hdrccc, ccc[i]) snr[i] = snratio(datai, delta, twcorr) sacdh.sethdr(hdrsnr, snr[i]) coh[i] = coherence(datai * ccpol, sdatanorm) sacdh.sethdr(hdrcoh, coh[i]) # get maximum time window for plot (excluding taperwindow) bb, ee = [], [] for i in list(range(nseis)): sacdh = saclist[i] b = sacdh.b - tfins[i] e = b + (sacdh.npts - 1) * delta bb.append(b + delta) ee.append(e - delta) b = max(bb) e = min(ee) d = (e - b) * taperwidth / 2 twplot = [b + d, e - d] # calculate final stack at twplot, save to a sacdh object: stkdh # set time picks of stkdh as mean of tinis and tfins taperwindow = taperWindow(twplot, taperwidth) nstart, ntotal = windowIndex(saclist, tfins, twplot, taperwindow) windata = windowData(saclist, nstart, ntotal, taperwidth, tapertype) sdata = normWeightStack(windata, wgts, taperwidth, tapertype) tinimean = mean(tinis) tfinmean = mean(tfins) stkdh = copy.copy(saclist[0]) stkdh.thdrs = [ -12345., ] * 10 stkdh.users = [ -12345., ] * 10 stkdh.kusers = [ '-12345 ', ] * 3 stkdh.b = twplot[0] - taperwindow * 0.5 + tfinmean stkdh.npts = len(sdata) stkdh.data = sdata stkdh.sethdr(cchdr0, tinimean) stkdh.sethdr(cchdr1, tfinmean) stkdh.knetwk = 'Array ' stkdh.kstnm = 'Stack ' stkdh.netsta = 'Array.Stack' stkdh.gcarc = -1 stkdh.dist = -1 stkdh.baz = -1 stkdh.az = -1 # set time window stkdh.sethdr(twhdrs[0], twcorr[0] + tfinmean) stkdh.sethdr(twhdrs[1], twcorr[1] + tfinmean) if opts.fstack is None: stkdh.filename = ccpara.fstack else: stkdh.filename = opts.fstack for sacdh, tfin in zip(saclist, tfins): sacdh.sethdr(twhdrs[0], tfin + twcorr[0]) sacdh.sethdr(twhdrs[1], tfin + twcorr[1]) quas = array([ccc, snr, coh]) return stkdh, stkdata, quas
def ccWeightStack(saclist, opts): """ Align seismograms by the iterative cross-correlation and stack algorithm. Parameters ---------- opts.delta : sample time interval opts.ccpara : a class instance for ICCS parameters * qqhdrs : SAC headers to save quality factors: ccc, snr, coh * maxiter : maximum number of iteration * converg : convergence critrion * cchdrs : inputand output picks of cross-correlation * twcorr : time window for cross-correlation """ ccpara = opts.ccpara delta = opts.delta maxiter = ccpara.maxiter convtype = ccpara.convtype convepsi = ccpara.convepsi taperwidth = ccpara.taperwidth tapertype = ccpara.tapertype xcorr = ccpara.xcorr shift = ccpara.shift twhdrs = ccpara.twhdrs qqhdrs = ccpara.qheaders cchdrs = ccpara.cchdrs twcorr = ccpara.twcorr hdrccc, hdrsnr, hdrcoh, = qqhdrs[:3] cchdr0, cchdr1 = cchdrs if convtype == 'coef': convergence = coConverg elif convtype == 'resi': convergence = reConverg else: print('Unknown convergence criterion: {:s}. Exit.'.format(convtype)) sys.exit() out = '\n--> Run ICCS at window [{0:5.1f}, {1:5.1f}] wrt {2:s}. Write to header: {3:s}' print(out.format(twcorr[0], twcorr[1], cchdr0, cchdr1)) print(' Convergence criterion: {:s}'.format(convtype)) if ccpara.stackwgt == 'coef': wgtcoef = True else: wgtcoef = False taperwindow = taperWindow(twcorr, taperwidth) # get initial time picks tinis = array([ sacdh.gethdr(cchdr0) for sacdh in saclist]) tfins = tinis.copy() nseis = len(saclist) ccc = zeros(nseis) snr = zeros(nseis) coh = zeros(nseis) wgts = ones(nseis) stkdata = [] for it in range(maxiter): # recut data and update array stack nstart, ntotal = windowIndex(saclist, tfins, twcorr, taperwindow) windata = windowData(saclist, nstart, ntotal, taperwidth, tapertype) sdata = normWeightStack(windata, wgts, taperwidth, tapertype) stkdata.append(sdata) if it == 0: print ('=== Iteration {0:d} : epsilon'.format(it)) else: conv = convergence(stkdata[it], stkdata[it-1]) print ('=== Iteration {0:d} : {1:8.6f}'.format(it, conv)) if conv <= convepsi: print ('Array stack converged... Done. Mean corrcoef={0:.3f}'.format(mean(ccc))) break # Find time lag at peak correlation between each trace and the array stack. # Calculate cross correlation coefficient, signal/noise ratio and temporal coherence sdatanorm = sdata/l2norm(sdata) for i in range(nseis): datai = windata[i] delay, ccmax, ccpol = corrmax(sdata, datai, delta, xcorr, shift) tfins[i] += delay sacdh = saclist[i] sacdh.sethdr(cchdr1, tfins[i]) if wgtcoef: # update weight only when stackwgt == coef wgts[i] = ccpol * ccmax ccc[i] = ccmax sacdh.sethdr(hdrccc, ccc[i]) snr[i] = snratio(datai, delta, twcorr) sacdh.sethdr(hdrsnr, snr[i]) coh[i] = coherence(datai*ccpol, sdatanorm) sacdh.sethdr(hdrcoh, coh[i]) # get maximum time window for plot (excluding taperwindow) bb, ee = [], [] for i in range(nseis): sacdh = saclist[i] b = sacdh.b - tfins[i] e = b + (sacdh.npts-1)* delta bb.append(b+delta) ee.append(e-delta) b = max(bb) e = min(ee) d = (e-b)*taperwidth/2 twplot = [b+d, e-d] # calculate final stack at twplot, save to a sacdh object: stkdh # set time picks of stkdh as mean of tinis and tfins taperwindow = taperWindow(twplot, taperwidth) nstart, ntotal = windowIndex(saclist, tfins, twplot, taperwindow) windata = windowData(saclist, nstart, ntotal, taperwidth, tapertype) sdata = normWeightStack(windata, wgts, taperwidth, tapertype) tinimean = mean(tinis) tfinmean = mean(tfins) stkdh = copy.copy(saclist[0]) stkdh.thdrs = [-12345.,] * 10 stkdh.users = [-12345.,] * 10 stkdh.kusers = ['-12345 ',] * 3 stkdh.b = twplot[0] - taperwindow*0.5 + tfinmean stkdh.npts = len(sdata) stkdh.data = sdata stkdh.sethdr(cchdr0, tinimean) stkdh.sethdr(cchdr1, tfinmean) stkdh.knetwk = 'Array ' stkdh.kstnm = 'Stack ' stkdh.netsta = 'Array.Stack' stkdh.gcarc = -1 stkdh.dist = -1 stkdh.baz = -1 stkdh.az = -1 # set time window stkdh.sethdr(twhdrs[0], twcorr[0]+tfinmean) stkdh.sethdr(twhdrs[1], twcorr[1]+tfinmean) if opts.fstack is None: stkdh.filename = ccpara.fstack else: stkdh.filename = opts.fstack for sacdh, tfin in zip(saclist, tfins): sacdh.sethdr(twhdrs[0], tfin+twcorr[0]) sacdh.sethdr(twhdrs[1], tfin+twcorr[1]) quas = array([ ccc, snr, coh ]) return stkdh, stkdata, quas