Beispiel #1
0
def plot_vel():

    init_plotting_isi(8,8)
    plt.rcParams['axes.labelsize'] = 7

    max_dep = input('\n+++ Maximum depth to plot:\n')
    vel_rng = input('\n+++ Minimum, Maximum velocity range to plot:\n')
    
    velmods  = sorted(glob(os.path.join('velinp','*.mod')))
    projects = sorted(glob(os.path.join('figs','*')))
    color    = ['b','g','r','c','m','y','k',]

    for velmod, project, c in zip(velmods, projects, color):

        rep_file_ini = os.path.join(velmod)
        rep_file_fin = os.path.join(project,'report.dat')

        v_ini,d_ini  = f_ini(rep_file_ini)
        title_ini    = rep_file_ini.split(os.sep)[-1].split('.')[0]
        d_ini        = [-1*_ for _ in d_ini]
        
        v_fin,d_fin  = f_fin(rep_file_fin)
        title_fin    = rep_file_fin.split(os.sep)[1]
        d_fin        = [-1*_ for _ in d_fin]

        x_ini  = list(hstack([[m,n] for m,n in zip(v_ini,v_ini)]))
        y_ini  = list(hstack([[m,n] for m,n in zip(d_ini,d_ini)]))

        x_fin  = list(hstack([[m,n] for m,n in zip(v_fin,v_fin)]))
        y_fin  = list(hstack([[m,n] for m,n in zip(d_fin,d_fin)]))

        y_ini.pop(0)
        y_ini.append(-1*max_dep)
        
        y_fin.pop(0)
        y_fin.append(-1*max_dep)        
            
        plt.plot(x_fin, -array(y_fin), linewidth=1,label=title_fin,color=c, zorder=10,alpha=.9)
        plt.plot(x_ini, -array(y_ini), linewidth=1,label=title_ini,color=c, linestyle='--',zorder=10,alpha=.9)
        
        plt.ylim(ymin=max_dep,ymax=-0.1)
        plt.xlim(vel_rng[0],vel_rng[1])
        plt.xlabel("Velocity (km/s)")
        plt.ylabel("Depth (km)")
        plt.legend(loc=1, fontsize=6)
        plt.grid(True, linestyle='--', linewidth=.5, color='k', alpha=.3)
        [i.set_linewidth(0.6) for i in plt.gca().spines.itervalues()]

    plt.tight_layout()    
    plt.savefig(os.path.join('ModelComparsion.tiff'),dpi=300)
    plt.close()

    print '\n+++ Output file "ModelComparsion.tiff" was created.\n'
Beispiel #2
0
def vpvs(d):

    for evt in sorted(d.keys()):

        a = d[evt]
        ot_year = a['HEADER']['L1']['Year']
        ot_month = a['HEADER']['L1']['Month']
        ot_day = a['HEADER']['L1']['Day']
        ot_hour = a['HEADER']['L1']['Hour']
        ot_min = a['HEADER']['L1']['Min']
        ot_sec = a['HEADER']['L1']['Sec']
        ot_msec = int((ot_sec - int(ot_sec)) * 1e6)

        if ot_sec >= 60:

            ot_sec = ot_sec - 60
            ot_min += 1

        if ot_min >= 60:

            ot_min = ot_min - 60
            ot_hour += 1

        if ot_hour >= 24:

            ot_hour = ot_hour - 24
            ot_day += 1

        ot = dt(ot_year, ot_month, ot_day, ot_hour, ot_min, int(ot_sec),
                ot_msec)

        if not phase_dic.has_key(ot):

            phase_dic[ot] = {}

        for i in a['PHASE']:

            if not phase_dic[ot].has_key(i):

                phase_dic[ot][i] = {'P': None, 'S': None}

            otday = ot_day

            for Pphase in a['PHASE'][i]['P']:

                try:

                    ar_hour = a['PHASE'][i]['P'][Pphase]['Hour']
                    ar_min = a['PHASE'][i]['P'][Pphase]['Min']
                    ar_sec = a['PHASE'][i]['P'][Pphase]['Sec']
                    ar_msec = int((ar_sec - int(ar_sec)) * 1e6)

                except (TypeError, ValueError):

                    break

                if ar_sec >= 60:

                    ar_sec = ar_sec - 60
                    ar_min += 1

                if ar_min >= 60:

                    ar_min = ar_min - 60
                    ar_hour += 1

                if ar_hour >= 24:

                    ar_hour = ar_hour - 24
                    otday += 1

                ar = dt(ot_year, ot_month, otday, ar_hour, ar_min, int(ar_sec),
                        ar_msec)
                delta = ar - ot
                delta = delta.total_seconds()

                if delta > 0.0 and a['PHASE'][i]['P'][Pphase]['WI'] != 4:

                    phase_dic[ot][i]['P'] = delta

            otday = ot_day

            for Sphase in a['PHASE'][i]['S']:

                ar_hour = a['PHASE'][i]['S'][Sphase]['Hour']
                ar_min = a['PHASE'][i]['S'][Sphase]['Min']
                ar_sec = a['PHASE'][i]['S'][Sphase]['Sec']
                ar_msec = int((ar_sec - int(ar_sec)) * 1e6)

                if ar_sec >= 60:

                    ar_sec = ar_sec - 60
                    ar_min += 1

                if ar_min >= 60:

                    ar_min = ar_min - 60
                    ar_hour += 1

                if ar_hour >= 24:

                    ar_hour = ar_hour - 24
                    otday += 1

                ar = dt(ot_year, ot_month, otday, ar_hour, ar_min, int(ar_sec),
                        ar_msec)
                delta = ar - ot
                delta = delta.total_seconds()

                if delta > 0.0 and a['PHASE'][i]['S'][Sphase]['WI'] != 4:

                    phase_dic[ot][i]['S'] = delta

            if not phase_dic[ot][i]['S'] or not phase_dic[ot][i]['P']:

                del phase_dic[ot][i]

    # Pepare for plot

    p_time = []
    s_time = []

    for ev in sorted(phase_dic):

        if phase_dic[ev]:

            sorted_ev = sorted(phase_dic[ev].items(),
                               key=operator.itemgetter(1))

            x = sorted_ev[0][1]['P']
            y = sorted_ev[0][1]['S']

            for p in sorted_ev:

                p_time.append(p[1]['P'] - x)
                s_time.append(p[1]['S'] - y)

    googd_id, outliers_ind = outlier(p_time, s_time)
    god_p = []
    god_s = []
    out_p = []
    out_s = []

    for _ in googd_id:

        god_p.append(p_time[_])
        god_s.append(s_time[_])

    for _ in outliers_ind:

        out_p.append(p_time[_])
        out_s.append(s_time[_])

    slope, intercept, r_value, p_value, std_err = linregress(god_p, god_s)

    y = slope * array(god_p) + intercept

    init_plotting_isi(9, 6)

    ax = plt.subplot(111)

    ax.plot(god_p, god_s, marker='o', ls='', ms=3, mew=.1, mfc='r', mec='k')
    ax.plot(god_p, y, marker='', ls='--', lw=1, color='b')
    ax.scatter(out_p, out_s, marker='x', c='k', s=5, linewidth=.75, zorder=100)
    ax.set_xlim(0, max(p_time))
    ax.set_ylim(0, max(s_time))
    ax.set_xlabel('$P_{j}-P_{i}$ (s)')
    ax.set_ylabel('$S_{j}-S_{i}$ (s)')
    ax.set_title('Vp/Vs=%4.2f; N=%d; CC=%.2f; StdErr=%.2f' %
                 (slope, len(god_p), r_value, std_err),
                 fontsize=6)
    ax.grid(True, linestyle='--', linewidth=.5, color='k', alpha=.3)

    plt.tight_layout()
    plt.savefig('VpVs_%4.2f.pdf' % (slope), dpi=300)
    plt.close()
    print '\n+++ Vp/Vs=%.2f\n' % (slope)
Beispiel #3
0
def plot(best, best_rs, best_v, best_d, best_r):

    init_plotting_isi(16, 8)

    #___________________Plot final results
    #

    if eval(inp_dic['SYNTHETIC_F']):

        vel_list = [velocity_min, velocity_max, best_v, velocity_real]
        dep_list = [depth_min, depth_max, best_d, depth_real]
        vpvs_list = [vpvs_min, vpvs_max, best_r, vpvs_real]
        colors = ['k', 'k', 'b', 'r']
        labels = ['Min', 'Max', 'Best', 'Real']

    else:

        vel_list = [
            velocity_min,
            velocity_max,
            best_v,
        ]
        dep_list = [depth_min, depth_max, best_d]
        vpvs_list = [vpvs_min, vpvs_max, best_r]
        colors = ['r', 'k', 'g']
        labels = ['Min', 'Max', 'Best']

    ax = plt.subplot(121)
    [i.set_linewidth(0.6) for i in ax.spines.itervalues()]

    for v, d, c, l in zip(vel_list, dep_list, colors, labels):

        xs = []
        ys = []

        x = array(v)
        y = array(d)

        for i, j in zip(x, y):

            xs.append(i)
            xs.append(i)
            ys.append(j)
            ys.append(j)

        xs.pop(-1)
        ys.pop(0)
        xs.append(xs[-1])
        ys.append(max(inp_dic['PLT_DP_RNG']))

        if l == 'Min':
            ax.plot(array(xs),
                    -array(ys),
                    linewidth=1.5,
                    color='k',
                    linestyle='--',
                    label=l)
        elif l == 'Max':
            ax.plot(array(xs),
                    -array(ys),
                    linewidth=1.5,
                    color='k',
                    linestyle='-.',
                    label=l)
        else:
            ax.plot(array(xs),
                    -array(ys),
                    linewidth=1.5,
                    color=c,
                    linestyle='-',
                    label=l)

    ax.set_xlabel('Velocity [km/s]')
    ax.set_ylabel('Depth [km]')
    ax.set_xlim(inp_dic['PLT_VP_RNG'])
    ax.set_ylim(-array(inp_dic['PLT_DP_RNG'])[::-1])
    ax.locator_params(axis='x', nbins=6)
    ax.locator_params(axis='y', nbins=6)
    ax.grid()
    ax.legend(loc=3)

    ax = plt.subplot(122)
    [i.set_linewidth(0.6) for i in ax.spines.itervalues()]

    for r, d, c, l in zip(vpvs_list, dep_list, colors, labels):

        xs = []
        ys = []

        x = array(r)
        y = array(d)

        for i, j in zip(x, y):

            xs.append(i)
            xs.append(i)
            ys.append(j)
            ys.append(j)

        xs.pop(-1)
        ys.pop(0)
        xs.append(xs[-1])
        ys.append(max(inp_dic['PLT_DP_RNG']))

        if l == 'Min':
            ax.plot(array(xs),
                    -array(ys),
                    linewidth=1.5,
                    color='k',
                    linestyle='--',
                    label=l)
        elif l == 'Max':
            ax.plot(array(xs),
                    -array(ys),
                    linewidth=1.5,
                    color='k',
                    linestyle='-.',
                    label=l)
        else:
            ax.plot(array(xs),
                    -array(ys),
                    linewidth=1.5,
                    color=c,
                    linestyle='-',
                    label=l)

    ax.set_xlabel('VpVs [km/s]')
    ax.set_ylabel('Depth [km]')
    ax.set_xlim(inp_dic['PLT_R_RNG'])
    ax.set_ylim(-array(inp_dic['PLT_DP_RNG'])[::-1])
    ax.locator_params(axis='x', nbins=6)
    ax.locator_params(axis='y', nbins=6)
    ax.grid()
    ax.legend(loc=2)

    plt.tight_layout()
    plt.savefig(dbase_name + '.tiff', dpi=300)
    plt.close()

    #__________StdDev (V,D,R)

    models = loadtxt('models.dat')
    model_std = std(models, axis=0)[0]
    model_men = mean(models, axis=0)[0]
    best = array([best_v, best_d, best_r]).flatten()
    tot_ax = ceil(models.shape[1] / 4.)

    init_plotting_isi(16, 16)
    plt.rcParams['xtick.labelsize'] = 6
    plt.rcParams['ytick.labelsize'] = 6
    plt.rcParams['axes.labelsize'] = 6

    for par in range(models.shape[1]):

        ax = plt.subplot(tot_ax, 4, par + 1)
        [i.set_linewidth(0.6) for i in ax.spines.itervalues()]

        ax.text(0.50,
                1.18,
                'Parameter-%d' % (par + 1),
                fontsize=6,
                transform=ax.transAxes,
                ha='center',
                va='top')

        mu = mean(models[:, par])
        sig = std(models[:, par])
        x = linspace(mu - 3 * sig, mu + 3 * sig, 100)
        data = models[:, par]
        a, b, c = plt.hist(data, 50, color='r', linewidth=0, alpha=.6)
        plt.vlines(best[par], 0, max(a), color='g', zorder=20, linewidth=2)
        plt.vlines(mu, 0, max(a), color='b', zorder=20, linewidth=2)
        plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
        plt.locator_params(axis='x', nbins=6)
        plt.locator_params(axis='y', nbins=4)

    plt.tight_layout(True)
    plt.savefig('models_stat.tiff', dpi=300)
    plt.close()
Beispiel #4
0
def vpvs(d):

    for evt in sorted(d.keys()):
        
        a        = d[evt]
        ot_year  = a['HEADER']['L1']['Year']
        ot_month = a['HEADER']['L1']['Month']
        ot_day   = a['HEADER']['L1']['Day']
        ot_hour  = a['HEADER']['L1']['Hour']
        ot_min   = a['HEADER']['L1']['Min']
        ot_sec   = a['HEADER']['L1']['Sec']
        ot_msec  = int((ot_sec - int(ot_sec))*1e6)

        if ot_sec >= 60:

            ot_sec = ot_sec - 60
            ot_min+=1

        if ot_min >= 60:

            ot_min = ot_min - 60
            ot_hour+=1

        if ot_hour >= 24:

            ot_hour = ot_hour - 24
            ot_day+=1
                
        ot = dt(ot_year,ot_month,ot_day,ot_hour,ot_min,int(ot_sec),ot_msec)

        if not phase_dic.has_key(ot):

            phase_dic[ot] = {}
                    
        for i in a['PHASE']:

            if not phase_dic[ot].has_key(i):

                phase_dic[ot][i] = {'P':None, 'S':None}
                    
            otday    = ot_day

            for Pphase in a['PHASE'][i]['P']:
            
                try:

                    ar_hour = a['PHASE'][i]['P'][Pphase]['Hour']
                    ar_min  = a['PHASE'][i]['P'][Pphase]['Min']
                    ar_sec  = a['PHASE'][i]['P'][Pphase]['Sec']
                    ar_msec = int((ar_sec - int(ar_sec))*1e6)

                except (TypeError, ValueError):
                   
                    break

                if ar_sec >= 60:

                    ar_sec = ar_sec - 60
                    ar_min+=1

                if ar_min >= 60:

                    ar_min = ar_min - 60
                    ar_hour+=1

                if ar_hour >= 24:

                    ar_hour = ar_hour - 24
                    otday+=1
                        
                ar    = dt(ot_year,ot_month,otday,ar_hour,ar_min,int(ar_sec),ar_msec)  
                delta = ar - ot
                delta = delta.total_seconds()

                if delta > 0.0 and a['PHASE'][i]['P'][Pphase]['WI'] != 4:

                    phase_dic[ot][i]['P'] = delta
                    

            otday    = ot_day
            
            for Sphase in a['PHASE'][i]['S']:

                ar_hour = a['PHASE'][i]['S'][Sphase]['Hour']
                ar_min  = a['PHASE'][i]['S'][Sphase]['Min']
                ar_sec  = a['PHASE'][i]['S'][Sphase]['Sec']
                ar_msec = int((ar_sec - int(ar_sec))*1e6)
              
                if ar_sec >= 60:

                    ar_sec = ar_sec - 60
                    ar_min+=1

                if ar_min >= 60:

                    ar_min = ar_min - 60
                    ar_hour+=1

                if ar_hour >= 24:

                    ar_hour = ar_hour - 24
                    otday+=1

                ar    = dt(ot_year,ot_month,otday,ar_hour,ar_min,int(ar_sec),ar_msec)
                delta = ar - ot
                delta = delta.total_seconds()
                
                if delta > 0.0 and a['PHASE'][i]['S'][Sphase]['WI'] != 4:

                    phase_dic[ot][i]['S'] = delta

            if not phase_dic[ot][i]['S'] or not phase_dic[ot][i]['P']:

                del phase_dic[ot][i]


    # Pepare for plot
    
    p_time = []
    s_time = []

    for ev in sorted(phase_dic):

        if phase_dic[ev]:

            sorted_ev = sorted(phase_dic[ev].items(), key=operator.itemgetter(1))

            x = sorted_ev[0][1]['P']
            y = sorted_ev[0][1]['S']
                
            for p in sorted_ev:

                p_time.append(p[1]['P'] - x)
                s_time.append(p[1]['S'] - y)
    
    googd_id, outliers_ind = outlier(p_time,s_time)
    god_p        = []
    god_s        = []
    out_p        = []
    out_s        = []
    
    for _ in googd_id:

        god_p.append(p_time[_])
        god_s.append(s_time[_])

    for _ in outliers_ind:

        out_p.append(p_time[_])
        out_s.append(s_time[_])

    slope, intercept, r_value, p_value, std_err = linregress(god_p, god_s)

    y = slope*array(god_p) + intercept


    init_plotting_isi(9,6)

    ax = plt.subplot(111)

    ax.plot(god_p, god_s, marker='o', ls='', ms=3, mew=.1, mfc='r', mec='k')
    ax.plot(god_p, y, marker='', ls='--', lw=1, color='b')
    ax.scatter(out_p,out_s, marker='x', c='k', s=5, linewidth=.75, zorder=100)
    ax.set_xlim(0,max(p_time))
    ax.set_ylim(0,max(s_time))
    ax.set_xlabel('$P_{j}-P_{i}$ (s)')
    ax.set_ylabel('$S_{j}-S_{i}$ (s)')
    ax.set_title('Vp/Vs=%4.2f; N=%d; CC=%.2f; StdErr=%.2f'%(slope,len(god_p),r_value, std_err), fontsize=6)
    ax.grid(True, linestyle='--', linewidth=.5, color='k', alpha=.3)

    plt.tight_layout()
    plt.savefig('VpVs_%4.2f.tiff'%(slope),dpi=300)
    plt.close()
    print '\n+++ Vp/Vs=%.2f\n'%(slope)
Beispiel #5
0
                    sel_evt.append(x[i][0])

            except IndexError:

                pass

for i in sel_evt:

    x_fin.append(data[i]['HEADER']['L1']['Lon'])
    y_fin.append(data[i]['HEADER']['L1']['Lat'])
    z_fin.append(-data[i]['HEADER']['L1']['Dep'])

#___________________PLOT RESULTS

init_plotting_isi(18, 9)
plt.rcParams['axes.labelsize'] = 6
plt.rcParams['axes.titlesize'] = 6

#_____AX1

ax1 = plt.subplot(2, 2, 1)
[i.set_linewidth(0.6) for i in ax1.spines.itervalues()]
ax1.tick_params(labelsize=6)
ax1.locator_params(axis='x', nbins=5)
ax1.locator_params(axis='y', nbins=5)

ax1.plot(x_ini, y_ini, marker='o', color='r', ms=2, ls='', mew=.1, mec='k')

x, y = get_cells(sel_dic, 2)
Beispiel #6
0
                    sel_evt.append(x[i][0])

            except IndexError:

                pass
        
for i in sel_evt:

    x_fin.append(data[i]['HEADER']['L1']['Lon'])
    y_fin.append(data[i]['HEADER']['L1']['Lat'])
    z_fin.append(-data[i]['HEADER']['L1']['Dep'])

#___________________PLOT RESULTS

init_plotting_isi(18,9)
plt.rcParams['axes.labelsize'] = 6
plt.rcParams['axes.titlesize'] = 6

#_____AX1
    
ax1 = plt.subplot(2,2,1)
[i.set_linewidth(0.6) for i in ax1.spines.itervalues()]
ax1.tick_params(labelsize=6)
ax1.locator_params(axis='x',nbins=5)
ax1.locator_params(axis='y',nbins=5)

ax1.plot(x_ini, y_ini, marker='o', color='r', ms=2, ls='',  mew=.1, mec='k')

x,y = get_cells(sel_dic,2)
Beispiel #7
0
def stability_test():
    
    project_name = raw_input('\n+++ Enter project name (project that returned the best results):\n\n')
    model_mod    = raw_input('\n+++ Which velocity model should to be used?\n\n1- Mean Model.\n2- Minimum Model (default).\n\n')
    max_dep      = raw_input('\n+++ Maximum depth to plot velocity model (def=30.0km):\n\n')

    print '\n+++ Please set the following damping and inversion mode parameters:\n\n'

    othet   = raw_input('Origin Time (def=0.01)       = ')
    xythet  = raw_input('Epicenter (def=0.01)         = ')
    zthet   = raw_input('Depth (def=0.01)             = ')
    vthet   = raw_input('Velocity (def=1.0)           = ')
    stathet = raw_input('Station Correction (def=1.0) = ')
    invrat  = raw_input('Invert ratio (def=2)         = ')
    numitr  = raw_input('Number of iteration (def=7)  = ')

    dmp_def_val = {'othet':0.01, 'xythet':0.01, 'zthet':0.01, 'vthet':1.0, 'stathet':1.0, 'invrat':2, 'numitr':7, 'max_dep':30.0}

    for i in dmp_def_val.keys():

        dmp = eval(i)

        if dmp:

            dmp_def_val[i] = float(dmp)

    if not os.path.exists(os.path.join('Check-Test',project_name)):

        os.makedirs(os.path.join('Check-Test',project_name))

    # PREPARE REQUIRED FILES

    vel_mod_file = open(os.path.join('Check-Test',project_name,'model.mod'),'w')

    with open(os.path.join('figs',project_name,'model.mod')) as f:

        flag = False

        for l in f:

            if model_mod == '1':

                if 'Model: mean.mod' in l: flag = True
                if flag and not l.strip(): flag = False

                if flag:

                    vel_mod_file.write(l)

            else:

                if 'Model: min.mod' in l: flag = True
                if flag and not l.strip(): flag = False

                if flag:

                    vel_mod_file.write(l)            

    vel_mod_file.close()

    with open(os.path.join('figs',project_name,'report.dat')) as f:

        for l in f:

            if 'ID number' in l:

                ind = float(l.split()[-1])
                
    copy(os.path.join('velout',project_name,'velest%d.out'%(ind)), 'tmp.dat')

    velestcmn = open(os.path.join('Check-Test',project_name,'velest.cmn'), 'w')
    velestcmn.write('Velocity Stability Test')

    with open('tmp.dat') as f:
        
        h0 = '***   othet   xythet    zthet    vthet   stathet'
        h1 = '*** Modelfile:'
        h2 = '*** Stationfile:'
        h3 = '*** File with Earthquake data:'
        h4 = '*** Main print output file:'
        h5 = '*** File with final hypocenters in *.cnv format:'
        h6 = '*** File with new station corrections:'
        h7 = '*** delmin   ittmax   invertratio'

        f0         = False 
        f1, f2, f3 = False, False, False
        f4, f5, f6 = False, False, False
        f7         = False

        c = 0

        for l in f:

            if 31 <= c <= 121:

                if h0 in l:

                    f0 = True

                if f0 and h0 not in l:

                    velestcmn.write('     %.3f     %.3f     %.3f     %.3f     %.3f\n'%(dmp_def_val['othet'],
                                                                                       dmp_def_val['xythet'],
                                                                                       dmp_def_val['zthet'],
                                                                                       dmp_def_val['vthet'],
                                                                                       dmp_def_val['stathet']))
                    f0 = False
                    continue
                
                if h1 in l:

                    f1 = True

                if f1 and h1 not in l:

                    velestcmn.write('model.mod\n')
                    f1 = False
                    continue


                if h2 in l:

                    f2 = True

                if f2 and h2 not in l:

                    velestcmn.write(os.path.join('..'+os.sep+'..','figs',project_name,'sta_cor.out')+'\n')
                    f2 = False
                    continue


                if h3 in l:

                    f3 = True

                if f3 and h3 not in l:

                    velestcmn.write(os.path.join('..'+os.sep+'..','velinp','noisy.cnv')+'\n')
                    f3 = False
                    continue


                if h4 in l:

                    f4 = True

                if f4 and h4 not in l:

                    velestcmn.write('velest.out\n')
                    f4 = False
                    continue


                if h5 in l:

                    f5 = True

                if f5 and h5 not in l:

                    velestcmn.write('final_loc.cnv\n')
                    f5 = False
                    continue


                if h6 in l:

                    f6 = True

                if f6 and h6 not in l:

                    velestcmn.write('station_cor.sta\n')
                    f6 = False
                    continue


                if h7 in l:

                    f7 = True

                if f7 and h7 not in l:

                    velestcmn.write('    0.010       %d          %d\n'%(dmp_def_val['numitr'],
                                                                        dmp_def_val['invrat']))
                    f7 = False
                    continue
              
                velestcmn.write(l)

            c+=1

    velestcmn.close()

    # RUN VELEST TO PERFORM TEST

    here = os.getcwd()
    os.chdir(os.path.join('Check-Test',project_name))
    os.system('velest > /dev/null')
    os.chdir(here)

    # PLOT STABILITY RESULTS

    x_ini = []
    y_ini = []
    z_ini = []

    x_nsy = []
    y_nsy = []
    z_nsy = []

    x_fin = []
    y_fin = []
    z_fin = []

    # DEFINE RESOURCES

    d_ini = os.path.join('figs',project_name,'fin_hyp.cnv')
    d_nsy = os.path.join('velinp','noisy.cnv')
    d_fin = os.path.join('Check-Test',project_name,'final_loc.cnv')

    for x,y,z,inp in zip([x_ini,x_nsy,x_fin],[y_ini,y_nsy,y_fin],[z_ini,z_nsy,z_fin],[d_ini,d_nsy,d_fin]):
        
        with open(inp) as f:

            for l in f:

                if l[25:26] == 'N' and l[35:36] == 'E':

                    y.append(float(l[17:25]))
                    x.append(float(l[26:35]))
                    z.append(float(l[36:43]))

    vel_ini = [[],[]]

    with open(os.path.join('Check-Test',project_name,'model.mod')) as f:

        flag = False

        for l in f:

            if 'P-VELOCITY MODEL' in l: flag = True
            if flag and len(l.split()) == 1: break

            if flag:

                vel_ini[0].append(float(l.split()[0]))
                vel_ini[1].append(float(l.split()[1]))

    vi = dp(vel_ini[0])
    y  = [-1*d for d in vel_ini[1]]
    x  = list(hstack([[m,n] for m,n in zip(vel_ini[0],vel_ini[0])]))
    y  = list(hstack([[m,n] for m,n in zip(y,y)]))
    y.pop(0)
    y.append(-1*dmp_def_val['max_dep'])        

    vel_ini = [x,y]

    with open(os.path.join('Check-Test',project_name,'velest.out')) as f:

        flag = False

        for l in f:

            if ' Velocity model   1' in l:

                flag    = True
                vel_fin = [[],[]]
                continue

            if flag and not l.strip(): flag = False

            if flag and l.split()[0][0].isdigit():

                vel_fin[0].append(float(l.split()[0]))
                vel_fin[1].append(float(l.split()[2]))

    vf = dp(vel_fin[0])
    y  = [-1*d for d in vel_fin[1]]
    x  = list(hstack([[m,n] for m,n in zip(vel_fin[0],vel_fin[0])]))
    y  = list(hstack([[m,n] for m,n in zip(y,y)]))
    y.pop(0)
    y.append(-1*dmp_def_val['max_dep'])        

    vel_fin = [x,y]

    init_plotting_isi(17,9)
    plt.rcParams['axes.labelsize'] = 7
    
    ax1 = plt.subplot(221)

    x_diff =  d2k(array(x_nsy) - array(x_ini))
    xma, xmi = max(x_ini), min(x_ini)
    yma, ymi = max(x_diff), min(x_diff)
    ax1.plot(x_ini, x_diff, color='r', marker='x', ms=3, linestyle='', zorder=102)
    x_diff =  d2k(array(x_fin) - array(x_ini))
    ax1.plot(x_ini, x_diff, color='b', marker='x', ms=3, linestyle='', zorder=102)
    ax1.set_xlim(xmi-(xma-xmi)*.05, xma+(xma-xmi)*.05)
    ax1.set_ylim(ymi-(yma-ymi)*.05, yma+(yma-ymi)*.05)
    ax1.set_xlabel('Longitude')
    ax1.set_ylabel('Dislocation (km)')
    ax1.grid(True, linestyle='--', linewidth=.5, color='k', alpha=.3)
    ax1.locator_params(axis='x',nbins=6)
    ax1.locator_params(axis='y',nbins=6)

    # Plot KDE

    xmin, xmax = ax1.get_xlim()
    ymin, ymax = ax1.get_ylim()  
    X, Y       = mgrid[xmin:xmax:100j, ymin:ymax:100j]
    positions  = vstack([X.ravel(), Y.ravel()])
    values     = vstack([x_ini, x_diff])
    kernel     = gaussian_kde(values)
    Z          = reshape(kernel(positions).T, X.shape)
    im         = ax1.contourf(X, Y, Z, cmap=plt.cm.gist_earth_r, alpha=.9, zorder=101)
    divider    = make_axes_locatable(ax1)
    cax        = divider.append_axes("right", size="4%", pad=0.05)
    cb         = plt.colorbar(im, ax=ax1, cax=cax)
    tk_locator = ticker.MaxNLocator(nbins=6)
    cb.locator = tk_locator
    cb.update_ticks()
    cb.outline.set_linewidth(.75)
    cb.set_label(label='PDF', size=6)
    cb.ax.tick_params(labelsize=6) 
          
    ax2 = plt.subplot(222)

    y_diff =  d2k(array(y_nsy) - array(y_ini))
    xma, xmi = max(y_ini), min(y_ini)
    yma, ymi = max(y_diff), min(y_diff)
    ax2.plot(y_ini, y_diff, color='r', marker='x', ms=3, linestyle='', zorder=102)
    y_diff =  d2k(array(y_fin) - array(y_ini))
    ax2.plot(y_ini, y_diff, color='b', marker='x', ms=3, linestyle='', zorder=102)
    ax2.set_xlim(xmi-(xma-xmi)*.05, xma+(xma-xmi)*.05)
    ax2.set_ylim(ymi-(yma-ymi)*.05, yma+(yma-ymi)*.05)
    ax2.set_xlabel('Latitude')
    ax2.set_ylabel('Dislocation (km)')
    ax2.grid(True, linestyle='--', linewidth=.5, color='k', alpha=.3)
    ax2.locator_params(axis='x',nbins=6)
    ax2.locator_params(axis='y',nbins=6)

    # Plot KDE

    xmin, xmax = ax2.get_xlim()
    ymin, ymax = ax2.get_ylim()
    X, Y       = mgrid[xmin:xmax:100j, ymin:ymax:100j]
    positions  = vstack([X.ravel(), Y.ravel()])
    values     = vstack([y_ini, y_diff])
    kernel     = gaussian_kde(values)
    Z          = reshape(kernel(positions).T, X.shape)
    im         = ax2.contourf(X, Y, Z, cmap=plt.cm.gist_earth_r, alpha=.9, zorder=101)
    divider    = make_axes_locatable(ax2)
    cax        = divider.append_axes("right", size="4%", pad=0.05)
    cb         = plt.colorbar(im, ax=ax2, cax=cax)
    tk_locator = ticker.MaxNLocator(nbins=5)
    cb.locator = tk_locator
    cb.update_ticks()
    cb.outline.set_linewidth(.75)
    cb.set_label(label='PDF', size=6)
    cb.ax.tick_params(labelsize=6)
    
    ax3 = plt.subplot(223)

    z_diff =  array(z_nsy) - array(z_ini)
    xma, xmi = max(z_ini), min(z_ini)
    yma, ymi = max(z_diff), min(z_diff)
    ax3.plot(z_ini, z_diff, color='r', marker='x', ms=3, linestyle='', zorder=102)
    z_diff =  array(z_fin) - array(z_ini)
    ax3.plot(z_ini, z_diff, color='b', marker='x', ms=3, linestyle='', zorder=102)
    ax3.set_xlim(xmi-(xma-xmi)*.05, xma+(xma-xmi)*.05)
    ax3.set_ylim(ymi-(yma-ymi)*.05, yma+(yma-ymi)*.05)
    ax3.set_xlabel('Depth (km)')
    ax3.set_ylabel('Dislocation (km)')
    ax3.grid(True, linestyle='--', linewidth=.5, color='k', alpha=.3)
    ax3.locator_params(axis='x',nbins=6)
    ax3.locator_params(axis='y',nbins=6)

    # Plot KDE

    xmin, xmax = ax3.get_xlim()
    ymin, ymax = ax3.get_ylim()
    X, Y       = mgrid[xmin:xmax:100j, ymin:ymax:100j]
    positions  = vstack([X.ravel(), Y.ravel()])
    values     = vstack([z_ini, z_diff])
    kernel     = gaussian_kde(values)
    Z          = reshape(kernel(positions).T, X.shape)
    im         = ax3.contourf(X, Y, Z, cmap=plt.cm.gist_earth_r, alpha=.9, zorder=101)
    divider    = make_axes_locatable(ax3)
    cax        = divider.append_axes("right", size="4%", pad=0.05)
    cb         = plt.colorbar(im, ax=ax3, cax=cax)
    tk_locator = ticker.MaxNLocator(nbins=6)
    cb.locator = tk_locator
    cb.update_ticks()
    cb.outline.set_linewidth(.75)
    cb.set_label(label='PDF', size=6)
    cb.ax.tick_params(labelsize=6)
    
    ax4 = plt.subplot(224)
    [i.set_linewidth(0.6) for i in ax4.spines.itervalues()]
    
    ax4.plot(vel_ini[0], -array(vel_ini[1]), 'r-', linewidth=1.5, label='Initial')
    ax4.plot(vel_fin[0], -array(vel_fin[1]), 'b-', linewidth=1.5, label='Inverted')
    model_rms_fit = norm(array(vf)-array(vi))
    ax4.set_xlabel('Velocity (km/s)')
    ax4.set_ylabel('Depth (km)')
    ax4.invert_yaxis()
    ax4.text(0.82, 0.9,'||model||=%.2f (km/s)'%(model_rms_fit), ha='center', va='center', transform=ax4.transAxes,
             bbox=dict(facecolor='w', alpha=0.5, pad=2), fontsize=6)
    ax4.grid(True, linestyle='--', linewidth=.5, color='k', alpha=.3)
    ax4.locator_params(axis='x',nbins=6)
    ax4.locator_params(axis='y',nbins=5)
    ax4.set_ylim(-min(vel_ini[1]),0)
    plt.legend(loc=3, fontsize=6)


    plt.tight_layout()
    plt.savefig(os.path.join('Check-Test',project_name,'StabilityTest.tiff'),dpi=300)
    plt.close()

    print '\n+++ Result was saved in "Check-Test%s" directory.\n'%(os.sep+project_name)
Beispiel #8
0
def damping_test():
    
    project_nm = raw_input('\n+++ Enter project name:\n\n')
    velmod_nm  = raw_input('\n+++ Enter best inversion id number (e.g. minimum model id):\n\n')
    which_dmp  = input('\n+++ Which damping parameter do you want to test?\n\n1- othet (Origin Time Damping Factor)\n2- xythet (Epicenter Damping Factor)\n3- zthet (Depth Damping Factor)\n4- vthet (Velocity Damping Factor)\n5- stathet (Station correction Damping Factor)\n\n')
    vel_dmp    = input('\n+++ selected damping range (Min, Max, Num of damping point):\n\n')

    if not os.path.exists(os.path.join('Check-Test',project_nm)):

        os.makedirs(os.path.join('Check-Test',project_nm))
        
    damp_dic = {1:'Origin Time Damping Test',
                2:'Epicenter Damping Test',
                3:'Depth Damping Test',
                4:'Velocity Damping Test',
                5:'Station Correction Damping Test',}

    copy(os.path.join('velout',project_nm,'velest%s.out'%(velmod_nm)), 'velest.cmn')

    print '\n+++ Warning! selected dampping parameter for the following file will be changed!'
    print '\n%s\n'%(os.path.join('velout',project_nm,'velest%s.out'%(velmod_nm)))

    tmp = open('tmp.dat', 'w')
    tmp.write('damping test')

    with open('velest.cmn') as f:

        c = 0

        for l in f:

            if 31 <= c <= 129:

                tmp.write(l)

            c+=1

    tmp.close()

    model_var = []
    data_var  = []

    vel_dmp = linspace(vel_dmp[0], vel_dmp[1], vel_dmp[2], dtype=int)

    flag = False

    print ''

    for vd in vel_dmp:

        print '+++ Test on Damping = %4d'%(vd)

        output = open('velest.cmn','w')

        with open('tmp.dat') as f:

            hint = '***   othet   xythet    zthet    vthet   stathet'

            for l in f:

                if hint in l:

                    flag = True

                if flag and hint not in l:

                    l    = l.split()
                    l[which_dmp-1] = '%.3f'%(vd)
                    l    = '     '+'    '.join(l)+'\n'
                    flag = False

                output.write(l)
                
        output.close()

        os.system('velest > /dev/null')

        x, y = read_velestout(inp=os.path.join('velout',project_nm,'velest%s.out'%(velmod_nm)))

        data_var.append(x)
        model_var.append(y)

    data_var  = array(data_var)
    model_var = array(model_var)

    init_plotting_isi(8,8)
    plt.rcParams['axes.labelsize'] = 7
    
    ax = plt.subplot(111)
    [i.set_linewidth(0.6) for i in ax.spines.itervalues()]
    
    ax.plot(model_var[:,0],data_var[:,0],marker='o', ms=3, lw=1, color='r')
    for x,y,z in zip(model_var[:,0], data_var[:,0], vel_dmp): ax.text(x,y,'%d'%(z))
    ax.grid(True, linestyle='--', linewidth=.5, color='k', alpha=.3)
    ax.set_title('%s'%(damp_dic[which_dmp]))
    ax.set_xlabel('Model Variance (km/s)$^2$')
    ax.set_ylabel('Data Variance (s)$^2$')

    ax.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
    ax.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
    ax.locator_params(axis='x',nbins=5)
    ax.locator_params(axis='y',nbins=5)
            
    name = damp_dic[which_dmp].replace(' ','_')

    plt.tight_layout()
    plt.savefig(os.path.join('Check-Test',project_nm,name+'.tiff'),dpi=300)
    plt.close()

    print '\n+++ Output file "%s" was created.\n'%(os.path.join('Check-Test',project_nm,name+'.tiff'))