def apply_tfa_magseries(lcfile,<EOL>timecol,<EOL>magcol,<EOL>errcol,<EOL>templateinfo,<EOL>mintemplatedist_arcmin=<NUM_LIT>,<EOL>lcformat='<STR_LIT>',<EOL>lcformatdir=None,<EOL>interp='<STR_LIT>',<EOL>sigclip=<NUM_LIT>):
try:<EOL><INDENT>formatinfo = get_lcformat(lcformat,<EOL>use_lcformat_dir=lcformatdir)<EOL>if formatinfo:<EOL><INDENT>(dfileglob, readerfunc,<EOL>dtimecols, dmagcols, derrcols,<EOL>magsarefluxes, normfunc) = formatinfo<EOL><DEDENT>else:<EOL><INDENT>LOGERROR("<STR_LIT>")<EOL>return None<EOL><DEDENT><DEDENT>except Exception as e:<EOL><INDENT>LOGEXCEPTION("<STR_LIT>")<EOL>return None<EOL><DEDENT>if isinstance(templateinfo,str) and os.path.exists(templateinfo):<EOL><INDENT>with open(templateinfo,'<STR_LIT:rb>') as infd:<EOL><INDENT>templateinfo = pickle.load(infd)<EOL><DEDENT><DEDENT>lcdict = readerfunc(lcfile)<EOL>if ((isinstance(lcdict, (tuple, list))) and<EOL>isinstance(lcdict[<NUM_LIT:0>], dict)):<EOL><INDENT>lcdict = lcdict[<NUM_LIT:0>]<EOL><DEDENT>objectid = lcdict['<STR_LIT>']<EOL>tmagseries = templateinfo[magcol][<EOL>'<STR_LIT>'<EOL>][::]<EOL>if objectid in templateinfo[magcol]['<STR_LIT>']:<EOL><INDENT>LOGWARNING('<STR_LIT>' %<EOL>objectid)<EOL>templateind = templateinfo[magcol]['<STR_LIT>'] == objectid<EOL>tmagseries = tmagseries[~templateind,:]<EOL><DEDENT>object_matches = coordutils.conesearch_kdtree(<EOL>templateinfo[magcol]['<STR_LIT>'],<EOL>lcdict['<STR_LIT>']['<STR_LIT>'], lcdict['<STR_LIT>']['<STR_LIT>'],<EOL>mintemplatedist_arcmin/<NUM_LIT><EOL>)<EOL>if len(object_matches) > <NUM_LIT:0>:<EOL><INDENT>LOGWARNING(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" %<EOL>(objectid, mintemplatedist_arcmin, len(object_matches))<EOL>)<EOL>removalind = np.full(<EOL>templateinfo[magcol]['<STR_LIT>'].size,<EOL>False, dtype=np.bool<EOL>)<EOL>removalind[np.array(object_matches)] = True<EOL>tmagseries = tmagseries[~removalind,:]<EOL><DEDENT>normal_matrix = np.dot(tmagseries, tmagseries.T)<EOL>normal_matrix_inverse = spla.pinv2(normal_matrix)<EOL>timebase = templateinfo[magcol]['<STR_LIT>']<EOL>reformed_targetlc = _reform_templatelc_for_tfa((<EOL>lcfile,<EOL>lcformat,<EOL>lcformatdir,<EOL>timecol,<EOL>magcol,<EOL>errcol,<EOL>timebase,<EOL>interp,<EOL>sigclip<EOL>))<EOL>scalar_products = np.dot(tmagseries, reformed_targetlc['<STR_LIT>'])<EOL>corrections = np.dot(normal_matrix_inverse, scalar_products)<EOL>corrected_magseries = (<EOL>reformed_targetlc['<STR_LIT>'] -<EOL>np.dot(tmagseries.T, corrections)<EOL>)<EOL>outdict = {<EOL>'<STR_LIT>':timebase,<EOL>'<STR_LIT>':corrected_magseries,<EOL>'<STR_LIT>':reformed_targetlc['<STR_LIT>'],<EOL>'<STR_LIT>':np.median(corrected_magseries),<EOL>'<STR_LIT>': np.median(np.abs(corrected_magseries -<EOL>np.median(corrected_magseries))),<EOL>'<STR_LIT>':{'<STR_LIT>':tmagseries,<EOL>'<STR_LIT>':normal_matrix,<EOL>'<STR_LIT>':normal_matrix_inverse,<EOL>'<STR_LIT>':scalar_products,<EOL>'<STR_LIT>':corrections,<EOL>'<STR_LIT>':reformed_targetlc},<EOL>}<EOL>lcdict['<STR_LIT>'] = outdict<EOL>outfile = os.path.join(<EOL>os.path.dirname(lcfile),<EOL>'<STR_LIT>' % (<EOL>squeeze(objectid).replace('<STR_LIT:U+0020>','<STR_LIT:->'),<EOL>magcol<EOL>)<EOL>)<EOL>with open(outfile,'<STR_LIT:wb>') as outfd:<EOL><INDENT>pickle.dump(lcdict, outfd, pickle.HIGHEST_PROTOCOL)<EOL><DEDENT>return outfile<EOL>
This applies the TFA correction to an LC given TFA template information.

    Parameters
    ----------

    lcfile : str
        This is the light curve file to apply the TFA correction to.

    timecol,magcol,errcol : str
        These are the column keys in the lcdict for the LC file to apply the TFA
        correction to.

    templateinfo : dict or str
        This is either the dict produced by `tfa_templates_lclist` or the pickle
        produced by the same function.

    mintemplatedist_arcmin : float
        This sets the minimum distance required from the target object for
        objects in the TFA template ensemble. Objects closer than this distance
        will be removed from the ensemble.

    lcformat : str
        This is the `formatkey` associated with your light curve format, which
        you previously passed in to the `lcproc.register_lcformat`
        function. This will be used to look up how to find and read the light
        curves specified in `basedir` or `use_list_of_filenames`.

    lcformatdir : str or None
        If this is provided, gives the path to a directory when you've stored
        your lcformat description JSONs, other than the usual directories lcproc
        knows to search for them in. Use this along with `lcformat` to specify
        an LC format JSON file that's not currently registered with lcproc.

    interp : str
        This is passed to scipy.interpolate.interp1d as the kind of
        interpolation to use when reforming this light curve to the timebase of
        the TFA templates.

    sigclip : float or sequence of two floats or None
        This is the sigma clip to apply to this light curve before running TFA
        on it.

    Returns
    -------

    str
        This returns the filename of the light curve file generated after TFA
        applications. This is a pickle (that can be read by `lcproc.read_pklc`)
        in the same directory as `lcfile`. The `magcol` will be encoded in the
        filename, so each `magcol` in `lcfile` gets its own output file.