def subfault_STFs(rupt,epicenter,nstrike,ndip,beta=None,covfile=None): ''' Extract subfault source-time functions If analyzing an output .inv file make beta=0 ''' from numpy import genfromtxt,unique,zeros,where,meshgrid,linspace,load,arange,expand_dims,squeeze,tile,r_ from mudpy.forward import get_source_time_function,add2stf from mudpy.inverse import d2epi,ds2rot f=genfromtxt(rupt) num=f[:,0] nfault=nstrike*ndip #Get slips all_ss=f[:,8] all_ds=f[:,9] all=zeros(len(all_ss)*2) iss=2*arange(0,len(all)/2,1) ids=2*arange(0,len(all)/2,1)+1 all[iss]=all_ss all[ids]=all_ds #Compute CI #Load covariances if covfile!=None: rot=ds2rot(expand_dims(all,1),beta) C=load(covfile) CIplus=squeeze(rot)+1*(C**0.5) CIminus=squeeze(rot)-1*(C**0.5) CIminus[CIminus<0]=0 slipCIplus=(CIplus[iss]**2+CIplus[ids]**2)**0.5 slipCIminus=(CIminus[iss]**2+CIminus[ids]**2)**0.5 #Now parse for multiple rupture speeds unum=unique(num) #Count number of windows nwin=len(where(num==unum[0])[0]) #Get rigidities mu=f[0:len(unum),13] #Get rise times rise_time=f[0:len(unum),7] #Get areas area=f[0:len(unum),10]*f[0:len(unum),11] #Get coordinates and compute distances source=f[0:len(unum),1:4] d=d2epi(epicenter,source) #Loop over subfaults Mmax=0 Mout=[] for kfault in range(nfault): if kfault%10==0: print '... working on subfault '+str(kfault)+' of '+str(nfault) #Get rupture times for subfault windows i=where(num==unum[kfault])[0] trup=f[i,12] #Get slips on windows ss=all_ss[i] ds=all_ds[i] #Add it up slip=(ss**2+ds**2)**0.5 if covfile !=None: slip_plus=slipCIplus[i] slip_minus=slipCIminus[i] #Get first source time function t1,M1=get_source_time_function(mu[kfault],area[kfault],rise_time[kfault],trup[0],slip[0]) if covfile !=None: t1plus,M1plus=get_source_time_function(mu[kfault],area[kfault],rise_time[kfault],trup[0],slip_plus[0]) t1minus,M1minus=get_source_time_function(mu[kfault],area[kfault],rise_time[kfault],trup[0],slip_minus[0]) #Loop over windows for kwin in range(nwin-1): #Get next source time function t2,M2=get_source_time_function(mu[kfault],area[kfault],rise_time[kfault],trup[kwin+1],slip[kwin+1]) if covfile !=None: t2plus,M2plus=get_source_time_function(mu[kfault],area[kfault],rise_time[kfault],trup[kwin+1],slip_plus[kwin+1]) t2minus,M2minus=get_source_time_function(mu[kfault],area[kfault],rise_time[kfault],trup[kwin+1],slip_minus[kwin+1]) #Add the soruce time functions t1,M1=add2stf(t1,M1,t2,M2) if covfile !=None: t1plus,M1plus=add2stf(t1plus,M1plus,t2plus,M2plus) t1minus,M1minus=add2stf(t1minus,M1minus,t2minus,M2minus) #Save M1 for output if kfault==0: Mout=expand_dims(M1,1).T tout=expand_dims(t1,1).T else: Mout=r_[Mout,expand_dims(M1,1).T] tout=r_[tout,expand_dims(t1,1).T] #Track maximum moment Mmax=max(Mmax,M1.max()) print 'Maximum moment was '+str(Mmax)+'N-m' return tout,Mout
def subfault_STFs(rupt, epicenter, nstrike, ndip, beta=None, covfile=None): """ Extract subfault source-time functions If analyzing an output .inv file make beta=0 """ from numpy import genfromtxt, unique, zeros, where, meshgrid, linspace, load, arange, expand_dims, squeeze, tile, r_ from mudpy.forward import get_source_time_function, add2stf from mudpy.inverse import d2epi, ds2rot f = genfromtxt(rupt) num = f[:, 0] nfault = nstrike * ndip # Get slips all_ss = f[:, 8] all_ds = f[:, 9] all = zeros(len(all_ss) * 2) iss = 2 * arange(0, len(all) / 2, 1) ids = 2 * arange(0, len(all) / 2, 1) + 1 all[iss] = all_ss all[ids] = all_ds # Compute CI # Load covariances if covfile != None: rot = ds2rot(expand_dims(all, 1), beta) C = load(covfile) CIplus = squeeze(rot) + 1 * (C ** 0.5) CIminus = squeeze(rot) - 1 * (C ** 0.5) CIminus[CIminus < 0] = 0 slipCIplus = (CIplus[iss] ** 2 + CIplus[ids] ** 2) ** 0.5 slipCIminus = (CIminus[iss] ** 2 + CIminus[ids] ** 2) ** 0.5 # Now parse for multiple rupture speeds unum = unique(num) # Count number of windows nwin = len(where(num == unum[0])[0]) # Get rigidities mu = f[0 : len(unum), 13] # Get rise times rise_time = f[0 : len(unum), 7] # Get areas area = f[0 : len(unum), 10] * f[0 : len(unum), 11] # Get coordinates and compute distances source = f[0 : len(unum), 1:4] d = d2epi(epicenter, source) # Loop over subfaults Mmax = 0 Mout = [] for kfault in range(nfault): if kfault % 10 == 0: print "... working on subfault " + str(kfault) + " of " + str(nfault) # Get rupture times for subfault windows i = where(num == unum[kfault])[0] trup = f[i, 12] # Get slips on windows ss = all_ss[i] ds = all_ds[i] # Add it up slip = (ss ** 2 + ds ** 2) ** 0.5 if covfile != None: slip_plus = slipCIplus[i] slip_minus = slipCIminus[i] # Get first source time function t1, M1 = get_source_time_function(mu[kfault], area[kfault], rise_time[kfault], trup[0], slip[0]) if covfile != None: t1plus, M1plus = get_source_time_function( mu[kfault], area[kfault], rise_time[kfault], trup[0], slip_plus[0] ) t1minus, M1minus = get_source_time_function( mu[kfault], area[kfault], rise_time[kfault], trup[0], slip_minus[0] ) # Loop over windows for kwin in range(nwin - 1): # Get next source time function t2, M2 = get_source_time_function( mu[kfault], area[kfault], rise_time[kfault], trup[kwin + 1], slip[kwin + 1] ) if covfile != None: t2plus, M2plus = get_source_time_function( mu[kfault], area[kfault], rise_time[kfault], trup[kwin + 1], slip_plus[kwin + 1] ) t2minus, M2minus = get_source_time_function( mu[kfault], area[kfault], rise_time[kfault], trup[kwin + 1], slip_minus[kwin + 1] ) # Add the soruce time functions t1, M1 = add2stf(t1, M1, t2, M2) if covfile != None: t1plus, M1plus = add2stf(t1plus, M1plus, t2plus, M2plus) t1minus, M1minus = add2stf(t1minus, M1minus, t2minus, M2minus) # Save M1 for output if kfault == 0: Mout = expand_dims(M1, 1).T tout = expand_dims(t1, 1).T else: Mout = r_[Mout, expand_dims(M1, 1).T] tout = r_[tout, expand_dims(t1, 1).T] # Track maximum moment Mmax = max(Mmax, M1.max()) print "Maximum moment was " + str(Mmax) + "N-m" return tout, Mout
beta = 45 #Rotational offset (in degrees) applied to rake (0 for normal) Ltype = 0 # 0 for Tikhonov and 2 for Laplacian solver = 'nnls' # 'lstsq','nnls' top = 'free' bottom = 'locked' left = 'locked' right = 'locked' #'locked' or 'free' bounds = (top, bottom, left, right) ################################################################################e= ######## Run-time modifications to the time series ############ weight = False decimate = None #Decimate by constant (=None for NO decimation) bandpass = None #Corner frequencies in Hz =None if no filter is desired ################################################################################ G_name = 'test' m = zeros((600, 1)) iss = arange(0, len(m), 2) ids = arange(1, len(m), 2) mod = genfromtxt( u'/Users/dmelgar/Slip_inv/Nepal_Avouac_1s/output/inverse_models/models/ALOS_GPS_3.3_20win_pulse_vall3.0001.inv.total' ) m[iss, 0] = mod[:, 8] m[ids, 0] = mod[:, 9] mrot = inverse.ds2rot(m, 45) G = inverse.getG(home, project_name, fault_name, model_name, GF_list, G_from_file, G_name, epicenter, rupture_speed, num_windows, decimate, bandpass) d = inverse.getdata(home, project_name, GF_list, decimate, bandpass=None) ds = G.dot(mrot)
Make a lowpass zero phase filter ''' from scipy.signal import butter, filtfilt, lfilter from numpy import size, array if size(fcorner) == 2: ftype = 'bandpass' else: ftype = 'lowpass' fnyquist = fsample / 2 b, a = butter(order, array(fcorner) / (fnyquist), ftype) if zerophase == True: data_filt = filtfilt(b, a, data) else: data_filt = lfilter(b, a, data) return data_filt m = zeros((len(f) * 2, 1)) i = arange(1, len(f) * 2, 2) m[i, 0] = f[:, 9] mrot = ds2rot(m, 225) d = G.dot(mrot) #tsunami #d=lowpass(squeeze(d),array([1./7200,1./600]),1./60,2) save(fout, d)
fault_name = 'lefkada65.fault' nfaults = (40, 12) num_windows = 1 #Load model and roatet #f=genfromtxt(u'/Users/dmelgar/Slip_inv/Lefkada_fwd/forward_models/checkerboard.rupt') f = genfromtxt( u'/Users/dmelgar/Slip_inv/Lefkada_fwd/forward_models/checkerboard_1win.rupt' ) iss = arange(0, 2 * len(f), 2) ids = arange(1, 2 * len(f), 2) m1 = zeros((2 * len(f), 1)) m1[iss] = expand_dims(f[:, 8], 1) m1[ids] = expand_dims(f[:, 9], 1) m = ds2rot(m1, 135) #GFs G = load( "/Users/dmelgar/Slip_inv/Lefkada_fwd/GFs/matrices/gps_sm_insar_5win_vr2.6.npy" ) G = G[:, 0:960] dsynth = G.dot(m) #Smoothing N = nfaults[0] * nfaults[ 1] * num_windows * 2 #Get total no. of model parameters Ls = eye(N) LsLs = Ls.transpose().dot(Ls) #WHich data are we doing?
def tile_moment(rupt,epicenter,nstrike,ndip,covfile,beta): ''' Tile plot of subfault source-time functions ''' import matplotlib.pyplot as plt from matplotlib import cm from numpy import genfromtxt,unique,zeros,where,meshgrid,linspace,load,arange,expand_dims,squeeze from mudpy.forward import get_source_time_function,add2stf from mudpy.inverse import d2epi,ds2rot f=genfromtxt(rupt) num=f[:,0] nfault=nstrike*ndip #Get slips all_ss=f[:,8] all_ds=f[:,9] all=zeros(len(all_ss)*2) iss=2*arange(0,len(all)/2,1) ids=2*arange(0,len(all)/2,1)+1 all[iss]=all_ss all[ids]=all_ds rot=ds2rot(expand_dims(all,1),beta) #Compute CI #Load covariances if covfile!=None: C=load(covfile) CIplus=squeeze(rot)+1*(C**0.5) CIminus=squeeze(rot)-1*(C**0.5) CIminus[CIminus<0]=0 slipCIplus=(CIplus[iss]**2+CIplus[ids]**2)**0.5 slipCIminus=(CIminus[iss]**2+CIminus[ids]**2)**0.5 #Now parse for multiple rupture speeds unum=unique(num) #Count number of windows nwin=len(where(num==unum[0])[0]) #Get rigidities mu=f[0:len(unum),13] #Get rise times rise_time=f[0:len(unum),7] #Get areas area=f[0:len(unum),10]*f[0:len(unum),11] #Get coordinates and compute distances source=f[0:len(unum),1:4] d=d2epi(epicenter,source) #Define velocity limits vfast=3.8 vslow=1.0 #Get indices for plot istrike=zeros(nstrike*ndip) idip=zeros(nstrike*ndip) k=0 for i in range(ndip): for j in range(nstrike): istrike[k]=nstrike-j-1 idip[k]=i k+=1 #Define canvas fig, axarr = plt.subplots(ndip, nstrike) #Loop over subfaults Mmax=0 for kfault in range(nfault): if kfault%10==0: print '... working on subfault '+str(kfault)+' of '+str(nfault) #Get rupture times for subfault windows i=where(num==unum[kfault])[0] trup=f[i,12] #Get slips on windows ss=all_ss[i] ds=all_ds[i] #Add it up slip=(ss**2+ds**2)**0.5 if covfile !=None: slip_plus=slipCIplus[i] slip_minus=slipCIminus[i] #Get first source time function t1,M1=get_source_time_function(mu[kfault],area[kfault],rise_time[kfault],trup[0],slip[0]) if covfile !=None: t1plus,M1plus=get_source_time_function(mu[kfault],area[kfault],rise_time[kfault],trup[0],slip_plus[0]) t1minus,M1minus=get_source_time_function(mu[kfault],area[kfault],rise_time[kfault],trup[0],slip_minus[0]) #Loop over windows for kwin in range(nwin-1): #Get next source time function t2,M2=get_source_time_function(mu[kfault],area[kfault],rise_time[kfault],trup[kwin+1],slip[kwin+1]) if covfile !=None: t2plus,M2plus=get_source_time_function(mu[kfault],area[kfault],rise_time[kfault],trup[kwin+1],slip_plus[kwin+1]) t2minus,M2minus=get_source_time_function(mu[kfault],area[kfault],rise_time[kfault],trup[kwin+1],slip_minus[kwin+1]) #Add the soruce time functions t1,M1=add2stf(t1,M1,t2,M2) if covfile !=None: t1plus,M1plus=add2stf(t1plus,M1plus,t2plus,M2plus) t1minus,M1minus=add2stf(t1minus,M1minus,t2minus,M2minus) #Track maximum moment Mmax=max(Mmax,M1.max()) #Done now plot them #get current axis ax=axarr[idip[kfault], istrike[kfault]] #Make contourf Mc=linspace(0,0.98*max(M1),100) T,M=meshgrid(t1,Mc) i=where(T==0)[0] T[i]=0.01 V=d[kfault]/T im=ax.contourf(T,M,V,100,vmin=vslow,vmax=vfast,cmap=cm.spectral) #Cover upper part ax.fill_between(t1,y1=M1,y2=1.01*M1.max(),color='white') #Plot confidence intervals if covfile !=None: ax.fill_between(t1,M1minus,M1plus,facecolor='grey',alpha=0.4) ax.plot(t1,M1plus,color='black') ax.plot(t1,M1minus,color='white',lw=2) #Plot curve ax.plot(t1, M1,color='k') ax.grid() ax.set_xlim([t1[0],t1[-1]]) ax.xaxis.set_ticks(linspace(t1[0],t1[-1],5)) ax.xaxis.set_ticklabels([]) ax.yaxis.set_ticklabels([]) #Go back and rescale all subplots by maximum moment for k in range(ndip): for k2 in range(nstrike): ax=axarr[k,k2] ax.set_ylim([0,Mmax]) #Fix subplot arrangement plt.subplots_adjust(left=0.02, bottom=0.02, right=0.9, top=0.98, wspace=0, hspace=0) #Add colorbar cbar_ax = fig.add_axes([0.91, 0.15, 0.01, 0.7]) cb=fig.colorbar(im, cax=cbar_ax) cb.set_label('Reference rupture velocity (km/s)') print 'Maximum moment was '+str(Mmax)+'N-m'