class ProcessingTests(unittest.TestCase): def setUp(self): self.wo=WavelocOptions() self.wo.set_test_options() self.wo.verify_SDS_processing_options() def test_positive_gradient(self): from OP_waveforms import stream_positive_derivative from obspy.core import read base_path=self.wo.opdict['base_path'] test_datadir=self.wo.opdict['test_datadir'] st=read(os.path.join(base_path,test_datadir,'raw_data','YA.UV15.00.HHZ.MSEED')) tr=st[0] npts= len(tr.data) dt = tr.stats.delta x = np.arange(npts)*dt # set up a polynomial function y = (3 + 2*x +4*x*x +5*x*x*x) dy_exp = (2 + 8*x +15*x*x) tr.data=y st=stream_positive_derivative(st) np.testing.assert_almost_equal(tr.data[20:100], dy_exp[20:100],2) def test_channel_read(self): from SDS_processing import read_channel_file base_path=self.wo.opdict['base_path'] filename=os.path.join(base_path,'lib','test_channel_file') triplet_list=read_channel_file(filename) self.assertEquals(len(triplet_list),4) self.assertEquals(triplet_list[0][1],'ECH') self.assertEquals(triplet_list[-1][2],'UHZ') #@unittest.skip('Skip for now') def test_processing(self): base_path=self.wo.opdict['base_path'] datadir=self.wo.opdict['datadir'] test_datadir=self.wo.opdict['test_datadir'] expected_signature_filename = os.path.join(base_path,test_datadir,'test_data_signature.dat') expected_signature_file = open(expected_signature_filename,'r') expected_lines=expected_signature_file.readlines() #self.wo.opdict['load_ttimes_buf']=False do_SDS_processing_setup_and_run(self.wo.opdict) waveforms_to_signature(base_path,os.path.join('data',datadir),'*mseed','data_signature.dat') signature_filename=os.path.join(base_path,'data',datadir,'data_signature.dat') signature_file = open(signature_filename,'r') lines=signature_file.readlines()
class ProcessingTests(unittest.TestCase): def setUp(self): self.wo = WavelocOptions() self.wo.set_test_options() self.wo.verify_SDS_processing_options() def test_positive_gradient(self): from OP_waveforms import stream_positive_derivative from obspy.core import read base_path = self.wo.opdict["base_path"] test_datadir = self.wo.opdict["test_datadir"] st = read(os.path.join(base_path, test_datadir, "raw_data", "YA.UV15.00.HHZ.MSEED")) tr = st[0] npts = len(tr.data) dt = tr.stats.delta x = np.arange(npts) * dt # set up a polynomial function y = 3 + 2 * x + 4 * x * x + 5 * x * x * x dy_exp = 2 + 8 * x + 15 * x * x tr.data = y st = stream_positive_derivative(st) np.testing.assert_almost_equal(tr.data[20:100], dy_exp[20:100], 2) def test_channel_read(self): from SDS_processing import read_channel_file base_path = self.wo.opdict["base_path"] filename = os.path.join(base_path, "lib", "test_channel_file") triplet_list = read_channel_file(filename) self.assertEquals(len(triplet_list), 4) self.assertEquals(triplet_list[0][1], "ECH") self.assertEquals(triplet_list[-1][2], "UHZ") def test_processing(self): base_path = self.wo.opdict["base_path"] datadir = self.wo.opdict["datadir"] test_datadir = self.wo.opdict["test_datadir"] expected_signature_filename = os.path.join(base_path, test_datadir, "test_data_signature.dat") expected_signature_file = open(expected_signature_filename, "r") expected_lines = expected_signature_file.readlines() do_SDS_processing_setup_and_run(self.wo.opdict) waveforms_to_signature(base_path, os.path.join("data", datadir), "*mseed", "data_signature.dat") signature_filename = os.path.join(base_path, "data", datadir, "data_signature.dat") signature_file = open(signature_filename, "r") lines = signature_file.readlines() lines.sort() expected_lines.sort() self.assertSequenceEqual(lines, expected_lines)
class MigrationTests(unittest.TestCase): def setUp(self): self.wo = WavelocOptions() self.wo.set_test_options() self.wo.verify_migration_options() # @unittest.skip('Not running small test') @unittest.expectedFailure def test_migration(self): self.wo.opdict['load_ttimes_buf'] = True self.wo.opdict['data_length'] = 300 base_path = self.wo.opdict['base_path'] test_datadir = self.wo.opdict['test_datadir'] outdir = self.wo.opdict['outdir'] expected_signature_filename = os.path.join(base_path, test_datadir, 'test_stack_signature.dat') expected_signature_file = open(expected_signature_filename, 'r') expected_lines = expected_signature_file.readlines() do_migration_setup_and_run(self.wo.opdict) #waveforms_to_signature(base_path,os.path.join('out',outdir,'stack'),'stack*mseed','stack_signature.dat') hdf5_to_signature(base_path, os.path.join('out', outdir, 'stack'), 'stack_all_2010-10-14T00:14:00.000000Z.hdf5', 'stack_signature.dat') signature_filename = os.path.join(base_path, 'out', outdir, 'stack', 'stack_signature.dat') signature_file = open(signature_filename, 'r') lines = signature_file.readlines() self.assertSequenceEqual(lines, expected_lines) @unittest.skip('Not running full resolution test') #@profile def test_migration_fullRes(self): self.wo.opdict['search_grid'] = 'grid.Taisne.search.hdr' self.wo.opdict['outdir'] = 'TEST_fullRes' self.wo.opdict['load_ttimes_buf'] = True self.wo.opdict['data_length'] = 300 self.wo.verify_migration_options() base_path = self.wo.opdict['base_path'] test_datadir = self.wo.opdict['test_datadir'] outdir = self.wo.opdict['outdir'] expected_signature_filename = os.path.join( base_path, test_datadir, 'TEST_fullRes_stack_signature.dat') expected_signature_file = open(expected_signature_filename, 'r') expected_lines = expected_signature_file.readlines() do_migration_setup_and_run(self.wo.opdict) #waveforms_to_signature(base_path,os.path.join('out',outdir,'stack'),'stack*mseed','stack_signature.dat') hdf5_to_signature(base_path, os.path.join('out', outdir, 'stack'), 'stack*hdf5', 'stack_signature.dat') signature_filename = os.path.join(base_path, 'out', outdir, 'stack', 'stack_signature.dat') signature_file = open(signature_filename, 'r') lines = signature_file.readlines() self.assertSequenceEqual(lines, expected_lines)
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() if __name__=='__main__': from options import WavelocOptions logging.basicConfig(level=logging.INFO, format='%(levelname)s : %(asctime)s : %(message)s') wo = WavelocOptions() args=wo.p.parse_args() wo.set_all_arguments(args) wo.verify_location_options() do_locations_prob_setup_and_run(wo.opdict)
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) # Plot graphs #plot_graphs(locs,stations,nbsta,CLUSTER,nbmin,threshold) # ----------------------------------------------------------------------------------------- if __name__ == '__main__': from options import WavelocOptions logging.basicConfig(level=logging.INFO, format='%(levelname)s : %(asctime)s : %(message)s') wo = WavelocOptions() args=wo.p.parse_args() wo.set_all_arguments(args) wo.verify_cluster_options() do_clustering_setup_and_run(wo.opdict)
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() # ======================================================= if __name__ == '__main__' : from options import WavelocOptions logging.basicConfig(level=logging.INFO, format='%(levelname)s : %(asctime)s : %(message)s') wo = WavelocOptions() args=wo.p.parse_args() wo.set_all_arguments(args) wo.verify_magnitude_options() do_comp_mag(wo.opdict)
# 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() if __name__ == '__main__': from options import WavelocOptions logging.basicConfig(level=logging.INFO, format='%(levelname)s : %(asctime)s : %(message)s') wo = WavelocOptions() args = wo.p.parse_args() wo.set_all_arguments(args) wo.verify_plotting_options() do_plotting_setup_and_run(wo.opdict)
def setUp(self): self.wo = WavelocOptions() self.wo.set_test_options() self.wo.verify_SDS_processing_options()
def setUp(self): self.wo=WavelocOptions() self.wo.set_test_options() self.wo.verify_location_options()
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)) if __name__ == '__main__': from options import WavelocOptions logging.basicConfig(level=logging.INFO, format='%(levelname)s : %(asctime)s : %(message)s') wo = WavelocOptions() args = wo.p.parse_args() wo.set_all_arguments(args) wo.verify_SDS_processing_options() do_SDS_processing_setup_and_run(wo.opdict)
class MigrationTests(unittest.TestCase): def setUp(self): self.wo = WavelocOptions() self.wo.set_test_options() self.wo.verify_migration_options() def test_migration(self): self.wo.opdict['load_ttimes_buf'] = True self.wo.opdict['data_length'] = 300 base_path = self.wo.opdict['base_path'] test_datadir = self.wo.opdict['test_datadir'] outdir = self.wo.opdict['outdir'] expected_signature_filename = os.path.join(base_path, test_datadir, 'test_stack_signature.dat') expected_signature_file = open(expected_signature_filename, 'r') expected_lines = expected_signature_file.readlines() do_migration_setup_and_run(self.wo.opdict) hdf5_to_signature(base_path, os.path.join('out', outdir, 'stack'), 'stack_all_2010-10-14T00:14:00.000000Z.hdf5', 'stack_signature.dat') signature_filename = os.path.join(base_path, 'out', outdir, 'stack', 'stack_signature.dat') signature_file = open(signature_filename, 'r') lines = signature_file.readlines() self.assertSequenceEqual(lines, expected_lines) def test_take(self): nbuf = 9000 nt = 3600 matrix = np.random.randn(nbuf, nt) max_ib = np.argmax(matrix, 0) max_val = np.max(matrix, 0) max_val_take = np.diag(matrix.take(max_ib, 0)) self.assertEqual(len(max_ib), nt) self.assertEqual(len(max_val), nt) self.assertEqual(len(max_val_take), nt) self.assertEqual(max_val.shape, max_val_take.shape) np.testing.assert_allclose(max_val_take, max_val) def test_migration_use_ram(self): self.wo.opdict['load_ttimes_buf'] = True self.wo.opdict['data_length'] = 300 base_path = self.wo.opdict['base_path'] outdir = self.wo.opdict['outdir'] # do migration without use_ram self.wo.opdict['use_ram'] = False do_migration_setup_and_run(self.wo.opdict) lines_no_ram =\ hdf5_max_values( os.path.join(base_path, 'out', outdir, 'stack', 'stack_all_2010-10-14T00:14:00.000000Z.hdf5')) # do migration with use_ram self.wo.opdict['use_ram'] = True do_migration_setup_and_run(self.wo.opdict) lines_use_ram =\ hdf5_max_values( os.path.join(base_path, 'out', outdir, 'stack', 'stack_all_2010-10-14T00:14:00.000000Z.hdf5')) # verify that the two give the same result np.testing.assert_allclose(lines_use_ram, lines_no_ram) def test_migration_fullRes(self): self.wo.opdict['search_grid'] = 'grid.Taisne.search.hdr' self.wo.opdict['outdir'] = 'TEST_fullRes' self.wo.opdict['load_ttimes_buf'] = True self.wo.opdict['data_length'] = 300 self.wo.opdict['use_ram'] = True self.wo.verify_migration_options() base_path = self.wo.opdict['base_path'] test_datadir = self.wo.opdict['test_datadir'] outdir = self.wo.opdict['outdir'] expected_signature_filename =\ os.path.join(base_path, test_datadir, 'TEST_fullRes_stack_signature.dat') expected_signature_file = open(expected_signature_filename, 'r') expected_lines = expected_signature_file.readlines() do_migration_setup_and_run(self.wo.opdict) hdf5_to_signature(base_path, os.path.join('out', outdir, 'stack'), 'stack*hdf5', 'stack_signature.dat') signature_filename = os.path.join(base_path, 'out', outdir, 'stack', 'stack_signature.dat') signature_file = open(signature_filename, 'r') lines = signature_file.readlines() self.assertSequenceEqual(lines, expected_lines)
test_info['dat_file']=test_grid_file test_info['stack_file']=test_stack_file test_info['grid_shape']=nx,ny,nz,nt test_info['grid_spacing']=dx,dy,dz,s_delta test_info['grid_orig']=x_orig,y_orig,z_orig test_info['true_indexes']=(ix,iy,iz,shifted_it) test_info['start_time']=-stack_shift_time logging.debug(test_info) f=open(test_info_file,'w') f.write(str(test_info)) return test_info if __name__ == '__main__': from options import WavelocOptions logging.basicConfig(level=logging.INFO, format='%(levelname)s : %(asctime)s : %(message)s') wo = WavelocOptions() args=wo.p.parse_args() wo.set_all_arguments(args) wo.verify_migration_options() wo.verify_synthetic_options() generateSynteticDirac(wo.opdict)
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) if __name__ == '__main__' : from options import WavelocOptions logging.basicConfig(level=logging.INFO, format="%(levelname)s : %(asctime)s : %(message)s") wo = WavelocOptions() args=wo.p.parse_args() wo.set_all_arguments(args) wo.verify_correlation_options() do_correlation_setup_and_run(wo.opdict)
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() ############################################################################################### if __name__ == '__main__': from options import WavelocOptions logging.basicConfig(level=logging.INFO, format="%(levelname)s : %(asctime)s : %(message)s") wo = WavelocOptions() args=wo.p.parse_args() wo.set_all_arguments(args) wo.verify_doublediff_options() do_double_diff_setup_and_run(wo.opdict)
def test_dirac_migration(self): from locations_trigger import trigger_locations_inner from filters import smooth wo = WavelocOptions() wo.set_test_options() wo.opdict['outdir'] = 'TEST_Dirac' wo.opdict['search_grid'] = 'grid.Taisne.search.hdr' wo.opdict['loclevel'] = 10 wo.opdict[ 'load_ttimes_buf'] = True # Optimized in time, but you must be usre you're reading the right grid for the test wo.opdict['syn_addnoise'] = False wo.opdict['syn_amplitude'] = 1.0 wo.opdict['syn_datalength'] = 20.0 wo.opdict['syn_samplefreq'] = 100.0 wo.opdict['syn_kwidth'] = 0.1 wo.opdict['syn_otime'] = 6.0 wo.opdict['syn_ix'] = 16 wo.opdict['syn_iy'] = 8 wo.opdict['syn_iz'] = 6 wo.opdict['syn_filename'] = 'test_grid4D_hires.hdf5' wo.verify_migration_options() wo.verify_location_options() wo.verify_synthetic_options() ########################## # generate the test case and retrieve necessary information ########################## logging.info('Running synthetic test case generation...') test_info = generateSyntheticDirac(wo.opdict) logging.debug(test_info) # retrieve info grid_filename = test_info['dat_file'] stack_filename = test_info['stack_file'] nx, ny, nz, nt = test_info['grid_shape'] dx, dy, dz, dt = test_info['grid_spacing'] x_orig, y_orig, z_orig = test_info['grid_orig'] ix_true, iy_true, iz_true, it_true = test_info['true_indexes'] stack_start_time = test_info['start_time'] # plot base filename base_path = wo.opdict['base_path'] outdir = wo.opdict['outdir'] # loclevel for triggers loclevel = wo.opdict['loclevel'] # set up x, y, z, t arrays x = np.arange(nx) * dx y = np.arange(ny) * dy z = np.arange(nz) * dz t = np.arange(nt) * dt + stack_start_time # extract the max stacks f_stack = h5py.File(stack_filename, 'r') max_val = f_stack['max_val'] max_x = f_stack['max_x'] max_y = f_stack['max_y'] max_z = f_stack['max_z'] locs = trigger_locations_inner(max_val, max_x, max_y, max_z, loclevel, loclevel, stack_start_time, dt) self.assertTrue(len(locs) > 0) #print locs # This is a dirac test, so only have one element in locs imax = np.argmax([loc['max_trig'] for loc in locs]) trig_loc = locs[imax] self.assertAlmostEqual(wo.opdict['syn_otime'], trig_loc['o_time'], 2) self.assertAlmostEqual(wo.opdict['syn_ix'] * dx + x_orig, trig_loc['x_mean']) self.assertAlmostEqual(wo.opdict['syn_iy'] * dy + y_orig, trig_loc['y_mean']) self.assertAlmostEqual(wo.opdict['syn_iz'] * dz + z_orig, trig_loc['z_mean']) f_stack.close()
# 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() if __name__ == '__main__': from options import WavelocOptions logging.basicConfig(level=logging.INFO, format='%(levelname)s : %(asctime)s : %(message)s') wo = WavelocOptions() args=wo.p.parse_args() wo.set_all_arguments(args) wo.verify_plotting_options() do_plotting_setup_and_run(wo.opdict)
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) # Plot graphs #plot_graphs(locs,stations,nbsta,CLUSTER,nbmin,threshold) # ----------------------------------------------------------------------------------------- if __name__ == '__main__': from options import WavelocOptions logging.basicConfig(level=logging.INFO, format='%(levelname)s : %(asctime)s : %(message)s') wo = WavelocOptions() args = wo.p.parse_args() wo.set_all_arguments(args) wo.verify_cluster_options() do_clustering_setup_and_run(wo.opdict)
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() ############################################################################################### if __name__ == '__main__': from options import WavelocOptions logging.basicConfig(level=logging.INFO, format="%(levelname)s : %(asctime)s : %(message)s") wo = WavelocOptions() args = wo.p.parse_args() wo.set_all_arguments(args) wo.verify_doublediff_options() do_double_diff_setup_and_run(wo.opdict)
import os, logging, glob import numpy as np from options import WavelocOptions from synth_migration import generateSyntheticDirac from grids_paths import QDGrid, StationList, ChannelList, QDTimeGrid from NllGridLib import * from plot_mpl import plotDiracTest logging.basicConfig(level=logging.INFO, format='%(levelname)s : %(asctime)s : %(message)s') recalc_grids=True # set up default parameters wo = WavelocOptions() # cheat - the test set is from PdF # (for tests with other geometries must replicate the set_test_options parameters here) base_path = os.getenv('WAVELOC_PATH') wo.opdict['outdir'] = 'TEST_DiracEmilia' wo.opdict['time_grid'] = 'emilia.P' wo.opdict['stations'] = 'coord_stations_temp_emilia' wo.opdict['search_grid']='grid.emilia.search.hdr' wo.opdict['loclevel'] = 10 wo.opdict['load_ttimes_buf'] = False # Optimized in time, but you must be usre you're reading the right grid for the test wo.opdict['syn_amplitude']=1.0 wo.opdict['syn_datalength']=50.0 wo.opdict['syn_samplefreq']=100.0 wo.opdict['syn_kwidth']=0.1 wo.opdict['syn_otime']=5.0 wo.opdict['syn_addnoise']=False
class MigrationTests(unittest.TestCase): def setUp(self): self.wo=WavelocOptions() self.wo.set_test_options() self.wo.verify_migration_options() # @unittest.skip('Not running small test') @unittest.expectedFailure def test_migration(self): self.wo.opdict['load_ttimes_buf'] = True self.wo.opdict['data_length'] = 300 base_path=self.wo.opdict['base_path'] test_datadir=self.wo.opdict['test_datadir'] outdir=self.wo.opdict['outdir'] expected_signature_filename = os.path.join(base_path,test_datadir,'test_stack_signature.dat') expected_signature_file = open(expected_signature_filename,'r') expected_lines=expected_signature_file.readlines() do_migration_setup_and_run(self.wo.opdict) #waveforms_to_signature(base_path,os.path.join('out',outdir,'stack'),'stack*mseed','stack_signature.dat') hdf5_to_signature(base_path,os.path.join('out',outdir,'stack'),'stack_all_2010-10-14T00:14:00.000000Z.hdf5','stack_signature.dat') signature_filename=os.path.join(base_path,'out',outdir,'stack','stack_signature.dat') signature_file = open(signature_filename,'r') lines=signature_file.readlines() self.assertSequenceEqual(lines,expected_lines) @unittest.skip('Not running full resolution test') #@profile def test_migration_fullRes(self): self.wo.opdict['search_grid'] = 'grid.Taisne.search.hdr' self.wo.opdict['outdir'] = 'TEST_fullRes' self.wo.opdict['load_ttimes_buf'] = True self.wo.opdict['data_length'] = 300 self.wo.verify_migration_options() base_path=self.wo.opdict['base_path'] test_datadir=self.wo.opdict['test_datadir'] outdir=self.wo.opdict['outdir'] expected_signature_filename = os.path.join(base_path,test_datadir,'TEST_fullRes_stack_signature.dat') expected_signature_file = open(expected_signature_filename,'r') expected_lines=expected_signature_file.readlines() do_migration_setup_and_run(self.wo.opdict) #waveforms_to_signature(base_path,os.path.join('out',outdir,'stack'),'stack*mseed','stack_signature.dat') hdf5_to_signature(base_path,os.path.join('out',outdir,'stack'),'stack*hdf5','stack_signature.dat') signature_filename=os.path.join(base_path,'out',outdir,'stack','stack_signature.dat') signature_file = open(signature_filename,'r') lines=signature_file.readlines() self.assertSequenceEqual(lines,expected_lines)
import os, logging from options import WavelocOptions from synth_migration import generateSyntheticDirac from plot_mpl import plotDiracTest logging.basicConfig(level=logging.INFO, format='%(levelname)s : %(asctime)s : %(message)s') # set up default parameters wo = WavelocOptions() # cheat - the test set is from PdF # (for tests with other geometries must replicate the set_test_options parameters here) wo.set_test_options() base_path = os.getenv('WAVELOC_PATH') wo.opdict['outdir'] = 'TEST_Dirac' wo.opdict['search_grid'] = 'grid.Taisne.search.hdr' wo.opdict['loclevel'] = 10 wo.opdict[ 'load_ttimes_buf'] = True # Optimized in time, but you must be usre you're reading the right grid for the test wo.opdict['syn_amplitude'] = 1.0 wo.opdict['syn_datalength'] = 20.0 wo.opdict['syn_samplefreq'] = 100.0 wo.opdict['syn_kwidth'] = 0.1 wo.opdict['syn_otime'] = 6.0 wo.opdict['syn_ix'] = 16 wo.opdict['syn_iy'] = 8 wo.opdict['syn_iz'] = 6 wo.opdict['syn_addnoise'] = False wo.opdict['syn_filename'] = 'test_grid4D_hires.dat'
def test_dirac_migration(self): from locations_trigger import trigger_locations_inner from filters import smooth wo=WavelocOptions() wo.set_test_options() wo.opdict['outdir'] = 'TEST_Dirac' wo.opdict['search_grid']='grid.Taisne.search.hdr' wo.opdict['loclevel'] = 10 wo.opdict['load_ttimes_buf'] = True # Optimized in time, but you must be usre you're reading the right grid for the test wo.opdict['syn_addnoise']=False wo.opdict['syn_amplitude']=1.0 wo.opdict['syn_datalength']=20.0 wo.opdict['syn_samplefreq']=100.0 wo.opdict['syn_kwidth']=0.1 wo.opdict['syn_otime']=6.0 wo.opdict['syn_ix']=16 wo.opdict['syn_iy']=8 wo.opdict['syn_iz']=6 wo.opdict['syn_filename']='test_grid4D_hires.hdf5' wo.verify_migration_options() wo.verify_location_options() wo.verify_synthetic_options() ########################## # generate the test case and retrieve necessary information ########################## logging.info('Running synthetic test case generation...') test_info=generateSyntheticDirac(wo.opdict) logging.debug(test_info) # retrieve info grid_filename=test_info['dat_file'] stack_filename=test_info['stack_file'] nx,ny,nz,nt=test_info['grid_shape'] dx,dy,dz,dt=test_info['grid_spacing'] x_orig,y_orig,z_orig=test_info['grid_orig'] ix_true,iy_true,iz_true,it_true=test_info['true_indexes'] stack_start_time=test_info['start_time'] # plot base filename base_path=wo.opdict['base_path'] outdir=wo.opdict['outdir'] # loclevel for triggers loclevel=wo.opdict['loclevel'] # set up x, y, z, t arrays x=np.arange(nx)*dx y=np.arange(ny)*dy z=np.arange(nz)*dz t=np.arange(nt)*dt+stack_start_time # extract the max stacks f_stack=h5py.File(stack_filename,'r') max_val=f_stack['max_val'] max_x=f_stack['max_x'] max_y=f_stack['max_y'] max_z=f_stack['max_z'] locs=trigger_locations_inner(max_val,max_x,max_y,max_z,loclevel,loclevel,stack_start_time,dt) self.assertTrue(len(locs)>0) #print locs # This is a dirac test, so only have one element in locs imax=np.argmax([loc['max_trig'] for loc in locs]) trig_loc=locs[imax] self.assertAlmostEqual(wo.opdict['syn_otime'],trig_loc['o_time'],2) self.assertAlmostEqual(wo.opdict['syn_ix']*dx+x_orig,trig_loc['x_mean']) self.assertAlmostEqual(wo.opdict['syn_iy']*dy+y_orig,trig_loc['y_mean']) self.assertAlmostEqual(wo.opdict['syn_iz']*dz+z_orig,trig_loc['z_mean']) f_stack.close()
test_info = {} test_info['dat_file'] = test_grid_file test_info['stack_file'] = test_stack_file test_info['grid_shape'] = nx, ny, nz, nt test_info['grid_spacing'] = dx, dy, dz, s_delta test_info['grid_orig'] = x_orig, y_orig, z_orig test_info['true_indexes'] = (ix, iy, iz, shifted_it) test_info['start_time'] = -stack_shift_time logging.debug(test_info) f = open(test_info_file, 'w') f.write(str(test_info)) return test_info if __name__ == '__main__': from options import WavelocOptions logging.basicConfig(level=logging.INFO, format='%(levelname)s : %(asctime)s : %(message)s') wo = WavelocOptions() args = wo.p.parse_args() wo.set_all_arguments(args) wo.verify_migration_options() wo.verify_synthetic_options() generateSynteticDirac(wo.opdict)
class LocationTests(unittest.TestCase): def setUp(self): self.wo = WavelocOptions() self.wo.set_test_options() self.wo.verify_location_options() 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()
class MigrationTests(unittest.TestCase): def setUp(self): self.wo = WavelocOptions() self.wo.set_test_options() self.wo.verify_migration_options() def test_migration(self): self.wo.opdict['load_ttimes_buf'] = False self.wo.opdict['data_length'] = 300 base_path = self.wo.opdict['base_path'] test_datadir = self.wo.opdict['test_datadir'] outdir = self.wo.opdict['outdir'] expected_signature_filename = os.path.join(base_path, test_datadir, 'test_stack_signature.dat') expected_signature_file = open(expected_signature_filename, 'r') expected_lines = expected_signature_file.readlines() do_migration_setup_and_run(self.wo.opdict) hdf5_to_signature(base_path, os.path.join('out', outdir, 'stack'), 'stack_all_2010-10-14T00:14:00.000000Z.hdf5', 'stack_signature.dat') signature_filename = os.path.join(base_path, 'out', outdir, 'stack', 'stack_signature.dat') signature_file = open(signature_filename, 'r') lines = signature_file.readlines() self.assertSequenceEqual(lines, expected_lines) def test_take(self): nbuf = 9000 nt = 3600 matrix = np.random.randn(nbuf, nt) max_ib = np.argmax(matrix, 0) max_val = np.max(matrix, 0) max_val_take = np.diag(matrix.take(max_ib, 0)) self.assertEqual(len(max_ib), nt) self.assertEqual(len(max_val), nt) self.assertEqual(len(max_val_take), nt) self.assertEqual(max_val.shape, max_val_take.shape) np.testing.assert_allclose(max_val_take, max_val) def test_migration_use_ram(self): self.wo.opdict['load_ttimes_buf'] = True self.wo.opdict['data_length'] = 300 base_path = self.wo.opdict['base_path'] outdir = self.wo.opdict['outdir'] # do migration without use_ram self.wo.opdict['use_ram'] = False do_migration_setup_and_run(self.wo.opdict) lines_no_ram =\ hdf5_max_values( os.path.join(base_path, 'out', outdir, 'stack', 'stack_all_2010-10-14T00:14:00.000000Z.hdf5')) # do migration with use_ram self.wo.opdict['use_ram'] = True do_migration_setup_and_run(self.wo.opdict) lines_use_ram =\ hdf5_max_values( os.path.join(base_path, 'out', outdir, 'stack', 'stack_all_2010-10-14T00:14:00.000000Z.hdf5')) # verify that the two give the same result np.testing.assert_allclose(lines_use_ram, lines_no_ram) @unittest.skip('Time-consuming and uninteresting for code checking') def test_migration_fullRes(self): self.wo.opdict['search_grid'] = 'grid.Taisne.search.hdr' self.wo.opdict['outdir'] = 'TEST_fullRes' self.wo.opdict['load_ttimes_buf'] = False self.wo.opdict['data_length'] = 300 self.wo.opdict['use_ram'] = True self.wo.verify_migration_options() base_path = self.wo.opdict['base_path'] test_datadir = self.wo.opdict['test_datadir'] outdir = self.wo.opdict['outdir'] expected_signature_filename =\ os.path.join(base_path, test_datadir, 'TEST_fullRes_stack_signature.dat') expected_signature_file = open(expected_signature_filename, 'r') expected_lines = expected_signature_file.readlines() do_migration_setup_and_run(self.wo.opdict) hdf5_to_signature(base_path, os.path.join('out', outdir, 'stack'), 'stack*hdf5', 'stack_signature.dat') signature_filename = os.path.join(base_path, 'out', outdir, 'stack', 'stack_signature.dat') signature_file = open(signature_filename, 'r') lines = signature_file.readlines() self.assertSequenceEqual(lines, expected_lines)
wf.take_positive_derivative(pre_taper=True,post_taper=True) wf.write_to_file_filled(kurt_grad_filename,format='MSEED',fill_value=0) 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)) if __name__ == '__main__' : from options import WavelocOptions logging.basicConfig(level=logging.INFO, format='%(levelname)s : %(asctime)s : %(message)s') wo = WavelocOptions() args=wo.p.parse_args() wo.set_all_arguments(args) wo.verify_SDS_processing_options() do_SDS_processing_setup_and_run(wo.opdict)
import os, logging, glob import numpy as np from options import WavelocOptions from synth_migration import generateSyntheticDirac from grids_paths import QDGrid, StationList, ChannelList, QDTimeGrid from NllGridLib import * from plot_mpl import plotDiracTest logging.basicConfig(level=logging.INFO, format='%(levelname)s : %(asctime)s : %(message)s') recalc_grids = True # set up default parameters wo = WavelocOptions() # cheat - the test set is from PdF # (for tests with other geometries must replicate the set_test_options parameters here) base_path = os.getenv('WAVELOC_PATH') wo.opdict['outdir'] = 'TEST_DiracEmilia' wo.opdict['time_grid'] = 'emilia.P' wo.opdict['stations'] = 'coord_stations_temp_emilia' wo.opdict['search_grid'] = 'grid.emilia.search.hdr' wo.opdict['loclevel'] = 10 wo.opdict[ 'load_ttimes_buf'] = False # Optimized in time, but you must be usre you're reading the right grid for the test wo.opdict['syn_amplitude'] = 1.0 wo.opdict['syn_datalength'] = 50.0 wo.opdict['syn_samplefreq'] = 100.0 wo.opdict['syn_kwidth'] = 0.1
f = open(filename, 'r') lines = f.readlines() f.close() opdict['c1'] = np.float(lines[0].split()[2]) opdict['c2'] = np.float(lines[0].split()[4]) opdict['kwin'] = np.float(lines[1].split()[3]) opdict['krec'] = lines[1].split()[6][:-1] opdict['kderiv'] = lines[1].split()[8][:-1] opdict['gauss'] = lines[1].split()[10] opdict['reloc'] = lines[2].split()[3] opdict['loclevel'] = np.int(lines[3].split()[3][:-1]) opdict['snr_tr_limit'] = np.float(lines[3].split()[14][:-1]) return opdict if __name__ == '__main__': from options import WavelocOptions logging.basicConfig(level=logging.INFO, format='%(levelname)s : %(asctime)s : %(message)s') wo = WavelocOptions() args = wo.p.parse_args() wo.set_all_arguments(args) wo.verify_location_options() do_locations_trigger_setup_and_run(wo.opdict)
def setUp(self): self.wo = WavelocOptions() self.wo.set_test_options() self.wo.verify_migration_options()
class LocationTests(unittest.TestCase): def setUp(self): self.wo=WavelocOptions() self.wo.set_test_options() self.wo.verify_location_options() # @unittest.skip('Not bothering with trigger test') 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)) # for now, trigger uncertainties are rather too small for comparison #self.assertLess(np.abs(loc['x_mean']-prob_loc['x_mean']), loc['x_sigma']) #self.assertLess(np.abs(loc['y_mean']-prob_loc['y_mean']), loc['y_sigma']) #self.assertLess(np.abs(loc['z_mean']-prob_loc['z_mean']), loc['z_sigma']) f_marginals.close()
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() # ======================================================= if __name__ == '__main__': from options import WavelocOptions logging.basicConfig(level=logging.INFO, format='%(levelname)s : %(asctime)s : %(message)s') wo = WavelocOptions() args = wo.p.parse_args() wo.set_all_arguments(args) wo.verify_magnitude_options() do_comp_mag(wo.opdict)
import os, logging from options import WavelocOptions from synth_migration import generateSyntheticDirac from plot_mpl import plotDiracTest logging.basicConfig(level=logging.INFO, format="%(levelname)s : %(asctime)s : %(message)s") # set up default parameters wo = WavelocOptions() # cheat - the test set is from PdF # (for tests with other geometries must replicate the set_test_options parameters here) wo.set_test_options() base_path = os.getenv("WAVELOC_PATH") wo.opdict["outdir"] = "TEST_Dirac" wo.opdict["search_grid"] = "grid.Taisne.search.hdr" wo.opdict["loclevel"] = 10 wo.opdict[ "load_ttimes_buf" ] = True # Optimized in time, but you must be usre you're reading the right grid for the test wo.opdict["syn_amplitude"] = 1.0 wo.opdict["syn_datalength"] = 20.0 wo.opdict["syn_samplefreq"] = 100.0 wo.opdict["syn_kwidth"] = 0.1 wo.opdict["syn_otime"] = 6.0 wo.opdict["syn_ix"] = 16 wo.opdict["syn_iy"] = 8 wo.opdict["syn_iz"] = 6