def align_traces(trace_list, shift_len, master=False, positive=False,<EOL>plot=False):
from eqcorrscan.core.match_filter import normxcorr2<EOL>from eqcorrscan.utils.plotting import xcorr_plot<EOL>traces = deepcopy(trace_list)<EOL>if not master:<EOL><INDENT>master = traces[<NUM_LIT:0>]<EOL>MAD_master = np.median(np.abs(master.data))<EOL>for i in range(<NUM_LIT:1>, len(traces)):<EOL><INDENT>if np.median(np.abs(traces[i].data)) > MAD_master:<EOL><INDENT>master = traces[i]<EOL>MAD_master = np.median(np.abs(master.data))<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>print('<STR_LIT>')<EOL><DEDENT>shifts = []<EOL>ccs = []<EOL>for i in range(len(traces)):<EOL><INDENT>if not master.stats.sampling_rate == traces[i].stats.sampling_rate:<EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>cc_vec = normxcorr2(template=traces[i].data.<EOL>astype(np.float32)[shift_len:-shift_len],<EOL>image=master.data.astype(np.float32))<EOL>cc_vec = cc_vec[<NUM_LIT:0>]<EOL>shift = np.abs(cc_vec).argmax()<EOL>cc = cc_vec[shift]<EOL>if plot:<EOL><INDENT>xcorr_plot(template=traces[i].data.<EOL>astype(np.float32)[shift_len:-shift_len],<EOL>image=master.data.astype(np.float32), shift=shift,<EOL>cc=cc)<EOL><DEDENT>shift -= shift_len<EOL>if cc < <NUM_LIT:0> and positive:<EOL><INDENT>cc = cc_vec.max()<EOL>shift = cc_vec.argmax() - shift_len<EOL><DEDENT>shifts.append(shift / master.stats.sampling_rate)<EOL>ccs.append(cc)<EOL><DEDENT>return shifts, ccs<EOL>
Align traces relative to each other based on their cross-correlation value.

Uses the :func:`eqcorrscan.core.match_filter.normxcorr2` function to find
the optimum shift to align traces relative to a master event.  Either uses
a given master to align traces, or uses the trace with the highest MAD
amplitude.

:type trace_list: list
:param trace_list: List of traces to align
:type shift_len: int
:param shift_len: Length to allow shifting within in samples
:type master: obspy.core.trace.Trace
:param master: Master trace to align to, if set to False will align to \
    the largest amplitude trace (default)
:type positive: bool
:param positive: Return the maximum positive cross-correlation, or the \
    absolute maximum, defaults to False (absolute maximum).
:type plot: bool
:param plot: If true, will plot each trace aligned with the master.

:returns: list of shifts and correlations for best alignment in seconds.
:rtype: list