/** @file ppSubLoop.c
 *
 *  @brief
 *
 *  @ingroup ppSub
 *
 *  @author IfA
 *  @version $Revision: 1.24 $ $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 <pslib.h>
#include <psmodules.h>

#include "ppSub.h"

bool ppSubLoop(ppSubData *data)
{
    psAssert(data, "Require processing data");
    pmConfig *config = data->config;    // Configuration
    psAssert(config, "Require configuration.");

    pmConfigCamerasCull(config, NULL);
    pmConfigRecipesCull(config, "PPSUB,PPSTATS,PSPHOT,MASKS,JPEG");


    pmFPAfile *input = psMetadataLookupPtr(NULL, config->files, "PPSUB.INPUT");
    pmFPAfile *reference = psMetadataLookupPtr(NULL, config->files, "PPSUB.REF");
    pmFPAfile *output = psMetadataLookupPtr(NULL, config->files, "PPSUB.OUTPUT");
    psAssert(input && reference && output, "Require files");

    if (!ppSubFilesIterateDown(config, PPSUB_FILES_INPUT | PPSUB_FILES_CONV)) {
        psError(PPSUB_ERR_IO, false, "Unable to load files.");
        return false;
    }

    psTimerStart("PPSUB_MATCH");

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

    if (!ppSubMatchPSFs(data)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to match PSFs.");
        return false;
    }
    if (data->quality) {
        // Can't do anything at all
        return true;
    }

    psMetadataAddF32(data->stats, PS_LIST_TAIL, "TIME_MATCH", 0, "Time to match PSFs",
                     psTimerClear("PPSUB_MATCH"));

    // Close input files
    if (!ppSubFilesIterateUp(config, PPSUB_FILES_INPUT)) {
        psError(PPSUB_ERR_IO, false, "Unable to close input files.");
        return false;
    }

    // Set up subtraction files
    if (!ppSubFilesIterateDown(config, PPSUB_FILES_SUB)) {
        psError(PPSUB_ERR_IO, false, "Unable to set up subtraction files.");
        return false;
    }

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

    if (!data->quality && !ppSubMakePSF(data)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to generate PSF.");
        return false;
    }

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

    // Close convolved files
    if (!ppSubFilesIterateUp(config, PPSUB_FILES_PSF | PPSUB_FILES_CONV)) {
        psError(PPSUB_ERR_IO, false, "Unable to close input files.");
        return false;
    }

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

    if (!ppSubFilesIterateDown(config, PPSUB_FILES_PHOT_SUB)) {
        psError(PPSUB_ERR_IO, false, "Unable to set up photometry files.");
        return false;
    }

    if (!data->quality && !ppSubReadoutPhotometry("PPSUB.OUTPUT", data)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to perform photometry.");
        return false;
    }

    if (!ppSubFilesIterateUp(config, PPSUB_FILES_PHOT_SUB)) {
        psError(PPSUB_ERR_IO, false, "Unable to set up photometry files.");
        return false;
    }

    // Perform statistics on the cell
    if (!ppSubReadoutStats(data)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to collect statistics");
        return false;
    }

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

    if (data->inverse) {
        // Set up inverse subtraction files
        if (!ppSubFilesIterateDown(config, PPSUB_FILES_INV)) {
            psError(PPSUB_ERR_IO, false, "Unable to set up inverse files.");
            return false;
        }

        if (data->inverse && !ppSubDefineOutput("PPSUB.INVERSE", config)) {
            psError(PS_ERR_UNKNOWN, false, "Unable to define inverse.");
            return false;
        }

        if (!ppSubReadoutInverse(config)) {
            psError(PS_ERR_UNKNOWN, false, "Unable to invert images.");
            return false;
        }

        // Close subtraction files and open inverse photometry files
        if (!ppSubFilesIterateUp(config, PPSUB_FILES_SUB)) {
            psError(PPSUB_ERR_IO, false, "Unable to close subtraction files.");
            return false;
        }

        if (!ppSubFilesIterateDown(config, PPSUB_FILES_PHOT_INV)) {
            psError(PPSUB_ERR_IO, false, "Unable to set up inverse files.");
            return false;
        }

        if (!data->quality && !ppSubReadoutPhotometry("PPSUB.INVERSE", data)) {
            psError(PS_ERR_UNKNOWN, false, "Unable to perform photometry.");
            return false;
        }

        // Close inverse subtraction files
        if (!ppSubFilesIterateUp(config, PPSUB_FILES_INV | PPSUB_FILES_PHOT_INV)) {
            psError(PPSUB_ERR_IO, false, "Unable to close subtraction files.");
            return false;
        }
    } else {
        // Close subtraction files
        if (!ppSubFilesIterateUp(config, PPSUB_FILES_SUB)) {
            psError(PPSUB_ERR_IO, false, "Unable to close subtraction files.");
            return false;
        }
    }

    return true;
}
