Exemple #1
0
def fill_values(vals, tdeb, data_glob, data_dir, comp):
    """
    Create a dictionnary with all values for each channel of each station.
    TODO : Flesh out this doc-string.

    :param vals:
    :param tdeb:
    :param data_glob:
    :param data_dir:
    :param comp:
    """

    data_files = glob.glob(os.path.join(data_dir, data_glob))
    if len(data_files) == 0:
        data_dir = os.path.join(data_dir, comp)
    data_files = glob.glob(os.path.join(data_dir, data_glob))
    data_files.sort()

    for datafile in data_files:
        wf = Waveform()
        wf.read_from_file(datafile)
        vals[wf.station][comp] = wf.values
        tdeb[wf.station][comp] = wf.starttime
        dt = wf.delta

    return vals, tdeb, dt
Exemple #2
0
def waveform(filename):
    """
    Convenience function to read a seismogram and return its data, time-step,
    station name and start-time

    :param filename: File to read
    :type filename: string

    :returns: val, dt, name, tdeb

        * val = seismogram values
        * dt = time-step
        * name = station name
        * tdeb = start-time (UTCDateTime)

    """

    wf = Waveform()
    wf.read_from_file(filename)
    name = wf.station
    val = wf.values
    dt = wf.delta
    tdeb = wf.starttime

    return val, dt, name, tdeb
def waveforms_to_signature(base_path, datadir, dataglob, output_filename):

    sig_file = open(os.path.join(base_path, datadir, output_filename), 'w')
    allfiles = glob.glob(os.path.join(base_path, datadir, dataglob))
    for filename in sorted(allfiles):
        basename = os.path.basename(filename)
        wf = Waveform()
        wf.read_from_file(filename, format='MSEED')
        (maximum, datasum) = wf.compute_signature()
        sig_file.write("%s \t\t %.6f \t %.6f\n" % (basename, maximum, datasum))
Exemple #4
0
def waveforms_to_signature(base_path, datadir, dataglob, output_filename):

    sig_file = open(os.path.join(base_path, datadir, output_filename), "w")
    allfiles = glob.glob(os.path.join(base_path, datadir, dataglob))
    for filename in sorted(allfiles):
        basename = os.path.basename(filename)
        wf = Waveform()
        wf.read_from_file(filename, format="MSEED")
        (maximum, datasum) = wf.compute_signature()
        sig_file.write("%s \t\t %.6f \t %.6f\n" % (basename, maximum, datasum))
Exemple #5
0
def number_good_kurtosis_for_location(kurt_files,data_files,loc,time_dict,snr_limit=10.0,snr_tr_limit=10.0,sn_time=10.0):

  o_time=loc['o_time']
  stack_x=loc['x_mean']
  stack_y=loc['y_mean']
  stack_z=loc['z_mean']
  # TODO - Fix this to estimate K-time from o_time and propagation time to station
  n_good_kurt=0
  wf=Waveform()

  for ifile in xrange(len(kurt_files)):
    kfilename=kurt_files[ifile]
    dfilename=data_files[ifile]

    st=read(kfilename,headonly=True)
    staname=st.traces[0].stats.station

    if staname in time_dict.keys():
      traveltime=time_dict[staname].value_at_point(stack_x,stack_y,stack_z)
      start_time=o_time+traveltime-sn_time
      end_time=o_time+traveltime+sn_time
      try:
        wf.read_from_file(kfilename,starttime=start_time,endtime=end_time)
        snr=wf.get_snr(o_time+traveltime,start_time,end_time)

        wf.read_from_file(dfilename,starttime=start_time,endtime=end_time)
        snr_tr=wf.get_snr(o_time+traveltime,start_time,end_time)

        if snr > snr_limit and snr_tr > snr_tr_limit:
          n_good_kurt = n_good_kurt + 1
      except UserWarning:
        logging.info('No data around %s for file %s.'%(o_time.isoformat(),kfilename))

  return n_good_kurt
def number_good_kurtosis_for_location(kurt_files,
                                      data_files,
                                      loc,
                                      time_dict,
                                      snr_limit=10.0,
                                      snr_tr_limit=10.0,
                                      sn_time=10.0):

    o_time = loc['o_time']
    stack_x = loc['x_mean']
    stack_y = loc['y_mean']
    stack_z = loc['z_mean']
    # TODO - Fix this to estimate K-time from o_time and propagation time to station
    n_good_kurt = 0
    wf = Waveform()

    for ifile in xrange(len(kurt_files)):
        kfilename = kurt_files[ifile]
        dfilename = data_files[ifile]

        st = read(kfilename, headonly=True)
        staname = st.traces[0].stats.station

        if staname in time_dict.keys():
            traveltime = time_dict[staname].value_at_point(
                stack_x, stack_y, stack_z)
            start_time = o_time + traveltime - sn_time
            end_time = o_time + traveltime + sn_time
            try:
                wf.read_from_file(kfilename,
                                  starttime=start_time,
                                  endtime=end_time)
                snr = wf.get_snr(o_time + traveltime, start_time, end_time)

                wf.read_from_file(dfilename,
                                  starttime=start_time,
                                  endtime=end_time)
                snr_tr = wf.get_snr(o_time + traveltime, start_time, end_time)

                if snr > snr_limit and snr_tr > snr_tr_limit:
                    n_good_kurt = n_good_kurt + 1
            except UserWarning:
                logging.info('No data around %s for file %s.' %
                             (o_time.isoformat(), kfilename))

    return n_good_kurt
Exemple #7
0
def do_SDS_processing_setup_and_run(opdict):
    """
    Does all the processing for an SDS archive. All options are given within a
    *WavlocOptions* object. Steps are:

    * reading data
    * filtering
    * resampling if requested
    * applying kurtosis processing
    * calculating kurtosis gradient if requested
    * convolving with gaussian if requested
    * writing processed files at each stage

    :param opdict: Dictionary of options. Refers the the opdict attribute of
        *WavelocOptions* objects.
    :type opdict: dictionary

    """

    base_path = opdict['base_path']
    data_dir = os.path.join(base_path, 'data', opdict['datadir'])

    filter_c1 = opdict['c1']
    filter_c2 = opdict['c2']
    kurt_window = opdict['kwin']

    dataglob = opdict['dataglob']
    kurtglob = opdict['kurtglob']
    gradglob = opdict['gradglob']
    if opdict['gauss']:
        gaussglob = opdict['gaussglob']

    # start and end time to process
    start_time = utcdatetime.UTCDateTime(opdict['starttime'])
    end_time = utcdatetime.UTCDateTime(opdict['endtime'])

    # if have a channel file then read it
    if 'channel_file' in opdict:
        fname = os.path.join(base_path, 'lib', opdict['channel_file'])
        triplet_list = read_channel_file(fname)
    else:
        # else make triplet list from net, sta, comp lists
        triplet_list = []
        net_list = opdict['net_list'].split(',')
        sta_list = opdict['sta_list'].split(',')
        comp_list = opdict['comp_list'].split(',')
        for net in net_list:
            for sta in sta_list:
                for comp in comp_list:
                    triplet_list.append((net, sta, comp))

    # loop over data
    for net, sta, comp in triplet_list:
        full_path = os.path.join(data_dir, net, sta, "%s.D" % comp)
        logging.debug("Full path : %s" % full_path)
        if os.path.exists(full_path):

            # construct the base filename for output and create the wf object
            filt_filename = os.path.join(data_dir,
                                         "%s.%s.%s.%s.%s" %
                                         (start_time.isoformat(),
                                          net, sta, comp, dataglob[1:]))
            logging.debug("Processing to create %s" % (filt_filename))
            wf = Waveform()

            # read and process the data
            try:
                # read and filter data
                wf.read_from_SDS(data_dir, net, sta, comp,
                                 starttime=start_time, endtime=end_time)
                wf.bp_filter(filter_c1, filter_c2, rmean=True, taper=True)
                if opdict['resample']:
                    wf.resample(opdict['fs'])
                wf.write_to_file_filled(filt_filename, format='MSEED',
                                        fill_value=0)

                # do kurtosis processing
                kurt_filename = os.path.join(data_dir,
                                             "%s.%s.%s.%s.%s" %
                                             (start_time.isoformat(),
                                              net, sta, comp, kurtglob[1:]))
                logging.debug("Processing to create %s" % (kurt_filename))
                wf.process_kurtosis(kurt_window, recursive=opdict['krec'],
                                    pre_taper=True, post_taper=True)
                wf.write_to_file_filled(kurt_filename, format='MSEED',
                                        fill_value=0)

                # calculate kurtosis gradient if requested
                if opdict['kderiv']:
                    kurt_grad_filename = os.path.join(data_dir,
                                                      "%s.%s.%s.%s.%s" %
                                                      (start_time.isoformat(),
                                                       net, sta, comp,
                                                       gradglob[1:]))
                    logging.debug("Processing to create %s" %
                                  (kurt_grad_filename))
                    wf.take_positive_derivative(pre_taper=True,
                                                post_taper=True)
                    wf.write_to_file_filled(kurt_grad_filename, format='MSEED',
                                            fill_value=0)

                # do convolution with gaussian if requested
                if opdict['gauss']:
                    thres = opdict['gthreshold']
                    mu = opdict['mu']
                    sigma = opdict['sigma']
                    gauss_filename = os.path.join(data_dir,
                                                  "%s.%s.%s.%s.%s" %
                                                  (start_time.isoformat(),
                                                   net, sta, comp,
                                                   gaussglob[1:]))
                    logging.debug("Processing to create %s" % (gauss_filename))
                    wf.process_gaussian(thres, mu, sigma)
                    wf.write_to_file_filled(gauss_filename, format='MSEED',
                                            fill_value=0)

            except UserWarning:
                logging.info('No data within time limits for %s %s %s' %
                             (net, sta, comp))
def number_good_kurtosis_for_location(kurt_files,
                                      data_files,
                                      loc,
                                      time_dict,
                                      snr_limit=10.0,
                                      snr_tr_limit=10.0,
                                      sn_time=10.0):
    """
    Analyses the filtered data and the kurtosis time-series to determine the
    number of stations whose traces have sufficiently high signal-to-noise
    ratio (SNR) to be useful for the location. Both time-series need to satisfy
    the conditions for a station to be counted as contributing to the location.

    :param kurt_files: Filenames for kurtosis files. Depending on the filenames
        given in this list, the function will analyse kurtosis,
        kurtosis-gradient or gaussian waveforms.
    :param data_files: Filenames for filtered data.
    :param loc: Location dictionary for the event to be analysed
    :param time_dict: Dictionary of travel-times for the location to be
        analysed
    :param snr_limit: SNR limit for kurtosis-type data
    :param snr_limit_tr: SNR limit for filtered data
    :param snr_time: Length of time in seconds before the event for computation
        of SNR.

    :rtype: integer
    :returns: Numer of stations that have contributed to the location.
    """

    o_time = loc['o_time']
    stack_x = loc['x_mean']
    stack_y = loc['y_mean']
    stack_z = loc['z_mean']

    n_good_kurt = 0
    wf = Waveform()

    for ifile in xrange(len(kurt_files)):
        kfilename = kurt_files[ifile]
        dfilename = data_files[ifile]

        st = read(kfilename, headonly=True)
        staname = st.traces[0].stats.station

        if staname in time_dict.keys():
            traveltime = time_dict[staname].value_at_point(
                stack_x, stack_y, stack_z)
            start_time = o_time + traveltime - sn_time
            end_time = o_time + traveltime + sn_time
            try:
                wf.read_from_file(kfilename,
                                  starttime=start_time,
                                  endtime=end_time)
                snr = wf.get_snr(o_time + traveltime, start_time, end_time)

                wf.read_from_file(dfilename,
                                  starttime=start_time,
                                  endtime=end_time)
                snr_tr = wf.get_snr(o_time + traveltime, start_time, end_time)

                if snr > snr_limit and snr_tr > snr_tr_limit:
                    n_good_kurt = n_good_kurt + 1

            except UserWarning:
                logging.info('No data around %s for file %s.' %
                             (o_time.isoformat(), kfilename))

    return n_good_kurt
Exemple #9
0
def kurto(origin_time, info, opdict):
    """
    Finds for each Waveloc event and for each station the best filtering 
    parameters for kurtosis computation.
    Writes them into the dictionary info.

    :param origin_time: origin time of the signal
    :param info: dictionary of parameters
    :param opdict: dictionary of the Waveloc parameters and options

    :type origin_time: utcdatetime
    :type info: dictionary
    :type opdict: dictionary

    :rtype: dictionary
    :returns: info
    """
    verbose = opdict['verbose']
    kwin = opdict['kwin']

    start_time = origin_time - 5.0
    end_time = origin_time + 20.0
    dt = info['dt']

    # Trace
    x = waveval(info['data_ini'], start_time, end_time, dt, info['tdeb_data'])
    if not x.any() and x.all():
        return info

    # Initial kurtosis (trace filtered between 4-10Hz)
    kurtx = waveval(info['kurt_ini'], start_time, end_time, dt,
                    info['tdeb_kurt'])
    kurtx = smooth(kurtx)

    N = len(x)
    N2 = np.log2(N) - 7
    nlevel = int(np.fix(N2))

    snr_ref = np.max(np.abs(x)) / np.mean(np.abs(x))
    snr_kurt_ref = np.max(np.abs(kurtx)) / np.mean(np.abs(kurtx))
    kmax_ref = np.max(kurtx)  # maximum of the kurtosis

    # Compute the kurtogram and keep best frequencies
    if verbose:
        import matplotlib.gridspec as gridspec
        G = gridspec.GridSpec(3, 2)
        fig = plt.figure(figsize=(15, 6))
        fig.set_facecolor('white')
        fig.add_subplot(G[:, 0])

    Kwav, Level_w, freq_w, c, f_lower, f_upper = \
        Fast_Kurtogram(np.array(x, dtype=float), nlevel, verbose, Fs=1/dt,
                       opt2=1)

    # Comparison of the kurtosis computed in the new frequency band and the old
    # one (criterion : snr, kmax)
    # 1. Read the initial data
    wf = Waveform()
    wf.read_from_file(info['data_file'],
                      starttime=start_time - kwin,
                      endtime=end_time + kwin)

    nbpts = int(kwin * 1. / dt)

    # 2. Filter the trace with kurtogram frequencies
    wf.bp_filter(f_lower, f_upper)
    x_filt = wf.values
    x_filt = x_filt[nbpts:-nbpts]

    # 3. Compute the kurtosis
    wf.process_kurtosis(kwin, recursive=opdict['krec'])
    new_kurtx = wf.values
    if opdict['krec']:
        new_kurtx = new_kurtx[nbpts + 1:-nbpts - 1]
    else:
        new_kurtx = new_kurtx[:-nbpts - 1]

    snr = np.max(np.abs(x_filt)) / np.mean(np.abs(x_filt))
    snr_kurt = np.max(np.abs(new_kurtx)) / np.mean(np.abs(new_kurtx))
    kmax = np.max(new_kurtx)

    if snr > snr_ref and kmax >= kmax_ref:
        info['filter'].append(
            (round(f_lower * 100) / 100, round(f_upper * 100) / 100))
        if 'new_kurt_file' in info:
            info = write_file(info, start_time, end_time, new_kurtx)
    else:
        info['filter'].append((0, 50))

    if verbose and snr > 3:
        print "snr:", snr, " ; snr_ref:", snr_ref
        print "snr new kurtosis:", snr_kurt, " ; snr kurtosis reference:",\
            snr_kurt_ref
        print "kurtosis max, kurt_ref :", kmax, kmax_ref
        plot_trace(fig, G, x, x_filt, kurtx, new_kurtx, info, f_lower, f_upper,
                   snr, snr_ref, snr_kurt, kmax, kmax_ref, origin_time)
        plt.show()

    return info
Exemple #10
0
def do_SDS_processing_setup_and_run(opdict):
    """
    Does all the processing for an SDS archive. All options are given within a
    *WavlocOptions* object. Steps are:

    * reading data
    * filtering
    * resampling if requested
    * applying kurtosis processing
    * calculating kurtosis gradient if requested
    * convolving with gaussian if requested
    * writing processed files at each stage

    :param opdict: Dictionary of options. Refers the the opdict attribute of
        *WavelocOptions* objects.
    :type opdict: dictionary

    """

    base_path = opdict['base_path']
    data_dir = os.path.join(base_path, 'data', opdict['datadir'])

    filter_c1 = opdict['c1']
    filter_c2 = opdict['c2']
    kurt_window = opdict['kwin']

    dataglob = opdict['dataglob']
    kurtglob = opdict['kurtglob']
    gradglob = opdict['gradglob']
    if opdict['gauss']:
        gaussglob = opdict['gaussglob']

    # start and end time to process
    start_time = utcdatetime.UTCDateTime(opdict['starttime'])
    end_time = utcdatetime.UTCDateTime(opdict['endtime'])

    # if have a channel file then read it
    if 'channel_file' in opdict:
        fname = os.path.join(base_path, 'lib', opdict['channel_file'])
        triplet_list = read_channel_file(fname)
    else:
        # else make triplet list from net, sta, comp lists
        triplet_list = []
        net_list = opdict['net_list'].split(',')
        sta_list = opdict['sta_list'].split(',')
        comp_list = opdict['comp_list'].split(',')
        for net in net_list:
            for sta in sta_list:
                for comp in comp_list:
                    triplet_list.append((net, sta, comp))

    # loop over data
    for net, sta, comp in triplet_list:
        full_path = os.path.join(data_dir, net, sta, "%s.D" % comp)
        logging.debug("Full path : %s" % full_path)
        if os.path.exists(full_path):

            # construct the base filename for output and create the wf object
            filt_filename = os.path.join(
                data_dir, "%s.%s.%s.%s.%s" %
                (start_time.isoformat(), net, sta, comp, dataglob[1:]))
            logging.debug("Processing to create %s" % (filt_filename))
            wf = Waveform()

            # read and process the data
            try:
                # read and filter data
                wf.read_from_SDS(data_dir,
                                 net,
                                 sta,
                                 comp,
                                 starttime=start_time,
                                 endtime=end_time)
                wf.bp_filter(filter_c1, filter_c2, rmean=True, taper=True)
                if opdict['resample']:
                    wf.resample(opdict['fs'])
                wf.write_to_file_filled(filt_filename,
                                        format='MSEED',
                                        fill_value=0)

                # do kurtosis processing
                kurt_filename = os.path.join(
                    data_dir, "%s.%s.%s.%s.%s" %
                    (start_time.isoformat(), net, sta, comp, kurtglob[1:]))
                logging.debug("Processing to create %s" % (kurt_filename))
                wf.process_kurtosis(kurt_window,
                                    recursive=opdict['krec'],
                                    pre_taper=True,
                                    post_taper=True)
                wf.write_to_file_filled(kurt_filename,
                                        format='MSEED',
                                        fill_value=0)

                # calculate kurtosis gradient if requested
                if opdict['kderiv']:
                    kurt_grad_filename = os.path.join(
                        data_dir, "%s.%s.%s.%s.%s" %
                        (start_time.isoformat(), net, sta, comp, gradglob[1:]))
                    logging.debug("Processing to create %s" %
                                  (kurt_grad_filename))
                    wf.take_positive_derivative(pre_taper=True,
                                                post_taper=True)
                    wf.write_to_file_filled(kurt_grad_filename,
                                            format='MSEED',
                                            fill_value=0)

                # do convolution with gaussian if requested
                if opdict['gauss']:
                    thres = opdict['gthreshold']
                    mu = opdict['mu']
                    sigma = opdict['sigma']
                    gauss_filename = os.path.join(
                        data_dir,
                        "%s.%s.%s.%s.%s" % (start_time.isoformat(), net, sta,
                                            comp, gaussglob[1:]))
                    logging.debug("Processing to create %s" % (gauss_filename))
                    wf.process_gaussian(thres, mu, sigma)
                    wf.write_to_file_filled(gauss_filename,
                                            format='MSEED',
                                            fill_value=0)

            except UserWarning:
                logging.info('No data within time limits for %s %s %s' %
                             (net, sta, comp))
Exemple #11
0
logging.basicConfig(level=logging.INFO,
                    format='%(levelname)s : %(asctime)s : %(message)s')

base_path = os.getenv('WAVELOC_PATH')
test_file = os.path.join(base_path, 'test_data', 'raw_data',
                         'YA.UV15.00.HHZ.MSEED')

starttime = utcdatetime.UTCDateTime("2010-10-14T00:14:00.0Z")
endtime = utcdatetime.UTCDateTime("2010-10-14T00:18:00.0Z")

c1 = 4.0
c2 = 10.0
kwin = 3.0

logging.info('Computing full kurtosis kwin=%.2fs' % kwin)
wf_raw = Waveform()
wf_raw.read_from_file(test_file,
                      starttime=starttime,
                      endtime=endtime,
                      rmean=True,
                      taper=True)
wf_raw.bp_filter(freqmin=c1, freqmax=c2)

wf_kurt = deepcopy(wf_raw)
wf_kurt.process_kurtosis(kwin)

tr1 = np.array(wf_kurt.trace.data)
tr1 = tr1 / np.max(tr1)
tr_len = len(tr1)

misfits = []
Exemple #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)
Exemple #13
0
def kurto(origin_time, info, opdict):
    """
    Finds for each Waveloc event and for each station the best filtering 
    parameters for kurtosis computation.
    Writes them into the dictionary info.

    :param origin_time: origin time of the signal
    :param info: dictionary of parameters
    :param opdict: dictionary of the Waveloc parameters and options

    :type origin_time: utcdatetime
    :type info: dictionary
    :type opdict: dictionary

    :rtype: dictionary
    :returns: info
    """
    verbose = opdict['verbose']
    kwin = opdict['kwin']

    start_time = origin_time-5.0
    end_time = origin_time+20.0
    dt = info['dt']

    # Trace
    x = waveval(info['data_ini'], start_time, end_time, dt, info['tdeb_data'])
    if not x.any() and x.all():
        return info

    # Initial kurtosis (trace filtered between 4-10Hz)
    kurtx = waveval(info['kurt_ini'], start_time, end_time, dt,
                    info['tdeb_kurt'])
    kurtx = smooth(kurtx)

    N = len(x)
    N2 = np.log2(N)-7
    nlevel = int(np.fix(N2))

    snr_ref = np.max(np.abs(x))/np.mean(np.abs(x))
    snr_kurt_ref = np.max(np.abs(kurtx))/np.mean(np.abs(kurtx))
    kmax_ref = np.max(kurtx) # maximum of the kurtosis

    # Compute the kurtogram and keep best frequencies
    if verbose:
        import matplotlib.gridspec as gridspec
        G = gridspec.GridSpec(3, 2)
        fig = plt.figure(figsize=(15, 6))
        fig.set_facecolor('white')
        fig.add_subplot(G[:, 0])

    Kwav, Level_w, freq_w, c, f_lower, f_upper = \
        Fast_Kurtogram(np.array(x, dtype=float), nlevel, verbose, Fs=1/dt,
                       opt2=1)

    # Comparison of the kurtosis computed in the new frequency band and the old
    # one (criterion : snr, kmax)
    # 1. Read the initial data
    wf = Waveform()
    wf.read_from_file(info['data_file'], starttime=start_time-kwin,
                      endtime=end_time+kwin)

    nbpts = int(kwin*1./dt)

    # 2. Filter the trace with kurtogram frequencies
    wf.bp_filter(f_lower, f_upper)
    x_filt = wf.values
    x_filt = x_filt[nbpts:-nbpts]

    # 3. Compute the kurtosis
    wf.process_kurtosis(kwin, recursive=opdict['krec'])
    new_kurtx = wf.values
    if opdict['krec']:
        new_kurtx = new_kurtx[nbpts+1:-nbpts-1]
    else:
        new_kurtx = new_kurtx[:-nbpts-1]

    snr = np.max(np.abs(x_filt))/np.mean(np.abs(x_filt))
    snr_kurt = np.max(np.abs(new_kurtx))/np.mean(np.abs(new_kurtx))
    kmax = np.max(new_kurtx)

    if snr > snr_ref and kmax >= kmax_ref:
        info['filter'].append((round(f_lower*100)/100, round(f_upper*100)/100))
        if 'new_kurt_file' in info:
            info = write_file(info, start_time, end_time, new_kurtx)
    else:
        info['filter'].append((0, 50))

    if verbose and snr > 3:
        print "snr:", snr, " ; snr_ref:", snr_ref
        print "snr new kurtosis:", snr_kurt, " ; snr kurtosis reference:",\
            snr_kurt_ref
        print "kurtosis max, kurt_ref :", kmax, kmax_ref
        plot_trace(fig, G, x, x_filt, kurtx, new_kurtx, info, f_lower,
                   f_upper, snr, snr_ref, snr_kurt, kmax, kmax_ref,
                   origin_time)
        plt.show()

    return info
Exemple #14
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)
Exemple #15
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()
Exemple #16
0
  stack_y_err=loc[7]
  stack_z=loc[8]
  stack_z_err=loc[9]
  # set start and end time of plot
  start_time=stack_time-10.0
  end_time=stack_time+20.0

  start_time_migration=stack_time-60.0
  end_time_migration=stack_time+60.0


  # make dictionary of station names with snr ratios
  snr_start_time=stack_time-options.sn_time
  snr_end_time=stack_time+options.sn_time
  snr_dict={}
  wf=Waveform()
  for filename in kurt_files:
    wf.read_from_file(filename,starttime=snr_start_time,endtime=snr_end_time)
    snr=wf.get_snr(stack_time,snr_start_time,snr_end_time)
    station_name=wf.trace.stats.station
    snr_dict[station_name]=snr

  logging.debug("Signal to noise ratios on kurtosis")
  logging.debug(snr_dict)

  # set output filename
  plot_filename=os.path.join(loc_path,"loc_%s.png"%stack_time.isoformat())

  # select grad files for which the snr is > snr_limit
  grad_files_selected=[]
  for filename in grad_files:
Exemple #17
0
def number_good_kurtosis_for_location(kurt_files, data_files, loc, time_dict,
                                      snr_limit=10.0, snr_tr_limit=10.0,
                                      sn_time=10.0):
    """
    Analyses the filtered data and the kurtosis time-series to determine the
    number of stations whose traces have sufficiently high signal-to-noise
    ratio (SNR) to be useful for the location. Both time-series need to satisfy
    the conditions for a station to be counted as contributing to the location.

    :param kurt_files: Filenames for kurtosis files. Depending on the filenames
        given in this list, the function will analyse kurtosis,
        kurtosis-gradient or gaussian waveforms.
    :param data_files: Filenames for filtered data.
    :param loc: Location dictionary for the event to be analysed
    :param time_dict: Dictionary of travel-times for the location to be
        analysed
    :param snr_limit: SNR limit for kurtosis-type data
    :param snr_limit_tr: SNR limit for filtered data
    :param snr_time: Length of time in seconds before the event for computation
        of SNR.

    :rtype: integer
    :returns: Numer of stations that have contributed to the location.
    """

    o_time = loc['o_time']
    stack_x = loc['x_mean']
    stack_y = loc['y_mean']
    stack_z = loc['z_mean']

    n_good_kurt = 0
    wf = Waveform()

    for ifile in xrange(len(kurt_files)):
        kfilename = kurt_files[ifile]
        dfilename = data_files[ifile]

        st = read(kfilename, headonly=True)
        staname = st.traces[0].stats.station

        if staname in time_dict.keys():
            traveltime = time_dict[staname].value_at_point(stack_x, stack_y,
                                                           stack_z)
            start_time = o_time+traveltime-sn_time
            end_time = o_time+traveltime+sn_time
            try:
                wf.read_from_file(kfilename, starttime=start_time,
                                  endtime=end_time)
                snr = wf.get_snr(o_time+traveltime, start_time, end_time)

                wf.read_from_file(dfilename, starttime=start_time,
                                  endtime=end_time)
                snr_tr = wf.get_snr(o_time+traveltime, start_time, end_time)

                if snr > snr_limit and snr_tr > snr_tr_limit:
                    n_good_kurt = n_good_kurt + 1

            except UserWarning:
                logging.info('No data around %s for file %s.' %
                             (o_time.isoformat(), kfilename))

    return n_good_kurt
Exemple #18
0
import logging

logging.basicConfig(level=logging.INFO, format='%(levelname)s : %(asctime)s : %(message)s')

base_path=os.getenv('WAVELOC_PATH')
test_file=os.path.join(base_path,'test_data','raw_data','YA.UV15.00.HHZ.MSEED')

starttime=utcdatetime.UTCDateTime("2010-10-14T00:14:00.0Z")
endtime=utcdatetime.UTCDateTime("2010-10-14T00:18:00.0Z")

c1=4.0
c2=10.0
kwin=3.0

logging.info('Computing full kurtosis kwin=%.2fs'%kwin)
wf_raw=Waveform()
wf_raw.read_from_file(test_file,starttime=starttime,endtime=endtime,rmean=True,taper=True)
wf_raw.bp_filter(freqmin=c1, freqmax=c2)

wf_kurt=deepcopy(wf_raw)
wf_kurt.process_kurtosis(kwin)

tr1=np.array(wf_kurt.trace.data)
tr1 = tr1/np.max(tr1)
tr_len=len(tr1)

misfits=[]
k_factors=np.linspace(0.01,0.2,30)
print k_factors
for k_factor in k_factors:
  k_rec=kwin*k_factor
Exemple #19
0
tdeb = utcdatetime.UTCDateTime(options.starttime)
tfin = utcdatetime.UTCDateTime(options.endtime)

if options.verbose:
    logging.info(
        "\n\
  Input filename  = %s\n\
  Output basename = %s\n\
  Start time = %s\n\
  End time   = %s\n"
        % (options.data_file, options.output_file, options.starttime, options.endtime)
    )

# read waveform between time limits
wf = Waveform()
wf.read_from_file(options.data_file, starttime=tdeb, endtime=tfin)
dt = wf.delta
x = wf.values
print(wf.stream)

# set up parameters for kurtogram analysis
N = len(x)
N2 = np.log2(N) - 7
nlevel = int(np.fix(N2))
c, flower, fupper = Fast_Kurtogram(x, nlevel, options.verbose, Fs=1 / dt, opt2=1)

logging.info("Frequency band for best kurtogram : %.2f Hz - %.2f Hz" % (flower, fupper))

filt_name = "filt_%s" % options.output_file
kurt_name = "kurt_%s" % options.output_file
             help='print debugging information to stout')
(options, arguments) = p.parse_args()

tdeb = utcdatetime.UTCDateTime(options.starttime)
tfin = utcdatetime.UTCDateTime(options.endtime)

if options.verbose:
    logging.info('\n\
  Input filename  = %s\n\
  Output basename = %s\n\
  Start time = %s\n\
  End time   = %s\n' % (options.data_file, options.output_file,
                        options.starttime, options.endtime))

# read waveform between time limits
wf = Waveform()
wf.read_from_file(options.data_file, starttime=tdeb, endtime=tfin)
dt = wf.delta
x = wf.values
print(wf.stream)

# set up parameters for kurtogram analysis
N = len(x)
N2 = np.log2(N) - 7
nlevel = int(np.fix(N2))
c, flower, fupper = Fast_Kurtogram(x,
                                   nlevel,
                                   options.verbose,
                                   Fs=1 / dt,
                                   opt2=1)