def _template_gen(picks, st, length, swin='<STR_LIT:all>', prepick=<NUM_LIT>,<EOL>all_horiz=False, delayed=True, plot=False, min_snr=None,<EOL>debug=<NUM_LIT:0>):
from eqcorrscan.utils.debug_log import debug_print<EOL>from eqcorrscan.utils.plotting import pretty_template_plot as tplot<EOL>from eqcorrscan.utils.plotting import noise_plot<EOL>from eqcorrscan.core.bright_lights import _rms<EOL>picks_copy = copy.deepcopy(picks)  <EOL>if not isinstance(swin, list):<EOL><INDENT>swin = [swin]<EOL><DEDENT>for _swin in swin:<EOL><INDENT>assert _swin in ['<STR_LIT:P>', '<STR_LIT:all>', '<STR_LIT:S>', '<STR_LIT>', '<STR_LIT>']<EOL><DEDENT>for pick in picks_copy:<EOL><INDENT>if not pick.waveform_id:<EOL><INDENT>debug_print(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(pick), <NUM_LIT:1>, debug)<EOL>picks_copy.remove(pick)<EOL>continue<EOL><DEDENT>if not pick.waveform_id.station_code or notpick.waveform_id.channel_code:<EOL><INDENT>debug_print(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(pick), <NUM_LIT:1>, debug)<EOL>picks_copy.remove(pick)<EOL>continue<EOL><DEDENT><DEDENT>for tr in st:<EOL><INDENT>if np.all(tr.data.astype(np.float16) == <NUM_LIT:0>):<EOL><INDENT>debug_print("<STR_LIT>"<EOL>"<STR_LIT>".format(tr), <NUM_LIT:4>,<EOL>debug)<EOL>st.remove(tr)<EOL><DEDENT><DEDENT>picks_copy.sort(key=lambda p: p.time)<EOL>first_pick = picks_copy[<NUM_LIT:0>]<EOL>if plot:<EOL><INDENT>stplot = st.slice(first_pick.time - <NUM_LIT:20>,<EOL>first_pick.time + length + <NUM_LIT>).copy()<EOL>noise = stplot.copy()<EOL><DEDENT>starttimes = []<EOL>for _swin in swin:<EOL><INDENT>for tr in st:<EOL><INDENT>starttime = {'<STR_LIT>': tr.stats.station,<EOL>'<STR_LIT>': tr.stats.channel, '<STR_LIT>': []}<EOL>station_picks = [pick for pick in picks_copy<EOL>if pick.waveform_id.station_code ==<EOL>tr.stats.station]<EOL>if _swin == '<STR_LIT>':<EOL><INDENT>p_pick = [pick for pick in station_picks<EOL>if pick.phase_hint.upper()[<NUM_LIT:0>] == '<STR_LIT:P>']<EOL>if len(p_pick) == <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>starttime.update({'<STR_LIT>': p_pick})<EOL><DEDENT>elif _swin == '<STR_LIT>':<EOL><INDENT>s_pick = [pick for pick in station_picks<EOL>if pick.phase_hint.upper()[<NUM_LIT:0>] == '<STR_LIT:S>']<EOL>if len(s_pick) == <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>starttime.update({'<STR_LIT>': s_pick})<EOL><DEDENT>elif _swin == '<STR_LIT:all>':<EOL><INDENT>if all_horiz and tr.stats.channel[-<NUM_LIT:1>] in ['<STR_LIT:1>', '<STR_LIT:2>', '<STR_LIT:3>',<EOL>'<STR_LIT:N>', '<STR_LIT:E>']:<EOL><INDENT>channel_pick = [<EOL>pick for pick in station_picks<EOL>if pick.waveform_id.channel_code[-<NUM_LIT:1>] in<EOL>['<STR_LIT:1>', '<STR_LIT:2>', '<STR_LIT:3>', '<STR_LIT:N>', '<STR_LIT:E>']]<EOL><DEDENT>else:<EOL><INDENT>channel_pick = [<EOL>pick for pick in station_picks<EOL>if pick.waveform_id.channel_code == tr.stats.channel]<EOL><DEDENT>if len(channel_pick) == <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>starttime.update({'<STR_LIT>': channel_pick})<EOL><DEDENT>elif _swin == '<STR_LIT:P>':<EOL><INDENT>p_pick = [pick for pick in station_picks<EOL>if pick.phase_hint.upper()[<NUM_LIT:0>] == '<STR_LIT:P>' and<EOL>pick.waveform_id.channel_code == tr.stats.channel]<EOL>if len(p_pick) == <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>starttime.update({'<STR_LIT>': p_pick})<EOL><DEDENT>elif _swin == '<STR_LIT:S>':<EOL><INDENT>if tr.stats.channel[-<NUM_LIT:1>] in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>continue<EOL><DEDENT>s_pick = [pick for pick in station_picks<EOL>if pick.phase_hint.upper()[<NUM_LIT:0>] == '<STR_LIT:S>']<EOL>if not all_horiz:<EOL><INDENT>s_pick = [pick for pick in s_pick<EOL>if pick.waveform_id.channel_code ==<EOL>tr.stats.channel]<EOL><DEDENT>starttime.update({'<STR_LIT>': s_pick})<EOL>if len(starttime['<STR_LIT>']) == <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT><DEDENT>if not delayed:<EOL><INDENT>starttime.update({'<STR_LIT>': [first_pick]})<EOL><DEDENT>starttimes.append(starttime)<EOL><DEDENT><DEDENT>st1 = Stream()<EOL>for _starttime in starttimes:<EOL><INDENT>debug_print("<STR_LIT>" %<EOL>(_starttime['<STR_LIT>'], _starttime['<STR_LIT>']),<EOL>debug_level=<NUM_LIT:0>, print_level=debug)<EOL>tr = st.select(<EOL>station=_starttime['<STR_LIT>'], channel=_starttime['<STR_LIT>'])[<NUM_LIT:0>]<EOL>debug_print("<STR_LIT>" % tr.__str__(), debug_level=<NUM_LIT:0>,<EOL>print_level=debug)<EOL>noise_amp = _rms(tr.data)<EOL>used_tr = False<EOL>for pick in _starttime['<STR_LIT>']:<EOL><INDENT>if not pick.phase_hint:<EOL><INDENT>warnings.warn(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>".format(<EOL>pick.waveform_id.station_code,<EOL>pick.waveform_id.channel_code))<EOL><DEDENT>starttime = pick.time - prepick<EOL>debug_print(<EOL>"<STR_LIT>" + tr.stats.station + '<STR_LIT:.>' + tr.stats.channel, <NUM_LIT:0>,<EOL>debug)<EOL>tr_cut = tr.slice(<EOL>starttime=starttime, endtime=starttime + length,<EOL>nearest_sample=False).copy()<EOL>if plot:<EOL><INDENT>noise.select(<EOL>station=_starttime['<STR_LIT>'],<EOL>channel=_starttime['<STR_LIT>']).trim(<EOL>noise[<NUM_LIT:0>].stats.starttime, starttime)<EOL><DEDENT>if len(tr_cut.data) == <NUM_LIT:0>:<EOL><INDENT>debug_print(<EOL>"<STR_LIT>".format(<EOL>tr.stats.station, tr.stats.channel, starttime), <NUM_LIT:3>,<EOL>debug)<EOL>continue<EOL><DEDENT>if len(tr_cut.data) == (tr_cut.stats.sampling_rate *<EOL>length) + <NUM_LIT:1>:<EOL><INDENT>tr_cut.data = tr_cut.data[<NUM_LIT:0>:-<NUM_LIT:1>]<EOL><DEDENT>debug_print(<EOL>'<STR_LIT>' %<EOL>(str(tr_cut.stats.starttime), str(tr_cut.stats.endtime)), <NUM_LIT:0>,<EOL>debug)<EOL>if min_snr is not None andmax(tr_cut.data) / noise_amp < min_snr:<EOL><INDENT>debug_print(<EOL>"<STR_LIT>".format(<EOL>tr_cut.stats.station, tr_cut.stats.channel), <NUM_LIT:3>, debug)<EOL>continue<EOL><DEDENT>st1 += tr_cut<EOL>used_tr = True<EOL><DEDENT>if not used_tr:<EOL><INDENT>debug_print('<STR_LIT>' + tr.stats.station + '<STR_LIT:.>' +<EOL>tr.stats.channel, <NUM_LIT:0>, debug)<EOL><DEDENT><DEDENT>if plot:<EOL><INDENT>fig1 = tplot(st1, background=stplot, picks=picks_copy,<EOL>title='<STR_LIT>' + str(st1[<NUM_LIT:0>].stats.starttime),<EOL>show=False, return_figure=True)<EOL>fig2 = noise_plot(<EOL>signal=st1, noise=noise, show=False, return_figure=True)<EOL>fig1.savefig("<STR_LIT>".format(st1[<NUM_LIT:0>].stats.starttime))<EOL>fig2.savefig("<STR_LIT>".format(st1[<NUM_LIT:0>].stats.starttime))<EOL>del(stplot, fig1, fig2)<EOL><DEDENT>return st1<EOL>
Master function to generate a multiplexed template for a single event.

Function to generate a cut template as :class:`obspy.core.stream.Stream`
from a given set of picks and data.  Should be given pre-processed
data (downsampled and filtered).

:type picks: list
:param picks: Picks to extract data around, where each pick in the \
    list is an obspy.core.event.origin.Pick object.
:type st: obspy.core.stream.Stream
:param st: Stream to extract templates from
:type length: float
:param length: Length of template in seconds
:type swin: str
:param swin:
    P, S, P_all, S_all or all, defaults to all: see note in
    :func:`eqcorrscan.core.template_gen.template_gen`
:type prepick: float
:param prepick:
    Length in seconds to extract before the pick time default is 0.05
    seconds.
:type all_horiz: bool
:param all_horiz:
    To use both horizontal channels even if there is only a pick on one
    of them.  Defaults to False.
:type delayed: bool
:param delayed:
    If True, each channel will begin relative to it's own pick-time, if
    set to False, each channel will begin at the same time.
:type plot: bool
:param plot:
    To plot the template or not, default is False. Plots are saved as
    `template-starttime_template.png` and `template-starttime_noise.png`,
    where `template-starttime` is the start-time of the template
:type min_snr: float
:param min_snr:
    Minimum signal-to-noise ratio for a channel to be included in the
    template, where signal-to-noise ratio is calculated as the ratio of
    the maximum amplitude in the template window to the rms amplitude in
    the whole window given.
:type debug: int
:param debug: Debug output level from 0-5.

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

.. note:: By convention templates are generated with P-phases on the \
    vertical channel and S-phases on the horizontal channels, normal \
    seismograph naming conventions are assumed, where Z denotes vertical \
    and N, E, R, T, 1 and 2 denote horizontal channels, either oriented \
    or not.  To this end we will **only** use Z channels if they have a \
    P-pick, and will use one or other horizontal channels **only** if \
    there is an S-pick on it.

.. note:: swin argument: Setting to `P` will return only data for channels
    with P picks, starting at the pick time (minus the prepick).
    Setting to `S` will return only data for channels with
    S picks, starting at the S-pick time (minus the prepick)
    (except if `all_horiz=True` when all horizontal channels will
    be returned if there is an S pick on one of them). Setting to `all`
    will return channels with either a P or S pick (including both
    horizontals if `all_horiz=True`) - with this option vertical channels
    will start at the P-pick (minus the prepick) and horizontal channels
    will start at the S-pick time (minus the prepick).
    `P_all` will return cut traces starting at the P-pick time for all
    channels. `S_all` will return cut traces starting at the S-pick
    time for all channels.

.. warning:: If there is no phase_hint included in picks, and swin=all, \
    all channels with picks will be used.