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 wf.bp_filter(flower, fupper, rmean=True, taper=True) wf.write_to_file_filled(filt_name, format="MSEED") wf.process_kurtosis(100 * dt, recursive=True, post_taper=True) wf.write_to_file_filled(kurt_name, format="MSEED")
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 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 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)
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)
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 wf.bp_filter(flower, fupper, rmean=True, taper=True) wf.write_to_file_filled(filt_name, format='MSEED') wf.process_kurtosis(100 * dt, recursive=True, post_taper=True) wf.write_to_file_filled(kurt_name, format='MSEED')