def test_read_tt(self): from eqcorrscan.core.bright_lights import _read_tt import os testing_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'test_data') + os.sep # Test reading S from S stations, nodes, lags = _read_tt(path=testing_path, stations=['COSA'], phase='S', phaseout='S') self.assertEqual(stations[0], 'COSA') self.assertEqual(len(nodes), len(lags[0])) # Test reading P from S stations, nodes, lags = _read_tt(path=testing_path, stations=['COSA'], phase='S', phaseout='P') self.assertEqual(stations[0], 'COSA') self.assertEqual(len(nodes), len(lags[0])) # Test reading S from P stations, nodes, lags = _read_tt(path=testing_path, stations=['COSA'], phase='P', phaseout='S') self.assertEqual(stations[0], 'COSA') self.assertEqual(len(nodes), len(lags[0])) # Test reading P from P stations, nodes, lags = _read_tt(path=testing_path, stations=['COSA'], phase='P', phaseout='P') self.assertEqual(stations[0], 'COSA') self.assertEqual(len(nodes), len(lags[0]))
def test_brightness(self): from eqcorrscan.core.bright_lights import brightness, _read_tt import os from obspy import Stream, Trace import numpy as np testing_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'test_data') + os.sep # Test reading S from S stations, nodes, lags = _read_tt(path=testing_path, stations=['COSA', 'LABE'], phase='S', phaseout='S') st = Stream(Trace()) st[0].stats.station = stations[0] st[0].data = np.random.randn(86400) * 3000 st[0].data = st[0].data.astype(np.int16) st += Trace(np.random.randn(86400) * 3000) st[1].stats.station = stations[1] st[1].stats.channel = 'HHZ' st[0].stats.channel = 'HHZ' detections, nodes_out = brightness(stations=stations, nodes=nodes, lags=lags, stream=st, threshold=1.885, thresh_type='MAD', template_length=1, template_saveloc='.', coherence_thresh=(10, 1)) self.assertEqual(len(detections), 0) self.assertEqual(len(detections), len(nodes_out))
def test_cum_net_resp(self): from eqcorrscan.core.bright_lights import _cum_net_resp from eqcorrscan.core.bright_lights import _node_loop, _read_tt import os from obspy import Stream, Trace import numpy as np testing_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'test_data') + os.sep stations, nodes, lags = _read_tt(path=testing_path, stations=['COSA', 'LABE'], phase='S', phaseout='S') st = Stream(Trace()) st[0].stats.station = stations[0] st[0].data = np.random.randn(86400) * 3000 st[0].data = st[0].data.astype(np.int16) st += Trace(np.random.randn(86400) * 3000) st[1].stats.station = stations[1] index, energy_file = _node_loop(stations=stations, lags=lags[:, 1], stream=st, clip_level=4, mem_issue=True) self.assertEqual(index, 0) self.assertTrue(type(energy_file) == str) cum_net_resp, indeces = _cum_net_resp(node_lis=[0]) self.assertEqual(len(cum_net_resp), 86400) self.assertEqual(len(indeces), 86400)
def test_resample_grid(self): minlon = 168 maxlon = 170 minlat = -46 maxlat = -43 mindepth = 4 maxdepth = 10 stations, allnodes, alllags = _read_tt(path=self.testing_path, stations=['COSA'], phase='S', phaseout='S') corners = [(minlon, minlat), (maxlon, minlat), (maxlon, maxlat), (minlon, maxlat)] corners = path.Path(corners, closed=True) stations, nodes, lags = _resample_grid(stations, allnodes, alllags, mindepth=mindepth, maxdepth=maxdepth, corners=corners) for node in nodes: self.assertTrue(minlon < node[0] < maxlon) self.assertTrue(minlat < node[1] < maxlat) self.assertTrue(mindepth < node[2] < maxdepth) for node in allnodes: if node not in nodes: self.assertFalse((minlon < node[0] < maxlon) and (minlat < node[1] < maxlat) and (mindepth < node[2] < maxdepth))
def test_cum_net_resp(self): stations, nodes, lags = _read_tt(path=self.testing_path, stations=['COSA', 'LABE'], phase='S', phaseout='S') st = Stream(Trace()) st[0].stats.station = stations[0] st[0].data = np.random.randn(86400) * 3000 st[0].data = st[0].data.astype(np.int16) st += Trace(np.random.randn(86400) * 3000) st[1].stats.station = stations[1] if not os.path.isdir('tmp0'): os.makedirs('tmp0') index, energy_file = _node_loop(stations=stations, lags=lags[:, 0], stream=st, clip_level=4, mem_issue=True, instance=0) self.assertEqual(index, 0) self.assertTrue(type(energy_file) == str) _node_loop(stations=stations, lags=lags[:, 1], stream=st, clip_level=4, mem_issue=True, i=1, instance=0) cum_net_resp, indeces = _cum_net_resp(node_lis=[0, 1], instance=0) self.assertEqual(len(cum_net_resp), 86400) self.assertEqual(len(indeces), 86400) shutil.rmtree('tmp0')
def test_resample_grid(self): from eqcorrscan.core.bright_lights import _read_tt, _resample_grid from matplotlib import path import os minlon = 168 maxlon = 170 minlat = -46 maxlat = -43 mindepth = 4 maxdepth = 10 testing_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'test_data') + os.sep stations, allnodes, alllags = _read_tt(path=testing_path, stations=['COSA'], phase='S', phaseout='S') corners = [(minlon, minlat), (maxlon, minlat), (maxlon, maxlat), (minlon, maxlat)] corners = path.Path(corners, closed=True) stations, nodes, lags = _resample_grid(stations, allnodes, alllags, mindepth=mindepth, maxdepth=maxdepth, corners=corners) for node in nodes: self.assertTrue(minlon < node[0] < maxlon) self.assertTrue(minlat < node[1] < maxlat) self.assertTrue(mindepth < node[2] < maxdepth) for node in allnodes: if node not in nodes: self.assertFalse((minlon < node[0] < maxlon) and (minlat < node[1] < maxlat) and (mindepth < node[2] < maxdepth))
def test_find_detections(self): stations, nodes, lags = _read_tt(path=self.testing_path, stations=['COSA', 'LABE'], phase='S', phaseout='S') st = Stream(Trace()) st[0].stats.station = stations[0] st[0].data = np.random.randn(86400) * 3000 st[0].data = st[0].data.astype(np.int16) st += Trace(np.random.randn(86400) * 3000) st[1].stats.station = stations[1] # Need to make a tmp0 folder os.makedirs('tmp1') _node_loop(stations=stations, lags=lags[:, 1], stream=st, clip_level=4, mem_issue=True, instance=1) cum_net_resp, indeces = _cum_net_resp(node_lis=[0], instance=1) all_nodes = [nodes[1] for i in range(len(cum_net_resp))] detections = _find_detections( cum_net_resp=cum_net_resp, nodes=all_nodes, threshold=10, thresh_type='MAD', samp_rate=1, realstations=[tr.stats.station for tr in st], length=10) self.assertEqual(len(detections), 0) detections = _find_detections( cum_net_resp=cum_net_resp, nodes=all_nodes, threshold=5, thresh_type='MAD', samp_rate=1, realstations=[tr.stats.station for tr in st], length=10) self.assertTrue(len(detections) > 0) detections = _find_detections( cum_net_resp=cum_net_resp, nodes=all_nodes, threshold=5, thresh_type='abs', samp_rate=1, realstations=[tr.stats.station for tr in st], length=10) self.assertTrue(len(detections) > 0) detections = _find_detections( cum_net_resp=cum_net_resp, nodes=all_nodes, threshold=5, thresh_type='RMS', samp_rate=1, realstations=[tr.stats.station for tr in st], length=10) self.assertTrue(len(detections) > 0) shutil.rmtree('tmp1')
def test_rm_similarlags(self): threshold = 2 stations, allnodes, alllags = _read_tt(path=self.testing_path, stations=['COSA'], phase='S', phaseout='S') stations, nodes, lags = _rm_similarlags(stations=stations, nodes=allnodes, lags=alllags, threshold=threshold) for lag in lags: for _lag in lag: other_lags = np.array([l for l in lag if not l == _lag]) self.assertTrue(np.all(np.abs(other_lags - _lag) > threshold))
def test_read_tt(self): # Test reading S from S stations, nodes, lags = _read_tt(path=self.testing_path, stations=['COSA'], phase='S', phaseout='S') self.assertEqual(stations[0], 'COSA') self.assertEqual(len(nodes), len(lags[0])) # Test reading P from S stations, nodes, lags = _read_tt(path=self.testing_path, stations=['COSA'], phase='S', phaseout='P') self.assertEqual(stations[0], 'COSA') self.assertEqual(len(nodes), len(lags[0])) # Test reading S from P stations, nodes, lags = _read_tt(path=self.testing_path, stations=['COSA'], phase='P', phaseout='S') self.assertEqual(stations[0], 'COSA') self.assertEqual(len(nodes), len(lags[0])) # Test reading P from P stations, nodes, lags = _read_tt(path=self.testing_path, stations=['COSA'], phase='P', phaseout='P') self.assertEqual(stations[0], 'COSA') self.assertEqual(len(nodes), len(lags[0])) # Test lag_switch stations, nodes, lags = _read_tt(path=self.testing_path, stations=['COSA'], phase='P', phaseout='P', lags_switch=False) self.assertEqual(stations[0], 'COSA') self.assertEqual(len(nodes), len(lags[0]))
def test_node_loop(self): stations, nodes, lags = _read_tt(path=self.testing_path, stations=['COSA', 'LABE'], phase='S', phaseout='S') st = Stream(Trace()) st[0].stats.station = stations[0] st[0].data = np.random.randn(86400) * 3000 st[0].data = st[0].data.astype(np.int16) st += Trace(np.random.randn(86400) * 3000) st[1].stats.station = 'LABE' index, energy = _node_loop(stations=stations, lags=lags[:, 1], stream=st, clip_level=4) self.assertEqual(index, 0) self.assertEqual(np.shape(energy), (1, 86400))
def test_find_detections(self): from eqcorrscan.core.bright_lights import _find_detections from eqcorrscan.core.bright_lights import _cum_net_resp from eqcorrscan.core.bright_lights import _node_loop, _read_tt import os from obspy import Stream, Trace import numpy as np testing_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'test_data') + os.sep stations, nodes, lags = _read_tt(path=testing_path, stations=['COSA', 'LABE'], phase='S', phaseout='S') st = Stream(Trace()) st[0].stats.station = stations[0] st[0].data = np.random.randn(86400) * 3000 st[0].data = st[0].data.astype(np.int16) st += Trace(np.random.randn(86400) * 3000) st[1].stats.station = stations[1] index, energy_file = _node_loop(stations=stations, lags=lags[:, 1], stream=st, clip_level=4, mem_issue=True) cum_net_resp, indeces = _cum_net_resp(node_lis=[0]) all_nodes = [nodes[1] for i in range(len(cum_net_resp))] detections = _find_detections( cum_net_resp=cum_net_resp, nodes=all_nodes, threshold=10, thresh_type='MAD', samp_rate=1, realstations=[tr.stats.station for tr in st], length=10) self.assertEqual(len(detections), 0) detections = _find_detections( cum_net_resp=cum_net_resp, nodes=all_nodes, threshold=5, thresh_type='MAD', samp_rate=1, realstations=[tr.stats.station for tr in st], length=10) self.assertTrue(len(detections) > 0)
def test_rm_similarlags(self): from eqcorrscan.core.bright_lights import _read_tt, _rm_similarlags import os import numpy as np threshold = 2 testing_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'test_data') + os.sep stations, allnodes, alllags = _read_tt(path=testing_path, stations=['COSA'], phase='S', phaseout='S') stations, nodes, lags = _rm_similarlags(stations=stations, nodes=allnodes, lags=alllags, threshold=threshold) for lag in lags: for _lag in lag: other_lags = np.array([l for l in lag if not l == _lag]) self.assertTrue(np.all(np.abs(other_lags - _lag) > threshold))
def setUpClass(cls): testing_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'test_data') + os.sep # Test reading S from S cls.stations, cls.nodes, cls.lags = _read_tt(path=testing_path, stations=['COSA', 'LABE'], phase='S', phaseout='S') templates, cls.st, seeds = generate_synth_data(nsta=2, ntemplates=2, nseeds=20, samp_rate=1, t_length=15, max_amp=10, max_lag=6) cls.st[0].stats.station = cls.stations[0] cls.st[1].stats.station = cls.stations[1] cls.st[0].data = cls.st[0].data.astype(np.int16) cls.st[1].data = cls.st[1].data.astype(np.int16) cls.st[1].stats.channel = 'HHZ' cls.st[0].stats.channel = 'HHZ'
def test_node_loop(self): from eqcorrscan.core.bright_lights import _node_loop, _read_tt import os from obspy import Stream, Trace import numpy as np testing_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'test_data') + os.sep stations, nodes, lags = _read_tt(path=testing_path, stations=['COSA', 'LABE'], phase='S', phaseout='S') st = Stream(Trace()) st[0].stats.station = stations[0] st[0].data = np.random.randn(86400) * 3000 st[0].data = st[0].data.astype(np.int16) st += Trace(np.random.randn(86400) * 3000) st[1].stats.station = 'LABE' index, energy = _node_loop(stations=stations, lags=lags[:, 1], stream=st, clip_level=4) self.assertEqual(index, 0) self.assertEqual(np.shape(energy), (1, 86400))
def test_find_detections(self): from eqcorrscan.core.bright_lights import _find_detections from eqcorrscan.core.bright_lights import _cum_net_resp from eqcorrscan.core.bright_lights import _node_loop, _read_tt import os from obspy import Stream, Trace import numpy as np testing_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'test_data') + os.sep stations, nodes, lags = _read_tt(path=testing_path, stations=['COSA', 'LABE'], phase='S', phaseout='S') st = Stream(Trace()) st[0].stats.station = stations[0] st[0].data = np.random.randn(86400) * 3000 st[0].data = st[0].data.astype(np.int16) st += Trace(np.random.randn(86400) * 3000) st[1].stats.station = stations[1] index, energy_file = _node_loop(stations=stations, lags=lags[:, 1], stream=st, clip_level=4, mem_issue=True) cum_net_resp, indeces = _cum_net_resp(node_lis=[0]) all_nodes = [nodes[1] for i in range(len(cum_net_resp))] detections = _find_detections(cum_net_resp=cum_net_resp, nodes=all_nodes, threshold=10, thresh_type='MAD', samp_rate=1, realstations=[tr.stats.station for tr in st], length=10) self.assertEqual(len(detections), 0) detections = _find_detections(cum_net_resp=cum_net_resp, nodes=all_nodes, threshold=5, thresh_type='MAD', samp_rate=1, realstations=[tr.stats.station for tr in st], length=10) self.assertTrue(len(detections) > 0)
stations=[] print 'Template generation parameters are:' print ' sfilebase: '+templatedef.sfilebase print ' samp_rate: '+str(templatedef.samp_rate)+' Hz' print ' lowcut: '+str(templatedef.lowcut)+' Hz' print ' highcut: '+str(templatedef.highcut)+' Hz' print ' length: '+str(templatedef.length)+' s' print ' swin: '+templatedef.swin+'\n' # # ## Use the brightness function to search for possible templates ## First read in the travel times print 'Reading in the original grids' stations, allnodes, alltravel_times = \ bright_lights._read_tt(brightdef.nllpath,brightdef.stations,\ brightdef.phase, phaseout='S', \ ps_ratio=brightdef.ps_ratio, lags_switch=False) print 'I have read in '+str(len(allnodes))+' nodes' # We now have a grid of travel-times which can then be used to generate synthetic\ # seismograms using the utils.synth_seis functions. # We should trim the grid to the area we want to work in print 'Cutting the grid' stations, nodes, travel_times = bright_lights._resample_grid(stations, allnodes, alltravel_times, brightdef.mindepth, brightdef.maxdepth, brightdef.corners, brightdef.resolution) del allnodes, alltravel_times
stations=[] print 'Template generation parameters are:' print ' sfilebase: '+templatedef.sfilebase print ' samp_rate: '+str(templatedef.samp_rate)+' Hz' print ' lowcut: '+str(templatedef.lowcut)+' Hz' print ' highcut: '+str(templatedef.highcut)+' Hz' print ' length: '+str(templatedef.length)+' s' print ' swin: '+templatedef.swin+'\n' if not oldnodes: # Use the brightness function to search for possible templates # First read in the travel times print 'Reading in the original grids' stations, allnodes, alllags = \ bright_lights._read_tt(brightdef.nllpath,brightdef.stations,\ brightdef.phase, phaseout='S', \ ps_ratio=brightdef.ps_ratio) print 'I have read in '+str(len(allnodes))+' nodes' # Resample the grid to allow us to run it quickly! print 'Cutting the grid' stations, nodes, lags = bright_lights._resample_grid(stations, allnodes, alllags, brightdef.mindepth, brightdef.maxdepth, brightdef.corners, brightdef.resolution) del allnodes, alllags # Check that we still have a grid! if len(nodes) == 0: raise IOError("You have no nodes left") # Remove lags that have a similar network moveout, e.g. the sum of the