Example #1
0
def read_kurtogram_frequencies(filename):
    """
    Reads the binary file with kurtogram frequencies.
    Plots the histograms of lower and upper frequencies 
    for each station.
    Aims at determining the best filtering parameters.

    :param filename: File to read

    :type filename: string
    """
    a = BinaryFile(filename)
    freqs = a.read_binary_file()

    for staname in sorted(freqs):
        print "%s %.1f %.1f" % (staname, np.mean(freqs[staname][:, 0]),
                                np.mean(freqs[staname][:, 1]))
        fig = plt.figure()
        fig.set_facecolor('white')
        plt.hist([freqs[staname][:, 0], freqs[staname][:, 1]], 35,
                 histtype='stepfilled', alpha=.2, color=('b', 'g'),
                 label=['f_low', 'f_up'])
        plt.title(staname)
        plt.xlabel('Frequency (Hz)')
        plt.figtext(0.15, 0.85, "Lower f = %.1f Hz" %
                    np.mean(freqs[staname][:, 0]))
        plt.figtext(0.15, 0.8, "Upper f = %.1f Hz" %
                    np.mean(freqs[staname][:, 1]))
        plt.show()
Example #2
0
def read_kurtogram_frequencies(filename):
    """
    Reads the binary file with kurtogram frequencies.
    Plots the histograms of lower and upper frequencies 
    for each station.
    Aims at determining the best filtering parameters.

    :param filename: File to read

    :type filename: string
    """
    a = BinaryFile(filename)
    freqs = a.read_binary_file()

    for staname in sorted(freqs):
        print "%s %.1f %.1f" % (staname, np.mean(
            freqs[staname][:, 0]), np.mean(freqs[staname][:, 1]))
        fig = plt.figure()
        fig.set_facecolor('white')
        plt.hist([freqs[staname][:, 0], freqs[staname][:, 1]],
                 35,
                 histtype='stepfilled',
                 alpha=.2,
                 color=('b', 'g'),
                 label=['f_low', 'f_up'])
        plt.title(staname)
        plt.xlabel('Frequency (Hz)')
        plt.figtext(0.15, 0.85,
                    "Lower f = %.1f Hz" % np.mean(freqs[staname][:, 0]))
        plt.figtext(0.15, 0.8,
                    "Upper f = %.1f Hz" % np.mean(freqs[staname][:, 1]))
        plt.show()
Example #3
0
def plot_traces(CLUSTER, delay_file, coeff, locs, stations, datadir, data_files, threshold):
  # Read the file containing the time delays
  a=BinaryFile(delay_file)
  delay=a.read_binary_file()

  t_before=0.5
  t_after=6.0

  rg_x=[362,370]
  rg_y=[7647,7653]
  rg_z=[-4.0,2.5]

  tr={}
  for data_file in data_files:
    wf=Waveform()
    wf.read_from_file(data_file)
    tr[wf.station]=wf.values
    dt=wf.delta
    tdeb=wf.starttime

  list_name=sorted(tr)

  for i in range(1,len(CLUSTER)+1): # cluster index
    i=51
    for j in range(len(CLUSTER[i])): # first event index
      e1=CLUSTER[i][j]
      for k in range(j+1,len(CLUSTER[i])): # second event index
        e2=CLUSTER[i][k]
        co=0
        fig = plt.figure()
        fig.set_facecolor('white')
        for l in range(len(list_name)):
          name=list_name[l]
          if delay[name][e1-1][e2-1]!='NaN':
            stack_time_1=locs[e1-1]['o_time']
            i_start_1, i_end_1=waveval(stack_time_1,t_before,t_after,dt,tdeb)
            val1=tr[name][i_start_1-1:i_end_1]
            stack_time_2=locs[e2-1]['o_time']
            i_start_2, i_end_2=waveval(stack_time_2-delay[name][e1-1][e2-1],t_before,t_after,dt,tdeb)
            val2=tr[name][i_start_2-1:i_end_2]
            t=np.linspace(0,t_after+t_before,(t_after+t_before)/dt+1)
            ax=fig.add_subplot(len(list_name),1,l+1)
            ax.set_axis_off()
            ax.plot(t,val1/max(val1))
            ax.plot(t,val2/max(val2),'r')
            c='k'
            if coeff[name][e1-1][e2-1]>=threshold:
              co=co+1
              c='r'
            ax.text(0.5,0.5,"%s, %s, %s"%(name,str(coeff[name][e1-1][e2-1]),str(delay[name][e1-1][e2-1])),color=c)
        fig.suptitle("Cluster : %s ; Event pair : (%s,%s) ; %d"%(str(i),str(e1),str(e2),co))
        plt.show()
Example #4
0
def do_clustering_setup_and_run(opdict):

  base_path=opdict['base_path']
  verbose=opdict['verbose']

  # stations
  stations_filename=os.path.join(base_path,'lib',opdict['stations'])

  # output directory
  output_dir=os.path.join(base_path,'out',opdict['outdir'])

  # data
  data_dir=os.path.join(base_path,'data',opdict['datadir'])
  data_glob=opdict['dataglob']
  data_files=glob.glob(os.path.join(data_dir,data_glob))
  data_files.sort()

  # location file
  locdir=os.path.join(base_path,'out',opdict['outdir'],'loc')
  loc_filename=os.path.join(locdir,'locations.dat')

  # file containing correlation values
  coeff_file=os.path.join(locdir,opdict['xcorr_corr'])
  # Read correlation values
  b=BinaryFile(coeff_file)
  coeff=b.read_binary_file()

  # file containing time delays
  delay_file=os.path.join(locdir,opdict['xcorr_delay'])

  # INPUT PARAMETERS
  nbmin=int(opdict['nbsta'])
  if nbmin > len(coeff.keys()):
    raise Error('the minimum number of stations cannot be > to the number of stations !!')
  event=len(coeff.values()[0])
  tplot=float(opdict['clus']) # threshold for which we save and plot 
  cluster_file="%s/cluster-%s-%s"%(locdir,str(tplot),str(nbmin))

  corr=[opdict['clus']]
  #corr=np.arange(0,1.1,0.1)
  for threshold in corr:
    threshold=float(threshold)
    nbsta=compute_nbsta(event,coeff,threshold)

    CLUSTER = do_clustering(event,nbsta,nbmin)

    if threshold == tplot:

      print "----------------------------------------------"
      print "THRESHOLD : ",threshold," # STATIONS : ",nbmin
      print "# CLUSTERS : ",len(CLUSTER)
      print CLUSTER

      c=BinaryFile(cluster_file)
      c.write_binary_file(CLUSTER)
      print "Written in %s"%cluster_file

      if verbose: # PLOT
        # Read location file
        locs=read_locs_from_file(loc_filename)
        # Read station file 
        stations=read_stations_file(stations_filename)

        # Look at the waveforms 
        plot_traces(CLUSTER, delay_file, coeff, locs, stations, data_dir, data_files, threshold)
Example #5
0
def do_double_diff_setup_and_run(opdict):
    """
    Do double difference (outer routine). Takes options from a
    WavelocOptions.opdict dictionary.

    :param opdict: Dictionary of parameters and options
    """

    base_path = opdict['base_path']
    verbose = opdict['verbose']
    dd_loc = opdict['dd_loc']

    # Station
    stations_filename = os.path.join(base_path, 'lib', opdict['stations'])
    stations = read_stations_file(stations_filename)

    # Location file
    locdir = os.path.join(base_path, 'out', opdict['outdir'], 'loc')
    loc_filename = os.path.join(locdir, 'locations.dat')
    locs = read_locs_from_file(loc_filename)
    opdict = read_header_from_file(loc_filename, opdict)

    # ------------------------------------------------------------------------
    # search grid
    search_grid_filename = os.path.join(base_path, 'lib',
                                        opdict['search_grid'])
    # traveltimes grid
    grid_info = read_hdr_file(search_grid_filename)
    time_grids = get_interpolated_time_grids(opdict)

    # Extract the UTM coordinates of the area of study
    xstart = grid_info['x_orig']
    xend = xstart+grid_info['nx']*grid_info['dx']
    ystart = grid_info['y_orig']
    yend = ystart+grid_info['ny']*grid_info['dy']
    zend = -grid_info['z_orig']
    zstart = -(-zend+grid_info['nz']*grid_info['dz'])
    area = [xstart, xend, ystart, yend, zstart, zend]

    # ------------------------------------------------------------------------
    nbmin = int(opdict['nbsta'])
    threshold = float(opdict['clus'])

    # Correlation,  time delay and cluster files
    corr_file = os.path.join(locdir, opdict['xcorr_corr'])
    cfile = BinaryFile(corr_file)
    coeff = cfile.read_binary_file()

    delay_file = os.path.join(locdir, opdict['xcorr_delay'])
    dfile = BinaryFile(delay_file)
    delay = dfile.read_binary_file()

    cluster_file = os.path.join(locdir, 'cluster-%s-%s' % (str(threshold),
                                                           str(nbmin)))
    clfile = BinaryFile(cluster_file)
    cluster = clfile.read_binary_file()

    # ------------------------------------------------------------------------
    # Input parameters
    len_cluster_min = 2

    if dd_loc:
        new_loc_filename = os.path.join(locdir, 'relocations.dat')
        new_loc_file = open(new_loc_filename, 'w')
        write_header_options(new_loc_file, opdict)

    # ------------------------------------------------------------------------
    # Iterate over clusters
    for i in cluster.keys():
        print "CLUSTER %d:" % i, cluster[i], len(cluster[i])
        N = len(cluster[i])

        # Hypocentral parameters to be changed
        x, y, z, z_ph, to = coord_cluster(cluster[i], locs)

        # Replace bad locations by the centroid coordinates
        centroid_x = np.mean(x)
        centroid_y = np.mean(y)
        centroid_z = np.mean(z)

        for ii in range(len(cluster[i])):
            if np.abs(x[ii]-centroid_x) > .75:
                x[ii] = centroid_x
            if np.abs(y[ii]-centroid_y) > .75:
                y[ii] = centroid_y
            if np.abs(z[ii]-centroid_z) > .75:
                z[ii] = centroid_z

        if N > len_cluster_min:
            # Theroretical traveltimes and arrival times
            t_th, arr_times = traveltimes(x, y, z, to, stations, time_grids)
            # do double difference location
            x, y, z, to = do_double_diff(x, y, z, to, stations, coeff, delay,
                                         cluster[i], threshold, t_th,
                                         arr_times)

        if verbose:
            from clustering import compute_nbsta
            nbsta = compute_nbsta(len(locs), coeff, threshold)
            plot_events(cluster, locs, stations, x, y, z, i, threshold, nbmin,
                        area, nbsta)

        if dd_loc:
            ind = 0
        for j in cluster[i]:
            locs[j-1]['x_mean'] = x[ind]
            locs[j-1]['y_mean'] = y[ind]
            locs[j-1]['z_mean'] = z[ind]
            locs[j-1]['o_time'] = to[ind]
            locs[j-1]['x_sigma'] = 0
            locs[j-1]['y_sigma'] = 0
            locs[j-1]['z_sigma'] = 0
            locs[j-1]['o_err_right'] = 0
            locs[j-1]['o_err_left'] = 0
            ind += 1
            new_loc_file.write("Max = %.2f, %s - %.2f s + %.2f s, x= %.4f pm\
                %.4f km, y= %.4f pm %.4f km, z= %.4f pm %.4f km\n" %
                (locs[j-1]['max_trig'], locs[j-1]['o_time'].isoformat(),
                locs[j-1]['o_err_left'], locs[j-1]['o_err_right'],
                locs[j-1]['x_mean'], locs[j-1]['x_sigma'],
                locs[j-1]['y_mean'], locs[j-1]['y_sigma'],
                locs[j-1]['z_mean'], locs[j-1]['z_sigma']))

    if dd_loc:
        new_loc_file.close()
Example #6
0
def do_double_diff_setup_and_run(opdict):
    """
    Do double difference (outer routine). Takes options from a
    WavelocOptions.opdict dictionary.

    :param opdict: Dictionary of parameters and options
    """

    base_path = opdict['base_path']
    verbose = opdict['verbose']
    dd_loc = opdict['dd_loc']

    # Station
    stations_filename = os.path.join(base_path, 'lib', opdict['stations'])
    stations = read_stations_file(stations_filename)

    # Location file
    locdir = os.path.join(base_path, 'out', opdict['outdir'], 'loc')
    loc_filename = os.path.join(locdir, 'locations.dat')
    locs = read_locs_from_file(loc_filename)
    opdict = read_header_from_file(loc_filename, opdict)

    # ------------------------------------------------------------------------
    # search grid
    search_grid_filename = os.path.join(base_path, 'lib',
                                        opdict['search_grid'])
    # traveltimes grid
    grid_info = read_hdr_file(search_grid_filename)
    time_grids = get_interpolated_time_grids(opdict)

    # Extract the UTM coordinates of the area of study
    xstart = grid_info['x_orig']
    xend = xstart + grid_info['nx'] * grid_info['dx']
    ystart = grid_info['y_orig']
    yend = ystart + grid_info['ny'] * grid_info['dy']
    zend = -grid_info['z_orig']
    zstart = -(-zend + grid_info['nz'] * grid_info['dz'])
    area = [xstart, xend, ystart, yend, zstart, zend]

    # ------------------------------------------------------------------------
    nbmin = int(opdict['nbsta'])
    threshold = float(opdict['clus'])

    # Correlation,  time delay and cluster files
    corr_file = os.path.join(locdir, opdict['xcorr_corr'])
    cfile = BinaryFile(corr_file)
    coeff = cfile.read_binary_file()

    delay_file = os.path.join(locdir, opdict['xcorr_delay'])
    dfile = BinaryFile(delay_file)
    delay = dfile.read_binary_file()

    cluster_file = os.path.join(locdir,
                                'cluster-%s-%s' % (str(threshold), str(nbmin)))
    clfile = BinaryFile(cluster_file)
    cluster = clfile.read_binary_file()

    # ------------------------------------------------------------------------
    # Input parameters
    len_cluster_min = 2

    if dd_loc:
        new_loc_filename = os.path.join(locdir, 'relocations.dat')
        new_loc_file = open(new_loc_filename, 'w')
        write_header_options(new_loc_file, opdict)

    # ------------------------------------------------------------------------
    # Iterate over clusters
    for i in cluster.keys():
        print "CLUSTER %d:" % i, cluster[i], len(cluster[i])
        N = len(cluster[i])

        # Hypocentral parameters to be changed
        x, y, z, z_ph, to = coord_cluster(cluster[i], locs)

        # Replace bad locations by the centroid coordinates
        centroid_x = np.mean(x)
        centroid_y = np.mean(y)
        centroid_z = np.mean(z)

        for ii in range(len(cluster[i])):
            if np.abs(x[ii] - centroid_x) > .75:
                x[ii] = centroid_x
            if np.abs(y[ii] - centroid_y) > .75:
                y[ii] = centroid_y
            if np.abs(z[ii] - centroid_z) > .75:
                z[ii] = centroid_z

        if N > len_cluster_min:
            # Theroretical traveltimes and arrival times
            t_th, arr_times = traveltimes(x, y, z, to, stations, time_grids)
            # do double difference location
            x, y, z, to = do_double_diff(x, y, z, to, stations, coeff, delay,
                                         cluster[i], threshold, t_th,
                                         arr_times)

        if verbose:
            from clustering import compute_nbsta
            nbsta = compute_nbsta(len(locs), coeff, threshold)
            plot_events(cluster, locs, stations, x, y, z, i, threshold, nbmin,
                        area, nbsta)

        if dd_loc:
            ind = 0
        for j in cluster[i]:
            locs[j - 1]['x_mean'] = x[ind]
            locs[j - 1]['y_mean'] = y[ind]
            locs[j - 1]['z_mean'] = z[ind]
            locs[j - 1]['o_time'] = to[ind]
            locs[j - 1]['x_sigma'] = 0
            locs[j - 1]['y_sigma'] = 0
            locs[j - 1]['z_sigma'] = 0
            locs[j - 1]['o_err_right'] = 0
            locs[j - 1]['o_err_left'] = 0
            ind += 1
            new_loc_file.write(
                "Max = %.2f, %s - %.2f s + %.2f s, x= %.4f pm\
                %.4f km, y= %.4f pm %.4f km, z= %.4f pm %.4f km\n" %
                (locs[j - 1]['max_trig'], locs[j - 1]['o_time'].isoformat(),
                 locs[j - 1]['o_err_left'], locs[j - 1]['o_err_right'],
                 locs[j - 1]['x_mean'], locs[j - 1]['x_sigma'],
                 locs[j - 1]['y_mean'], locs[j - 1]['y_sigma'],
                 locs[j - 1]['z_mean'], locs[j - 1]['z_sigma']))

    if dd_loc:
        new_loc_file.close()
Example #7
0
def plot_traces(CLUSTER, delay_file, coeff, locs, stations, datadir,
                data_files, threshold):
    # Read the file containing the time delays
    a = BinaryFile(delay_file)
    delay = a.read_binary_file()

    t_before = 0.5
    t_after = 6.0

    rg_x = [362, 370]
    rg_y = [7647, 7653]
    rg_z = [-4.0, 2.5]

    tr = {}
    for data_file in data_files:
        wf = Waveform()
        wf.read_from_file(data_file)
        tr[wf.station] = wf.values
        dt = wf.delta
        tdeb = wf.starttime

    list_name = sorted(tr)

    for i in range(1, len(CLUSTER) + 1):  # cluster index
        i = 51
        for j in range(len(CLUSTER[i])):  # first event index
            e1 = CLUSTER[i][j]
            for k in range(j + 1, len(CLUSTER[i])):  # second event index
                e2 = CLUSTER[i][k]
                co = 0
                fig = plt.figure()
                fig.set_facecolor('white')
                for l in range(len(list_name)):
                    name = list_name[l]
                    if delay[name][e1 - 1][e2 - 1] != 'NaN':
                        stack_time_1 = locs[e1 - 1]['o_time']
                        i_start_1, i_end_1 = waveval(stack_time_1, t_before,
                                                     t_after, dt, tdeb)
                        val1 = tr[name][i_start_1 - 1:i_end_1]
                        stack_time_2 = locs[e2 - 1]['o_time']
                        i_start_2, i_end_2 = waveval(
                            stack_time_2 - delay[name][e1 - 1][e2 - 1],
                            t_before, t_after, dt, tdeb)
                        val2 = tr[name][i_start_2 - 1:i_end_2]
                        t = np.linspace(0, t_after + t_before,
                                        (t_after + t_before) / dt + 1)
                        ax = fig.add_subplot(len(list_name), 1, l + 1)
                        ax.set_axis_off()
                        ax.plot(t, val1 / max(val1))
                        ax.plot(t, val2 / max(val2), 'r')
                        c = 'k'
                        if coeff[name][e1 - 1][e2 - 1] >= threshold:
                            co = co + 1
                            c = 'r'
                        ax.text(0.5,
                                0.5,
                                "%s, %s, %s" %
                                (name, str(coeff[name][e1 - 1][e2 - 1]),
                                 str(delay[name][e1 - 1][e2 - 1])),
                                color=c)
                fig.suptitle("Cluster : %s ; Event pair : (%s,%s) ; %d" %
                             (str(i), str(e1), str(e2), co))
                plt.show()
Example #8
0
def do_clustering_setup_and_run(opdict):

    base_path = opdict['base_path']
    verbose = opdict['verbose']

    # stations
    stations_filename = os.path.join(base_path, 'lib', opdict['stations'])

    # output directory
    output_dir = os.path.join(base_path, 'out', opdict['outdir'])

    # data
    data_dir = os.path.join(base_path, 'data', opdict['datadir'])
    data_glob = opdict['dataglob']
    data_files = glob.glob(os.path.join(data_dir, data_glob))
    data_files.sort()

    # location file
    locdir = os.path.join(base_path, 'out', opdict['outdir'], 'loc')
    loc_filename = os.path.join(locdir, 'locations.dat')

    # file containing correlation values
    coeff_file = os.path.join(locdir, opdict['xcorr_corr'])
    # Read correlation values
    b = BinaryFile(coeff_file)
    coeff = b.read_binary_file()

    # file containing time delays
    delay_file = os.path.join(locdir, opdict['xcorr_delay'])

    # INPUT PARAMETERS
    nbmin = int(opdict['nbsta'])
    if nbmin > len(coeff.keys()):
        raise Error(
            'the minimum number of stations cannot be > to the number of stations !!'
        )
    event = len(coeff.values()[0])
    tplot = float(opdict['clus'])  # threshold for which we save and plot
    cluster_file = "%s/cluster-%s-%s" % (locdir, str(tplot), str(nbmin))

    corr = [opdict['clus']]
    #corr=np.arange(0,1.1,0.1)
    for threshold in corr:
        threshold = float(threshold)
        nbsta = compute_nbsta(event, coeff, threshold)

        CLUSTER = do_clustering(event, nbsta, nbmin)

        if threshold == tplot:

            print "----------------------------------------------"
            print "THRESHOLD : ", threshold, " # STATIONS : ", nbmin
            print "# CLUSTERS : ", len(CLUSTER)
            print CLUSTER

            c = BinaryFile(cluster_file)
            c.write_binary_file(CLUSTER)
            print "Written in %s" % cluster_file

            if verbose:  # PLOT
                # Read location file
                locs = read_locs_from_file(loc_filename)
                # Read station file
                stations = read_stations_file(stations_filename)

                # Look at the waveforms
                plot_traces(CLUSTER, delay_file, coeff, locs, stations,
                            data_dir, data_files, threshold)
Example #9
0
def do_kurtogram_setup_and_run(opdict):
    """
    Run the kurtogram analysis using the parameters contained in the
    WavelocOptions.opdict.

    :param opdict: Dictionary containing the waveloc parameters and options.
    """

    base_path = opdict['base_path']

    # data
    data_dir = os.path.join(base_path, 'data', opdict['datadir'])
    data_glob = opdict['dataglob']
    data_files = glob.glob(os.path.join(data_dir, data_glob))
    data_files.sort()

    kurt_glob = opdict['kurtglob']
    kurt_files = glob.glob(os.path.join(data_dir, kurt_glob))
    kurt_files.sort()

    # output directory
    out_dir = os.path.join(base_path, 'out', opdict['outdir'])

    # location file
    locdir = os.path.join(out_dir, 'loc')
    locfile = os.path.join(locdir, 'locations.dat')
    # Read locations
    locs = read_locs_from_file(locfile)

    # create a file containing the best filtering parameters for each event and
    # each station
    kurto_file = os.path.join(out_dir, 'kurto')

    tdeb = utcdatetime.UTCDateTime(opdict['starttime'])
    tfin = utcdatetime.UTCDateTime(opdict['endtime'])

    # write filenames in a dictionary
    kurtdata = {}
    for filename in kurt_files:
        try:
            wf = Waveform()
            wf.read_from_file(filename)
            sta = wf.station
            kurtdata[sta] = filename
        except UserWarning:
            logging.info('No data around %s for file %s.' %
                         (tdeb.isoformat(), filename))

    data = {}
    for filename in data_files:
        try:
            wf = Waveform()
            wf.read_from_file(filename)
            sta = wf.station
            data[sta] = filename
        except UserWarning:
            logging.info('No data around %s for file %s.' %
                         (tdeb.isoformat(), filename))

    # ------------------------------------------------------------------------
    # Create an empty dictionnary that will contain the filtering parameters
    param = {}

    for station in sorted(data):

        wf1 = Waveform()
        wf1.read_from_file(data[station], starttime=tdeb, endtime=tfin)

        wf2 = Waveform()
        wf2.read_from_file(kurtdata[station], starttime=tdeb, endtime=tfin)

        info = {}
        info['data_file'] = data[station]
        info['station'] = station
        info['tdeb_data'] = wf1.starttime
        info['tdeb_kurt'] = wf2.starttime
        info['kurt_file'] = kurtdata[station]
        info['data_ini'] = wf1.values
        info['kurt_ini'] = wf2.values
        info['dt'] = wf1.dt
        info['filter'] = []

        logging.info('Processing station %s' % info['station'])

        if opdict['new_kurtfile']:
            new_filename = 'filt_kurtogram'
            new_kurt_filename = \
                os.path.join("%s%s" % (data[station].split(data_glob[1:])[0],
                                       new_filename))
            info['new_kurt_file'] = new_kurt_filename
            trace_kurt_fin = Waveform()
            trace_kurt_fin.read_from_file(new_kurt_filename)
            info['new_kurt'] = trace_kurt_fin.values

        for loc in locs:
            origin_time = loc['o_time']
            if opdict['verbose']:
                print "******************************************************"
                print logging.info(origin_time)

            if origin_time > tdeb and origin_time < tfin:
                info = kurto(origin_time, info, opdict)
            else:
                continue

        info['filter'] = np.matrix(info['filter'])
        sta = info['station']
        param[sta] = info['filter']

        if 'new_kurt_file' in info:
            trace_kurt_fin.values[:] = info['new_kurt']
            trace_kurt_fin.write_to_file_filled(info['new_kurt_file'],
                                                format='MSEED', fill_value=0)

    # Write the dictionnary 'param' in a binary file
    if os.path.isfile(kurto_file):
        ans = raw_input('%s file already exists. Do you really want to replace\
it ? (y or n):\n' % kurto_file)
        if ans != 'y':
            kurto_file = "%s_1" % kurto_file

    a = BinaryFile(kurto_file)
    a.write_binary_file(param)

    # read and plot the file you have just written
    read_kurtogram_frequencies(kurto_file)
Example #10
0
def do_clustering_setup_and_run(opdict):
    """
    Does clustering by applying the depth first search algorithm and saves the result 
    (= a dictionary containing the event indexes forming each cluster) in a binary file.
    Needs to define the correlation value threshold and the minimum number of stations 
    where this threshold should be reached to form a cluster (should be done in the options
    dictionary)

    :param opdict: Dictionary of waveloc options

    """

    base_path = opdict['base_path']
    verbose = opdict['verbose']

    # stations
    stations_filename = os.path.join(base_path, 'lib', opdict['stations'])

    # data
    data_dir = os.path.join(base_path, 'data', opdict['datadir'])
    data_glob = opdict['dataglob']
    data_files = glob.glob(os.path.join(data_dir, data_glob))
    data_files.sort()

    # location file
    locdir = os.path.join(base_path, 'out', opdict['outdir'], 'loc')
    loc_filename = os.path.join(locdir, 'locations.dat')

    # file containing correlation values
    coeff_file = os.path.join(locdir, opdict['xcorr_corr'])
    # Read correlation values
    b = BinaryFile(coeff_file)
    coeff = b.read_binary_file()

    # INPUT PARAMETERS
    nbmin = int(opdict['nbsta'])
    if nbmin > len(coeff.keys()):
        raise Exception('the minimum number of stations cannot be > to the\
                         number of stations !!')
    event = len(coeff.values()[0])
    tplot = float(opdict['clus'])  # threshold for which we save and plot
    cluster_file = "%s/cluster-%s-%s" % (locdir, str(tplot), str(nbmin))

    corr = [opdict['clus']]
    #corr = np.arange(0, 1.1, 0.1)
    for threshold in corr:
        threshold = float(threshold)
        nbsta = compute_nbsta(event, coeff, threshold)

        CLUSTER = do_clustering(event, nbsta, nbmin)

        if threshold == tplot:

            print "----------------------------------------------"
            print "THRESHOLD : ", threshold, " # STATIONS : ", nbmin
            print "# CLUSTERS : ", len(CLUSTER)
            print CLUSTER

            c = BinaryFile(cluster_file)
            c.write_binary_file(CLUSTER)
            print "Written in %s" % cluster_file

            if verbose:  # PLOT
                # Read location file
                locs = read_locs_from_file(loc_filename)
                # Read station file
                stations = read_stations_file(stations_filename)

                # Look at the waveforms
                #plot_traces(CLUSTER, delay_file, coeff, locs,
                #            data_dir, data_files, threshold)

                # Plot graphs
                plot_graphs(locs, stations, nbsta, CLUSTER, nbmin, threshold)
Example #11
0
def plot_traces(CLUSTER, delay_file, coeff, locs, datadir, data_files,
                threshold):
    """
    Plots the waveforms of all possible event pairs within a cluster.
    On the same figure, displays the superimposed waveforms of the event pair for all stations.
    Also displays the correlation value.

    :param CLUSTER: dictionary containing the event indexes belonging to each cluster
    :param delay_file: file name of the file containing the time delays
    :param coeff: cross-correlation values of all possible event pairs for all stations
    :param locs: list of the whole Waveloc locations (each element of the list is a dictionary)
    :param datadir: data directory path
    :param data_files: list of paths of data files
    :param threshold: correlation coefficient threshold

    :type CLUSTER: dictionary
    :type delay_file: string
    :type coeff: dictionary
    :type locs: list
    :type datadir: string
    :type data_files: list
    :type threshold: float
    """

    # Read the file containing the time delays
    a = BinaryFile(delay_file)
    delay = a.read_binary_file()

    t_before = 0.5
    t_after = 6.0

    tr = {}
    for data_file in data_files:
        wf = Waveform()
        wf.read_from_file(data_file)
        tr[wf.station] = wf.values
        dt = wf.delta
        tdeb = wf.starttime

    list_name = sorted(tr)

    for i in range(1, len(CLUSTER) + 1):  # cluster index
        for j in range(len(CLUSTER[i])):  # first event index
            e1 = CLUSTER[i][j]
            for k in range(j + 1, len(CLUSTER[i])):  # second event index
                e2 = CLUSTER[i][k]
                co = 0
                fig = plt.figure()
                fig.set_facecolor('white')
                for l in range(len(list_name)):
                    name = list_name[l]
                    if delay[name][e1 - 1][e2 - 1] != 'NaN':
                        stack_time_1 = locs[e1 - 1]['o_time']
                        i_start_1, i_end_1 = waveval(stack_time_1, t_before,
                                                     t_after, dt, tdeb)
                        val1 = tr[name][i_start_1 - 1:i_end_1]
                        stack_time_2 = locs[e2 - 1]['o_time']
                        i_start_2, i_end_2 = \
                            waveval(stack_time_2-delay[name][e1-1][e2-1],
                                    t_before, t_after, dt, tdeb)
                        val2 = tr[name][i_start_2 - 1:i_end_2]
                        t = np.linspace(0, t_after + t_before,
                                        (t_after + t_before) / dt + 1)
                        ax = fig.add_subplot(len(list_name), 1, l + 1)
                        ax.set_axis_off()
                        ax.plot(t, val1 / max(val1), 'k')
                        ax.plot(t, val2 / max(val2), 'y--')
                        c = 'k'
                        if coeff[name][e1 - 1][e2 - 1] >= threshold:
                            co = co + 1
                            c = 'r'
                        ax.text(0.2,
                                0.5,
                                "%s, %s, %s" %
                                (name, str(coeff[name][e1 - 1][e2 - 1]),
                                 str(delay[name][e1 - 1][e2 - 1])),
                                color=c)
                fig.suptitle("Cluster : %s ; Event pair : (%s,%s) ; %d" %
                             (str(i), str(e1), str(e2), co))
                plt.show()
Example #12
0
def do_kurtogram_setup_and_run(opdict):
    """
    Run the kurtogram analysis using the parameters contained in the
    WavelocOptions.opdict.

    :param opdict: Dictionary containing the waveloc parameters and options.
    """

    base_path = opdict['base_path']

    # data
    data_dir = os.path.join(base_path, 'data', opdict['datadir'])
    data_glob = opdict['dataglob']
    data_files = glob.glob(os.path.join(data_dir, data_glob))
    data_files.sort()

    kurt_glob = opdict['kurtglob']
    kurt_files = glob.glob(os.path.join(data_dir, kurt_glob))
    kurt_files.sort()

    # output directory
    out_dir = os.path.join(base_path, 'out', opdict['outdir'])

    # location file
    locdir = os.path.join(out_dir, 'loc')
    locfile = os.path.join(locdir, 'locations.dat')
    # Read locations
    locs = read_locs_from_file(locfile)

    # create a file containing the best filtering parameters for each event and
    # each station
    kurto_file = os.path.join(out_dir, 'kurto')

    tdeb = utcdatetime.UTCDateTime(opdict['starttime'])
    tfin = utcdatetime.UTCDateTime(opdict['endtime'])

    # write filenames in a dictionary
    kurtdata = {}
    for filename in kurt_files:
        try:
            wf = Waveform()
            wf.read_from_file(filename)
            sta = wf.station
            kurtdata[sta] = filename
        except UserWarning:
            logging.info('No data around %s for file %s.' %
                         (tdeb.isoformat(), filename))

    data = {}
    for filename in data_files:
        try:
            wf = Waveform()
            wf.read_from_file(filename)
            sta = wf.station
            data[sta] = filename
        except UserWarning:
            logging.info('No data around %s for file %s.' %
                         (tdeb.isoformat(), filename))

    # ------------------------------------------------------------------------
    # Create an empty dictionnary that will contain the filtering parameters
    param = {}

    for station in sorted(data):

        wf1 = Waveform()
        wf1.read_from_file(data[station], starttime=tdeb, endtime=tfin)

        wf2 = Waveform()
        wf2.read_from_file(kurtdata[station], starttime=tdeb, endtime=tfin)

        info = {}
        info['data_file'] = data[station]
        info['station'] = station
        info['tdeb_data'] = wf1.starttime
        info['tdeb_kurt'] = wf2.starttime
        info['kurt_file'] = kurtdata[station]
        info['data_ini'] = wf1.values
        info['kurt_ini'] = wf2.values
        info['dt'] = wf1.dt
        info['filter'] = []

        logging.info('Processing station %s' % info['station'])

        if opdict['new_kurtfile']:
            new_filename = 'filt_kurtogram'
            new_kurt_filename = \
                os.path.join("%s%s" % (data[station].split(data_glob[1:])[0],
                                       new_filename))
            info['new_kurt_file'] = new_kurt_filename
            trace_kurt_fin = Waveform()
            trace_kurt_fin.read_from_file(new_kurt_filename)
            info['new_kurt'] = trace_kurt_fin.values

        for loc in locs:
            origin_time = loc['o_time']
            if opdict['verbose']:
                print "******************************************************"
                print logging.info(origin_time)

            if origin_time > tdeb and origin_time < tfin:
                info = kurto(origin_time, info, opdict)
            else:
                continue

        info['filter'] = np.matrix(info['filter'])
        sta = info['station']
        param[sta] = info['filter']

        if 'new_kurt_file' in info:
            trace_kurt_fin.values[:] = info['new_kurt']
            trace_kurt_fin.write_to_file_filled(info['new_kurt_file'],
                                                format='MSEED',
                                                fill_value=0)

    # Write the dictionnary 'param' in a binary file
    if os.path.isfile(kurto_file):
        ans = raw_input('%s file already exists. Do you really want to replace\
it ? (y or n):\n' % kurto_file)
        if ans != 'y':
            kurto_file = "%s_1" % kurto_file

    a = BinaryFile(kurto_file)
    a.write_binary_file(param)

    # read and plot the file you have just written
    read_kurtogram_frequencies(kurto_file)