def test_locations_trigger(self): self.wo.opdict['outdir']='TEST' self.wo.verify_location_options() base_path=self.wo.opdict['base_path'] test_datadir=self.wo.opdict['test_datadir'] outdir=self.wo.opdict['outdir'] exp_loc_fname = os.path.join(base_path,test_datadir,'TEST_locations.dat') exp_locs=read_locs_from_file(exp_loc_fname) do_locations_trigger_setup_and_run(self.wo.opdict) loc_fname = os.path.join(base_path,'out',outdir,'loc','locations.dat') locs=read_locs_from_file(loc_fname) self.assertEqual(len(locs),len(exp_locs)) for i in xrange(len(locs)): loc=locs[i] exp_loc=exp_locs[i] self.assertGreater(loc['o_time'] , exp_loc['o_time']-exp_loc['o_err_left']) self.assertLess(loc['o_time'] , exp_loc['o_time']+exp_loc['o_err_right']) self.assertLess(np.abs(loc['x_mean']-exp_loc['x_mean']), exp_loc['x_sigma']) self.assertLess(np.abs(loc['x_mean']-exp_loc['x_mean']), loc['x_sigma']) self.assertLess(np.abs(loc['y_mean']-exp_loc['y_mean']), exp_loc['y_sigma']) self.assertLess(np.abs(loc['y_mean']-exp_loc['y_mean']), loc['y_sigma']) self.assertLess(np.abs(loc['z_mean']-exp_loc['z_mean']), exp_loc['z_sigma']) self.assertLess(np.abs(loc['z_mean']-exp_loc['z_mean']), loc['z_sigma'])
def test_locations_prob(self): self.wo.opdict['outdir'] = 'TEST' self.wo.opdict['probloc_spaceonly'] = True self.wo.verify_location_options() base_path = self.wo.opdict['base_path'] outdir = self.wo.opdict['outdir'] loc_fname = os.path.join(base_path, 'out', outdir, 'loc', 'locations.dat') prob_fname = os.path.join(base_path, 'out', outdir, 'loc', 'locations_prob.dat') hdf5_fname = os.path.join(base_path, 'out', outdir, 'loc', 'locations_prob.hdf5') do_locations_prob_setup_and_run(self.wo.opdict) locs = read_locs_from_file(loc_fname) prob_locs = read_prob_locs_from_file(prob_fname) f_marginals = h5py.File(hdf5_fname, 'r') self.assertEqual(len(locs), len(prob_locs)) for i in xrange(len(locs)): loc = locs[i] prob_loc = prob_locs[i] self.assertGreater(prob_loc['o_time'], loc['o_time'] - loc['o_err_left']) self.assertLess(prob_loc['o_time'], loc['o_time'] + loc['o_err_right']) self.assertLess(np.abs(loc['o_time'] - prob_loc['o_time']), prob_loc['o_err']) self.assertLess(np.abs(loc['x_mean'] - prob_loc['x_mean']), prob_loc['x_sigma']) self.assertLess(np.abs(loc['y_mean'] - prob_loc['y_mean']), prob_loc['y_sigma']) self.assertLess(np.abs(loc['z_mean'] - prob_loc['z_mean']), prob_loc['z_sigma']) grp = f_marginals[prob_loc['o_time'].isoformat()] nx = grp['x'].shape[0] ny = grp['y'].shape[0] nz = grp['z'].shape[0] self.assertEqual(grp['prob_x'].shape, (nx, )) self.assertEqual(grp['prob_y'].shape, (ny, )) self.assertEqual(grp['prob_z'].shape, (nz, )) self.assertEqual(grp['prob_xy'].shape, (nx, ny)) self.assertEqual(grp['prob_xz'].shape, (nx, nz)) self.assertEqual(grp['prob_yz'].shape, (ny, nz)) # if is a 4D grid if 't' in grp: nt = grp['t'].shape[0] self.assertEqual(grp['prob_t'].shape, (nt, )) self.assertEqual(grp['prob_xt'].shape, (nx, nt)) self.assertEqual(grp['prob_yt'].shape, (ny, nt)) self.assertEqual(grp['prob_zt'].shape, (nz, nt)) f_marginals.close()
def test_locations_prob(self): self.wo.opdict['outdir'] = 'TEST' self.wo.opdict['probloc_spaceonly'] = True self.wo.verify_location_options() base_path = self.wo.opdict['base_path'] outdir = self.wo.opdict['outdir'] loc_fname = os.path.join(base_path, 'out', outdir, 'loc', 'locations.dat') prob_fname = os.path.join(base_path, 'out', outdir, 'loc', 'locations_prob.dat') hdf5_fname = os.path.join(base_path, 'out', outdir, 'loc', 'locations_prob.hdf5') do_locations_prob_setup_and_run(self.wo.opdict) locs = read_locs_from_file(loc_fname) prob_locs = read_prob_locs_from_file(prob_fname) f_marginals = h5py.File(hdf5_fname, 'r') self.assertEqual(len(locs), len(prob_locs)) for i in xrange(len(locs)): loc = locs[i] prob_loc = prob_locs[i] self.assertGreater(prob_loc['o_time'], loc['o_time']-loc['o_err_left']) self.assertLess(prob_loc['o_time'], loc['o_time']+loc['o_err_right']) self.assertLess(np.abs(loc['o_time']-prob_loc['o_time']), prob_loc['o_err']) self.assertLess(np.abs(loc['x_mean']-prob_loc['x_mean']), prob_loc['x_sigma']) self.assertLess(np.abs(loc['y_mean']-prob_loc['y_mean']), prob_loc['y_sigma']) self.assertLess(np.abs(loc['z_mean']-prob_loc['z_mean']), prob_loc['z_sigma']) grp = f_marginals[prob_loc['o_time'].isoformat()] nx = grp['x'].shape[0] ny = grp['y'].shape[0] nz = grp['z'].shape[0] self.assertEqual(grp['prob_x'].shape, (nx, )) self.assertEqual(grp['prob_y'].shape, (ny, )) self.assertEqual(grp['prob_z'].shape, (nz, )) self.assertEqual(grp['prob_xy'].shape, (nx, ny)) self.assertEqual(grp['prob_xz'].shape, (nx, nz)) self.assertEqual(grp['prob_yz'].shape, (ny, nz)) # if is a 4D grid if 't' in grp: nt = grp['t'].shape[0] self.assertEqual(grp['prob_t'].shape, (nt, )) self.assertEqual(grp['prob_xt'].shape, (nx, nt)) self.assertEqual(grp['prob_yt'].shape, (ny, nt)) self.assertEqual(grp['prob_zt'].shape, (nz, nt)) f_marginals.close()
def test_locations_trigger(self): self.wo.opdict['outdir'] = 'TEST' self.wo.verify_location_options() base_path = self.wo.opdict['base_path'] test_datadir = self.wo.opdict['test_datadir'] outdir = self.wo.opdict['outdir'] exp_loc_fname = os.path.join(base_path, test_datadir, 'TEST_locations.dat') exp_locs = read_locs_from_file(exp_loc_fname) do_locations_trigger_setup_and_run(self.wo.opdict) loc_fname = os.path.join(base_path, 'out', outdir, 'loc', 'locations.dat') locs = read_locs_from_file(loc_fname) self.assertEqual(len(locs), len(exp_locs)) for i in xrange(len(locs)): loc = locs[i] exp_loc = exp_locs[i] self.assertGreater(loc['o_time'], exp_loc['o_time'] - exp_loc['o_err_left']) self.assertLess(loc['o_time'], exp_loc['o_time'] + exp_loc['o_err_right']) self.assertLess(np.abs(loc['x_mean'] - exp_loc['x_mean']), exp_loc['x_sigma']) self.assertLess(np.abs(loc['x_mean'] - exp_loc['x_mean']), loc['x_sigma']) self.assertLess(np.abs(loc['y_mean'] - exp_loc['y_mean']), exp_loc['y_sigma']) self.assertLess(np.abs(loc['y_mean'] - exp_loc['y_mean']), loc['y_sigma']) self.assertLess(np.abs(loc['z_mean'] - exp_loc['z_mean']), exp_loc['z_sigma']) self.assertLess(np.abs(loc['z_mean'] - exp_loc['z_mean']), loc['z_sigma'])
def do_probloc_plotting_setup_and_run(opdict): """ Plot the results of a wavloc run (migration and location using probability density). All options and parameters are taken from an opdict. :param opdict: WavlocOptions.opdict that contains the options / parameters. """ # get / set info base_path = opdict['base_path'] space_only = opdict['probloc_spaceonly'] locfile = os.path.join(base_path, 'out', opdict['outdir'], 'loc', 'locations.dat') problocfile = os.path.join(base_path, 'out', opdict['outdir'], 'loc', 'locations_prob.dat') problocgrid = os.path.join(base_path, 'out', opdict['outdir'], 'loc', 'locations_prob.hdf5') figdir = os.path.join(base_path, 'out', opdict['outdir'], 'fig') # read locations locs = read_locs_from_file(locfile) prob_locs = read_prob_locs_from_file(problocfile) # open hdf5 file f = h5py.File(problocgrid, 'r') # for each loc for i in xrange(len(locs)): loc = locs[i] prob_loc = prob_locs[i] # hdf5 group name is prob loc origin time as string grp = f[prob_loc['o_time'].isoformat()] # do plotting plotProbLoc(grp, prob_loc, loc, figdir, space_only) f.close()
def do_probloc_plotting_setup_and_run(opdict): # get / set info base_path = opdict['base_path'] space_only = opdict['probloc_spaceonly'] locfile = os.path.join(base_path, 'out', opdict['outdir'], 'loc', 'locations.dat') problocfile = os.path.join(base_path, 'out', opdict['outdir'], 'loc', 'locations_prob.dat') problocgrid = os.path.join(base_path, 'out', opdict['outdir'], 'loc', 'locations_prob.hdf5') output_dir = os.path.join(base_path, 'out', opdict['outdir']) figdir = os.path.join(base_path, 'out', opdict['outdir'], 'fig') # read locations locs = read_locs_from_file(locfile) prob_locs = read_prob_locs_from_file(problocfile) # open hdf5 file f = h5py.File(problocgrid, 'r') # for each loc for i in xrange(len(locs)): loc = locs[i] prob_loc = prob_locs[i] otime = prob_loc['o_time'] # hdf5 group name is prob loc origin time as string grp = f[prob_loc['o_time'].isoformat()] # do plotting plotProbLoc(grp, prob_loc, loc, figdir, space_only) # close hdf5 file f.close()
def do_probloc_plotting_setup_and_run(opdict): # get / set info base_path=opdict['base_path'] space_only=opdict['probloc_spaceonly'] locfile=os.path.join(base_path,'out',opdict['outdir'],'loc','locations.dat') problocfile=os.path.join(base_path,'out',opdict['outdir'],'loc','locations_prob.dat') problocgrid=os.path.join(base_path,'out',opdict['outdir'],'loc','locations_prob.hdf5') output_dir=os.path.join(base_path,'out',opdict['outdir']) figdir=os.path.join(base_path,'out',opdict['outdir'],'fig') # read locations locs=read_locs_from_file(locfile) prob_locs=read_prob_locs_from_file(problocfile) # open hdf5 file f = h5py.File(problocgrid,'r') # for each loc for i in xrange(len(locs)): loc = locs[i] prob_loc = prob_locs[i] otime=prob_loc['o_time'] # hdf5 group name is prob loc origin time as string grp = f[prob_loc['o_time'].isoformat()] # do plotting plotProbLoc(grp,prob_loc,loc,figdir, space_only) # close hdf5 file f.close()
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)
def do_locations_prob_setup_and_run(opdict): # get / set info base_path = opdict['base_path'] space_only = opdict['probloc_spaceonly'] locfile = os.path.join(base_path, 'out', opdict['outdir'], 'loc', 'locations.dat') locfile_prob = os.path.join(base_path, 'out', opdict['outdir'], 'loc', 'locations_prob.dat') locfile_hdf5 = os.path.join(base_path, 'out', opdict['outdir'], 'loc', 'locations_prob.hdf5') f_prob = open(locfile_prob, 'w') # if locfile does not exist then make it by running trigger location if not os.path.exists(locfile): logging.info( 'No location found at %s. Running trigger location first...' % locfile) do_locations_trigger_setup_and_run(opdict) # directories grid_dir = os.path.join(base_path, 'out', opdict['outdir'], 'grid') output_dir = os.path.join(base_path, 'out', opdict['outdir']) # data files data_dir = os.path.join(base_path, 'data', opdict['datadir']) data_glob = opdict['dataglob'] kurt_glob = opdict['kurtglob'] grad_glob = opdict['gradglob'] data_files = glob.glob(os.path.join(data_dir, data_glob)) kurt_files = glob.glob(os.path.join(data_dir, kurt_glob)) grad_files = glob.glob(os.path.join(data_dir, grad_glob)) data_files.sort() kurt_files.sort() grad_files.sort() # stations stations_filename = os.path.join(base_path, 'lib', opdict['stations']) stations = read_stations_file(stations_filename) # grids grid_filename_base = os.path.join(base_path, 'lib', opdict['time_grid']) search_grid_filename = os.path.join(base_path, 'lib', opdict['search_grid']) # read time grid information time_grids = get_interpolated_time_grids(opdict) # read locations locs = read_locs_from_file(locfile) # prepare file for output of marginals f_marginals = h5py.File(locfile_hdf5, 'w') # iterate over locations for loc in locs: # create the appropriate grid on the fly # generate the grids o_time = loc['o_time'] if space_only: start_time = o_time end_time = o_time else: start_time = o_time - 3 * loc['o_err_left'] end_time = o_time + 3 * loc['o_err_right'] # make a buffer for migration start_time_migration = start_time - 10.0 end_time_migration = end_time + 10.0 # re-read grid info to ensure clean copy grid_info = read_hdr_file(search_grid_filename) # read data grad_dict, delta = read_data_compatible_with_time_dict( grad_files, time_grids, start_time_migration, end_time_migration) # do migration (all metadata on grid is added to grid_info) do_migration_loop_continuous(opdict, grad_dict, delta, start_time_migration, grid_info, time_grids, keep_grid=True) # integrate to get the marginal probability density distributions # get required info grid_starttime = grid_info['start_time'] nx, ny, nz, nt = grid_info['grid_shape'] dx, dy, dz, dt = grid_info['grid_spacing'] x_orig, y_orig, z_orig = grid_info['grid_orig'] # we are only interested in the time around the origin time of the event it_left = np.int(np.round((start_time - grid_starttime) / dt)) it_right = np.int(np.round((end_time - grid_starttime) / dt)) it_true = np.int(np.round((o_time - grid_starttime) / dt)) nt = (it_right - it_left) + 1 # set up integration axes (wrt reference) x = np.arange(nx) * dx y = np.arange(ny) * dy z = np.arange(nz) * dz if not space_only: t = np.arange(nt) * dt # open the grid file grid_filename = grid_info['dat_file'] f = h5py.File(grid_filename, 'r') stack_grid = f['stack_grid'] # extract the portion of interest (copy data) if space_only: stack_3D = np.empty((nx, ny, nz)) stack_3D[:] = stack_grid[:, it_true].reshape(nx, ny, nz) else: stack_4D = np.empty((nx, ny, nz, nt)) stack_4D[:] = stack_grid[:, it_left:it_right + 1].reshape( nx, ny, nz, nt) # close the grid file f.close() # Get expected values (normalizes grid internally) if space_only: exp_x, exp_y, exp_z, cov_matrix, prob_dict = \ compute_expected_coordinates3D(stack_3D,x,y,z,return_2Dgrids=True) else: exp_x, exp_y, exp_z, exp_t, cov_matrix, prob_dict = \ compute_expected_coordinates4D(stack_4D,x,y,z,t,return_2Dgrids=True) # put reference location back exp_x = exp_x + x_orig exp_y = exp_y + y_orig exp_z = exp_z + z_orig if space_only: exp_t = o_time else: exp_t = start_time + exp_t # extract uncertainties from covariance matrix if space_only: sig_x, sig_y, sig_z = np.sqrt(np.diagonal(cov_matrix)) sig_t = (loc['o_err_left'] + loc['o_err_right']) / 2. else: sig_x, sig_y, sig_z, sig_t = np.sqrt(np.diagonal(cov_matrix)) # save the marginals to a hdf5 file in loc subdirectory (f_marginals) # each event becomes a group in this one file grp = f_marginals.create_group(exp_t.isoformat()) grp.create_dataset('x', data=x + x_orig) grp.create_dataset('y', data=y + y_orig) grp.create_dataset('z', data=z + z_orig) grp.create_dataset('prob_x', data=prob_dict['prob_x0']) grp.create_dataset('prob_y', data=prob_dict['prob_x1']) grp.create_dataset('prob_z', data=prob_dict['prob_x2']) grp.create_dataset('prob_xy', data=prob_dict['prob_x0_x1']) grp.create_dataset('prob_xz', data=prob_dict['prob_x0_x2']) grp.create_dataset('prob_yz', data=prob_dict['prob_x1_x2']) if not space_only: grp.create_dataset('t', data=t - (o_time - start_time)) grp.create_dataset('prob_t', data=prob_dict['prob_x3']) grp.create_dataset('prob_xt', data=prob_dict['prob_x0_x3']) grp.create_dataset('prob_yt', data=prob_dict['prob_x1_x3']) grp.create_dataset('prob_zt', data=prob_dict['prob_x2_x3']) # write the expected values to a plain text locations file f_prob.write("PROB DENSITY : T = %s s pm %.2f s, x= %.4f pm %.4f km, \ y= %.4f pm %.4f km, z= %.4f pm %.4f km\n" % (exp_t.isoformat(), sig_t, \ exp_x, sig_x, exp_y, sig_y, exp_z, sig_z)) # close location files f_prob.close() f_marginals.close()
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()
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)
def do_locations_prob_setup_and_run(opdict): # get / set info base_path=opdict['base_path'] space_only = opdict['probloc_spaceonly'] locfile=os.path.join(base_path,'out',opdict['outdir'],'loc','locations.dat') locfile_prob=os.path.join(base_path,'out',opdict['outdir'],'loc','locations_prob.dat') locfile_hdf5=os.path.join(base_path,'out',opdict['outdir'],'loc','locations_prob.hdf5') f_prob=open(locfile_prob,'w') # if locfile does not exist then make it by running trigger location if not os.path.exists(locfile): logging.info('No location found at %s. Running trigger location first...'%locfile) do_locations_trigger_setup_and_run(opdict) # directories grid_dir=os.path.join(base_path,'out',opdict['outdir'],'grid') output_dir=os.path.join(base_path,'out',opdict['outdir']) # data files data_dir=os.path.join(base_path,'data',opdict['datadir']) data_glob=opdict['dataglob'] kurt_glob=opdict['kurtglob'] grad_glob=opdict['gradglob'] data_files=glob.glob(os.path.join(data_dir,data_glob)) kurt_files=glob.glob(os.path.join(data_dir,kurt_glob)) grad_files=glob.glob(os.path.join(data_dir,grad_glob)) data_files.sort() kurt_files.sort() grad_files.sort() # stations stations_filename=os.path.join(base_path,'lib',opdict['stations']) stations=read_stations_file(stations_filename) # grids grid_filename_base=os.path.join(base_path,'lib',opdict['time_grid']) search_grid_filename=os.path.join(base_path,'lib',opdict['search_grid']) # read time grid information time_grids=get_interpolated_time_grids(opdict) # read locations locs=read_locs_from_file(locfile) # prepare file for output of marginals f_marginals = h5py.File(locfile_hdf5,'w') # iterate over locations for loc in locs: # create the appropriate grid on the fly # generate the grids o_time=loc['o_time'] if space_only: start_time=o_time end_time =o_time else: start_time=o_time-3*loc['o_err_left'] end_time=o_time+3*loc['o_err_right'] # make a buffer for migration start_time_migration = start_time - 10.0 end_time_migration = end_time + 10.0 # re-read grid info to ensure clean copy grid_info=read_hdr_file(search_grid_filename) # read data grad_dict,delta = read_data_compatible_with_time_dict(grad_files, time_grids, start_time_migration, end_time_migration) # do migration (all metadata on grid is added to grid_info) do_migration_loop_continuous(opdict, grad_dict, delta, start_time_migration, grid_info, time_grids, keep_grid=True) # integrate to get the marginal probability density distributions # get required info grid_starttime=grid_info['start_time'] nx,ny,nz,nt=grid_info['grid_shape'] dx,dy,dz,dt=grid_info['grid_spacing'] x_orig,y_orig,z_orig=grid_info['grid_orig'] # we are only interested in the time around the origin time of the event it_left = np.int(np.round((start_time - grid_starttime)/dt)) it_right = np.int(np.round((end_time - grid_starttime)/dt)) it_true = np.int(np.round((o_time - grid_starttime)/dt)) nt=(it_right-it_left)+1 # set up integration axes (wrt reference) x=np.arange(nx)*dx y=np.arange(ny)*dy z=np.arange(nz)*dz if not space_only: t=np.arange(nt)*dt # open the grid file grid_filename=grid_info['dat_file'] f=h5py.File(grid_filename,'r') stack_grid=f['stack_grid'] # extract the portion of interest (copy data) if space_only: stack_3D=np.empty((nx,ny,nz)) stack_3D[:] = stack_grid[:,it_true].reshape(nx,ny,nz) else: stack_4D=np.empty((nx,ny,nz,nt)) stack_4D[:] = stack_grid[:,it_left:it_right+1].reshape(nx,ny,nz,nt) # close the grid file f.close() # Get expected values (normalizes grid internally) if space_only: exp_x, exp_y, exp_z, cov_matrix, prob_dict = \ compute_expected_coordinates3D(stack_3D,x,y,z,return_2Dgrids=True) else: exp_x, exp_y, exp_z, exp_t, cov_matrix, prob_dict = \ compute_expected_coordinates4D(stack_4D,x,y,z,t,return_2Dgrids=True) # put reference location back exp_x = exp_x + x_orig exp_y = exp_y + y_orig exp_z = exp_z + z_orig if space_only: exp_t = o_time else: exp_t = start_time + exp_t # extract uncertainties from covariance matrix if space_only: sig_x,sig_y,sig_z = np.sqrt(np.diagonal(cov_matrix)) sig_t = (loc['o_err_left']+loc['o_err_right'])/2. else: sig_x,sig_y,sig_z,sig_t = np.sqrt(np.diagonal(cov_matrix)) # save the marginals to a hdf5 file in loc subdirectory (f_marginals) # each event becomes a group in this one file grp = f_marginals.create_group(exp_t.isoformat()) grp.create_dataset('x',data=x+x_orig) grp.create_dataset('y',data=y+y_orig) grp.create_dataset('z',data=z+z_orig) grp.create_dataset('prob_x',data=prob_dict['prob_x0']) grp.create_dataset('prob_y',data=prob_dict['prob_x1']) grp.create_dataset('prob_z',data=prob_dict['prob_x2']) grp.create_dataset('prob_xy',data=prob_dict['prob_x0_x1']) grp.create_dataset('prob_xz',data=prob_dict['prob_x0_x2']) grp.create_dataset('prob_yz',data=prob_dict['prob_x1_x2']) if not space_only: grp.create_dataset('t',data=t-(o_time - start_time)) grp.create_dataset('prob_t',data=prob_dict['prob_x3']) grp.create_dataset('prob_xt',data=prob_dict['prob_x0_x3']) grp.create_dataset('prob_yt',data=prob_dict['prob_x1_x3']) grp.create_dataset('prob_zt',data=prob_dict['prob_x2_x3']) # write the expected values to a plain text locations file f_prob.write("PROB DENSITY : T = %s s pm %.2f s, x= %.4f pm %.4f km, \ y= %.4f pm %.4f km, z= %.4f pm %.4f km\n" % (exp_t.isoformat(), sig_t, \ exp_x, sig_x, exp_y, sig_y, exp_z, sig_z)) # close location files f_prob.close() f_marginals.close()
def do_plotting_setup_and_run(opdict,plot_wfm=True,plot_grid=True): # get / set info base_path=opdict['base_path'] locfile=os.path.join(base_path,'out',opdict['outdir'],'loc','locations.dat') stackfile=os.path.join(base_path,'out',opdict['outdir'],'stack','combined_stack_all.hdf5') grid_dir=os.path.join(base_path,'out',opdict['outdir'],'grid') output_dir=os.path.join(base_path,'out',opdict['outdir']) 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() mig_files=kurt_files if opdict['kderiv']: grad_glob=opdict['gradglob'] grad_files=glob.glob(os.path.join(data_dir,grad_glob)) grad_files.sort() mig_files=grad_files if opdict['gauss']: gauss_glob=opdict['gaussglob'] gauss_files=glob.glob(os.path.join(data_dir,gauss_glob)) gauss_files.sort() mig_files=gauss_files figdir=os.path.join(base_path,'out',opdict['outdir'],'fig') # stations stations_filename=os.path.join(base_path,'lib',opdict['stations']) stations=read_stations_file(stations_filename) # grids grid_filename_base=os.path.join(base_path,'lib',opdict['time_grid']) search_grid_filename=os.path.join(base_path,'lib',opdict['search_grid']) # read time grid information time_grids=get_interpolated_time_grids(opdict) # read locations locs=read_locs_from_file(locfile) # open stack file f_stack=h5py.File(stackfile,'r') max_val=f_stack['max_val_smooth'] stack_start_time=UTCDateTime(max_val.attrs['start_time']) for loc in locs: # generate the grids o_time=loc['o_time'] start_time=o_time-opdict['plot_tbefore'] end_time=o_time+opdict['plot_tafter'] # re-read grid info to ensure clean copy grid_info=read_hdr_file(search_grid_filename) nx=grid_info['nx'] ny=grid_info['ny'] nz=grid_info['nz'] dx=grid_info['dx'] dy=grid_info['dy'] dz=grid_info['dz'] x=loc['x_mean'] y=loc['y_mean'] z=loc['z_mean'] # get the corresponding travel-times for time-shifting ttimes={} for sta in time_grids.keys(): ttimes[sta]=time_grids[sta].value_at_point(x,y,z) tshift_migration=max(ttimes.values()) start_time_migration=start_time-tshift_migration end_time_migration=end_time+tshift_migration if plot_grid: logging.info('Plotting grid for location %s'%o_time.isoformat()) # TODO implement a rough estimation of the stack shift based on propagation time across the whole network # read data mig_dict,delta = read_data_compatible_with_time_dict(mig_files, time_grids, start_time_migration, end_time_migration) # do migration do_migration_loop_continuous(opdict, mig_dict, delta, start_time_migration, grid_info, time_grids, keep_grid=True) # plot plotLocationGrid(loc,grid_info,figdir,opdict['plot_otime_window']) if plot_wfm: logging.info('Plotting waveforms for location %s'%o_time.isoformat()) # get the index of the location # ix=np.int(np.round((loc['x_mean']-grid_info['x_orig'])/dx)) # iy=np.int(np.round((loc['y_mean']-grid_info['y_orig'])/dy)) # iz=np.int(np.round((loc['z_mean']-grid_info['z_orig'])/dz)) # ib= ix*ny*nz + iy*nz + iz # read data data_dict,delta = read_data_compatible_with_time_dict(data_files, time_grids, start_time_migration, end_time_migration) mig_dict,delta = read_data_compatible_with_time_dict(mig_files, time_grids, start_time_migration, end_time_migration) # cut desired portion out of data for sta in data_dict.keys(): tmp=data_dict[sta] istart=np.int(np.round( (start_time + ttimes[sta] - start_time_migration) / delta)) iend=istart + np.int(np.round( (opdict['plot_tbefore'] + opdict['plot_tafter']) / delta)) # sanity check in case event is close to start or end of data if istart < 0 : istart=0 if iend > len(tmp) : iend = len(tmp) data_dict[sta]=tmp[istart:iend] # do slice tmp=mig_dict[sta] mig_dict[sta]=tmp[istart:iend] # retrieve relevant portion of stack max istart=np.int(np.round( (o_time - opdict['plot_tbefore'] -stack_start_time) / delta)) iend=istart + np.int(np.round( (opdict['plot_tbefore'] + opdict['plot_tafter']) / delta)) # sanity check in case event is close to start or end of data if istart < 0 : start_time = start_time + np.abs(istart)*dt istart=0 if iend > len(max_val) : iend = len(max_val) # do slice stack_wfm=max_val[istart:iend] # plot plotLocationWaveforms(loc,start_time,delta,data_dict,mig_dict,stack_wfm,figdir) f_stack.close()
def do_comp_mag(opdict): base_path=opdict['base_path'] # dataless dataless_glob=glob.glob(os.path.join(base_path,'lib',opdict['dataless'])) dataless_glob.sort() # 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'] # location file locdir=os.path.join(base_path,'out',opdict['outdir'],'loc') locfile=os.path.join(locdir,'locations.dat') locs=read_locs_from_file(locfile) opdict=read_header_from_file(locfile,opdict) snr_wf=np.float(opdict['snr_tr_limit']) # Stations stations_filename=os.path.join(base_path,'lib',opdict['stations']) stations=read_stations_file(stations_filename) paz=read_paz(dataless_glob) vals,tdeb={},{} for sta in sorted(stations): vals[sta]={} tdeb[sta]={} cha_list=opdict['comp_list'] for cha in cha_list: vals,tdeb,dt=fill_values(vals,tdeb,data_glob,data_dir,cha) new_file=open(locfile,'w') write_header_options(new_file,opdict) mags=[] for loc in locs: stack_time=loc['o_time'] loc_x=loc['x_mean'] loc_y=loc['y_mean'] loc_z=-loc['z_mean'] ml=[] for sta in sorted(stations): if vals[sta]: paz_list,p2p_amp,tspan=[],[],[] h_dist=np.sqrt((loc_x-stations[sta]['x'])**2+(loc_y-stations[sta]['y'])**2+(loc_z-stations[sta]['elev'])**2) for cha in cha_list: istart=int(round(stack_time-0.5-tdeb[sta][cha])*1./dt) iend=int(round(stack_time+5.5-tdeb[sta][cha])*1./dt) x=vals[sta][cha][istart:iend+1] if x.any() and np.max(x)/np.mean(np.abs(x)) > snr_wf: max_amp=np.max(x) i_max_amp=np.argmax(x) min_amp=np.abs(np.min(x)) i_min_amp=np.argmin(x) paz_list.append(paz[sta][cha]) tspan.append(np.abs(i_max_amp-i_min_amp)*dt) p2p_amp.append(max_amp+min_amp) if opdict['verbose']: fig=plt.figure() fig.set_facecolor('white') plt.plot(x) plt.plot(i_min_amp,x[i_min_amp],'ro') plt.plot(i_max_amp,x[i_max_amp],'ro') plt.title('%s,%s'%(sta,cha)) plt.show() if paz_list: mag=estimateMagnitude(paz_list,p2p_amp,tspan,h_dist) ml.append(mag) new_file.write("Max = %.2f, %s - %.2f s + %.2f s, x= %.4f pm %.4f km, y= %.4f pm %.4f km, z= %.4f pm %.4f km, ml= %.2f pm %.2f\n"%(loc['max_trig'],loc['o_time'].isoformat(),loc['o_err_left'], loc['o_err_right'],loc['x_mean'],loc['x_sigma'],loc['y_mean'],loc['y_sigma'],loc['z_mean'],loc['z_sigma'],np.mean(ml),np.std(ml))) if ml: mags.append(np.mean(ml)) new_file.close() r=np.arange(-3,3,0.1) p,logN,i1,i2 = bvalue(mags,r) print "b-value:",-p[0] if opdict['verbose']: fig=plt.figure(figsize=(10,5)) fig.set_facecolor('white') ax1 = fig.add_subplot(121) ax1.hist(mags,25) ax1.set_xlabel('Magnitude') ax2 = fig.add_subplot(122,title='Gutenberg Richter law') ax2.plot(r,logN) ax2.plot(r[i1:i2],np.polyval(p,r[i1:i2]),'r') ax2.set_xlabel('Magnitude') ax2.set_ylabel('log N') plt.show()
def do_plotting_setup_and_run(opdict, plot_wfm=True, plot_grid=True): # get / set info base_path = opdict['base_path'] locfile = os.path.join(base_path, 'out', opdict['outdir'], 'loc', 'locations.dat') stackfile = os.path.join(base_path, 'out', opdict['outdir'], 'stack', 'combined_stack_all.hdf5') grid_dir = os.path.join(base_path, 'out', opdict['outdir'], 'grid') output_dir = os.path.join(base_path, 'out', opdict['outdir']) 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() mig_files = kurt_files if opdict['kderiv']: grad_glob = opdict['gradglob'] grad_files = glob.glob(os.path.join(data_dir, grad_glob)) grad_files.sort() mig_files = grad_files if opdict['gauss']: gauss_glob = opdict['gaussglob'] gauss_files = glob.glob(os.path.join(data_dir, gauss_glob)) gauss_files.sort() mig_files = gauss_files figdir = os.path.join(base_path, 'out', opdict['outdir'], 'fig') # stations stations_filename = os.path.join(base_path, 'lib', opdict['stations']) stations = read_stations_file(stations_filename) # grids grid_filename_base = os.path.join(base_path, 'lib', opdict['time_grid']) search_grid_filename = os.path.join(base_path, 'lib', opdict['search_grid']) # read time grid information time_grids = get_interpolated_time_grids(opdict) # read locations locs = read_locs_from_file(locfile) # open stack file f_stack = h5py.File(stackfile, 'r') max_val = f_stack['max_val_smooth'] stack_start_time = UTCDateTime(max_val.attrs['start_time']) for loc in locs: # generate the grids o_time = loc['o_time'] start_time = o_time - opdict['plot_tbefore'] end_time = o_time + opdict['plot_tafter'] # re-read grid info to ensure clean copy grid_info = read_hdr_file(search_grid_filename) nx = grid_info['nx'] ny = grid_info['ny'] nz = grid_info['nz'] dx = grid_info['dx'] dy = grid_info['dy'] dz = grid_info['dz'] x = loc['x_mean'] y = loc['y_mean'] z = loc['z_mean'] # get the corresponding travel-times for time-shifting ttimes = {} for sta in time_grids.keys(): ttimes[sta] = time_grids[sta].value_at_point(x, y, z) tshift_migration = max(ttimes.values()) start_time_migration = start_time - tshift_migration end_time_migration = end_time + tshift_migration if plot_grid: logging.info('Plotting grid for location %s' % o_time.isoformat()) # TODO implement a rough estimation of the stack shift based on propagation time across the whole network # read data mig_dict, delta = read_data_compatible_with_time_dict( mig_files, time_grids, start_time_migration, end_time_migration) # do migration do_migration_loop_continuous(opdict, mig_dict, delta, start_time_migration, grid_info, time_grids, keep_grid=True) # plot plotLocationGrid(loc, grid_info, figdir, opdict['plot_otime_window']) if plot_wfm: logging.info('Plotting waveforms for location %s' % o_time.isoformat()) # get the index of the location # ix=np.int(np.round((loc['x_mean']-grid_info['x_orig'])/dx)) # iy=np.int(np.round((loc['y_mean']-grid_info['y_orig'])/dy)) # iz=np.int(np.round((loc['z_mean']-grid_info['z_orig'])/dz)) # ib= ix*ny*nz + iy*nz + iz # read data data_dict, delta = read_data_compatible_with_time_dict( data_files, time_grids, start_time_migration, end_time_migration) mig_dict, delta = read_data_compatible_with_time_dict( mig_files, time_grids, start_time_migration, end_time_migration) # cut desired portion out of data for sta in data_dict.keys(): tmp = data_dict[sta] istart = np.int( np.round( (start_time + ttimes[sta] - start_time_migration) / delta)) iend = istart + np.int( np.round((opdict['plot_tbefore'] + opdict['plot_tafter']) / delta)) # sanity check in case event is close to start or end of data if istart < 0: istart = 0 if iend > len(tmp): iend = len(tmp) data_dict[sta] = tmp[istart:iend] # do slice tmp = mig_dict[sta] mig_dict[sta] = tmp[istart:iend] # retrieve relevant portion of stack max istart = np.int( np.round((o_time - opdict['plot_tbefore'] - stack_start_time) / delta)) iend = istart + np.int( np.round( (opdict['plot_tbefore'] + opdict['plot_tafter']) / delta)) # sanity check in case event is close to start or end of data if istart < 0: start_time = start_time + np.abs(istart) * dt istart = 0 if iend > len(max_val): iend = len(max_val) # do slice stack_wfm = max_val[istart:iend] # plot plotLocationWaveforms(loc, start_time, delta, data_dict, mig_dict, stack_wfm, figdir) f_stack.close()
def do_correlation_setup_and_run(opdict): """ Runs correlation using options contained in a WavelocOptions.opdict dictionary. Explores and cross-correlates all possible event pairs of the Waveloc location file at all stations. Writes the correlation coefficients and time delays in 2-D numpy arrays for each station and saves the final dictionaries into 2 binary files. The correlation first takes place in the time domain. If the correlation value is over a given threshold, the correlation is also performed in the frequency domain so that a subsample precision can be obtained on the time delay. """ base_path = opdict['base_path'] verbose = opdict['verbose'] # 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') locfile = os.path.join(locdir, 'locations.dat') # file containing correlation values coeff_file = os.path.join(locdir, opdict['xcorr_corr']) # file containing time delays delay_file = os.path.join(locdir, opdict['xcorr_delay']) # threshold and time window threshold = float(opdict['xcorr_threshold']) t_before = float(opdict['xcorr_before']) t_after = float(opdict['xcorr_after']) # Read location locs = read_locs_from_file(locfile) # Create 2 empty dictionnaries coeff = {} delay = {} # ------------------------------------------------------------------------ # MAIN PROGRAM : Compute the correlation value and the time delay for each # possible event pair tref = time.time() for file in data_files: event = 0 val_all, dt, name, tdeb = waveform(file) logging.info(name) coeff[name] = [] delay[name] = [] for loc_a in locs: event = event+1 stack_time = loc_a['o_time']-tdeb start_time = int(round((stack_time-t_before)*1./dt)) end_time = int(round((stack_time+t_after)*1./dt)) val1 = val_all[start_time-1:end_time] compteur = event-1 liste, list_tau = [], [] # Add zeros to liste as we only compute a semi matrix liste.extend(np.zeros(event-1)) list_tau.extend(np.zeros(event-1)) # for every event occurring after the event "event" ; replace by # event-1 if the autocorrelation is considered for loc_b in locs[event-1:]: compteur = compteur+1 stack_time_2 = loc_b['o_time']-tdeb start_time_2 = int((stack_time_2-t_before)*1./dt) end_time_2 = int((stack_time_2+t_after)*1./dt) val2 = val_all[start_time_2-1:end_time_2] if not val1.any() or not val2.any(): liste.append('NaN') list_tau.append('NaN') else: tau, value = correlate(val1, val2, dt, verbose, 't') if value > threshold and event != compteur: ntau = int(round(tau*1./dt)) val3 = val_all[start_time_2-ntau-1:end_time_2-ntau] tau_t = tau tau = correlate(val1, val3, dt, verbose, 'f') tau = tau_t+tau if verbose: tau_f = tau plot_waveform(val1, val2, dt, [tau_t, tau_f], event, compteur) print "time: %.4f, %.4f" % (value, tau_t) print "frequency : %.4f, %.4f" % (value, tau_f) print "final delay : %.4f" % tau plt.show() liste.append(round(value*10**2)/10**2) if tau.size: list_tau.append(round(tau*10**4)/10**4) else: list_tau.append('NaN') coeff[name].append(liste) delay[name].append(list_tau) # finished run, print timing info print "Elapsed time: ", time.time()-tref # Save the results in 2 binary files logging.info("Saving coeff and delay files") a = BinaryFile(coeff_file) a.write_binary_file(coeff) b = BinaryFile(delay_file) b.write_binary_file(delay)
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_plotting_setup_and_run(opdict, plot_wfm=True, plot_grid=True): """ Plot the results of a wavloc run (migration and location). All options and parameters are taken from an opdict. :param opdict: WavlocOptions.opdict that contains the options / parameters. :param plot_wfm: If ``True`` plots waveforms after location (filtered data and kurtosis). :param plot_grid: If ``True``plots the migration grid. :type plot_wfm: boolean :type plot_grid: boolean """ # get / set info base_path = opdict['base_path'] locfile = os.path.join(base_path, 'out', opdict['outdir'], 'loc', 'locations.dat') stackfile = os.path.join(base_path, 'out', opdict['outdir'], 'stack', 'combined_stack_all.hdf5') 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() mig_files = kurt_files if opdict['kderiv']: grad_glob = opdict['gradglob'] grad_files = glob.glob(os.path.join(data_dir, grad_glob)) grad_files.sort() mig_files = grad_files if opdict['gauss']: gauss_glob = opdict['gaussglob'] gauss_files = glob.glob(os.path.join(data_dir, gauss_glob)) gauss_files.sort() mig_files = gauss_files figdir = os.path.join(base_path, 'out', opdict['outdir'], 'fig') # grids search_grid_filename = os.path.join(base_path, 'lib', opdict['search_grid']) # read time grid information time_grids = get_interpolated_time_grids(opdict) # read locations locs = read_locs_from_file(locfile) # open stack file f_stack = h5py.File(stackfile, 'r') max_val = f_stack['max_val_smooth'] stack_start_time = UTCDateTime(max_val.attrs['start_time']) for loc in locs: # generate the grids o_time = loc['o_time'] start_time = o_time-opdict['plot_tbefore'] end_time = o_time+opdict['plot_tafter'] # re-read grid info to ensure clean copy grid_info = read_hdr_file(search_grid_filename) x = loc['x_mean'] y = loc['y_mean'] z = loc['z_mean'] # get the corresponding travel-times for time-shifting ttimes = {} for sta in time_grids.keys(): ttimes[sta] = time_grids[sta].value_at_point(x, y, z) tshift_migration = max(ttimes.values()) start_time_migration = start_time-tshift_migration end_time_migration = end_time+tshift_migration if plot_grid: logging.info('Plotting grid for location %s' % o_time.isoformat()) # read data mig_dict, delta = \ read_data_compatible_with_time_dict(mig_files, time_grids, start_time_migration, end_time_migration) # do migration do_migration_loop_continuous(opdict, mig_dict, delta, start_time_migration, grid_info, time_grids, keep_grid=True) # plot plotLocationGrid(loc, grid_info, figdir, opdict['plot_otime_window']) if plot_wfm: logging.info('Plotting waveforms for location %s' % o_time.isoformat()) # read data data_dict, delta = \ read_data_compatible_with_time_dict(data_files, time_grids, start_time_migration, end_time_migration) mig_dict, delta = \ read_data_compatible_with_time_dict(mig_files, time_grids, start_time_migration, end_time_migration) # cut desired portion out of data for sta in data_dict.keys(): tmp = data_dict[sta] # alignment on origin time istart = np.int(np.round((start_time + ttimes[sta] - start_time_migration) / delta)) iend = istart + np.int(np.round((opdict['plot_tbefore'] + opdict['plot_tafter']) / delta)) # sanity check in case event is close to start or end of data if istart < 0: istart = 0 if iend > len(tmp): iend = len(tmp) data_dict[sta] = tmp[istart:iend] # do slice tmp = mig_dict[sta] mig_dict[sta] = tmp[istart:iend] # retrieve relevant portion of stack max istart = np.int(np.round((o_time - opdict['plot_tbefore'] - stack_start_time) / delta)) iend = istart + np.int(np.round((opdict['plot_tbefore'] + opdict['plot_tafter']) / delta)) # sanity check in case event is close to start or end of data if istart < 0: start_time = start_time + np.abs(istart)*delta istart = 0 if iend > len(max_val): iend = len(max_val) # do slice stack_wfm = max_val[istart:iend] # plot plotLocationWaveforms(loc, start_time, delta, data_dict, mig_dict, stack_wfm, figdir) f_stack.close()
def do_locations_prob_setup_and_run(opdict): """ Setup and run probability-based locations on migration grids. Takes all parameters from WavelocOptions.opdict. :param opdict: Parameters and options for Waveloc. """ # get / set info base_path = opdict["base_path"] space_only = opdict["probloc_spaceonly"] locfile = os.path.join(base_path, "out", opdict["outdir"], "loc", "locations.dat") locfile_prob = os.path.join(base_path, "out", opdict["outdir"], "loc", "locations_prob.dat") locfile_hdf5 = os.path.join(base_path, "out", opdict["outdir"], "loc", "locations_prob.hdf5") f_prob = open(locfile_prob, "w") # if locfile does not exist then make it by running trigger location if not os.path.exists(locfile): logging.info( "No location found at %s. Running trigger location \ first..." % locfile ) do_locations_trigger_setup_and_run(opdict) # data files data_dir = os.path.join(base_path, "data", opdict["datadir"]) data_glob = opdict["dataglob"] kurt_glob = opdict["kurtglob"] grad_glob = opdict["gradglob"] data_files = glob.glob(os.path.join(data_dir, data_glob)) kurt_files = glob.glob(os.path.join(data_dir, kurt_glob)) grad_files = glob.glob(os.path.join(data_dir, grad_glob)) data_files.sort() kurt_files.sort() grad_files.sort() # grids search_grid_filename = os.path.join(base_path, "lib", opdict["search_grid"]) # read time grid information time_grids = get_interpolated_time_grids(opdict) # read locations locs = read_locs_from_file(locfile) # prepare file for output of marginals f_marginals = h5py.File(locfile_hdf5, "w") # iterate over locations for loc in locs: # create the appropriate grid on the fly # generate the grids o_time = loc["o_time"] if space_only: start_time = o_time end_time = o_time else: start_time = o_time - 3 * loc["o_err_left"] end_time = o_time + 3 * loc["o_err_right"] # make a buffer for migration start_time_migration = start_time - 10.0 end_time_migration = end_time + 10.0 # re-read grid info to ensure clean copy grid_info = read_hdr_file(search_grid_filename) # read data grad_dict, delta = read_data_compatible_with_time_dict( grad_files, time_grids, start_time_migration, end_time_migration ) # do migration (all metadata on grid is added to grid_info) do_migration_loop_continuous( opdict, grad_dict, delta, start_time_migration, grid_info, time_grids, keep_grid=True ) # integrate to get the marginal probability density distributions # get required info grid_starttime = grid_info["start_time"] nx, ny, nz, nt = grid_info["grid_shape"] dx, dy, dz, dt = grid_info["grid_spacing"] x_orig, y_orig, z_orig = grid_info["grid_orig"] # we are only interested in the time around the origin time of the # event it_left = np.int(np.round((start_time - grid_starttime) / dt)) it_right = np.int(np.round((end_time - grid_starttime) / dt)) it_true = np.int(np.round((o_time - grid_starttime) / dt)) nt = (it_right - it_left) + 1 # set up integration axes (wrt reference) x = np.arange(nx) * dx y = np.arange(ny) * dy z = np.arange(nz) * dz if not space_only: t = np.arange(nt) * dt # open the grid file grid_filename = grid_info["dat_file"] f = h5py.File(grid_filename, "r") stack_grid = f["stack_grid"] # extract the portion of interest (copy data) if space_only: stack_3D = np.empty((nx, ny, nz)) stack_3D[:] = stack_grid[:, it_true].reshape(nx, ny, nz) else: stack_4D = np.empty((nx, ny, nz, nt)) stack_4D[:] = stack_grid[:, it_left : it_right + 1].reshape(nx, ny, nz, nt) # close the grid file f.close() # Get expected values (normalizes grid internally) if space_only: exp_x, exp_y, exp_z, cov_matrix, prob_dict = compute_expected_coordinates3D( stack_3D, x, y, z, return_2Dgrids=True ) else: exp_x, exp_y, exp_z, exp_t, cov_matrix, prob_dict = compute_expected_coordinates4D( stack_4D, x, y, z, t, return_2Dgrids=True ) # put reference location back exp_x = exp_x + x_orig exp_y = exp_y + y_orig exp_z = exp_z + z_orig if space_only: exp_t = o_time else: exp_t = start_time + exp_t # extract uncertainties from covariance matrix if space_only: sig_x, sig_y, sig_z = np.sqrt(np.diagonal(cov_matrix)) sig_t = (loc["o_err_left"] + loc["o_err_right"]) / 2.0 else: sig_x, sig_y, sig_z, sig_t = np.sqrt(np.diagonal(cov_matrix)) # save the marginals to a hdf5 file in loc subdirectory (f_marginals) # each event becomes a group in this one file grp = f_marginals.create_group(exp_t.isoformat()) grp.create_dataset("x", data=x + x_orig) grp.create_dataset("y", data=y + y_orig) grp.create_dataset("z", data=z + z_orig) grp.create_dataset("prob_x", data=prob_dict["prob_x0"]) grp.create_dataset("prob_y", data=prob_dict["prob_x1"]) grp.create_dataset("prob_z", data=prob_dict["prob_x2"]) grp.create_dataset("prob_xy", data=prob_dict["prob_x0_x1"]) grp.create_dataset("prob_xz", data=prob_dict["prob_x0_x2"]) grp.create_dataset("prob_yz", data=prob_dict["prob_x1_x2"]) if not space_only: grp.create_dataset("t", data=t - (o_time - start_time)) grp.create_dataset("prob_t", data=prob_dict["prob_x3"]) grp.create_dataset("prob_xt", data=prob_dict["prob_x0_x3"]) grp.create_dataset("prob_yt", data=prob_dict["prob_x1_x3"]) grp.create_dataset("prob_zt", data=prob_dict["prob_x2_x3"]) # write the expected values to a plain text locations file f_prob.write( "PROB DENSITY : T = %s s pm %.2f s, x= %.4f pm %.4f km, \ y= %.4f pm %.4f km, z= %.4f pm %.4f km\n" % (exp_t.isoformat(), sig_t, exp_x, sig_x, exp_y, sig_y, exp_z, sig_z) ) # close location files f_prob.close() f_marginals.close()
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()
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)
def do_correlation_setup_and_run(opdict): base_path = opdict['base_path'] verbose = opdict['verbose'] # 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') locfile = os.path.join(locdir, 'locations.dat') # file containing correlation values coeff_file = os.path.join(locdir, opdict['xcorr_corr']) # file containing time delays delay_file = os.path.join(locdir, opdict['xcorr_delay']) # threshold and time window threshold = float(opdict['xcorr_threshold']) t_before = float(opdict['xcorr_before']) t_after = float(opdict['xcorr_after']) # Read location locs = read_locs_from_file(locfile) # Create 2 empty dictionnaries coeff = {} delay = {} # -------------------------------------------------------------------------------------------- # MAIN PROGRAM : Compute the correlation value and the time delay for each possible event pair tref = time.time() for file in data_files: event = 0 val_all, dt, name, tdeb = waveform(file) logging.info(name) coeff[name] = [] delay[name] = [] for loc_a in locs: event = event + 1 stack_time = loc_a['o_time'] - tdeb start_time = int(round((stack_time - t_before) * 1. / dt)) end_time = int(round((stack_time + t_after) * 1. / dt)) val1 = val_all[start_time - 1:end_time] compteur = event - 1 liste, list_tau = [], [] # Add zeros to liste as we only compute a semi matrix liste.extend(np.zeros(event - 1)) list_tau.extend(np.zeros(event - 1)) for loc_b in locs[ event - 1:]: # for every event occurring after the event "event" ; replace by event-1 if the autocorrelation is considered compteur = compteur + 1 stack_time_2 = loc_b['o_time'] - tdeb start_time_2 = int((stack_time_2 - t_before) * 1. / dt) end_time_2 = int((stack_time_2 + t_after) * 1. / dt) val2 = val_all[start_time_2 - 1:end_time_2] if not val1.any() or not val2.any(): liste.append('NaN') list_tau.append('NaN') else: tau, value = correlate(val1, val2, dt, verbose, 't') if value > threshold and event != compteur: ntau = int(round(tau * 1. / dt)) val3 = val_all[start_time_2 - ntau - 1:end_time_2 - ntau] tau_t = tau tau = correlate(val1, val3, dt, verbose, 'f') tau = tau_t + tau if verbose: tau_f = tau plot_waveform(val1, val2, dt, [tau_t, tau_f], event, compteur) print "time: %.4f, %.4f" % (value, tau_t) print "frequency : %.4f, %.4f" % (value, tau_f) print "final delay : %.4f" % tau plt.show() liste.append(round(value * 10**2) / 10**2) if tau.size: list_tau.append(round(tau * 10**4) / 10**4) else: list_tau.append('NaN') coeff[name].append(liste) delay[name].append(list_tau) print "Elapsed time: ", time.time() - tref # Save the results in 2 binary files logging.info("Saving coeff and delay files") a = BinaryFile(coeff_file) a.write_binary_file(coeff) b = BinaryFile(delay_file) b.write_binary_file(delay)
def do_comp_mag(opdict): base_path = opdict['base_path'] # dataless dataless_glob = glob.glob( os.path.join(base_path, 'lib', opdict['dataless'])) dataless_glob.sort() # 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'] # location file locdir = os.path.join(base_path, 'out', opdict['outdir'], 'loc') locfile = os.path.join(locdir, 'locations.dat') locs = read_locs_from_file(locfile) opdict = read_header_from_file(locfile, opdict) snr_wf = np.float(opdict['snr_tr_limit']) # Stations stations_filename = os.path.join(base_path, 'lib', opdict['stations']) stations = read_stations_file(stations_filename) paz = read_paz(dataless_glob) vals, tdeb = {}, {} for sta in sorted(stations): vals[sta] = {} tdeb[sta] = {} cha_list = opdict['comp_list'] for cha in cha_list: vals, tdeb, dt = fill_values(vals, tdeb, data_glob, data_dir, cha) new_file = open(locfile, 'w') write_header_options(new_file, opdict) mags = [] for loc in locs: stack_time = loc['o_time'] loc_x = loc['x_mean'] loc_y = loc['y_mean'] loc_z = -loc['z_mean'] ml = [] for sta in sorted(stations): if vals[sta]: paz_list, p2p_amp, tspan = [], [], [] h_dist = np.sqrt((loc_x - stations[sta]['x'])**2 + (loc_y - stations[sta]['y'])**2 + (loc_z - stations[sta]['elev'])**2) for cha in cha_list: istart = int( round(stack_time - 0.5 - tdeb[sta][cha]) * 1. / dt) iend = int( round(stack_time + 5.5 - tdeb[sta][cha]) * 1. / dt) x = vals[sta][cha][istart:iend + 1] if x.any() and np.max(x) / np.mean(np.abs(x)) > snr_wf: max_amp = np.max(x) i_max_amp = np.argmax(x) min_amp = np.abs(np.min(x)) i_min_amp = np.argmin(x) paz_list.append(paz[sta][cha]) tspan.append(np.abs(i_max_amp - i_min_amp) * dt) p2p_amp.append(max_amp + min_amp) if opdict['verbose']: fig = plt.figure() fig.set_facecolor('white') plt.plot(x) plt.plot(i_min_amp, x[i_min_amp], 'ro') plt.plot(i_max_amp, x[i_max_amp], 'ro') plt.title('%s,%s' % (sta, cha)) plt.show() if paz_list: mag = estimateMagnitude(paz_list, p2p_amp, tspan, h_dist) ml.append(mag) new_file.write( "Max = %.2f, %s - %.2f s + %.2f s, x= %.4f pm %.4f km, y= %.4f pm %.4f km, z= %.4f pm %.4f km, ml= %.2f pm %.2f\n" % (loc['max_trig'], loc['o_time'].isoformat(), loc['o_err_left'], loc['o_err_right'], loc['x_mean'], loc['x_sigma'], loc['y_mean'], loc['y_sigma'], loc['z_mean'], loc['z_sigma'], np.mean(ml), np.std(ml))) if ml: mags.append(np.mean(ml)) new_file.close() r = np.arange(-3, 3, 0.1) p, logN, i1, i2 = bvalue(mags, r) print "b-value:", -p[0] if opdict['verbose']: fig = plt.figure(figsize=(10, 5)) fig.set_facecolor('white') ax1 = fig.add_subplot(121) ax1.hist(mags, 25) ax1.set_xlabel('Magnitude') ax2 = fig.add_subplot(122, title='Gutenberg Richter law') ax2.plot(r, logN) ax2.plot(r[i1:i2], np.polyval(p, r[i1:i2]), 'r') ax2.set_xlabel('Magnitude') ax2.set_ylabel('log N') plt.show()