def template_gen(method, lowcut, highcut, samp_rate, filt_order,<EOL>length, prepick, swin, process_len=<NUM_LIT>,<EOL>all_horiz=False, delayed=True, plot=False, debug=<NUM_LIT:0>,<EOL>return_event=False, min_snr=None, parallel=False,<EOL>num_cores=False, save_progress=False, **kwargs):
client_map = {'<STR_LIT>': '<STR_LIT>', '<STR_LIT>': '<STR_LIT>'}<EOL>assert method in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>')<EOL>if not isinstance(swin, list):<EOL><INDENT>swin = [swin]<EOL><DEDENT>process = True<EOL>if method in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>catalog = kwargs.get('<STR_LIT>', Catalog())<EOL>data_pad = kwargs.get('<STR_LIT>', <NUM_LIT>)<EOL>sub_catalogs = _group_events(<EOL>catalog=catalog, process_len=process_len, template_length=length,<EOL>data_pad=data_pad)<EOL>if method == '<STR_LIT>':<EOL><INDENT>client = FDSNClient(kwargs.get('<STR_LIT>', None))<EOL>available_stations = []<EOL><DEDENT>else:<EOL><INDENT>client = SeisHubClient(kwargs.get('<STR_LIT:url>', None), timeout=<NUM_LIT:10>)<EOL>available_stations = client.waveform.get_station_ids()<EOL><DEDENT><DEDENT>elif method == '<STR_LIT>':<EOL><INDENT>if isinstance(kwargs.get('<STR_LIT>'), Catalog):<EOL><INDENT>catalog = kwargs.get('<STR_LIT>')<EOL><DEDENT>elif kwargs.get('<STR_LIT>'):<EOL><INDENT>catalog = read_events(kwargs.get('<STR_LIT>'))<EOL><DEDENT>elif kwargs.get('<STR_LIT>'):<EOL><INDENT>catalog = kwargs.get('<STR_LIT>')<EOL><DEDENT>sub_catalogs = [catalog]<EOL>st = kwargs.get('<STR_LIT>', Stream())<EOL>process = kwargs.get('<STR_LIT>', True)<EOL><DEDENT>elif method == '<STR_LIT>':<EOL><INDENT>sac_files = kwargs.get('<STR_LIT>')<EOL>if isinstance(sac_files, list):<EOL><INDENT>if isinstance(sac_files[<NUM_LIT:0>], (Stream, Trace)):<EOL><INDENT>st = Stream(sac_files[<NUM_LIT:0>])<EOL>for sac_file in sac_files[<NUM_LIT:1>:]:<EOL><INDENT>st += sac_file<EOL><DEDENT><DEDENT>else:<EOL><INDENT>sac_files = [read(sac_file)[<NUM_LIT:0>] for sac_file in sac_files]<EOL>st = Stream(sac_files)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>st = sac_files<EOL><DEDENT>catalog = Catalog([sactoevent(st, debug=debug)])<EOL>sub_catalogs = [catalog]<EOL><DEDENT>temp_list = []<EOL>process_lengths = []<EOL>if "<STR_LIT>" in swin or "<STR_LIT>" in swin or all_horiz:<EOL><INDENT>all_channels = True<EOL><DEDENT>else:<EOL><INDENT>all_channels = False<EOL><DEDENT>for sub_catalog in sub_catalogs:<EOL><INDENT>if method in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>debug_print("<STR_LIT>", <NUM_LIT:1>, debug)<EOL>st = _download_from_client(<EOL>client=client, client_type=client_map[method],<EOL>catalog=sub_catalog, data_pad=data_pad,<EOL>process_len=process_len, available_stations=available_stations,<EOL>all_channels=all_channels, debug=debug)<EOL><DEDENT>debug_print('<STR_LIT>', <NUM_LIT:0>, debug)<EOL>st.merge()<EOL>if process:<EOL><INDENT>data_len = max([len(tr.data) / tr.stats.sampling_rate<EOL>for tr in st])<EOL>if <NUM_LIT> < data_len < <NUM_LIT>:<EOL><INDENT>daylong = True<EOL>starttime = min([tr.stats.starttime for tr in st])<EOL>min_delta = min([tr.stats.delta for tr in st])<EOL>if (starttime + min_delta).date > starttime.date:<EOL><INDENT>starttime = (starttime + min_delta)<EOL><DEDENT>if abs(starttime - UTCDateTime(starttime.date)) > <NUM_LIT>:<EOL><INDENT>print(abs(starttime - UTCDateTime(starttime.date)))<EOL>daylong = False<EOL><DEDENT>starttime = starttime.date<EOL><DEDENT>else:<EOL><INDENT>daylong = False<EOL><DEDENT>if daylong:<EOL><INDENT>st = pre_processing.dayproc(<EOL>st=st, lowcut=lowcut, highcut=highcut,<EOL>filt_order=filt_order, samp_rate=samp_rate, debug=debug,<EOL>parallel=parallel, starttime=UTCDateTime(starttime),<EOL>num_cores=num_cores)<EOL><DEDENT>else:<EOL><INDENT>st = pre_processing.shortproc(<EOL>st=st, lowcut=lowcut, highcut=highcut,<EOL>filt_order=filt_order, parallel=parallel,<EOL>samp_rate=samp_rate, debug=debug, num_cores=num_cores)<EOL><DEDENT><DEDENT>data_start = min([tr.stats.starttime for tr in st])<EOL>data_end = max([tr.stats.endtime for tr in st])<EOL>for event in sub_catalog:<EOL><INDENT>stations, channels, st_stachans = ([], [], [])<EOL>if len(event.picks) == <NUM_LIT:0>:<EOL><INDENT>debug_print('<STR_LIT>'.format(event.resource_id),<EOL><NUM_LIT:2>, debug)<EOL>continue<EOL><DEDENT>use_event = True<EOL>for pick in event.picks:<EOL><INDENT>if not data_start < pick.time < data_end:<EOL><INDENT>debug_print("<STR_LIT>"<EOL>"<STR_LIT>" %<EOL>(str(pick.time), str(data_start),<EOL>str(data_end)), <NUM_LIT:0>, debug)<EOL>use_event = False<EOL><DEDENT><DEDENT>if not use_event:<EOL><INDENT>debug_print('<STR_LIT>', <NUM_LIT:2>, debug)<EOL>continue<EOL><DEDENT>debug_print("<STR_LIT>", <NUM_LIT:0>, debug)<EOL>for pick in event.picks:<EOL><INDENT>if not pick.waveform_id:<EOL><INDENT>debug_print(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>'.format(pick), <NUM_LIT:1>, debug)<EOL>continue<EOL><DEDENT>debug_print(pick, <NUM_LIT:0>, debug)<EOL>stations.append(pick.waveform_id.station_code)<EOL>channels.append(pick.waveform_id.channel_code)<EOL><DEDENT>for tr in st:<EOL><INDENT>st_stachans.append('<STR_LIT:.>'.join([tr.stats.station,<EOL>tr.stats.channel]))<EOL><DEDENT>template = _template_gen(<EOL>event.picks, st, length, swin, prepick=prepick, plot=plot,<EOL>debug=debug, all_horiz=all_horiz, delayed=delayed,<EOL>min_snr=min_snr)<EOL>process_lengths.append(len(st[<NUM_LIT:0>].data) / samp_rate)<EOL>temp_list.append(template)<EOL><DEDENT>if save_progress:<EOL><INDENT>if not os.path.isdir("<STR_LIT>"):<EOL><INDENT>os.makedirs("<STR_LIT>")<EOL><DEDENT>for template in temp_list:<EOL><INDENT>template.write(<EOL>"<STR_LIT>".format(<EOL>os.path.sep, template[<NUM_LIT:0>].stats.starttime),<EOL>format="<STR_LIT>")<EOL><DEDENT><DEDENT>del st<EOL><DEDENT>if return_event:<EOL><INDENT>return temp_list, catalog, process_lengths<EOL><DEDENT>return temp_list<EOL>
Generate processed and cut waveforms for use as templates.

:type method: str
:param method:
    Template generation method, must be one of ('from_client',
    'from_seishub', 'from_sac', 'from_meta_file'). - Each method requires
    associated arguments, see note below.
:type lowcut: float
:param lowcut: Low cut (Hz), if set to None will not apply a lowcut.
:type highcut: float
:param highcut: High cut (Hz), if set to None will not apply a highcut.
:type samp_rate: float
:param samp_rate: New sampling rate in Hz.
:type filt_order: int
:param filt_order: Filter level (number of corners).
:type length: float
:param length: Extract length in seconds.
:type prepick: float
:param prepick: Pre-pick time 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 process_len: int
:param process_len: Length of data in seconds to download and process.
: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: Plot templates or not.
:type debug: int
:param debug: Level of debugging output, higher=more
:type return_event: bool
:param return_event: Whether to return the event and process length or not.
: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 parallel: bool
:param parallel: Whether to process data in parallel or not.
:type num_cores: int
:param num_cores:
    Number of cores to try and use, if False and parallel=True, will use
    either all your cores, or as many traces as in the data (whichever is
    smaller).
:type save_progress: bool
:param save_progress:
    Whether to save the resulting party at every data step or not.
    Useful for long-running processes.

:returns: List of :class:`obspy.core.stream.Stream` Templates
:rtype: list

.. note::
    *Method specific arguments:*

    - `from_client` requires:
        :param str client_id: string passable by obspy to generate Client
        :param `obspy.core.event.Catalog` catalog:
            Catalog of events to generate template for
        :param float data_pad: Pad length for data-downloads in seconds
    - `from_seishub` requires:
        :param str url: url to seishub database
        :param `obspy.core.event.Catalog` catalog:
            Catalog of events to generate template for
        :param float data_pad: Pad length for data-downloads in seconds
    - `from_sac` requires:
        :param list sac_files:
            osbpy.core.stream.Stream of sac waveforms, or list of paths to
            sac waveforms.
    - `from_meta_file` requires:
        :param str meta_file: Path to obspy-readable event file.
        :param `obspy.core.stream.Stream` st:
            Stream containing waveform data for template. Note that this
            should be the same length of stream as you will use for the
            continuous detection, e.g. if you detect in day-long files,
            give this a day-long file!
        :param bool process:
            Whether to process the data or not, defaults to True.

.. note::
    process_len should be set to the same length as used when computing
    detections using match_filter.match_filter, e.g. if you read
    in day-long data for match_filter, process_len should be 86400.

    .. rubric:: Example

>>> from obspy.clients.fdsn import Client
>>> from eqcorrscan.core.template_gen import template_gen
>>> client = Client('NCEDC')
>>> catalog = client.get_events(eventid='72572665', includearrivals=True)
>>> # We are only taking two picks for this example to speed up the
>>> # example, note that you don't have to!
>>> catalog[0].picks = catalog[0].picks[0:2]
>>> templates = template_gen(
...    method='from_client', catalog=catalog, client_id='NCEDC',
...    lowcut=2.0, highcut=9.0, samp_rate=20.0, filt_order=4, length=3.0,
...    prepick=0.15, swin='all', process_len=300, all_horiz=True)
>>> templates[0].plot(equal_scale=False, size=(800,600)) # doctest: +SKIP

.. figure:: ../../plots/template_gen.from_client.png

.. rubric:: Example

>>> from obspy import read
>>> from eqcorrscan.core.template_gen import template_gen
>>> # Get the path to the test data
>>> import eqcorrscan
>>> import os
>>> TEST_PATH = os.path.dirname(eqcorrscan.__file__) + '/tests/test_data'
>>> st = read(TEST_PATH + '/WAV/TEST_/' +
...           '2013-09-01-0410-35.DFDPC_024_00')
>>> quakeml = TEST_PATH + '/20130901T041115.xml'
>>> templates = template_gen(
...    method='from_meta_file', meta_file=quakeml, st=st, lowcut=2.0,
...    highcut=9.0, samp_rate=20.0, filt_order=3, length=2, prepick=0.1,
...    swin='S', all_horiz=True)
>>> print(len(templates[0]))
10
>>> templates = template_gen(
...    method='from_meta_file', meta_file=quakeml, st=st, lowcut=2.0,
...    highcut=9.0, samp_rate=20.0, filt_order=3, length=2, prepick=0.1,
...    swin='S_all', all_horiz=True)
>>> print(len(templates[0]))
15

.. rubric:: Example

>>> from eqcorrscan.core.template_gen import template_gen
>>> import glob
>>> # Get all the SAC-files associated with one event.
>>> sac_files = glob.glob(TEST_PATH + '/SAC/2014p611252/*')
>>> templates = template_gen(
...    method='from_sac', sac_files=sac_files, lowcut=2.0, highcut=10.0,
...    samp_rate=25.0, filt_order=4, length=2.0, swin='all', prepick=0.1,
...    all_horiz=True)
>>> print(templates[0][0].stats.sampling_rate)
25.0
>>> print(len(templates[0]))
15