def extract_from_stack(stack, template, length, pre_pick, pre_pad,<EOL>Z_include=False, pre_processed=True, samp_rate=None,<EOL>lowcut=None, highcut=None, filt_order=<NUM_LIT:3>):
new_template = stack.copy()<EOL>mintime = min([tr.stats.starttime for tr in template])<EOL>delays = [(tr.stats.station, tr.stats.channel[-<NUM_LIT:1>],<EOL>tr.stats.starttime - mintime) for tr in template]<EOL>if not pre_processed:<EOL><INDENT>new_template = pre_processing.shortproc(<EOL>st=new_template, lowcut=lowcut, highcut=highcut,<EOL>filt_order=filt_order, samp_rate=samp_rate, debug=<NUM_LIT:0>)<EOL><DEDENT>out = Stream()<EOL>for tr in new_template:<EOL><INDENT>delay = [d[<NUM_LIT:2>] for d in delays if d[<NUM_LIT:0>] == tr.stats.station and<EOL>d[<NUM_LIT:1>] == tr.stats.channel[-<NUM_LIT:1>]]<EOL>if Z_include and len(delay) == <NUM_LIT:0>:<EOL><INDENT>delay = [d[<NUM_LIT:2>] for d in delays if d[<NUM_LIT:0>] == tr.stats.station]<EOL><DEDENT>if len(delay) == <NUM_LIT:0>:<EOL><INDENT>debug_print("<STR_LIT>"<EOL>"<STR_LIT>".format(tr.stats.station, tr.stats.channel),<EOL><NUM_LIT:2>, <NUM_LIT:3>)<EOL>new_template.remove(tr)<EOL><DEDENT>else:<EOL><INDENT>for d in delay:<EOL><INDENT>out += tr.copy().trim(<EOL>starttime=tr.stats.starttime + d + pre_pad - pre_pick,<EOL>endtime=tr.stats.starttime + d + pre_pad + length -<EOL>pre_pick)<EOL><DEDENT><DEDENT><DEDENT>return out<EOL>
Extract a multiplexed template from a stack of detections.

Function to extract a new template from a stack of previous detections.
Requires the stack, the template used to make the detections for the \
stack, and we need to know if the stack has been pre-processed.

:type stack: obspy.core.stream.Stream
:param stack: Waveform stack from detections.  Can be of any length and \
    can have delays already included, or not.
:type template: obspy.core.stream.Stream
:param template: Template used to make the detections in the stack. Will \
    use the delays of this for the new template.
:type length: float
:param length: Length of new template in seconds
:type pre_pick: float
:param pre_pick: Extract additional data before the detection, seconds
:type pre_pad: float
:param pre_pad: Pad used in seconds when extracting the data, e.g. the \
    time before the detection extracted.  If using \
    clustering.extract_detections this half the length of the extracted \
    waveform.
:type Z_include: bool
:param Z_include: If True will include any Z-channels even if there is \
    no template for this channel, as long as there is a template for this \
    station at a different channel.  If this is False and Z channels are \
    included in the template Z channels will be included in the \
    new_template anyway.
:type pre_processed: bool
:param pre_processed: Have the data been pre-processed, if True (default) \
    then we will only cut the data here.
:type samp_rate: float
:param samp_rate: If pre_processed=False then this is required, desired \
    sampling rate in Hz, defaults to False.
:type lowcut: float
:param lowcut: If pre_processed=False then this is required, lowcut in \
    Hz, defaults to False.
:type highcut: float
:param highcut: If pre_processed=False then this is required, highcut in \
    Hz, defaults to False
:type filt_order: int
:param filt_order: If pre_processed=False then this is required, filter \
    order, defaults to False

:returns: Newly cut template.
:rtype: :class:`obspy.core.stream.Stream`