def single_event(ievent): nsta = len(stalist) stapos = np.zeros((nsta, 2)) idx = 0 staposcl = np.zeros((nsta, 2)) for ista in range(nsta): stanet = stalist[ista].split('.')[0] staname = stalist[ista].split('.')[1] stasub = irissta[(irissta['net'] == stanet) & (irissta['sta'] == staname)] stlat = stasub.iloc[0]['lat'] stlon = stasub.iloc[0]['lon'] stapos[ista, 0] = stlat stapos[ista, 1] = stlon strmacc1 = obspy.core.stream.Stream() strmori = obspy.core.stream.Stream() if evdep1 < 5.0: print(evdep1) return strmacc1, strmori idx = np.arange(nsta) for ista in range(len(idx)): stlat = stapos[idx[ista], 0] stlon = stapos[idx[ista], 1] dis1 = distaz.DistAz(evlat1, evlon1, stlat, stlon) if dis1.delta < mindist or dis1.delta > maxdist: continue #return strmacc1,strmori if phase == 'P': parr = ftelep(dis1.delta, evdep1)[0] else: parr = fteles(dis1.delta, evdep1)[0] trace = '/'.join([eqdir, evname1, datatype, stalist[idx[ista]]]) strm = obspy.read(trace) strm.merge() if comp == 'BHE': tracen = trace.split('.')[:-1] tracen = '.'.join(tracen + ['BHN']) if not os.path.isfile(tracen): continue strm += obspy.read(tracen) strm.resample(rsample) strm.trim(evtime1 + parr - trimb, evtime1 + parr + trima, pad=True, fill_value=0) #print(strm) strm.rotate('NE->RT', back_azimuth=dis1.baz) tracet = strm.select(channel='BHT') strm = tracet #strm.remove(tracet[0]) else: strm.trim(evtime1 + parr - trimb, evtime1 + parr + trima, pad=True, fill_value=0) strm.resample(rsample) tr = strm[0] tr.stats.distance = dis1.delta #inc_angle tr.stats.baz = dis1.baz if tr.stats.npts < 50: continue #return strmacc1,strmori tr.stats.coordinates = AttribDict({ 'latitude': stlat, 'longitude': stlon, 'elevation': 0 }) npts = tr.stats.npts tr.stats.starttime = 0 tr.detrend() tr.taper(tpratio) trdata = np.zeros(npts + 20 * rsample, ) trdata[10 * rsample:-10 * rsample] = tr.data trdata = bandpass(trdata, frqmin, frqmax, tr.stats.sampling_rate, 2, True) tr.data = trdata[10 * rsample:-10 * rsample] tr.normalize() if envolope: tr.data = np.abs(hilbert(tr.data)) strmori.append(tr.copy()) if timedomain: tr = autocorr_td(tr, sig_bs, sig_es, conlen=conlen) else: tr = autocorr_fd(tr, conlen=conlen) npts = tr.stats.npts tr.taper(tpratio) trdata = np.zeros(npts + 20 * rsample, ) trdata[10 * rsample:-10 * rsample] = tr.data trdata = bandpass(trdata, frqmin, frqmax, tr.stats.sampling_rate, 2, True) tr.data = trdata[10 * rsample:-10 * rsample] tr.normalize() if np.isnan(tr.data).any(): #return strmacc1,strmori continue strmacc1.append(tr.copy()) print('finishing acc') return strmacc1, strmori
nsta = len(stalist1) stapos = np.zeros((nsta,2)) print ('the',ievent,'th event of all:',nevent) print ('%s %s %4.1f %7.2f %7.2f %7.2f %d\n' % ('evinfo :',evname,evmag1,evlat1,evlon1,evdep1,nsta)) for ista in range(nsta): stanet = stalist[ista].split('.')[0] staname = stalist[ista].split('.')[1] location = stalist[ista].split('.')[2] stasub = irissta[(irissta['net']==stanet) & (irissta['sta']==staname)] if len(stasub)<1: continue stlat = stasub.iloc[0]['lat'] stlon = stasub.iloc[0]['lon'] dis1 = distaz.DistAz(evlat1,evlon1,stlat,stlon) if dis1.delta<mindist or dis1.delta>maxdist: continue parr = ftelep(dis1.delta,evdep1)[0] trace = '/'.join([eqdir,evname,datatp,stalist[ista]]) if os.path.getsize(trace) < 10000: continue strm = obspy.read(trace) #if comp == 'BHR' or comp == 'BHT': if comp in ['BHR','BHT']: tracen = trace.split('.')[:-1] tracen = '.'.join(tracen+['BHN'])
def plot_event(strmacc1, strmori): datasave_sub = defaultdict(list) strmstack = obspy.core.stream.Stream() accstack = obspy.core.stream.Stream() tr = strmacc1[0].copy() reftime0 = fppdp(refdismax, evdep1 + mindep) reftime1 = fppdp(refdismax, evdep1 + maxdep) reftime0s = fspdp(refdismax, evdep1 + mindep) reftime1s = fspdp(refdismax, evdep1 + maxdep) if len(strmacc1) < 10: print('small no. of traces:', len(strmacc1)) return staposcl = np.zeros((len(strmacc1), 2)) for idx in range(len(strmacc1)): staposcl[idx, 0] = strmacc1[idx].stats.coordinates['latitude'] staposcl[idx, 1] = strmacc1[idx].stats.coordinates['longitude'] stidx, aslat, aslon = cluster.clustersta(staposcl[:, 0], staposcl[:, 1]) uidx, ucounts = np.unique(stidx, return_counts=True) idx = np.where(ucounts > 5)[0] useidx = int(len(idx) * 0.8) npts = strmacc1[0].stats.npts stacksub = np.zeros((npts, useidx)) starttime = strmacc1[0].stats.starttime stacklinear = np.zeros((npts, )) buall = np.zeros(npts, ) phi = np.zeros((npts, ), dtype=complex) idx = random.sample(list(idx), useidx) reftime = fppdp(refdismax, evdep1) for ii in idx: bu = np.zeros(npts, ) data = np.zeros(npts, ) cellidx = np.where(stidx == uidx[ii])[0] reflat = np.rad2deg(aslat[cellidx[0]]) reflon = np.rad2deg(aslon[cellidx[0]]) disref = distaz.DistAz(evlat1, evlon1, reflat, reflon) sidx = random.sample(list(cellidx), 1)[0] refdis = strmacc1[sidx].stats.distance ctime = fppdp(refdis, evdep1) ctime = reftime - ctime data = np.roll(strmacc1[sidx].data[:npts], int(np.round(ctime * rsample))) phi = phi + np.exp(1j * np.angle(hilbert(data))) buall = buall + data for jj in range(len(cellidx)): refdis = strmacc1[cellidx[jj]].stats.distance ctime = fppdp(refdis, evdep1) ctime = reftime - ctime data = np.roll(strmacc1[cellidx[jj]].data[:npts], int(np.round(ctime * rsample))) phi = phi + np.exp(1j * np.angle(hilbert(data))) bu = bu + data bu = bu / np.max(abs(bu)) tr.data = bu tr.stats.distance = disref.delta tr.stats.baz = disref.baz tr.normalize() accstack.append(tr.copy()) nsta = len(idx) stacklinear = buall / np.max(abs(buall)) if pws: aphi = (np.abs(phi) / nsta)**mu buall = bu * aphi stacklinear = buall / np.max(abs(buall)) * aphi if len(strmori) < 20: print('small no. of traces:', len(strmori)) #return staposcl = np.zeros((len(strmori), 2)) for idx in range(len(strmori)): staposcl[idx, 0] = strmori[idx].stats.coordinates['latitude'] staposcl[idx, 1] = strmori[idx].stats.coordinates['longitude'] stidx, aslat, aslon = cluster.clustersta(staposcl[:, 0], staposcl[:, 1], ncell=8000) uidx, ucounts = np.unique(stidx, return_counts=True) idx = np.where(ucounts >= 5)[0] if len(idx) > 20: idx = random.sample(range(len(idx)), 20) npts = strmori[0].stats.npts for ii in idx: data = np.zeros(npts, ) cellidx = np.where(stidx == uidx[ii])[0] reflat = np.rad2deg(aslat[cellidx[0]]) reflon = np.rad2deg(aslon[cellidx[0]]) disref = distaz.DistAz(evlat1, evlon1, reflat, reflon) bu = np.zeros(npts, ) for icell in cellidx: data = strmori[icell].data bu = bu + data[:npts] nsta = len(cellidx) aphi = (np.abs(phi) / nsta)**mu bu = bu #*aphi tr.data = bu tr.stats.distance = disref.delta tr.stats.baz = disref.baz tr.normalize() strmstack.append(tr.copy()) trec = np.linspace(-trimb, trima, npts) fig = plt.figure(figsize=(15, 20)) ax1 = fig.add_subplot(511) bg = 0 ed = np.min([int(reftime1 * rsample + 100), npts]) for ii in range(len(strmstack)): offset = strmstack[ii].stats.distance / 10.0 data = offset + strmstack[ii].data ax1.plot(trec[:endtime * rsample], data[:endtime * rsample]) ax1.set_title('Origin waveforms (epi) ' + str(ievent), fontsize=15) ax1.set_ylabel('Epi (x10)') ax2 = fig.add_subplot(512) naz = 4 aziint = 360.0 / naz for ii in range(len(strmstack)): offset = strmstack[ii].stats.baz / aziint data = offset + strmstack[ii].data ax2.plot(trec[:endtime * rsample], data[:endtime * rsample]) ax2.set_title('Origin waveforms (azi)') ax2.set_ylabel('Azimuth (x90)') ddep = np.linspace(mindep, maxdep, ndep) nsta = len(strmacc1) npts = strmacc1[0].stats.npts depstack = np.zeros(npts - 200, ) stack = np.zeros((ndep, npts)) evdep2 = evdep1 #+ddep[idep] reftime = fppdp(refdismax, evdep2) data = strmacc1[0].data.copy() bu = np.zeros(npts, ) phi = np.zeros((npts, ), dtype=complex) for ii in range(0, nsta): refdis = strmacc1[ii].stats.distance ctime = fppdp(refdis, evdep2) ctime = reftime - ctime data = np.roll(strmacc1[ii].data[:npts], int(np.round(ctime * rsample))) phi = phi + np.exp(1j * np.angle(hilbert(data))) bu = bu + data aphi = (np.abs(phi) / nsta)**mu bu = bu #*aphi if pws: stack = bu * aphi else: stack = bu depc = np.arange(mindep, maxdep, 5) ndepc = len(depc) stackazi = np.zeros((npts, ndepc, naz)) phiazi = np.zeros((npts, ndepc, naz), dtype=complex) nstaazi = np.zeros((ndepc, naz)) for jj in range(ndepc): evdepc = evdep1 + depc[jj] reftime = fppdp(refdismax, evdepc) for ii in range(len(strmacc1)): bu = np.zeros(npts, ) phi = np.zeros((npts, ), dtype=complex) data = np.zeros(npts, ) refdis = strmacc1[ii].stats.distance ctime = fppdp(refdis, evdepc) ctime = reftime - ctime data = np.roll(strmacc1[ii].data[:npts], int(np.round(ctime * rsample))) phi = np.exp(1j * np.angle(hilbert(data))) stbaz = strmacc1[ii].stats.baz idx = int(stbaz / aziint) stackazi[:, jj, idx] += data phiazi[:, jj, idx] += phi nstaazi[jj, idx] += 1 bg = np.max([int(reftime0 * rsample - 100), 0]) ed = np.min([int(reftime1s * rsample + 100), npts]) offset = 0 ttime = np.linspace(bg / rsample, ed / rsample, ed - bg) axacc = fig.add_subplot(513) for ii in range(len(accstack)): offset = accstack[ii].stats.distance / 10.0 data = offset + accstack[ii].data axacc.plot(ttime, data[bg:ed]) axacc.set_title('ACC (epi) ' + str(ievent), fontsize=15) axacc.set_ylabel('Epi (x10)') ax3 = fig.add_subplot(514) stackp = stacklinear[bg:ed] stackp = stack[bg:ed] ax3.plot(ttime, stackp / np.max(abs(stackp)) * 2, 'r-', linewidth=3.0) ax3.set_title('ACC stacking') for ii in range(naz): for jj in range(ndepc): offset = 3 + ii * 2 #nboots*2+ if pws: aphi = (np.abs(phiazi[:, jj, ii]) / nstaazi[jj, ii])**mu stackazi[:, jj, ii] = stackazi[:, jj, ii] * aphi data = offset + stackazi[:, jj, ii] / np.max( abs(stackazi[:, jj, ii])) * 2 ax3.plot(ttime, data[bg:ed]) ax3.text(bg / np.real(rsample), offset, str(int(nstaazi[jj, ii])), fontsize=15) ax3.axvline(reftime0, linestyle='--', color='magenta') ax3.axvline(reftime1, linestyle='--', color='magenta') ax3.axvline(reftime0s, linestyle='--', color='blue') ax3.axvline(reftime1s, linestyle='--', color='blue') datasave_sub['evid'] = evname1 datasave_sub['data'] = stacklinear datasave_sub['pP0'] = reftime0 * rsample datasave_sub['pP1'] = reftime1 * rsample depstack = np.zeros(ndep, ) data = np.zeros((naz, npts)) for ii in range(naz): for jj in range(ndepc): data[ii, :] += stackazi[:, jj, ii] / np.max( abs(stackazi[:, jj, ii])) stack = np.zeros(npts, ) for ii in range(naz): if nstaazi[0, ii] > 5: stack += np.abs(hilbert(data[ii, :] * nstaazi[0, ii] / nsta)) datasave_sub['stack'] = stack ax4 = fig.add_subplot(515) for idep in range(ndep): evdep2 = evdep1 + ddep[idep] pPtime = fppdp(refdismax, evdep2)[0] sPtime = fspdp(refdismax, evdep2)[0] depstack[idep] = 0.6 * stack[int( sPtime * rsample)]**2 + 0.4 * stack[int(pPtime * rsample)]**2 ax4.plot(ddep, depstack, 'rd-') ax4.set_xlabel('Depth') ymax = np.max([depstack.max(), 10.0]) ax4.set_ylim([-0.1, ymax]) ax4.set_title('Mapping to depth_' + str(evdep1)) fig.savefig(figdir + '/' + str(ievent) + '_' + evname1 + '_Mw' + str(evmag1) + '_td.png', dpi=200) plt.ioff() plt.close() datasave_sub['stackenv'] = stack datasave_sub['sdep'] = np.vstack([ddep, depstack]) datasave_sub['evlat'] = evlat1 datasave_sub['evlon'] = evlon1 datasave_sub['evdep_cat'] = evdep1 datasave_sub['evdep_inv'] = evdep1 + ddep[np.argmax(depstack)] datasave_sub['dist'] = refdismax datasave_sub['saveid'] = ievent return datasave_sub
def ellipDist(stla, stlo, evla, evlo): """elliptical distance in degrees""" d = distaz.DistAz(stla, stlo, evla, evlo) return d.getDelta()