def _channel_loop(detection, template, i=0): """ Utility function to take a stream of data for the detected event and parse the correct data to lag_gen :type detection: obspy.Stream :param detection: Stream of data for the slave event detected using template :type template: obspy.Stream :param template: Stream of data as the template for the detection. :type i: int, optional :param i: Used to track which process has occured when running in parallel :returns: picks, a tuple of (lag in s, cross-correlation value, station, chan) """ from eqcorrscan.utils.Sfile_util import PICK picks=[] for i in xrange(len(template)): image=detection.select(station=template[i].stats.station,\ channel=template[i].stats.channel) if image: #Ideally this if statement would be removed. ccc = normxcorr2(template[i].data, image[0].data) shiftlen = len(ccc)*image[0].stats.sample_rate # Convert the maximum cross-correlation time to an actual time picktime = image[0].stats.starttime+(np.argmax(ccc)*image[0].stats.delta) picks.append(PICK()) ((lag, np.max(ccc), template[i].stats.station, \ template[i].stats.channel)) return (i, picks)
def _channel_loop(detection, template, i=0): """ Utility function to take a stream of data for the detected event and parse the correct data to lag_gen :type detection: obspy.Stream :param detection: Stream of data for the slave event detected using \ template :type template: obspy.Stream :param template: Stream of data as the template for the detection. :type i: int, optional :param i: Used to track which process has occured when running in parallel :returns: picks, a tuple of (lag in s, cross-correlation value, station,\ chan) """ from eqcorrscan.utils.Sfile_util import PICK picks = [] for tr in template: image = detection.select(station=tr.stats.station, channel=tr.stats.channel) if image: # Ideally this if statement would be removed. ccc = normxcorr2(tr.data, image[0].data) shiftlen = len(ccc) * image[0].stats.sample_rate # Convert the maximum cross-correlation time to an actual time picktime = image[0].stats.starttime + (np.argmax(ccc) * image[0].stats.delta) # Note, this function is incomplete! # picks.append(PICK()) # ((lag, np.max(ccc), tr.stats.station, # tr.stats.channel)) return (i, picks)
def coherence(stream_in, stations=["all"], clip=False): r"""Function to determine the average network coherence of a given\ template or detection. You will want your stream to contain only\ signal as noise will reduce the coherence (assuming it is incoherant\ random noise). :type stream: obspy.Stream :param stream: The stream of seismic data you want to calculate the\ coherence for. :type stations: List of String :param stations: List of stations to use for coherence, default is all :type clip: Tuple of Float :param clip: Default is to use all the data given - \ tuple of start and end in seconds from start of trace :return: float - coherence, int number of channels used """ from match_filter import normxcorr2 stream = stream_in.copy() # Copy the data before we remove stations # First check that all channels in stream have data of the same length maxlen = np.max([len(tr.data) for tr in stream]) if maxlen == 0: warnings.warn("template without data") return 0.0 if not stations[0] == "all": for tr in stream: if tr.stats.station not in stations: stream.remove(tr) # Remove stations we don't want to use for tr in stream: if not len(tr.data) == maxlen and not len(tr.data) == 0: warnings.warn( tr.stats.station + "." + tr.stats.channel + " is not the same length, padding \n" + "Length is " + str(len(tr.data)) + " samples" ) pad = np.zeros(maxlen - len(tr.data)) if tr.stats.starttime.hour == 0: tr.data = np.concatenate((pad, tr.data), axis=0) else: tr.data = np.concatenate((tr.data, pad), axis=0) elif len(tr.data) == 0: tr.data = np.zeros(maxlen) # Clip the data to the set length if clip: for tr in stream: tr.trim(tr.stats.starttime + clip[0], tr.stats.starttime + clip[1]) coherence = 0.0 # Loop through channels and generate a correlation value for each # unique cross-channel pairing for i in range(len(stream)): for j in range(i + 1, len(stream)): coherence += np.abs(normxcorr2(stream[i].data, stream[j].data))[0][0] coherence = 2 * coherence / (len(stream) * (len(stream) - 1)) return coherence, len(stream)
def coherence(stream_in, stations=['all'], clip=False): r"""Function to determine the average network coherence of a given \ template or detection. You will want your stream to contain only \ signal as noise will reduce the coherence (assuming it is incoherant \ random noise). :type stream: obspy.Stream :param stream: The stream of seismic data you want to calculate the \ coherence for. :type stations: List of String :param stations: List of stations to use for coherence, default is all :type clip: tuple of Float :param clip: Default is to use all the data given - \ tuple of start and end in seconds from start of trace :return: float - coherence, int number of channels used """ from match_filter import normxcorr2 stream = stream_in.copy() # Copy the data before we remove stations # First check that all channels in stream have data of the same length maxlen = np.max([len(tr.data) for tr in stream]) if maxlen == 0: warnings.warn('template without data') return 0.0 if not stations[0] == 'all': for tr in stream: if tr.stats.station not in stations: stream.remove(tr) # Remove stations we don't want to use for tr in stream: if not len(tr.data) == maxlen and not len(tr.data) == 0: warnings.warn(tr.stats.station + '.' + tr.stats.channel + ' is not the same length, padding \n' + 'Length is ' + str(len(tr.data)) + ' samples') pad = np.zeros(maxlen - len(tr.data)) if tr.stats.starttime.hour == 0: tr.data = np.concatenate((pad, tr.data), axis=0) else: tr.data = np.concatenate((tr.data, pad), axis=0) elif len(tr.data) == 0: tr.data = np.zeros(maxlen) # Clip the data to the set length if clip: for tr in stream: tr.trim(tr.stats.starttime + clip[0], tr.stats.starttime + clip[1]) coherence = 0.0 # Loop through channels and generate a correlation value for each # unique cross-channel pairing for i in range(len(stream)): for j in range(i + 1, len(stream)): coherence += np.abs(normxcorr2(stream[i].data, stream[j].data))[0][0] coherence = 2 * coherence / (len(stream) * (len(stream) - 1)) return coherence, len(stream)
def ccc_gen(image, template): """ Function to test if a detection is possible at this moveout :type image: obspy.Stream :type template: obspy.Stream :type delays: list :type threhsold: float :returns: ccc, a matrix of correlation values """ ccc = np.array([np.array([0] * (len(image[0].data) - len(template[0].data) + 1))] * len(image)) print "Correlation matrix is shaped: " + str(ccc.shape) for i in xrange(len(image)): templatetr = template.select(station=image[i].stats.station, channel=image[i].stats.channel) ccc[i] = normxcorr2(templatetr[0].data, image[i].data)[0] return ccc
def ccc_gen(image, template): """ Function to test if a detection is possible at this moveout :type image: obspy.Stream :type template: obspy.Stream :type delays: list :type threhsold: float :returns: ccc, a matrix of correlation values """ ccc = np.array( [np.array([0] * (len(image[0].data) - len(template[0].data) + 1))] * len(image)) print 'Correlation matrix is shaped: ' + str(ccc.shape) for i in xrange(len(image)): templatetr=template.select(station=image[i].stats.station,\ channel=image[i].stats.channel) ccc[i] = normxcorr2(templatetr[0].data, image[i].data)[0] return ccc