예제 #1
0
def RTdeconv(data, om0, h, G, dt,corners=4, baselinelen=60., taperlen= 10.\
             , fmin = 0.001, fmax = 0.005 ):
    '''
    Using the coef. of one station computes the displacement trace.

    :param data: Raw data to be deconvoluted.
    :param om0: Natural frequency.
    :param h: damping constant.
    :param s: sensitivity of the seismometer.
    :param dt: sampling rate (sec).
    :param corners : order of the Butterworth filter to be applied.
    :param baselinelen: time (in Sec) to consider to define the baseline.
    :param taperlen:  Percentage of the trace to be tapered at the beginning.
    :param (fmin, fmax): Corner frequencies of the Butterworth filter to be applied.

    :return: Time serie with the deconvoluted displacement trace.
    '''

    baseline = np.mean(data[:int(baselinelen / dt)])
    nwin = int(10. * len(data) / 100.)

    if len(data) < 2 * nwin: nwin = len(data)

    data -= baseline
    taper = 0.5 * (1. - np.cos(np.pi * np.linspace(0., 1., nwin)))
    data[:nwin] *= taper
    #data[-nwin:]*= taper[::-1]

    datap1 = data[1:]
    datap2 = data[2:]

    ## Acceleration + double integration
    c0 = 1. / G / dt
    c1 = -2. * (1. + h * om0 * dt) / G / dt
    c2 = (1. + 2. * h * om0 * dt + dt * dt * om0 * om0) / G / dt

    accel = np.zeros(len(data))
    aux = c2 * datap2 + c1 * datap1[:-1] + c0 * data[:-2]
    accel[2:] = np.cumsum(aux)

    accel = bp.bandpassfilter(accel, len(accel), dt, corners, 1, fmin, fmax)

    vel = np.zeros(len(data))
    vel[1:] = 0.5 * dt * np.cumsum(accel[:-1] + accel[1:])

    dis = np.zeros(len(data))
    dis[1:] = 0.5 * dt * np.cumsum(vel[:-1] + vel[1:])
    return dis
예제 #2
0
def RTdeconv(data, om0, h, G, dt,corners=4, baselinelen=60., taperlen= 10.\
             , fmin = 0.001, fmax = 0.005 ):
 '''  Using the coef. of one station computes 
     the displacement trace.  
     Input: 
     data: Raw data to be deconvoluted
     om0, h, G: Natural frequency, damping constant, sensitivity of the seismometer 
     dt : sampling rate (sec)
     corners : order of the Butterworth filter to be applied.      
     baselinelen : time (in Sec ) to consider to define the baseline.
     taperlen:  Percentage of the trace to be tapered at the beginning.
     fmin, fmax: Corner frequencies of the Butterworth filter to be applied.
     Output
     Time serie with the deconvoluted displacement trace     
               '''

 baseline = np.mean(data[:int(baselinelen/dt)])
 nwin = int(10.*len(data)/100.)
 
 if len(data) < 2*nwin: nwin = len(data)
 
 data -= baseline 
 taper = 0.5*(1.-np.cos(np.pi*np.linspace(0.,1.,nwin)))
 data[:nwin]*= taper  
 #data[-nwin:]*= taper[::-1]
  
 datap1 = data[1:] 
 datap2 = data[2:] 
 
 ## Acceleration + double integration
 c0 = 1./G/dt
 c1 = -2.*(1. + h*om0*dt)/G/dt
 c2 = (1. + 2.*h*om0*dt + dt*dt*om0*om0)/G/dt 
 
 accel = np.zeros(len(data))
 aux = c2*datap2 + c1*datap1[:-1] + c0*data[:-2] 
 accel[2:] = np.cumsum(aux) 
 
 accel = bp.bandpassfilter(accel,len(accel),dt ,corners , 1 , fmin,fmax)  

 vel = np.zeros(len(data)) 
 vel[1:] = 0.5*dt*np.cumsum(accel[:-1]+accel[1:])
 
 dis = np.zeros(len(data))
 dis[1:] = 0.5*dt*np.cumsum(vel[:-1]+vel[1:])
 return dis 
예제 #3
0
def main(argv=sys.argv): 
    
    #Earth's parameters 
    #~ beta = 4.e3 #m/s 
    #~ rho = 3.e3 #kg/m^3 
    #~ mu = rho*beta*beta
    
    PLotSt = ["IU.TRQA.00.LHZ",
             "IU.LVC.00.LHZ",
             "II.NNA.00.LHZ",
              "IU.RAR.00.LHZ"]
             
             
    #PlotSubf = [143, 133, 123, 113, 103, 93,
     #           83, 73, 63, 53]
    PlotSubf = [6,3]

    
    
    #Set rup_vel = 0 to have a point source solution
    RupVel = 2.1 #Chilean eq from Lay et al
    t_h     = 10. # Half duration for each sf  
    noiselevel = 0.0# L1 norm level of noise
    mu =40e9
    #W-Phase filter 
    corners = 4.
    fmin = 0.001
    fmax = 0.005
    
    ### Data from Chilean 2010 EQ (Same as W phase inv.) 
    strike = 18.
    dip    = 18.
    rake   = 104. # 109.
    
    rakeA = rake + 45.
    rakeB = rake - 45.
    
    
    ### Fault's grid parameters
    nsx   = 21 #Number of sf along strike
    nsy   = 11 #Number of sf along dip
    flen  = 600. #Fault's longitude [km] along strike
    fwid  = 300. #Fault's longitude [km] along dip
    direc = 0    #Directivity 0 = bilateral
    Min_h = 10.  #Min depth of the fault
    
    
    ### Derivated parameters:
    nsf = nsx*nsy
    sflen = flen/float(nsx)         
    sfwid = fwid/float(nsy)
    swp = [1, 0, 2] # useful to swap (lat,lon, depth)  
    mindist = flen*fwid # minimun dist to the hypcen (initializing)
    
    ###Chessboard
    #weight = np.load("RealSol.npy") 
    weight = np.zeros(nsf)
    weight[::2] = 1 
    #weight[::2] = 1 
    #~ weight[10]=15
    #~ weight[5001]=10
    #~ weight[3201]=2
    
    
    
    ## Setting dirs and reading files.
    GFdir = "/home/roberto/data/GFS/"
    workdir = os.path.abspath(".")+"/"
    datadir = workdir + "DATA/"
    tracesfilename = workdir + "goodtraces.dat"
    tracesdir = workdir + "WPtraces/"
    
    try:
        reqfilename    = glob.glob(workdir + '*.syn.req')[0]
    except IndexError:   
        print "There is not *.syn.req file in the dir"
        sys.exit()
    
    basename = reqfilename.split("/")[-1][:-4]
    
    if not os.path.exists(tracesfilename): 
        print tracesfilename, "does not exist."
        exit()
    
    if not os.path.exists(datadir):
            os.makedirs(datadir)
    
    if not os.path.exists(tracesdir):
            os.makedirs(tracesdir)
 
    tracesfile = open(tracesfilename)    
    reqfile =  open(reqfilename)    
    
    trlist = readtraces(tracesfile)
    eqdata = readreq(reqfile)    

    tracesfile.close()
    reqfile.close()   
    
    ####Hypocentre from
    ### http://earthquake.usgs.gov/earthquakes/eqinthenews/2010/us2010tfan/    
    cmteplat = -35.91#-35.85#-36.03#-35.83
    cmteplon = -72.73#-72.72#-72.83# -72.67
    cmtepdepth= 35.
    eq_hyp = (cmteplat,cmteplon,cmtepdepth)
    
    
      ############
    

    # Defining the sf system
    grid, sblt = fault_grid('CL-2010',cmteplat,cmteplon,
                            cmtepdepth, direc,
                            Min_h, strike, dip, rake, flen,fwid ,nsx,nsy,
                            Verbose=False,ffi_io=True,gmt_io=True)
    
    print ('CL-2010',cmteplat,cmteplon,
                            cmtepdepth, direc,
                            Min_h, strike, dip, rake, flen,fwid ,nsx,nsy)
    print grid[0][1]
    #sys.exit()
    #This calculation is inside of the loop
    #~ NP = [strike, dip, rake]
    #~ M = np.array(NodalPlanetoMT(NP))  
    #~ Mp = np.sum(M**2)/np.sqrt(2)    
     
    #############################################################################
    ######Determining the sf closest to the hypocentre:    
    min_Dist_hyp_subf = flen *fwid
    for subf in range(nsf):
        sblat   = grid[subf][1]
        sblon   = grid[subf][0]
        sbdepth = grid[subf][2]              
        sf_hyp =  (sblat,sblon, sbdepth)        
        Dist_hyp_subf = hypo2dist(eq_hyp,sf_hyp)
        if Dist_hyp_subf < min_Dist_hyp_subf:
            min_Dist_hyp_subf = Dist_hyp_subf
            min_sb_hyp = sf_hyp
            hyp_subf = subf
    ####Determining trimming times:    
    test_tr = read(GFdir + "H003.5/PP/GF.0001.SY.LHZ.SAC")[0]
    t0 = test_tr.stats.starttime
    TrimmingTimes = {}   # Min. Distace from the fault to each station. 
    A =0
    for trid in trlist:     
        metafile = workdir + "DATA/" + "META." + trid + ".xml"
        META = DU.getMetadataFromXML(metafile)[trid]
        stlat = META['latitude']
        stlon = META['longitude'] 
        dist =   locations2degrees(min_sb_hyp[0],min_sb_hyp[1],\
                                   stlat,stlon) 
        parrivaltime = getTravelTimes(dist,min_sb_hyp[2])[0]['time']        
        ta = t0 + parrivaltime
        tb = ta + round(15.*dist) 
        TrimmingTimes[trid] = (ta, tb)
        
    
    ###########################

      
    
    DIST = []
    # Ordering the stations in terms of distance
    for trid in trlist: 
        metafile = workdir + "DATA/" + "META." + trid + ".xml"
        META = DU.getMetadataFromXML(metafile)[trid]
        lat = META['latitude']
        lon = META['longitude']
        trdist = locations2degrees(cmteplat,
                                   cmteplon,lat,lon) 
        DIST.append(trdist)   

    DistIndex = lstargsort(DIST)
    trlist = [trlist[i] for i in DistIndex]
  
    stdistribution = StDistandAzi(trlist, eq_hyp , workdir + "DATA/")
    StDistributionPlot(stdistribution)
    #exit()
    #Main loop
   

 

        
    for subf in range(nsf):
        print subf
        sflat   = grid[subf][1]
        sflon   = grid[subf][0]           
        sfdepth = grid[subf][2]
        #~ strike = grid[subf][3] #+ 360.
        #~ dip    = grid[subf][4]
        #~ rake   = grid[subf][5] #     
        NP = [strike, dip, rake]  
        NPA = [strike, dip, rakeA]
        NPB = [strike, dip, rakeB]        


        
        M = np.array(NodalPlanetoMT(NP))   
        MA = np.array(NodalPlanetoMT(NPA)) 
        MB = np.array(NodalPlanetoMT(NPB)) 
        #Time delay is calculated as the time in which 
        #the rupture reach the subfault
            
        sf_hyp = (sflat, sflon, sfdepth) 
        Dist_ep_subf = hypo2dist(eq_hyp,sf_hyp)
        
        if Dist_ep_subf < mindist:
            mindist = Dist_ep_subf
            minsubf = subf
        
                
        if RupVel == 0:
            t_d = eqdata['time_shift']
        else:
            t_d = round(Dist_ep_subf/RupVel) #-59.
       
        print sflat, sflon, sfdepth
        # Looking for the best depth dir:
        depth = []
        depthdir = []
        for file in os.listdir(GFdir):
            if file[-2:] == ".5":
                depthdir.append(file)
                depth.append(float(file[1:-2]))            
        BestDirIndex = np.argsort(abs(sfdepth\
                                  - np.array(depth)))[0]      
        hdir = GFdir + depthdir[BestDirIndex] + "/"     
        
        ###

        SYN = np.array([])
        SYNA = np.array([])
        SYNB = np.array([])
        for trid in trlist:     
            
            metafile = workdir + "DATA/" + "META." + trid + ".xml"
            META = DU.getMetadataFromXML(metafile)[trid]
            lat = META['latitude']
            lon = META['longitude']  
            
            #Subfault loop               
            #GFs Selection:
            ##Change to folloing loop
            
            dist = locations2degrees(sflat,sflon,lat,lon)                                
            azi =  -np.pi/180.*gps2DistAzimuth(lat,lon,
                       sflat,sflon)[2] 
            trPPsy,  trRRsy, trRTsy,  trTTsy = \
                                       GFSelectZ(hdir,dist)          
            
            
 
            
            trROT =  MTrotationZ(azi, trPPsy,  trRRsy, trRTsy,  trTTsy) 
            orig = trROT[0].stats.starttime  
            dt = trROT[0].stats.delta                       

            trianglen = 2.*int(t_h/dt)-1.
            FirstValid = int(trianglen/2.) + 1 # to delete
            window = triang(trianglen)
            window /= np.sum(window)
            #window = np.array([1.])
            
      
            
            
            parrivaltime = getTravelTimes(dist,sfdepth)[0]['time']
            
            t1 = TrimmingTimes[trid][0] - t_d
            t2 = TrimmingTimes[trid][1] - t_d
            
            
            
            for trR in trROT:
                trR.data *= 10.**-21 ## To get M in Nm                   
                trR.data -= trR.data[0]
                AUX1 = len(trR)
                trR.data = convolve(trR.data,window,mode='valid') 
                AUX2 = len(trR)
                mean = np.mean(np.hstack((trR.data[0]*np.ones(FirstValid),\
                               trR.data[:60./trR.stats.delta*1.-FirstValid+1])))
                #mean = np.mean(trR.data[:60])
                trR.data -= mean      
                trR.data = bp.bandpassfilter(trR.data,len(trR), trR.stats.delta,\
                                             corners , 1 , fmin, fmax)  
                t_l = dt*0.5*(AUX1 - AUX2)                             
                trR.trim(t1-t_l,t2-t_l, pad=True, fill_value=trR.data[0])  #We lost t_h due to the convolution        
            


                   
            #~ for trR in trROT:
                #~ trR.data *= 10.**-23 ## To get M in Nm                   
                #~ trR.data -= trR.data[0]
 
                #~ trR.data = convolve(trR.data,window,mode='same') 

                #~ #mean = np.mean(np.hstack((trR.data[0]*np.ones(FirstValid),\
                               #~ #trR.data[:60./trR.stats.delta*1.-FirstValid+1])))
                #~ mean = np.mean(trR.data[:60])
                #~ trR.data -= mean      
                #~ trR.data = bp.bandpassfilter(trR.data,len(trR), trR.stats.delta,\
                                             #~ corners , 1 , fmin, fmax)  
                            
                #~ trR.trim(t1,t2,pad=True, fill_value=trR.data[0])     
           
            trROT = np.array(trROT)  
            syn  =  np.dot(trROT.T,M) 
            synA =  np.dot(trROT.T,MA)
            synB =  np.dot(trROT.T,MB)
            
            SYN = np.append(SYN,syn)  
            SYNA = np.append(SYNA,synA)
            SYNB = np.append(SYNB,synB)
            
            
        print np.shape(A), np.shape(np.array([SYN]))    
        if subf == 0: 
            A = np.array([SYN])
            AA = np.array([SYNA])
            AB = np.array([SYNB])
        else:
            A = np.append(A,np.array([SYN]),0)    
            AA = np.append(AA,np.array([SYNA]),0)
            AB = np.append(AB,np.array([SYNB]),0)
            
            
            
    AC = np.vstack((AA,AB))
    print np.shape(AC)
    print np.shape(weight)
    B = np.dot(A.T,weight)
    stsyn = Stream()
    n = 0
    Ntraces= {}
    for trid in trlist: 
        spid = trid.split(".")        
        print trid
        NMIN = 1. + (TrimmingTimes[trid][1] - TrimmingTimes[trid][0]) / dt
        Ntraces[trid] = (n,NMIN + n)
        trsyn = Trace(B[n:NMIN+n])   
        n += NMIN        
        trsyn.stats.network = spid[0]
        trsyn.stats.station = spid[1]
        trsyn.stats.location = spid[2]
        trsyn.stats.channel = spid[3] 
        trsyn = AddNoise(trsyn,level = noiselevel)
        #trsyn.stats.starttime = 
        stsyn.append(trsyn)
        
       
    stsyn.write(workdir+"WPtraces/" + basename + ".decov.trim.mseed",
                 format="MSEED")           
                
    #####################################################    
    # Plotting:
    #####################################################
    #we are going to reflect the y axis later, so:
    print minsubf
    hypsbloc = [minsubf / nsy , -(minsubf % nsy) - 2]

    #Creating the strike and dip axis:
    StrikeAx= np.linspace(0,flen,nsx+1)
    DipAx= np.linspace(0,fwid,nsy+1)
    DepthAx = DipAx*np.sin(np.pi/180.*dip) + Min_h    
    hlstrike = StrikeAx[hypsbloc[0]] + sflen*0.5
        
    hldip = DipAx[hypsbloc[1]] + sfwid*0.5 
    hldepth = DepthAx[hypsbloc[1]] + sfwid*0.5*np.sin(np.pi/180.*dip)
       
    StrikeAx = StrikeAx - hlstrike
    DipAx =     DipAx   - hldip
 

    
    XX, YY = np.meshgrid(StrikeAx, DepthAx)
    XX, ZZ = np.meshgrid(StrikeAx, DipAx )

   
    sbarea = sflen*sfwid
    
    SLIPS = weight.reshape(nsx,nsy).T#[::-1,:]
    SLIPS /= mu*1.e6*sbarea
    
    ######Plot:#####################
    plt.figure()
    ax = host_subplot(111)
    im = ax.pcolor(XX, YY, SLIPS, cmap="jet")    
    ax.set_ylabel('Depth [km]')       
    ax.set_ylim(DepthAx[-1],DepthAx[0])  
    
    # Creating a twin plot 
    ax2 = ax.twinx()
    #im2 = ax2.pcolor(XX, ZZ, SLIPS[::-1,:], cmap="Greys") 
    im2 = ax2.pcolor(XX, ZZ, SLIPS[::-1,:], cmap="jet")    
    
    ax2.set_ylabel('Distance along the dip [km]')
    ax2.set_xlabel('Distance along the strike [km]')    
    ax2.set_ylim(DipAx[0],DipAx[-1])
    ax2.set_xlim(StrikeAx[0],StrikeAx[-1])       
                         
                         
    ax.axis["bottom"].major_ticklabels.set_visible(False) 
    ax2.axis["bottom"].major_ticklabels.set_visible(False)
    ax2.axis["top"].set_visible(True)
    ax2.axis["top"].label.set_visible(True)
    
    
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("bottom", size="5%", pad=0.1)
    cb = plt.colorbar(im, cax=cax, orientation="horizontal")
    cb.set_label("Slip [m]") 
    ax2.plot([0], [0], '*', ms=225./(nsy+4))
    ax2.set_xticks(ax2.get_xticks()[1:-1])
    #ax.set_yticks(ax.get_yticks()[1:])
    #ax2.set_yticks(ax2.get_yticks()[:-1])
    

    
    #########Plotting the selected traces:
    nsp = len(PLotSt) * len(PlotSubf)
    plt.figure(figsize=(13,11))
    plt.title("Synthetics for rake = " + str(round(rake)))
    mindis = []
    maxdis = []
    for i, trid in enumerate(PLotSt):   
        x = np.arange(0,Ntraces[trid][1]-Ntraces[trid][0],
                      dt)
        for j, subf in enumerate(PlotSubf):
            y = A[subf, Ntraces[trid][0]:Ntraces[trid][1]]
            if j == 0:
                yy = y
            else:
                yy = np.vstack((yy,y))        
        maxdis.append(np.max(yy))
        mindis.append(np.min(yy))
        
    

    for i, trid in enumerate(PLotSt):   
        x = np.arange(0,Ntraces[trid][1]-Ntraces[trid][0],
                      dt)

        for j, subf in enumerate(PlotSubf):
            y = A[subf, Ntraces[trid][0]:Ntraces[trid][1]]
            plt.subplot2grid((len(PlotSubf), len(PLotSt)),
                              (j, i))                                
            plt.plot(x,y, linewidth=2.5)
            if j == 0:
                plt.title(trid)
            fig = plt.gca()            
            fig.axes.get_yaxis().set_ticks([])
            fig.set_ylabel(str(subf),rotation=0)
            fig.set_xlim((x[0],x[-1]))
            fig.set_ylim((mindis[i],maxdis[i]))
            if subf != PlotSubf[-1]:
                fig.axes.get_xaxis().set_ticks([])

    
    plt.show()
예제 #4
0
def main(argv=sys.argv):

    #Earth's parameters
    #~ beta = 4.e3 #m/s
    #~ rho = 3.e3 #kg/m^3
    #~ mu = rho*beta*beta

    PLotSt = [
        "IU.TRQA.00.LHZ", "IU.LVC.00.LHZ", "II.NNA.00.LHZ", "IU.RAR.00.LHZ"
    ]

    #PlotSubf = [143, 133, 123, 113, 103, 93,
    #           83, 73, 63, 53]
    PlotSubf = [6, 3]

    #Set rup_vel = 0 to have a point source solution
    RupVel = 2.1  #Chilean eq from Lay et al
    t_h = 10.  # Half duration for each sf
    noiselevel = 0.0  # L1 norm level of noise
    mu = 40e9
    #W-Phase filter
    corners = 4.
    fmin = 0.001
    fmax = 0.005

    ### Data from Chilean 2010 EQ (Same as W phase inv.)
    strike = 18.
    dip = 18.
    rake = 104.  # 109.

    rakeA = rake + 45.
    rakeB = rake - 45.

    ### Fault's grid parameters
    nsx = 21  #Number of sf along strike
    nsy = 11  #Number of sf along dip
    flen = 600.  #Fault's longitude [km] along strike
    fwid = 300.  #Fault's longitude [km] along dip
    direc = 0  #Directivity 0 = bilateral
    Min_h = 10.  #Min depth of the fault

    ### Derivated parameters:
    nsf = nsx * nsy
    sflen = flen / float(nsx)
    sfwid = fwid / float(nsy)
    swp = [1, 0, 2]  # useful to swap (lat,lon, depth)
    mindist = flen * fwid  # minimun dist to the hypcen (initializing)

    ###Chessboard
    #weight = np.load("RealSol.npy")
    weight = np.zeros(nsf)
    weight[::2] = 1
    #weight[::2] = 1
    #~ weight[10]=15
    #~ weight[5001]=10
    #~ weight[3201]=2

    ## Setting dirs and reading files.
    GFdir = "/home/roberto/data/GFS/"
    workdir = os.path.abspath(".") + "/"
    datadir = workdir + "DATA/"
    tracesfilename = workdir + "goodtraces.dat"
    tracesdir = workdir + "WPtraces/"

    try:
        reqfilename = glob.glob(workdir + '*.syn.req')[0]
    except IndexError:
        print "There is not *.syn.req file in the dir"
        sys.exit()

    basename = reqfilename.split("/")[-1][:-4]

    if not os.path.exists(tracesfilename):
        print tracesfilename, "does not exist."
        exit()

    if not os.path.exists(datadir):
        os.makedirs(datadir)

    if not os.path.exists(tracesdir):
        os.makedirs(tracesdir)

    tracesfile = open(tracesfilename)
    reqfile = open(reqfilename)

    trlist = readtraces(tracesfile)
    eqdata = readreq(reqfile)

    tracesfile.close()
    reqfile.close()

    ####Hypocentre from
    ### http://earthquake.usgs.gov/earthquakes/eqinthenews/2010/us2010tfan/
    cmteplat = -35.91  #-35.85#-36.03#-35.83
    cmteplon = -72.73  #-72.72#-72.83# -72.67
    cmtepdepth = 35.
    eq_hyp = (cmteplat, cmteplon, cmtepdepth)

    ############

    # Defining the sf system
    grid, sblt = fault_grid('CL-2010',
                            cmteplat,
                            cmteplon,
                            cmtepdepth,
                            direc,
                            Min_h,
                            strike,
                            dip,
                            rake,
                            flen,
                            fwid,
                            nsx,
                            nsy,
                            Verbose=False,
                            ffi_io=True,
                            gmt_io=True)

    print('CL-2010', cmteplat, cmteplon, cmtepdepth, direc, Min_h, strike, dip,
          rake, flen, fwid, nsx, nsy)
    print grid[0][1]
    #sys.exit()
    #This calculation is inside of the loop
    #~ NP = [strike, dip, rake]
    #~ M = np.array(NodalPlanetoMT(NP))
    #~ Mp = np.sum(M**2)/np.sqrt(2)

    #############################################################################
    ######Determining the sf closest to the hypocentre:
    min_Dist_hyp_subf = flen * fwid
    for subf in range(nsf):
        sblat = grid[subf][1]
        sblon = grid[subf][0]
        sbdepth = grid[subf][2]
        sf_hyp = (sblat, sblon, sbdepth)
        Dist_hyp_subf = hypo2dist(eq_hyp, sf_hyp)
        if Dist_hyp_subf < min_Dist_hyp_subf:
            min_Dist_hyp_subf = Dist_hyp_subf
            min_sb_hyp = sf_hyp
            hyp_subf = subf
    ####Determining trimming times:
    test_tr = read(GFdir + "H003.5/PP/GF.0001.SY.LHZ.SAC")[0]
    t0 = test_tr.stats.starttime
    TrimmingTimes = {}  # Min. Distace from the fault to each station.
    A = 0
    for trid in trlist:
        metafile = workdir + "DATA/" + "META." + trid + ".xml"
        META = DU.getMetadataFromXML(metafile)[trid]
        stlat = META['latitude']
        stlon = META['longitude']
        dist =   locations2degrees(min_sb_hyp[0],min_sb_hyp[1],\
                                   stlat,stlon)
        parrivaltime = getTravelTimes(dist, min_sb_hyp[2])[0]['time']
        ta = t0 + parrivaltime
        tb = ta + round(15. * dist)
        TrimmingTimes[trid] = (ta, tb)

    ###########################

    DIST = []
    # Ordering the stations in terms of distance
    for trid in trlist:
        metafile = workdir + "DATA/" + "META." + trid + ".xml"
        META = DU.getMetadataFromXML(metafile)[trid]
        lat = META['latitude']
        lon = META['longitude']
        trdist = locations2degrees(cmteplat, cmteplon, lat, lon)
        DIST.append(trdist)

    DistIndex = lstargsort(DIST)
    trlist = [trlist[i] for i in DistIndex]

    stdistribution = StDistandAzi(trlist, eq_hyp, workdir + "DATA/")
    StDistributionPlot(stdistribution)
    #exit()
    #Main loop

    for subf in range(nsf):
        print subf
        sflat = grid[subf][1]
        sflon = grid[subf][0]
        sfdepth = grid[subf][2]
        #~ strike = grid[subf][3] #+ 360.
        #~ dip    = grid[subf][4]
        #~ rake   = grid[subf][5] #
        NP = [strike, dip, rake]
        NPA = [strike, dip, rakeA]
        NPB = [strike, dip, rakeB]

        M = np.array(NodalPlanetoMT(NP))
        MA = np.array(NodalPlanetoMT(NPA))
        MB = np.array(NodalPlanetoMT(NPB))
        #Time delay is calculated as the time in which
        #the rupture reach the subfault

        sf_hyp = (sflat, sflon, sfdepth)
        Dist_ep_subf = hypo2dist(eq_hyp, sf_hyp)

        if Dist_ep_subf < mindist:
            mindist = Dist_ep_subf
            minsubf = subf

        if RupVel == 0:
            t_d = eqdata['time_shift']
        else:
            t_d = round(Dist_ep_subf / RupVel)  #-59.

        print sflat, sflon, sfdepth
        # Looking for the best depth dir:
        depth = []
        depthdir = []
        for file in os.listdir(GFdir):
            if file[-2:] == ".5":
                depthdir.append(file)
                depth.append(float(file[1:-2]))
        BestDirIndex = np.argsort(abs(sfdepth\
                                  - np.array(depth)))[0]
        hdir = GFdir + depthdir[BestDirIndex] + "/"

        ###

        SYN = np.array([])
        SYNA = np.array([])
        SYNB = np.array([])
        for trid in trlist:

            metafile = workdir + "DATA/" + "META." + trid + ".xml"
            META = DU.getMetadataFromXML(metafile)[trid]
            lat = META['latitude']
            lon = META['longitude']

            #Subfault loop
            #GFs Selection:
            ##Change to folloing loop

            dist = locations2degrees(sflat, sflon, lat, lon)
            azi = -np.pi / 180. * gps2DistAzimuth(lat, lon, sflat, sflon)[2]
            trPPsy,  trRRsy, trRTsy,  trTTsy = \
                                       GFSelectZ(hdir,dist)

            trROT = MTrotationZ(azi, trPPsy, trRRsy, trRTsy, trTTsy)
            orig = trROT[0].stats.starttime
            dt = trROT[0].stats.delta

            trianglen = 2. * int(t_h / dt) - 1.
            FirstValid = int(trianglen / 2.) + 1  # to delete
            window = triang(trianglen)
            window /= np.sum(window)
            #window = np.array([1.])

            parrivaltime = getTravelTimes(dist, sfdepth)[0]['time']

            t1 = TrimmingTimes[trid][0] - t_d
            t2 = TrimmingTimes[trid][1] - t_d

            for trR in trROT:
                trR.data *= 10.**-21  ## To get M in Nm
                trR.data -= trR.data[0]
                AUX1 = len(trR)
                trR.data = convolve(trR.data, window, mode='valid')
                AUX2 = len(trR)
                mean = np.mean(np.hstack((trR.data[0]*np.ones(FirstValid),\
                               trR.data[:60./trR.stats.delta*1.-FirstValid+1])))
                #mean = np.mean(trR.data[:60])
                trR.data -= mean
                trR.data = bp.bandpassfilter(trR.data,len(trR), trR.stats.delta,\
                                             corners , 1 , fmin, fmax)
                t_l = dt * 0.5 * (AUX1 - AUX2)
                trR.trim(t1 - t_l, t2 - t_l, pad=True, fill_value=trR.data[0]
                         )  #We lost t_h due to the convolution

            #~ for trR in trROT:
            #~ trR.data *= 10.**-23 ## To get M in Nm
            #~ trR.data -= trR.data[0]

            #~ trR.data = convolve(trR.data,window,mode='same')

            #~ #mean = np.mean(np.hstack((trR.data[0]*np.ones(FirstValid),\
            #~ #trR.data[:60./trR.stats.delta*1.-FirstValid+1])))
            #~ mean = np.mean(trR.data[:60])
            #~ trR.data -= mean
            #~ trR.data = bp.bandpassfilter(trR.data,len(trR), trR.stats.delta,\
            #~ corners , 1 , fmin, fmax)

            #~ trR.trim(t1,t2,pad=True, fill_value=trR.data[0])

            trROT = np.array(trROT)
            syn = np.dot(trROT.T, M)
            synA = np.dot(trROT.T, MA)
            synB = np.dot(trROT.T, MB)

            SYN = np.append(SYN, syn)
            SYNA = np.append(SYNA, synA)
            SYNB = np.append(SYNB, synB)

        print np.shape(A), np.shape(np.array([SYN]))
        if subf == 0:
            A = np.array([SYN])
            AA = np.array([SYNA])
            AB = np.array([SYNB])
        else:
            A = np.append(A, np.array([SYN]), 0)
            AA = np.append(AA, np.array([SYNA]), 0)
            AB = np.append(AB, np.array([SYNB]), 0)

    AC = np.vstack((AA, AB))
    print np.shape(AC)
    print np.shape(weight)
    B = np.dot(A.T, weight)
    stsyn = Stream()
    n = 0
    Ntraces = {}
    for trid in trlist:
        spid = trid.split(".")
        print trid
        NMIN = 1. + (TrimmingTimes[trid][1] - TrimmingTimes[trid][0]) / dt
        Ntraces[trid] = (n, NMIN + n)
        trsyn = Trace(B[n:NMIN + n])
        n += NMIN
        trsyn.stats.network = spid[0]
        trsyn.stats.station = spid[1]
        trsyn.stats.location = spid[2]
        trsyn.stats.channel = spid[3]
        trsyn = AddNoise(trsyn, level=noiselevel)
        #trsyn.stats.starttime =
        stsyn.append(trsyn)

    stsyn.write(workdir + "WPtraces/" + basename + ".decov.trim.mseed",
                format="MSEED")

    #####################################################
    # Plotting:
    #####################################################
    #we are going to reflect the y axis later, so:
    print minsubf
    hypsbloc = [minsubf / nsy, -(minsubf % nsy) - 2]

    #Creating the strike and dip axis:
    StrikeAx = np.linspace(0, flen, nsx + 1)
    DipAx = np.linspace(0, fwid, nsy + 1)
    DepthAx = DipAx * np.sin(np.pi / 180. * dip) + Min_h
    hlstrike = StrikeAx[hypsbloc[0]] + sflen * 0.5

    hldip = DipAx[hypsbloc[1]] + sfwid * 0.5
    hldepth = DepthAx[hypsbloc[1]] + sfwid * 0.5 * np.sin(np.pi / 180. * dip)

    StrikeAx = StrikeAx - hlstrike
    DipAx = DipAx - hldip

    XX, YY = np.meshgrid(StrikeAx, DepthAx)
    XX, ZZ = np.meshgrid(StrikeAx, DipAx)

    sbarea = sflen * sfwid

    SLIPS = weight.reshape(nsx, nsy).T  #[::-1,:]
    SLIPS /= mu * 1.e6 * sbarea

    ######Plot:#####################
    plt.figure()
    ax = host_subplot(111)
    im = ax.pcolor(XX, YY, SLIPS, cmap="jet")
    ax.set_ylabel('Depth [km]')
    ax.set_ylim(DepthAx[-1], DepthAx[0])

    # Creating a twin plot
    ax2 = ax.twinx()
    #im2 = ax2.pcolor(XX, ZZ, SLIPS[::-1,:], cmap="Greys")
    im2 = ax2.pcolor(XX, ZZ, SLIPS[::-1, :], cmap="jet")

    ax2.set_ylabel('Distance along the dip [km]')
    ax2.set_xlabel('Distance along the strike [km]')
    ax2.set_ylim(DipAx[0], DipAx[-1])
    ax2.set_xlim(StrikeAx[0], StrikeAx[-1])

    ax.axis["bottom"].major_ticklabels.set_visible(False)
    ax2.axis["bottom"].major_ticklabels.set_visible(False)
    ax2.axis["top"].set_visible(True)
    ax2.axis["top"].label.set_visible(True)

    divider = make_axes_locatable(ax)
    cax = divider.append_axes("bottom", size="5%", pad=0.1)
    cb = plt.colorbar(im, cax=cax, orientation="horizontal")
    cb.set_label("Slip [m]")
    ax2.plot([0], [0], '*', ms=225. / (nsy + 4))
    ax2.set_xticks(ax2.get_xticks()[1:-1])
    #ax.set_yticks(ax.get_yticks()[1:])
    #ax2.set_yticks(ax2.get_yticks()[:-1])

    #########Plotting the selected traces:
    nsp = len(PLotSt) * len(PlotSubf)
    plt.figure(figsize=(13, 11))
    plt.title("Synthetics for rake = " + str(round(rake)))
    mindis = []
    maxdis = []
    for i, trid in enumerate(PLotSt):
        x = np.arange(0, Ntraces[trid][1] - Ntraces[trid][0], dt)
        for j, subf in enumerate(PlotSubf):
            y = A[subf, Ntraces[trid][0]:Ntraces[trid][1]]
            if j == 0:
                yy = y
            else:
                yy = np.vstack((yy, y))
        maxdis.append(np.max(yy))
        mindis.append(np.min(yy))

    for i, trid in enumerate(PLotSt):
        x = np.arange(0, Ntraces[trid][1] - Ntraces[trid][0], dt)

        for j, subf in enumerate(PlotSubf):
            y = A[subf, Ntraces[trid][0]:Ntraces[trid][1]]
            plt.subplot2grid((len(PlotSubf), len(PLotSt)), (j, i))
            plt.plot(x, y, linewidth=2.5)
            if j == 0:
                plt.title(trid)
            fig = plt.gca()
            fig.axes.get_yaxis().set_ticks([])
            fig.set_ylabel(str(subf), rotation=0)
            fig.set_xlim((x[0], x[-1]))
            fig.set_ylim((mindis[i], maxdis[i]))
            if subf != PlotSubf[-1]:
                fig.axes.get_xaxis().set_ticks([])

    plt.show()
예제 #5
0
def main(argv=sys.argv): 

    global eplat, eplon, epdepth, orig
    
    
    GFdir = "/home/roberto/data/GFS/"
    beta = 4.e3 #m/s 
    rho = 3.e3 #kg/m^3 
    mu = rho*beta*beta
    mu =40e9
    
    Lbdm0min = 1e-26*np.array([125.])
    Lbdsmooth = 1e-26*np.array([100.])
    
    #~ Lbdm0min = 1e-26*np.linspace(60.,500,40)
    #~ Lbdsmooth = 1e-26*np.linspace(60.,500,40)#*0.5

    corners = 4.
    fmin = 0.001
    fmax = 0.005
    
    ### Data from Chilean 2010 EQ (Same as W phase inv.) 
    strike = 18.
    dip    = 18.
    rake   = 104. # 109.
    #rake = 45.
    
    rakeA = rake + 45.
    rakeB = rake - 45.
    ####################
    nsx = 21
    nsy = 11
    Min_h = 10.
    flen  = 600. #Fault's longitude [km] along strike
    fwid  = 300. #Fault's longitude [km] along dip
    sflen = flen/float(nsx)
    sfwid = fwid/float(nsy)    
    swp = [1, 0, 2]
    nsf = nsx*nsy
    ###################
    
    t_h     = 10.
    MISFIT = np.array([])
    #RUPVEL = np.arange(1.0, 5.0, 0.05)
    RupVel = 2.1 # Best fit
    #RupVel = 2.25 #From Lay et al.
    
    
    #for RupVel in RUPVEL:
    print "****************************"
    print RupVel
    print "****************************"
    NP = [strike, dip, rake]
    NPA = [strike, dip, rakeA]
    NPB = [strike, dip, rakeB]
    
    M  = np.array(NodalPlanetoMT(NP))  
    MA = np.array(NodalPlanetoMT(NPA)) 
    MB = np.array(NodalPlanetoMT(NPB)) 
    
    Mp = np.sum(M**2)/np.sqrt(2)
    
    
    #############
        #Loading req file and EQparameters
    parameters={}    
    with open(argv[1],'r') as file:
        for line in file:
            line = line.split()
            key = line[0]
            val = line[1:]
            parameters[key] = val 
    #~ cmteplat = float(parameters['eplat'][0])
    #~ cmteplon = float(parameters['eplon'][0])
    #~ cmtepdepth=float(parameters['epdepth'][0])    
    orig = UTCDateTime(parameters['origin_time'][0])
    
    ####Hypocentre from
    ### http://earthquake.usgs.gov/earthquakes/eqinthenews/2010/us2010tfan/    
    cmteplat = -35.91#-35.85#-36.03#-35.83
    cmteplon = -72.73#-72.72#-72.83# -72.67
    cmtepdepth= 35.
    eq_hyp = (cmteplat,cmteplon,cmtepdepth)
      ############

    
    
    grid, sblt = fault_grid('CL-2010',cmteplat,cmteplon,cmtepdepth,0, Min_h,\
                             strike, dip, rake, flen, fwid, nsx, nsy,                            Verbose=False, ffi_io=True, gmt_io=True)
                             
    print ('CL-2010',cmteplat,cmteplon,cmtepdepth,0, Min_h,\
                             strike, dip, rake, flen, fwid, nsx, nsy,\
                            )
    print grid[0][1]
    #sys.exit()
    #############
    #Loading files and setting dirs:
    
    inputfile =  os.path.abspath(argv[1]) 
    if not os.path.exists(inputfile): print inputfile, "does not exist."; exit() 
    workdir = "/".join(inputfile.split("/")[:-1]) 
    basename = inputfile.split("/")[-1][:-4]
    if workdir[-1] != "/": workdir += "/"
    
    try :
        os.mkdir(workdir+"WPinv")
    except OSError:
        pass#print "Directory WPtraces already exists. Skipping"
    
    trfile = open(workdir+"goodtraces.dat")
    trlist = []
    #Loading Good traces files:
    while 1:
        line = trfile.readline().rstrip('\r\n')
        if not line: break        
        trlist.append(line.split()[0])        
    trfile.close()
    #############
    
    # Reading traces:    
    st = read(workdir+"WPtraces/" + basename + ".decov.trim.mseed")  
    #############################################################################
    ######Determining the sf closest to the hypocentre:    
    min_Dist_hyp_subf = flen *fwid
    for subf in range(nsf):
        sblat   = grid[subf][1]
        sblon   = grid[subf][0]
        sbdepth = grid[subf][2]              
        sf_hyp =  (sblat,sblon, sbdepth)        
        Dist_hyp_subf = hypo2dist(eq_hyp,sf_hyp)
        if Dist_hyp_subf < min_Dist_hyp_subf:
            min_Dist_hyp_subf = Dist_hyp_subf
            min_sb_hyp = sf_hyp
            hyp_subf = subf
    print hyp_subf,  min_sb_hyp,  min_Dist_hyp_subf    
    
    
    ####Determining trimming times:
    
    test_tr = read(GFdir + "H003.5/PP/GF.0001.SY.LHZ.SAC")[0]
    t0 = test_tr.stats.starttime
    TrimmingTimes = {}   # Min. Distace from the fault to each station. 
    A =0
    for trid in trlist:     
        tr = st.select(id=trid)[0]
        metafile = workdir + "DATA/" + "META." + tr.id + ".xml"
        META = DU.getMetadataFromXML(metafile)[tr.id]
        stlat = META['latitude']
        stlon = META['longitude'] 
        dist =   locations2degrees(min_sb_hyp[0],min_sb_hyp[1],\
                                   stlat,stlon) 
        parrivaltime = getTravelTimes(dist,min_sb_hyp[2])[0]['time']        
        ta = t0 + parrivaltime
        tb = ta + round(15.*dist) 
        TrimmingTimes[trid] = (ta, tb)
        
    
    ##############################################################################
     
    #####

    DIST = []
    # Ordering the stations in terms of distance
    for trid in trlist: 
        metafile = workdir + "DATA/" + "META." + trid + ".xml"
        META = DU.getMetadataFromXML(metafile)[trid]
        lat = META['latitude']
        lon = META['longitude']
        trdist = locations2degrees(cmteplat,cmteplon,lat,lon) 
        DIST.append(trdist)   

    
    DistIndex = lstargsort(DIST)
    
    if len(argv) == 3:
        trlist = [argv[2]]
        OneStation = True
    else:
        trlist = [trlist[i] for i in DistIndex]
        OneStation = False
        
   ##### 

    client = Client()
    ObservedDisp = np.array([])   
    gridlat = []
    gridlon = []
    griddepth = []
    sbarea = []
    mindist = flen*fwid # min distance hyp-subfault 
    

    ##########Loop for each subfault
    for subf in range(nsf):
        print "**********"
        print subf
        eplat   = grid[subf][1]
        eplon   = grid[subf][0]           
        epdepth = grid[subf][2]
        
        ## Storing the subfault's location centered in the hypcenter 
        gridlat.append(eplat-cmteplat)
        gridlon.append(eplon-cmteplon)
        griddepth.append(epdepth)
        
        
        strike = grid[subf][3] #+ 360.
        dip    = grid[subf][4]
        rake   = grid[subf][5] #     
        NP = [strike, dip, rake]
        
        M = np.array(NodalPlanetoMT(NP))   

        
                #Calculating the time dalay:
            
        sf_hyp = (eplat,eplon, epdepth)        
        Dist_ep_subf = hypo2dist(eq_hyp,sf_hyp)
        t_d = round(Dist_ep_subf/RupVel) #-59.
        print eplat,eplon, epdepth
    
        #t_d  = 0.
        
    
        # Determining depth dir:
        depth = []
        depthdir = []
        for file in os.listdir(GFdir):
            if file[-2:] == ".5":
                depthdir.append(file)
                depth.append(float(file[1:-2]))            
        BestDirIndex = np.argsort(abs(epdepth-np.array(depth)))[0]      
        hdir = GFdir + depthdir[BestDirIndex] + "/"   
        # hdir is the absolute path to the closest deepth. 
        
        
        
        SYN = np.array([])
        SYNA = np.array([])
        SYNB = np.array([])

        
        #Main loop :
        for trid in trlist:  
                       
            tr = st.select(id=trid)[0]    
            metafile = workdir + "DATA/" + "META." + tr.id + ".xml"
            META = DU.getMetadataFromXML(metafile)[tr.id]
            lat = META['latitude']
            lon = META['longitude']    
            trPPsy,  trRRsy, trRTsy,  trTTsy = \
                                   GFSelectZ(lat,lon,hdir) 
            
            tr.stats.delta = trPPsy.stats.delta
            azi =   -np.pi/180.*gps2DistAzimuth(lat,lon,\
                                               eplat,eplon)[2]
            trROT = MTrotationZ(azi, trPPsy,  trRRsy, trRTsy,  trTTsy)        
                        
            
                    #Triangle 
            dt = trROT[0].stats.delta          
            trianglen = 2.*t_h/dt-1.
            window = triang(trianglen)
            window /= np.sum(window)
            #window = np.array([1.])
            
            FirstValid = int(trianglen/2.) + 1
            dist =   locations2degrees(eplat,eplon,lat,lon) 
            parrivaltime = getTravelTimes(dist,epdepth)[0]['time']
            
            t1 = TrimmingTimes[trid][0] - t_d
            t2 = TrimmingTimes[trid][1] - t_d
            
   
            #~ t1 = trROT[0].stats.starttime + parrivaltime- t_d
            #~ t2 = t1+ round(MinDist[tr.id]*15. )
                           
           
            N = len(trROT[0])
            for trR in trROT:
                trR.data *= 10.**-21 ## To get M in Nm                   
                trR.data -= trR.data[0]
                AUX1 = len(trR)
                trR.data = convolve(trR.data,window,mode='valid') 
                AUX2 = len(trR)
                mean = np.mean(np.hstack((trR.data[0]*np.ones(FirstValid),\
                               trR.data[:60./trR.stats.delta*1.-FirstValid+1])))
                #mean = np.mean(trR.data[:60])
                trR.data -= mean      
                trR.data = bp.bandpassfilter(trR.data,len(trR), trR.stats.                                              delta,
                corners , 1 , fmin, fmax)  
                t_l = dt*0.5*(AUX1 - AUX2)                             
                trR.trim(t1-t_l,t2-t_l, pad=True, fill_value=trR.data[0])  #We lost t_h due to the convolution                  
            
            
         
            #~ for trR in trROT:
                #~ trR.data *= 10.**-23 ## To get M in Nm
                #~ trR.data -= trR.data[0]                
                #~ trR.data = convolve(trR.data,window,mode='same')
                #~ # mean = np.mean(np.hstack((trR.data[0]*np.ones(FirstValid),\
                             #~ # trR.data[:60./trR.stats.delta*1.-FirstValid+1])))
                #~ mean = np.mean(trR.data[:60])               
                #~ trR.data -= mean
                #~ trR.data = bp.bandpassfilter(trR.data,len(trR), trR.stats.delta,\
                                             #~ corners ,1 , fmin, fmax)
                #~ trR.trim(t1,t2,pad=True, fill_value=trR.data[0])  

            nmin = min(len(tr.data),len(trROT[0].data))             
            tr.data = tr.data[:nmin]
            for trR in trROT:
                trR.data = trR.data[:nmin]
              
                
             #############            
            trROT = np.array(trROT)  
            syn  =  np.dot(trROT.T,M) 
            synA =  np.dot(trROT.T,MA)
            synB =  np.dot(trROT.T,MB)
            
            SYN = np.append(SYN,syn)  
            SYNA = np.append(SYNA,synA)
            SYNB = np.append(SYNB,synB)
            
            if subf == 0 : ObservedDisp =  np.append(ObservedDisp,tr.data,0) 
            
  
        sbarea.append(grid[subf][6])
   
        print np.shape(A), np.shape(np.array([SYN]))
        if subf == 0: 
            A  = np.array([SYN])
            AA = np.array([SYNA])
            AB = np.array([SYNB])
        else:
            A = np.append(A,np.array([SYN]),0) 
            AA = np.append(AA,np.array([SYNA]),0)
            AB = np.append(AB,np.array([SYNB]),0)
        
    
    
    #Full matrix with the two rake's component
    AC = np.vstack((AA,AB))

#MISFIT = np.array([])
########## Stabilizing the solution:         

#### Moment minimization:
#~ constraintD  = np.zeros(nsf)
#~ ObservedDispcons = np.append(ObservedDisp,constraintD)
#~ for lbd in Lbd:
    #~ constraintF  = lbd*np.eye(nsf,nsf)         
    #~ Acons = np.append(A,constraintF,1)   
    #~ print np.shape(Acons.T), np.shape(ObservedDispcons)
    #~ R = nnls(Acons.T,ObservedDispcons)
    #~ M = R[0]
    #~ #M = np.zeros(nsf)
    #~ #M[::2] = 1
    #~ fit = np.dot(A.T,M)
    #~ misfit = 100.*np.sum(np.abs(fit-ObservedDisp))\
             #~ /np.sum(np.abs(ObservedDisp))
    
    #~ MISFIT = np.append(MISFIT,misfit)
#~ plt.figure()
#~ plt.plot(Lbd,MISFIT)
#~ ###########################################
#~ ### Smoothing:
#~ constraintF_base = SmoothMatrix(nsx,nsy)
#~ constraintD  = np.zeros(np.shape(constraintF_base)[0])
#~ ObservedDispcons = np.append(ObservedDisp,constraintD)
#~ for lbd in Lbd:
    #~ constraintF  = lbd*constraintF_base   
    #~ Acons = np.append(A,constraintF.T,1)   
    #~ #print np.shape(Acons.T), np.shape(ObservedDispcons)
    #~ R = nnls(Acons.T,ObservedDispcons)
    #~ M = R[0]
    #~ fit = np.dot(A.T,M)
    #~ misfit = 100.*np.sum(np.abs(fit-ObservedDisp))\
             #~ /np.sum(np.abs(ObservedDisp))
    #~ print lbd, misfit
    #~ MISFIT = np.append(MISFIT,misfit)
#~ ###########################################    
###########################################
#~ ##### Moment Minimization (including rake projections):
#~ constraintD  = np.zeros(2*nsf)
#~ ObservedDispcons = np.append(ObservedDisp,constraintD)
#~ for lbd in Lbd:
    #~ constraintF  = lbd*np.eye(2*nsf,2*nsf)         
    #~ ACcons = np.append(AC,constraintF,1)   
    #~ print np.shape(ACcons.T), np.shape(ObservedDispcons)
    #~ R = nnls(ACcons.T,ObservedDispcons)
    #~ M = R[0]
    #~ fit = np.dot(AC.T,M)
    #~ misfit = 100.*np.sum(np.abs(fit-ObservedDisp))\
             #~ /np.sum(np.abs(ObservedDisp))        
    #~ MISFIT = np.append(MISFIT,misfit)  
    #~ M = np.sqrt(M[:nsf]**2+M[nsf:]**2)

##############################################
### Smoothing (including rake projections):
#~ constraintF_base = SmoothMatrix(nsx,nsy)
#~ Nbase = np.shape(constraintF_base)[0]
#~ constraintD  = np.zeros(2*Nbase)
#~ constraintF_base_big = np.zeros((2*Nbase, 2*nsf))
#~ constraintF_base_big[:Nbase,:nsf]= constraintF_base
#~ constraintF_base_big[Nbase:,nsf:]= constraintF_base 
#~ ObservedDispcons = np.append(ObservedDisp,constraintD)
#~ for lbd in Lbd:
    #~ constraintF  = lbd*constraintF_base_big   
    #~ ACcons = np.append(AC,constraintF.T,1)   
    #~ #print np.shape(Acons.T), np.shape(ObservedDispcons)
    #~ R = nnls(ACcons.T,ObservedDispcons)
    #~ M = R[0]
    #~ fit = np.dot(AC.T,M)
    #~ misfit = 100.*np.sum(np.abs(fit-ObservedDisp))\
             #~ /np.sum(np.abs(ObservedDisp))
    #~ print lbd, misfit
    #~ MISFIT = np.append(MISFIT,misfit)
#~ M = np.sqrt(M[:nsf]**2+M[nsf:]**2)    
###########################################    
#~ ##### Moment Minimization and  Smoothing
  #~ #### (including rake projections):
    #~ mom0 = []
    #~ constraintF_base = SmoothMatrix(nsx,nsy)
    #~ Nbase = np.shape(constraintF_base)[0]
    #~ constraintDsmoo = np.zeros(2*Nbase)
    #~ constraintDmin  = np.zeros(2*nsf)
    #~ constraintF_base_big = np.zeros((2*Nbase, 2*nsf))
    #~ constraintF_base_big[:Nbase,:nsf]= constraintF_base
    #~ constraintF_base_big[Nbase:,nsf:]= constraintF_base 
    #~ ObservedDispcons = np.concatenate((ObservedDisp,
                                  #~ constraintDmin,
                             #~ constraintDsmoo  ))    
   
    #~ for lbdm0 in Lbdm0min:
        #~ constraintFmin  = lbdm0*np.eye(2*nsf,2*nsf)
        #~ for lbdsm in Lbdsmooth:              
            #~ constraintFsmoo  = lbdsm*constraintF_base_big 
            #~ ACcons = np.hstack((AC, constraintFmin, constraintFsmoo.T))   
            #~ print lbdm0, lbdsm
            #~ R = nnls(ACcons.T,ObservedDispcons)
            #~ M = R[0]
            #~ fit = np.dot(AC.T,M)
            #~ misfit = 100.*np.sum(np.abs(fit-ObservedDisp))\
                     #~ /np.sum(np.abs(ObservedDisp))        
            #~ MISFIT = np.append(MISFIT,misfit) 
            #~ MA = M[:nsf]
            #~ MB = M[nsf:]
            #~ M = np.sqrt(MA**2+MB**2)
            #~ mom0.append(np.sum(M))
    ##############################################

    # Rotation to the rake's conventional angle:  
    #MB, MA = Rot2D(MB,MA,-rakeB)
    print np.shape(M), np.shape(A.T)
    R = nnls(A.T,ObservedDisp)
    M = R[0]
    
    #~ M = np.zeros(nsf)
    #~ M[::2] = 1 
    fit = np.dot(A.T,M)
    MA = M
    MB = M
    
    np.save("RealSol", M)
      
    nm0 = np.size(Lbdm0min) 
    nsmth = np.size(Lbdsmooth)
    #~ plt.figure()
    #~ plt.pcolor(1./Lbdsmooth, 1./Lbdm0min,MISFIT.reshape(nm0,nsmth))
    #~ plt.xlabel(r'$1/ \lambda_{2}$', fontsize = 24)
    #~ plt.ylabel(r'$1/ \lambda_{1}$',fontsize = 24 )
    #~ plt.ylim((1./Lbdm0min).min(),(1./Lbdm0min).max() )
    #~ plt.ylim((1./Lbdsmooth).min(),(1./Lbdsmooth).max() )
    #~ cbar = plt.colorbar()
    #~ cbar.set_label("Misfit %")
    #~ print np.shape(Lbdm0min), np.shape(mom0)
    
    #~ plt.figure()
    #~ CS = plt.contour(1./Lbdsmooth, 1./Lbdm0min,MISFIT.reshape(nm0,nsmth) )
    #~ plt.xlabel(r'$1/ \lambda_{2}$', fontsize = 24)
    #~ plt.ylabel(r'$1/ \lambda_{1}$',fontsize = 24 )
    #~ plt.clabel(CS, inline=1, fontsize=10)
    #~ plt.title('Misfit')
    
    
    
    #~ plt.figure()
    #~ plt.plot(1./Lbdm0min,MISFIT)
    #~ plt.xlabel(r'$1/ \lambda_{2}$', fontsize = 24)
    #~ plt.ylabel("Misfit %")
    #~ plt.figure()
    #~ plt.plot(Lbdm0min,mom0)
    #~ plt.ylabel(r'$M_0\, [Nm]$', fontsize = 24)
    #~ plt.xlabel(r'$\lambda_{M0}$', fontsize = 24)

   
    misfit = 100.*np.sum(np.abs(fit-ObservedDisp))/np.sum(np.abs(ObservedDisp))
    print "Residual: ", 1000.*R[1]
    print misfit
    
    
    #SLIP = M*Mp/mu/(1.e6*np.array(sbarea))
    
    sbarea = sflen*sfwid
    SLIP = M/(mu*1.e6*sbarea)
    SLIP = SLIP.reshape(nsx,nsy).T[::-1]
    moment = M.reshape(nsx,nsy).T[::-1]
    
    plt.figure(figsize = (13,5))
    plt.plot(fit,'b' ,label="Fit")
    plt.plot(ObservedDisp,'r',label="Observed")
    plt.xlabel("Time [s]")
    plt.ylabel("Displacement [m]")
    plt.legend()
    
    
    np.set_printoptions(linewidth=1000,precision=3)
    print "***********"
    print sbarea
    print SLIP
    print np.mean(SLIP)
    print "Moment:"
    print np.sum(M)
 

    ### SLIPS Distribution (as the synthetics) :
    SLIPS = M.reshape(nsx,nsy).T
    SLIPS /=  mu*1.e6*sbarea
    
    
    #~ #########Ploting slip distribution:
    #~ #we are going to reflect the y axis later, so:
    hypsbloc = [hyp_subf / nsy , -(hyp_subf % nsy) - 2]

    #Creating the strike and dip axis:
    StrikeAx= np.linspace(0,flen,nsx+1)
    DipAx= np.linspace(0,fwid,nsy+1)
    DepthAx = DipAx*np.sin(np.pi/180.*dip) + Min_h
    print DepthAx
    hlstrike = StrikeAx[hypsbloc[0]] + sflen*0.5
    #we are going to reflect the axis later, so:
    hldip = DipAx[hypsbloc[1]] + sfwid*0.5 
    hldepth = DepthAx[hypsbloc[1]] + sfwid*0.5*np.sin(np.pi/180.*dip)    
    
    StrikeAx = StrikeAx - hlstrike
    DipAx =     DipAx   - hldip
    
    XX, YY = np.meshgrid(StrikeAx, DepthAx)
    XX, ZZ = np.meshgrid(StrikeAx, DipAx )  

   ######Plot: (Old colormap: "gist_rainbow_r")
    plt.figure(figsize = (13,6))
    ax = host_subplot(111)
    im = ax.pcolor(XX, YY, SLIPS, cmap="jet")    
    ax.set_ylabel('Depth [km]')       
    ax.set_ylim(DepthAx[-1],DepthAx[0])  
    
    # Creating a twin plot 
    ax2 = ax.twinx()
    im2 = ax2.pcolor(XX, ZZ, SLIPS[::-1,:], cmap="jet")    
    ax2.set_ylabel('Distance along the dip [km]')
    ax2.set_xlabel('Distance along the strike [km]')    
    ax2.set_ylim(DipAx[0],DipAx[-1])
    ax2.set_xlim(StrikeAx[0],StrikeAx[-1])       
                         
                         
    ax.axis["bottom"].major_ticklabels.set_visible(False) 
    ax2.axis["bottom"].major_ticklabels.set_visible(False)
    ax2.axis["top"].set_visible(True)
    ax2.axis["top"].label.set_visible(True)
    
    
    divider = make_axes_locatable(ax)
    cax = divider.append_axes("bottom", size="5%", pad=0.1)
    cb = plt.colorbar(im, cax=cax, orientation="horizontal")
    cb.set_label("Slip [m]")             
    ax2.plot([0], [0], '*', ms=225./(nsy+4))
    ax2.set_xticks(ax2.get_xticks()[1:-1])

    
    #~ ### Rake plot:
    plt.figure(figsize = (13,6))
    fig = host_subplot(111)
    XXq, ZZq = np.meshgrid(StrikeAx[:-1]+sflen, DipAx[:-1]+sfwid )
    Q = plt.quiver(XXq,ZZq, MB.reshape(nsx,nsy).T[::-1,:]/(mu*1.e6*sbarea), 
                    MA.reshape(nsx,nsy).T[::-1,:]/(mu*1.e6*sbarea),
                    SLIPS[::-1,:],
                units='xy',scale = 0.5  ,  linewidths=(2,), 
                edgecolors=('k'), headaxislength=5  )
    fig.set_ylim([ZZq.min()-80,ZZq.max()+80])
    fig.set_xlim([XXq.min()-20, XXq.max()+20 ])
    fig.set_ylabel('Distance along dip [km]') 
    fig.set_xlabel('Distance along the strike [km]') 
    
    fig2 = fig.twinx()
    fig2.set_xlabel('Distance along the strike [km]') 
    
    fig.axis["bottom"].major_ticklabels.set_visible(False) 
    fig.axis["bottom"].label.set_visible(False)
    fig2.axis["top"].set_visible(True)
    fig2.axis["top"].label.set_visible(True)
    fig2.axis["right"].major_ticklabels.set_visible(False)

    divider = make_axes_locatable(fig)
    cax = divider.append_axes("bottom", size="5%", pad=0.1)
    cb = plt.colorbar(im, cax=cax, orientation="horizontal")
    cb.set_label("Slip [m]") 
    

    
    
    plt.show()

    
        #############
    #~ print np.shape(MISFIT),  np.shape(RUPVEL)
    #~ plt.figure()
    #~ plt.plot(RUPVEL,MISFIT)
    #~ plt.xlabel("Rupture Velocity [km/s]")
    #~ plt.ylabel("Misfit %")
    #~ plt.show()
     

    print np.shape(MB.reshape(nsx,nsy).T)
    print np.shape(ZZ)