/** @file ppSubReadout.c
 *
 *  @brief
 *
 *  @ingroup ppSub
 *
 *  @author IfA
 *  @version $Revision: 1.113 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2009-02-18 00:31:20 $
 *  Copyright 2009 Institute for Astronomy, University of Hawaii
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <pslib.h>
#include <psmodules.h>

#include "ppSub.h"

bool ppSubReadout(pmConfig *config, psMetadata *stats, const pmFPAview *view)
{
    psTimerStart("PPSUB_MATCH");

    if (!ppSubSetMasks(config, view)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to set masks.");
        return false;
    }

    if (!ppSubMatchPSFs(config, view)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to match PSFs.");
        return false;
    }

    if (!ppSubDefineOutput(config, view)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to define output.");
        return false;
    }

    if (!ppSubMakePSF(config, view)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to generate PSF.");
        return false;
    }

    if (!ppSubReadoutSubtract(config, view)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to subtract images.");
        return false;
    }

    // Higher order background subtraction using psphot
    if (!ppSubBackground(config, view)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to subtract background.");
        return false;
    }

    if (!ppSubReadoutPhotometry(config, stats, view)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to perform photometry.");
        return false;
    }

    if (!ppSubReadoutUpdate(config, stats, view)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to update.");
        return false;
    }

    return true;
}
