def coherence(stream_in, stations=['all'], clip=False):
    """
    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 incoherent random noise).

    :type stream_in: obspy.core.stream.Stream
    :param stream_in: The stream of seismic data you want to calculate the \
            coherence for.
    :type stations: list
    :param stations: List of stations to use for coherence, default is all
    :type clip: tuple
    :param clip: Default is to use all the data given (`False`) - \
            tuple of start and end in seconds from start of trace

    :return: tuple of coherence and number of channels used.
    :rtype: tuple
    """
    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, len(stream)
    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 ) : stream = stream_in . copy ( ) maxlen = np . max ( [ len ( tr . data ) for tr in stream ] ) if maxlen == 0 : warnings . warn ( 'template without data' ) return 0.0 , len ( stream ) if not stations [ 0 ] == 'all' : for tr in stream : if tr . stats . station not in stations : stream . remove ( tr ) 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 ) if clip : for tr in stream : tr . trim ( tr . stats . starttime + clip [ 0 ] , tr . stats . starttime + clip [ 1 ] ) _coherence = 0.0 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 )
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 incoherent random noise).

    :type stream_in: obspy.core.stream.Stream
    :param stream_in: The stream of seismic data you want to calculate the \
            coherence for.
    :type stations: list
    :param stations: List of stations to use for coherence, default is all
    :type clip: tuple
    :param clip: Default is to use all the data given (`False`) - \
            tuple of start and end in seconds from start of trace

    :return: tuple of coherence and number of channels used.
    :rtype: tuple