def coherence(stream_in, stations=['<STR_LIT:all>'], clip=False):
stream = stream_in.copy()  <EOL>maxlen = np.max([len(tr.data) for tr in stream])<EOL>if maxlen == <NUM_LIT:0>:<EOL><INDENT>warnings.warn('<STR_LIT>')<EOL>return <NUM_LIT:0.0>, len(stream)<EOL><DEDENT>if not stations[<NUM_LIT:0>] == '<STR_LIT:all>':<EOL><INDENT>for tr in stream:<EOL><INDENT>if tr.stats.station not in stations:<EOL><INDENT>stream.remove(tr)  <EOL><DEDENT><DEDENT><DEDENT>for tr in stream:<EOL><INDENT>if not len(tr.data) == maxlen and not len(tr.data) == <NUM_LIT:0>:<EOL><INDENT>warnings.warn(tr.stats.station + '<STR_LIT:.>' + tr.stats.channel +<EOL>'<STR_LIT>' +<EOL>'<STR_LIT>' + str(len(tr.data)) + '<STR_LIT>')<EOL>pad = np.zeros(maxlen - len(tr.data))<EOL>if tr.stats.starttime.hour == <NUM_LIT:0>:<EOL><INDENT>tr.data = np.concatenate((pad, tr.data), axis=<NUM_LIT:0>)<EOL><DEDENT>else:<EOL><INDENT>tr.data = np.concatenate((tr.data, pad), axis=<NUM_LIT:0>)<EOL><DEDENT><DEDENT>elif len(tr.data) == <NUM_LIT:0>:<EOL><INDENT>tr.data = np.zeros(maxlen)<EOL><DEDENT><DEDENT>if clip:<EOL><INDENT>for tr in stream:<EOL><INDENT>tr.trim(tr.stats.starttime + clip[<NUM_LIT:0>], tr.stats.starttime + clip[<NUM_LIT:1>])<EOL><DEDENT><DEDENT>_coherence = <NUM_LIT:0.0><EOL>for i in range(len(stream)):<EOL><INDENT>for j in range(i + <NUM_LIT:1>, len(stream)):<EOL><INDENT>_coherence += np.abs(normxcorr2(stream[i].data,<EOL>stream[j].data))[<NUM_LIT:0>][<NUM_LIT:0>]<EOL><DEDENT><DEDENT>_coherence = <NUM_LIT:2> * _coherence / (len(stream) * (len(stream) - <NUM_LIT:1>))<EOL>return _coherence, len(stream)<EOL>
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