Example #1
0
def tsunami_waveforms(home,project_name,fault_name,rupture_name,station_file,model_name,run_name,GF_list,G_from_file,G_name,epicenter,
                rupture_speed,num_windows,coord_type,decimate,lowpass,resample,beta):            
    '''
    Forward compute tsunami waveforms the right way, load the GF matrix and just multiply by fault model
    '''
    from mudpy.inverse import getG
    from numpy import genfromtxt,zeros,arange,deg2rad,cos,sin,vstack,array,where
    from obspy import read

    #Read GFs
    G=getG(home,project_name,fault_name,model_name,GF_list,G_from_file,G_name,epicenter,
                rupture_speed,num_windows,coord_type,decimate,lowpass)
    #Read rupture model and convert into vector
    ss=genfromtxt(home+project_name+'/forward_models/'+rupture_name,usecols=8)
    ds=genfromtxt(home+project_name+'/forward_models/'+rupture_name,usecols=9)
    #Rotate by beta
    if beta != None:
        beta_rot=deg2rad(beta)
        R=array([[cos(beta_rot),sin(beta_rot)],[-sin(beta_rot),cos(beta_rot)]])
        rot=R.dot(vstack((ss,ds)))
        ss_rot=rot[0,:]
        ds_rot=rot[1,:]
    #Assemble into column vector
    m=zeros(len(ss)*2)
    iss=arange(0,len(m),2)
    ids=arange(1,len(m),2)
    m[iss]=ss_rot
    m[ids]=ds_rot
    #Multiply
    dtsun=G.dot(m)
    #Write to file (Modified from inverse.write_synthetics)
    #Read gf file and decide what needs to get loaded
    gf_file=home+project_name+'/data/station_info/'+GF_list
    stations=genfromtxt(gf_file,usecols=[0],skip_header=1,dtype='S')
    GF=genfromtxt(gf_file,usecols=[3,4,5,6,7],skip_header=1,dtype='f8')
    GFfiles=genfromtxt(gf_file,usecols=[8,9,10,11,12],skip_header=1,dtype='S')
    #Separate into its constituent parts (statics,displacaments, velocities, etc...)
    kinsert=0
    kgf=3
    i=where(GF[:,kgf]==1)[0]
    if len(i)>0:
        for ksta in range(len(i)):
            sta=stations[i[ksta]]
            tsun=read(GFfiles[i[ksta],kgf])
            npts=tsun[0].stats.npts
            synth=tsun.copy()
            synth[0].data=dtsun[kinsert:kinsert+npts]
            kinsert+=npts
            synth.write(home+project_name+'/output/forward_models/'+run_name+'.'+sta+'.tsun',format='SAC')
Example #2
0
def run_inversion(home,
                  project_name,
                  run_name,
                  fault_name,
                  model_name,
                  GF_list,
                  G_from_file,
                  G_name,
                  epicenter,
                  rupture_speed,
                  num_windows,
                  reg_spatial,
                  reg_temporal,
                  nfaults,
                  beta,
                  decimate,
                  bandpass,
                  solver,
                  bounds,
                  weight=False,
                  Ltype=2,
                  target_moment=None,
                  data_vector=None):
    '''
    Assemble G and d, determine smoothing and run the inversion
    '''
    from mudpy import inverse as inv
    from mudpy.forward import get_mu_and_area
    from numpy import zeros, dot, array, squeeze, expand_dims, empty, tile, eye, ones, arange, load, size
    from numpy.linalg import lstsq
    from scipy.sparse import csr_matrix as sparse
    from scipy.optimize import nnls
    from datetime import datetime
    import gc

    t1 = datetime.now()
    #Get data vector
    if data_vector == None:
        d = inv.getdata(home,
                        project_name,
                        GF_list,
                        decimate,
                        bandpass=bandpass)
    else:
        d = load(data_vector)
    #Get GFs
    G = inv.getG(home, project_name, fault_name, model_name, GF_list,
                 G_from_file, G_name, epicenter, rupture_speed, num_windows,
                 decimate, bandpass)
    gc.collect()
    #Get data weights
    if weight == True:
        print 'Applying data weights'
        w = inv.get_data_weights(home, project_name, GF_list, d, decimate)
        W = empty(G.shape)
        W = tile(w, (G.shape[1], 1)).T
        WG = empty(G.shape)
        WG = W * G
        wd = w * d.squeeze()
        wd = expand_dims(wd, axis=1)
        #Clear up extraneous variables
        W = None
        w = None
        #Define inversion quantities
        x = WG.transpose().dot(wd)
        print 'Computing G\'G'
        K = (WG.T).dot(WG)
    else:
        #Define inversion quantities if no weightd
        x = G.transpose().dot(d)
        print 'Computing G\'G'
        K = (G.T).dot(G)
    #Get regularization matrices (set to 0 matrix if not needed)
    static = False  #Is it jsut a static inversion?
    if size(reg_spatial) > 1:
        if Ltype == 2:  #Laplacian smoothing
            Ls = inv.getLs(home, project_name, fault_name, nfaults,
                           num_windows, bounds)
        elif Ltype == 0:  #Tikhonov smoothing
            N = nfaults[0] * nfaults[
                1] * num_windows * 2  #Get total no. of model parameters
            Ls = eye(N)
        elif Ltype == 3:  #moment regularization
            N = nfaults[0] * nfaults[
                1] * num_windows * 2  #Get total no. of model parameters
            Ls = ones((1, N))
            #Add rigidity and subfault area
            mu, area = get_mu_and_area(home, project_name, fault_name,
                                       model_name)
            istrike = arange(0, N, 2)
            Ls[0, istrike] = area * mu
            idip = arange(1, N, 2)
            Ls[0, idip] = area * mu
            #Modify inversion quantities
            x = x + Ls.T.dot(target_moment)
        else:
            print 'ERROR: Unrecognized regularization type requested'
            return
        Ninversion = len(reg_spatial)
    else:
        Ls = zeros(K.shape)
        reg_spatial = array([0.])
        Ninversion = 1
    if size(reg_temporal) > 1:
        Lt = inv.getLt(home, project_name, fault_name, num_windows)
        Ninversion = len(reg_temporal) * Ninversion
    else:
        Lt = zeros(K.shape)
        reg_temporal = array([0.])
        static = True
    #Make L's sparse
    Ls = sparse(Ls)
    Lt = sparse(Lt)
    #Get regularization tranposes for ABIC
    LsLs = Ls.transpose().dot(Ls)
    LtLt = Lt.transpose().dot(Lt)
    #inflate
    Ls = Ls.todense()
    Lt = Lt.todense()
    LsLs = LsLs.todense()
    LtLt = LtLt.todense()
    #off we go
    dt = datetime.now() - t1
    print 'Preprocessing wall time was ' + str(dt)
    print '\n--- RUNNING INVERSIONS ---\n'
    ttotal = datetime.now()
    kout = 0
    for kt in range(len(reg_temporal)):
        for ks in range(len(reg_spatial)):
            t1 = datetime.now()
            lambda_spatial = reg_spatial[ks]
            lambda_temporal = reg_temporal[kt]
            print 'Running inversion ' + str(kout + 1) + ' of ' + str(
                Ninversion) + ' at regularization levels: ls =' + repr(
                    lambda_spatial) + ' , lt = ' + repr(lambda_temporal)
            if static == True:  #Only statics inversion no Lt matrix
                Kinv = K + (lambda_spatial**2) * LsLs
                Lt = eye(len(K))
                LtLt = Lt.T.dot(Lt)
            else:  #Mixed inversion
                Kinv = K + (lambda_spatial**2) * LsLs + (lambda_temporal**
                                                         2) * LtLt
            if solver.lower() == 'lstsq':
                sol, res, rank, s = lstsq(Kinv, x)
            elif solver.lower() == 'nnls':
                x = squeeze(x.T)
                try:
                    sol, res = nnls(Kinv, x)
                except:
                    print '+++ WARNING: No solution found, writting zeros.'
                    sol = zeros(G.shape[1])
                x = expand_dims(x, axis=1)
                sol = expand_dims(sol, axis=1)
            else:
                print 'ERROR: Unrecognized solver \'' + solver + '\''
            #Compute synthetics
            ds = dot(G, sol)
            #Get stats
            L2, Lmodel = inv.get_stats(Kinv, sol, x)
            VR = inv.get_VR(home, project_name, GF_list, sol, d, ds, decimate)
            #VR=inv.get_VR(WG,sol,wd)
            #ABIC=inv.get_ABIC(WG,K,sol,wd,lambda_spatial,lambda_temporal,Ls,LsLs,Lt,LtLt)
            ABIC = inv.get_ABIC(G, K, sol, d, lambda_spatial, lambda_temporal,
                                Ls, LsLs, Lt, LtLt)
            #Get moment
            Mo, Mw = inv.get_moment(home, project_name, fault_name, model_name,
                                    sol)
            #If a rotational offset was applied then reverse it for output to file
            if beta != 0:
                sol = inv.rot2ds(sol, beta)
            #Write log
            inv.write_log(home, project_name, run_name, kout, rupture_speed,
                          num_windows, lambda_spatial, lambda_temporal, beta,
                          L2, Lmodel, VR, ABIC, Mo, Mw, model_name, fault_name,
                          G_name, GF_list, solver)
            #Write output to file
            inv.write_synthetics(home, project_name, run_name, GF_list, G, sol,
                                 ds, kout, decimate)
            inv.write_model(home, project_name, run_name, fault_name,
                            model_name, rupture_speed, num_windows, epicenter,
                            sol, kout)
            kout += 1
            dt1 = datetime.now() - t1
            dt2 = datetime.now() - ttotal
            print '... inversion wall time was ' + str(
                dt1) + ', total wall time elapsed is ' + str(dt2)
Example #3
0
def run_inversion(home,
                  project_name,
                  run_name,
                  fault_name,
                  model_name,
                  GF_list,
                  G_from_file,
                  G_name,
                  epicenter,
                  rupture_speed,
                  num_windows,
                  reg_spatial,
                  reg_temporal,
                  nfaults,
                  beta,
                  decimate,
                  bandpass,
                  solver,
                  bounds,
                  weight=False,
                  Ltype=2,
                  target_moment=None,
                  data_vector=None,
                  weights_file=None,
                  onset_file=None,
                  GOD_inversion=False):
    '''
    Assemble G and d, determine smoothing and run the inversion
    '''
    from mudpy import inverse as inv
    from mudpy.forward import get_mu_and_area
    from numpy import zeros, dot, array, squeeze, expand_dims, empty, tile, eye, ones, arange, load, size, genfromtxt
    from numpy import where, sort, r_, diag
    from numpy.linalg import lstsq
    from scipy.linalg import norm
    from scipy.sparse import csr_matrix as sparse
    from scipy.optimize import nnls
    from datetime import datetime
    import gc
    from matplotlib import path

    t1 = datetime.now()
    #Get data vector
    if data_vector == None:
        d = inv.getdata(home,
                        project_name,
                        GF_list,
                        decimate,
                        bandpass=bandpass)
    else:
        d = load(data_vector)
    #Get GFs
    G = inv.getG(home,
                 project_name,
                 fault_name,
                 model_name,
                 GF_list,
                 G_from_file,
                 G_name,
                 epicenter,
                 rupture_speed,
                 num_windows,
                 decimate,
                 bandpass,
                 onset_file=onset_file)
    print(G.shape)
    gc.collect()
    #Get data weights
    if weight == True:
        print('Applying data weights')
        if weights_file == None:
            w = inv.get_data_weights(home, project_name, GF_list, d, decimate)
        else:  # Remember weights are "uncertainties" alrger value is less trustworthy
            w = genfromtxt(weights_file)
            w = 1 / w

        #apply data weights
        wd = w * d.squeeze()

        #get norm after applying weights and normalize weghted data vector
        data_norm = norm(wd)
        wd = wd / data_norm

        #reshape for inversion
        wd = expand_dims(wd, axis=1)

        #Apply weights to left hand side of the equation (the GFs)
        W = empty(G.shape)
        #W=tile(w,(G.shape[1],1)).T #why this and not a diagonal matrix??? ANSWER: they have the same effect, don;t rememebr why I chose to do it this way
        W = diag(w)

        #Normalization effect
        W = W / data_norm
        WG = W.dot(G)

        #Clear up extraneous variables
        W = None
        w = None
        #Define inversion quantities
        x = WG.transpose().dot(wd)
        print('Computing G\'G')
        K = (WG.T).dot(WG)
    else:
        #Define inversion quantities if no weighted
        x = G.transpose().dot(d)
        print('Computing G\'G')
        K = (G.T).dot(G)
    #Get regularization matrices (set to 0 matrix if not needed)
    static = False  #Is it jsut a static inversion?
    if size(reg_spatial) > 1:
        if Ltype == 2:  #Laplacian smoothing
            Ls = inv.getLs(home, project_name, fault_name, nfaults,
                           num_windows, bounds)
        elif Ltype == 0:  #Tikhonov smoothing
            N = nfaults[0] * nfaults[
                1] * num_windows * 2  #Get total no. of model parameters
            Ls = eye(N)
        elif Ltype == 3:  #moment regularization
            N = nfaults[0] * nfaults[
                1] * num_windows * 2  #Get total no. of model parameters
            Ls = ones((1, N))
            #Add rigidity and subfault area
            mu, area = get_mu_and_area(home, project_name, fault_name,
                                       model_name)
            istrike = arange(0, N, 2)
            Ls[0, istrike] = area * mu
            idip = arange(1, N, 2)
            Ls[0, idip] = area * mu
            #Modify inversion quantities
            x = x + Ls.T.dot(target_moment)
        else:
            print('ERROR: Unrecognized regularization type requested')
            return
        Ninversion = len(reg_spatial)
    else:
        Ls = zeros(K.shape)
        reg_spatial = array([0.])
        Ninversion = 1
    if size(reg_temporal) > 1:
        Lt = inv.getLt(home, project_name, fault_name, num_windows)
        Ninversion = len(reg_temporal) * Ninversion
    else:
        Lt = zeros(K.shape)
        reg_temporal = array([0.])
        static = True
    #Make L's sparse
    Ls = sparse(Ls)
    Lt = sparse(Lt)
    #Get regularization tranposes for ABIC
    LsLs = Ls.transpose().dot(Ls)
    LtLt = Lt.transpose().dot(Lt)
    #inflate
    Ls = Ls.todense()
    Lt = Lt.todense()
    LsLs = LsLs.todense()
    LtLt = LtLt.todense()
    #off we go
    dt = datetime.now() - t1
    print('Preprocessing wall time was ' + str(dt))
    print('\n--- RUNNING INVERSIONS ---\n')
    ttotal = datetime.now()
    kout = 0
    for kt in range(len(reg_temporal)):
        for ks in range(len(reg_spatial)):
            t1 = datetime.now()
            lambda_spatial = reg_spatial[ks]
            lambda_temporal = reg_temporal[kt]
            print('Running inversion ' + str(kout + 1) + ' of ' +
                  str(Ninversion) + ' at regularization levels: ls =' +
                  repr(lambda_spatial) + ' , lt = ' + repr(lambda_temporal))
            if static == True:  #Only statics inversion no Lt matrix
                Kinv = K + (lambda_spatial**2) * LsLs
                Lt = eye(len(K))
                LtLt = Lt.T.dot(Lt)
            else:  #Mixed inversion
                Kinv = K + (lambda_spatial**2) * LsLs + (lambda_temporal**
                                                         2) * LtLt
            if solver.lower() == 'lstsq':
                sol, res, rank, s = lstsq(Kinv, x)
            elif solver.lower() == 'nnls':
                x = squeeze(x.T)
                try:
                    sol, res = nnls(Kinv, x)
                except:
                    print('+++ WARNING: No solution found, writting zeros.')
                    sol = zeros(G.shape[1])
                x = expand_dims(x, axis=1)
                sol = expand_dims(sol, axis=1)
            else:
                print('ERROR: Unrecognized solver \'' + solver + '\'')

            #Force faults outside a polygon to be zero
#            print('WARNING: Using fault polygon to force solutions to zero')
#            #load faulkt
#            fault=genfromtxt(home+project_name+'/data/model_info/'+fault_name)
#            polygon=genfromtxt('/Users/dmelgarm/Oaxaca2020/etc/zero_fault.txt')
#            polygon=path.Path(polygon)
#            i=where(polygon.contains_points(fault[:,1:3])==False)[0]
#            i=sort(r_[i*2,i*2+1])
#            N=nfaults[0]*2
#            i=r_[i,i+N,i+2*N,i+3*N]
#            sol[i]=0

#Compute synthetics
            ds = dot(G, sol)

            #Get stats
            L2, Lmodel = inv.get_stats(Kinv, sol, x, Ls)
            VR, L2data = inv.get_VR(home, project_name, GF_list, sol, d, ds,
                                    decimate, WG, wd)
            #VR=inv.get_VR(WG,sol,wd)
            #ABIC=inv.get_ABIC(WG,K,sol,wd,lambda_spatial,lambda_temporal,Ls,LsLs,Lt,LtLt)
            ABIC = inv.get_ABIC(G, K, sol, d, lambda_spatial, lambda_temporal,
                                Ls, LsLs, Lt, LtLt)
            #Get moment
            Mo, Mw = inv.get_moment(home, project_name, fault_name, model_name,
                                    sol)
            #If a rotational offset was applied then reverse it for output to file
            if beta != 0:
                sol = inv.rot2ds(sol, beta)
            #Write log
            inv.write_log(home, project_name, run_name, kout, rupture_speed,
                          num_windows, lambda_spatial, lambda_temporal, beta,
                          L2, Lmodel, VR, ABIC, Mo, Mw, model_name, fault_name,
                          G_name, GF_list, solver, L2data)
            #Write output to file
            if GOD_inversion == True:
                num = str(kout).rjust(4, '0')
                np.save(
                    home + project_name + '/output/inverse_models/' +
                    run_name + '.' + num + '.syn.npy', ds)
                inv.write_synthetics_GOD(home, project_name, run_name, GF_list,
                                         ds, kout, decimate)
            else:
                inv.write_synthetics(home, project_name, run_name, GF_list, G,
                                     sol, ds, kout, decimate)
            inv.write_model(home,
                            project_name,
                            run_name,
                            fault_name,
                            model_name,
                            rupture_speed,
                            num_windows,
                            epicenter,
                            sol,
                            kout,
                            onset_file=onset_file)
            kout += 1
            dt1 = datetime.now() - t1
            dt2 = datetime.now() - ttotal
            print('... inversion wall time was ' + str(dt1) +
                  ', total wall time elapsed is ' + str(dt2))
Example #4
0
def waveforms_matrix(home,project_name,fault_name,rupture_name,station_file,GF_list,
                model_name,run_name,epicenter,time_epi,integrate,tsunami,hot_start,
                resample,beta,rupture_speed,num_windows,dt,NFFT):
    '''
    To supplant waveforms() it needs to include resmapling and all that jazz...
    
    This routine will take synthetics and apply a slip dsitribution. It will delay each 
    subfault by the appropriate rupture time and linearly superimpose all of them. Output
    will be one sac waveform file per direction of motion (NEU) for each station defined in the
    station_file. Depending on the specified rake angle at each subfault the code will compute 
    the contribution to dip and strike slip directions. It will also compute the moment at that
    subfault and scale it according to the unit amount of momeent (1e15 N-m)
    
    IN:
        home: Home directory
        project_name: Name of the problem
        rupture_name: Name of rupture description file
        station_file: File with coordinates of stations
        model_Name: Name of Earth structure model file
        integrate: =0 if you want output to be velocity, =1 if you want output to de displacement
       
    OUT:
        Nothing
    '''
    from numpy import loadtxt,genfromtxt,allclose,vstack,deg2rad,array,sin,cos,where,zeros,arange
    from obspy import read,Stream,Trace
    from string import rjust
    import datetime
    import gc
    from mudpy.inverse import getG
    from linecache import getline
    from os import remove
    
    print 'Solving for kinematic problem'
    #Output where?
    outpath=home+project_name+'/output/forward_models/'
    logpath=home+project_name+'/logs/'
    log=''
    #Time for log file
    now=datetime.datetime.now()
    now=now.strftime('%b-%d-%H%M')
    #load source
    #source=loadtxt(home+project_name+'/forward_models/'+rupture_name,ndmin=2)
    #Load stations
    station_file=home+project_name+'/data/station_info/'+station_file
    staname=genfromtxt(station_file,dtype="S6",usecols=0)
    #Now load rupture model
    mss=genfromtxt(home+project_name+'/forward_models/'+rupture_name,usecols=8)
    mds=genfromtxt(home+project_name+'/forward_models/'+rupture_name,usecols=9)
    m=zeros(2*len(mss))
    i=arange(0,2*len(mss),2)
    m[i]=mss
    i=arange(1,2*len(mds),2)
    m[i]=mds
    #What am I processing v or d ?
    if integrate==1:
        vord='disp'
    else:
        vord='vel'
    #Load gflist
    gfsta=genfromtxt(home+project_name+'/data/station_info/'+GF_list,usecols=0,skip_header=1,dtype='S6')
    #Loop over stations
    for ksta in range(hot_start,len(staname)):
        print 'Working on station '+staname[ksta]+' ('+str(ksta+1)+'/'+str(len(staname))+')'
        #Initalize output
        n=Stream()
        e=Stream()
        z=Stream()
        sta=staname[ksta]
        #Make dummy data
        ndummy=Stream(Trace())
        ndummy[0].data=zeros(int(NFFT))
        ndummy[0].stats.delta=dt
        ndummy[0].stats.starttime=time_epi
        edummy=ndummy.copy()
        udummy=ndummy.copy()
        ndummy.write(home+project_name+'/data/waveforms/'+sta+'.'+vord+'.n',format='SAC')
        edummy.write(home+project_name+'/data/waveforms/'+sta+'.'+vord+'.e',format='SAC')
        udummy.write(home+project_name+'/data/waveforms/'+sta+'.'+vord+'.u',format='SAC')
        #Extract only one station from GF_list file
        ista=int(where(gfsta==staname[ksta])[0])+2
        #Make mini GF_file
        tmpgf='tmpfwd.gflist'
        try:
            remove(home+project_name+'/data/station_info/'+tmpgf)
        except:
            pass
        gflist=getline(home+project_name+'/data/station_info/'+GF_list,ista)
        f=open(home+project_name+'/data/station_info/'+tmpgf,'w')
        f.write('# Headers\n')
        f.write(gflist)
        f.close()
        #Get matrix for one station to all sources
        G_from_file=False
        G_name='tmpfwd'
        G=getG(home,project_name,fault_name,model_name,tmpgf,G_from_file,G_name,epicenter,
                rupture_speed,num_windows,decimate=None,bandpass=None,tsunami=False)
        # Matrix multiply and separate data streams
        d=G.dot(m)
        n=ndummy.copy()
        e=edummy.copy()
        u=udummy.copy()
        ncut=len(d)/3
        n[0].data=d[0:ncut]
        e[0].data=d[ncut:2*ncut]
        u[0].data=d[2*ncut:3*ncut]
        # Write to file
        n.write(home+project_name+'/output/forward_models/'+run_name+'.'+gfsta[ksta]+'.'+vord+'.n',format='SAC')
        e.write(home+project_name+'/output/forward_models/'+run_name+'.'+gfsta[ksta]+'.'+vord+'.e',format='SAC')
        u.write(home+project_name+'/output/forward_models/'+run_name+'.'+gfsta[ksta]+'.'+vord+'.u',format='SAC')
Example #5
0
right = 'locked'  #'locked' or 'free'
bounds = (top, bottom, left, right)
################################################################################e=
########      Run-time modifications to the time series             ############
weight = True
decimate = None  #Decimate by constant (=None for NO decimation)
displacement_bandpass = np.array([0.5])
velocity_bandpass = np.array([1. / 50, 0.3])
tsunami_bandpass = None  #np.array([0.0001,np.inf])
bandpass = [displacement_bandpass, velocity_bandpass, tsunami_bandpass]
################################################################################

dcomplete = inv.getdata(home, project_name, GF_list, decimate, bandpass=None)
#Get GFs
Gcomplete = inv.getG(home, project_name, fault_name, model_name, GF_list, True,
                     G_name, epicenter, rupture_speed, num_windows, decimate,
                     bandpass)

print 'Applying data weights'
w = inv.get_data_weights(home, project_name, GF_list, dcomplete, decimate)
W = empty(Gcomplete.shape)
W = tile(w, (Gcomplete.shape[1], 1)).T
WG = empty(Gcomplete.shape)
WG = W * Gcomplete
wd = w * dcomplete.squeeze()
wd = expand_dims(wd, axis=1)
WGcomplete = WG.copy()
wdcomplete = wd.copy()
#Clear up extraneous variables
W = None
w = None
Example #6
0
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)
Example #7
0
def run_inversion(home,project_name,run_name,fault_name,model_name,GF_list,G_from_file,G_name,epicenter,
                rupture_speed,num_windows,coord_type,reg_spatial,reg_temporal,nfaults,beta,decimate,lowpass,
                solver,bounds,segs):
    '''
    Assemble G and d, determine smoothing and run the inversion
    '''
    from mudpy import inverse as inv
    from mudpy import degadds
    from numpy import zeros,dot,array,squeeze,expand_dims,empty,tile,floor
    from numpy.linalg import lstsq
    from scipy.sparse import csr_matrix as sparse
    from scipy.optimize import nnls
    from datetime import datetime
    import gc
    
    

    t1=datetime.now()
    #Get data vector
    d=inv.getdata(home,project_name,GF_list,decimate,lowpass)
    #Get GFs
    G=inv.getG(home,project_name,fault_name,model_name,GF_list,G_from_file,G_name,epicenter,
                rupture_speed,num_windows,coord_type,decimate,lowpass)
    gc.collect()
    #Get data weights
    #w=inv.get_data_weights(home,project_name,GF_list,d,decimate)
    #W=empty(G.shape)
    #W=tile(w,(G.shape[1],1)).T
    #WG=empty(G.shape)
    #WG=W*G
    #wd=w*d.squeeze()
    #wd=expand_dims(wd,axis=1)
    ##Clear up extraneous variables
    #W=None
    #w=None
    ##Define inversion quantities
    #x=WG.transpose().dot(wd)
    #print 'Computing G\'G'
    #K=(WG.T).dot(WG)
    #Define inversion quantities
    x=G.transpose().dot(d)
    print 'Computing G\'G'
    K=(G.T).dot(G)
    #Get regularization matrices (set to 0 matrix if not needed)
    if type(reg_spatial)!=bool:
        ## DEG ADDED THIS IN TO DEAL WITH MULTIPLE FAULT SEGMENTS
        #if segs==0:
        #    Ls=inv.getLs(home,project_name,fault_name,nfaults,num_windows,bounds,segs)
        ##LsLs=Ls.transpose().dot(Ls)
        #elif segs==1: #This will make Ls a list of matrices, one index for each fault segment
        #    Ls=degadds.getLs_segs(home,project_name,fault_name,nfaults,num_windows,bounds,segs)
        Ls=degadds.getLs_segs(home,project_name,fault_name,nfaults,num_windows,bounds,segs)
        #    LsLs=[]
        #    nstrike_segs=nfaults[0]
        #    for i in range(len(nstrike_segs)):
        #        LsLs0 = Ls[i].transpose().dot(Ls[i])
        #        LsLs.append(LsLs0)
        Ninversion=len(reg_spatial)
    else:
        Ls=zeros(K.shape)
        reg_spatial=array([0.])
        Ninversion=1
    if type(reg_temporal)!=bool:
        Lt=inv.getLt(home,project_name,fault_name,num_windows)
        Ninversion=len(reg_temporal)*Ninversion
    else:
        Lt=zeros(K.shape)
        reg_temporal=array([0.])
    #Make L's sparse <-- does this actually change anything??--DEG
    #for i in range(len(nstrike_segs)):
    #    Ls=sparse(Ls[:,:,i])
    Ls=sparse(Ls)
    Lt=sparse(Lt)
    #Get regularization tranposes for ABIC
    LsLs=Ls.transpose().dot(Ls)
    LtLt=Lt.transpose().dot(Lt)
    #inflate
    Ls=Ls.todense()
    Lt=Lt.todense()
    LsLs=LsLs.todense()
    LtLt=LtLt.todense()
    #off we go
    dt=datetime.now()-t1
    print 'Preprocessing wall time was '+str(dt)
    print '\n--- RUNNING INVERSIONS ---\n'
    ttotal=datetime.now()
    kout=0
    for kt in range(len(reg_temporal)):
        for ks in range(len(reg_spatial)):
            t1=datetime.now()
            lambda_spatial=reg_spatial[ks]
            lambda_temporal=reg_temporal[kt]
            print 'Running inversion '+str(kout+1)+' of '+str(Ninversion)+' at regularization levels: ls ='+repr(lambda_spatial)+' , lt = '+repr(lambda_temporal)
            # DEG!! HERE THERE NEEDS TO BE SOME LOOP THROUGH THE LAMBDA_SPATIALS FOR EACH FAULT SEGMENT
            Kinv=K+(lambda_spatial**2)*+(lambda_temporal**2)*LtLt
            if solver.lower()=='lstsq':
                sol,res,rank,s=lstsq(Kinv,x)
            elif solver.lower()=='nnls':
                x=squeeze(x.T)
                sol,res=nnls(Kinv,x)
                x=expand_dims(x,axis=1)
                sol=expand_dims(sol,axis=1)
            else:
                print 'ERROR: Unrecognized solver \''+solver+'\''
            #Compute synthetics
            ds=dot(G,sol)
            #Get stats
            L2,Lmodel=inv.get_stats(Kinv,sol,x)
            VR=inv.get_VR(G,sol,d)
            ABIC=inv.get_ABIC(G,K,sol,d,lambda_spatial,lambda_temporal,Ls,LsLs,Lt,LtLt)
            ## CHANGE THIS FOR SEGS!!!! <-- DEG
            #ABIC=inv.get_ABIC(G,K,sol,d,lambda_spatial,lambda_temporal,Ls,LsLs,Lt,LtLt,nfaults,segs)
            #Get moment
            Mo,Mw=inv.get_moment(home,project_name,fault_name,model_name,sol)
            #If a rotational offset was applied then reverse it for output to file
            if beta !=0:
                sol=inv.rot2ds(sol,beta)
            #Write log
            inv.write_log(home,project_name,run_name,kout,rupture_speed,num_windows,lambda_spatial,lambda_temporal,beta,
                L2,Lmodel,VR,ABIC,Mo,Mw,model_name,fault_name,G_name,GF_list,solver)
            #Write output to file
            inv.write_synthetics(home,project_name,run_name,GF_list,G,sol,ds,kout,decimate)
            inv.write_model(home,project_name,run_name,fault_name,model_name,rupture_speed,num_windows,epicenter,sol,kout)
            kout+=1
            dt1=datetime.now()-t1
            dt2=datetime.now()-ttotal
            print '... inversion wall time was '+str(dt1)+', total wall time elapsed is '+str(dt2)
Example #8
0
def run_inversion(home,project_name,run_name,fault_name,model_name,GF_list,G_from_file,G_name,epicenter,
                rupture_speed,num_windows,reg_spatial,reg_temporal,nfaults,beta,decimate,bandpass,
                solver,bounds,weight=False,Ltype=2,target_moment=None,data_vector=None):
    '''
    Assemble G and d, determine smoothing and run the inversion
    '''
    from mudpy import inverse as inv
    from mudpy.forward import get_mu_and_area
    from numpy import zeros,dot,array,squeeze,expand_dims,empty,tile,eye,ones,arange,load
    from numpy.linalg import lstsq
    from scipy.sparse import csr_matrix as sparse
    from scipy.optimize import nnls
    from datetime import datetime
    import gc
    
    

    t1=datetime.now()
    #Get data vector
    if data_vector==None:
        d=inv.getdata(home,project_name,GF_list,decimate,bandpass=None)
    else:
        d=load(data_vector) 
    #Get GFs
    G=inv.getG(home,project_name,fault_name,model_name,GF_list,G_from_file,G_name,epicenter,
                rupture_speed,num_windows,decimate,bandpass)
    gc.collect()
    #Get data weights
    if weight==True:
        print 'Applying data weights'
        w=inv.get_data_weights(home,project_name,GF_list,d,decimate)
        W=empty(G.shape)
        W=tile(w,(G.shape[1],1)).T
        WG=empty(G.shape)
        WG=W*G
        wd=w*d.squeeze()
        wd=expand_dims(wd,axis=1)
        #Clear up extraneous variables
        W=None
        w=None
        #Define inversion quantities
        x=WG.transpose().dot(wd)
        print 'Computing G\'G'
        K=(WG.T).dot(WG)
    else:
        #Define inversion quantities if no weightd
        x=G.transpose().dot(d)
        print 'Computing G\'G'
        K=(G.T).dot(G)
    #Get regularization matrices (set to 0 matrix if not needed)
    static=False #Is it jsut a static inversion?
    if reg_spatial!=None:
        if Ltype==2: #Laplacian smoothing
            Ls=inv.getLs(home,project_name,fault_name,nfaults,num_windows,bounds)
        elif Ltype==0: #Tikhonov smoothing
            N=nfaults[0]*nfaults[1]*num_windows*2 #Get total no. of model parameters
            Ls=eye(N) 
        elif Ltype==3:  #moment regularization
            N=nfaults[0]*nfaults[1]*num_windows*2 #Get total no. of model parameters
            Ls=ones((1,N))
            #Add rigidity and subfault area
            mu,area=get_mu_and_area(home,project_name,fault_name,model_name)
            istrike=arange(0,N,2)
            Ls[0,istrike]=area*mu
            idip=arange(1,N,2)
            Ls[0,idip]=area*mu
            #Modify inversion quantities
            x=x+Ls.T.dot(target_moment)
        else:
            print 'ERROR: Unrecognized regularization type requested'
            return
        Ninversion=len(reg_spatial)
    else:
        Ls=zeros(K.shape)
        reg_spatial=array([0.])
        Ninversion=1
    if reg_temporal!=None:
        Lt=inv.getLt(home,project_name,fault_name,num_windows)
        Ninversion=len(reg_temporal)*Ninversion
    else:
        Lt=zeros(K.shape)
        reg_temporal=array([0.])
        static=True
    #Make L's sparse
    Ls=sparse(Ls)
    Lt=sparse(Lt)
    #Get regularization tranposes for ABIC
    LsLs=Ls.transpose().dot(Ls)
    LtLt=Lt.transpose().dot(Lt)
    #inflate
    Ls=Ls.todense()
    Lt=Lt.todense()
    LsLs=LsLs.todense()
    LtLt=LtLt.todense()
    #off we go
    dt=datetime.now()-t1
    print 'Preprocessing wall time was '+str(dt)
    print '\n--- RUNNING INVERSIONS ---\n'
    ttotal=datetime.now()
    kout=0
    for kt in range(len(reg_temporal)):
        for ks in range(len(reg_spatial)):
            t1=datetime.now()
            lambda_spatial=reg_spatial[ks]
            lambda_temporal=reg_temporal[kt]
            print 'Running inversion '+str(kout+1)+' of '+str(Ninversion)+' at regularization levels: ls ='+repr(lambda_spatial)+' , lt = '+repr(lambda_temporal)
            if static==True: #Only statics inversion no Lt matrix
                Kinv=K+(lambda_spatial**2)*LsLs
                Lt=eye(len(K))
                LtLt=Lt.T.dot(Lt)
            else: #Mixed inversion
                Kinv=K+(lambda_spatial**2)*LsLs+(lambda_temporal**2)*LtLt
            if solver.lower()=='lstsq':
                sol,res,rank,s=lstsq(Kinv,x)
            elif solver.lower()=='nnls':
                x=squeeze(x.T)
                try:
                    sol,res=nnls(Kinv,x)
                except:
                    print '+++ WARNING: No solution found, writting zeros.'
                    sol=zeros(G.shape[1])
                x=expand_dims(x,axis=1)
                sol=expand_dims(sol,axis=1)
            else:
                print 'ERROR: Unrecognized solver \''+solver+'\''
            #Compute synthetics
            ds=dot(G,sol)
            #Get stats
            L2,Lmodel=inv.get_stats(Kinv,sol,x)
            VR=inv.get_VR(home,project_name,GF_list,sol,d,ds,decimate)
            #VR=inv.get_VR(WG,sol,wd)
            #ABIC=inv.get_ABIC(WG,K,sol,wd,lambda_spatial,lambda_temporal,Ls,LsLs,Lt,LtLt)
            ABIC=inv.get_ABIC(G,K,sol,d,lambda_spatial,lambda_temporal,Ls,LsLs,Lt,LtLt)
            #Get moment
            Mo,Mw=inv.get_moment(home,project_name,fault_name,model_name,sol)
            #If a rotational offset was applied then reverse it for output to file
            if beta !=0:
                sol=inv.rot2ds(sol,beta)
            #Write log
            inv.write_log(home,project_name,run_name,kout,rupture_speed,num_windows,lambda_spatial,lambda_temporal,beta,
                L2,Lmodel,VR,ABIC,Mo,Mw,model_name,fault_name,G_name,GF_list,solver)
            #Write output to file
            inv.write_synthetics(home,project_name,run_name,GF_list,G,sol,ds,kout,decimate)
            inv.write_model(home,project_name,run_name,fault_name,model_name,rupture_speed,num_windows,epicenter,sol,kout)
            kout+=1
            dt1=datetime.now()-t1
            dt2=datetime.now()-ttotal
            print '... inversion wall time was '+str(dt1)+', total wall time elapsed is '+str(dt2)