def _prepare_data(detect_data, detections, template, delays,<EOL>shift_len, plot):
detect_streams = []<EOL>for detection in detections:<EOL><INDENT>if detection.template_name != template[<NUM_LIT:0>]:<EOL><INDENT>continue<EOL><DEDENT>detect_stream = []<EOL>max_delay = <NUM_LIT:0><EOL>for tr in detect_data:<EOL><INDENT>template_tr = template[<NUM_LIT:1>].select(<EOL>station=tr.stats.station, channel=tr.stats.channel)<EOL>if len(template_tr) >= <NUM_LIT:1>:<EOL><INDENT>template_len = (<EOL>len(template_tr[<NUM_LIT:0>]) / template_tr[<NUM_LIT:0>].stats.sampling_rate)<EOL><DEDENT>else:<EOL><INDENT>continue<EOL><DEDENT>delay = delays[tr.stats.station + '<STR_LIT:.>' + tr.stats.channel]<EOL>if delay > max_delay:<EOL><INDENT>max_delay = delay<EOL><DEDENT>detect_stream.append(tr.slice(<EOL>starttime=detection.detect_time - shift_len + delay,<EOL>endtime=detection.detect_time + delay + shift_len +<EOL>template_len).copy())<EOL><DEDENT>for tr in detect_stream:<EOL><INDENT>if len(tr.data) == <NUM_LIT:0>:<EOL><INDENT>msg = ('<STR_LIT>' %<EOL>(tr.stats.station, tr.stats.channel,<EOL>detection.detect_time))<EOL>warnings.warn(msg)<EOL>detect_stream.remove(tr)<EOL><DEDENT>elif tr.stats.endtime - tr.stats.starttime < (<EOL><NUM_LIT:2> * shift_len) + template_len:<EOL><INDENT>msg = ("<STR_LIT>"<EOL>% (tr.stats.station, tr.stats.channel))<EOL>warnings.warn(msg)<EOL>detect_stream.remove(tr)<EOL><DEDENT>elif np.ma.is_masked(tr.data):<EOL><INDENT>msg = ("<STR_LIT>"<EOL>% (tr.stats.station, tr.stats.channel))<EOL>warnings.warn(msg)<EOL>detect_stream.remove(tr)<EOL><DEDENT><DEDENT>stachans = [(tr.stats.station, tr.stats.channel)<EOL>for tr in detect_stream]<EOL>c_stachans = Counter(stachans)<EOL>for key in c_stachans.keys():<EOL><INDENT>if c_stachans[key] > <NUM_LIT:1>:<EOL><INDENT>msg = ('<STR_LIT>'<EOL>% (key[<NUM_LIT:0>], key[<NUM_LIT:1>]))<EOL>raise LagCalcError(msg)<EOL><DEDENT><DEDENT>if plot:<EOL><INDENT>background = detect_data.slice(<EOL>starttime=detection.detect_time - (shift_len + <NUM_LIT:5>),<EOL>endtime=detection.detect_time +<EOL>shift_len + max_delay + <NUM_LIT:7>).copy()<EOL>for tr in background:<EOL><INDENT>if len(tr.data) == <NUM_LIT:0>:<EOL><INDENT>background.remove(tr)<EOL><DEDENT><DEDENT>detection_multiplot(<EOL>stream=background, template=Stream(detect_stream),<EOL>times=[detection.detect_time - shift_len],<EOL>title='<STR_LIT>')<EOL><DEDENT>if not len(detect_stream) == <NUM_LIT:0>:<EOL><INDENT>detect_stream = Stream(detect_stream).split()<EOL>detect_streams.append((detection.template_name,<EOL>Stream(detect_stream)))<EOL><DEDENT><DEDENT>return detect_streams<EOL>
Prepare data for lag_calc - reduce memory here.

:type detect_data: obspy.core.stream.Stream
:param detect_data: Stream to extract detection streams from.
:type detections: list
:param detections:
    List of :class:`eqcorrscan.core.match_filter.Detection` to get
    data for.
:type template: tuple
:param template: tuple of (template_name, template)
:type delays: list
:param delays:
    Dictionary of delay times in seconds keyed by sta.channel.
:type shift_len: float
:param shift_len: Shift length in seconds allowed for picking.
:type plot: bool
:param plot:
    Whether to plot the data extracted or not, used for debugging.

:returns: List of detect_streams to be worked on
:rtype: list