# include "psphotInternal.h"

// XXX we need to be able to distinguish several cases:
// 1) the particular output data was not requested
// 2) the particular output data was requested but was not generated (skipped)
// 3) the particular output data was requested but was not generated (for valid failure)
// 4) the particular output data was requested but was not generated (surprising failure)

// define the needed / desired I/O files
bool psphotDefineFiles (pmConfig *config, pmFPAfile *input) {

    bool status = false;

    // select recipe options supplied on command line
    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);

    // the output sources are carried on the input->fpa structures
    if (!pmFPAfileDefineOutput (config, input->fpa, "PSPHOT.OUTPUT")) {
        psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PSPHOT.OUTPUT");
        return false;
    }

    // if we are choosing not to save the output detections, mark this file inactive so it will
    // not be written, but will keep the detections in memory for other functions (eg, astrometry)
    if (!psMetadataLookupBool (NULL, recipe, "SAVE.OUTPUT")) {
        pmFPAfileActivate (config->files, false, "PSPHOT.OUTPUT");
    }

    // optionally save the residual image
    if (psMetadataLookupBool(NULL, recipe, "SAVE.RESID")) {
        if (!pmFPAfileDefineOutput (config, input->fpa, "PSPHOT.RESID")) {
            psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PSPHOT.RESID");
            return false;
        }
    }
    // optionally save the background model (small FITS image)
    if (psMetadataLookupBool(NULL, recipe, "SAVE.BACKMDL")) {
        int DX = psMetadataLookupS32 (&status, recipe, "BACKGROUND.XBIN");
        int DY = psMetadataLookupS32 (&status, recipe, "BACKGROUND.YBIN");
        if (!pmFPAfileDefineFromFPA (config, input->fpa, DX, DY, "PSPHOT.BACKMDL")) {
            psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PSPHOT.BACKMDL");
            return false;
        }
    }
    // optionally save the background model's standard deviation (small FITS image)
    if (psMetadataLookupBool(NULL, recipe, "SAVE.BACKMDL.STDEV")) {
        int DX = psMetadataLookupS32 (&status, recipe, "BACKGROUND.XBIN");
        int DY = psMetadataLookupS32 (&status, recipe, "BACKGROUND.YBIN");
        if (!pmFPAfileDefineFromFPA (config, input->fpa, DX, DY, "PSPHOT.BACKMDL.STDEV")) {
            psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PSPHOT.BACKMDL.STDEV");
            return false;
        }
    }
    // optionally save the full background image
    if (psMetadataLookupBool(NULL, recipe, "SAVE.BACKGND")) {
        if (!pmFPAfileDefineFromFPA (config, input->fpa,  1,  1, "PSPHOT.BACKGND")) {
            psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PSPHOT.BACKGND");
            return false;
        }
    }
    // optionally save the background-subtracted image
    if (psMetadataLookupBool(NULL, recipe, "SAVE.BACKSUB")) {
        if (!pmFPAfileDefineFromFPA (config, input->fpa,  1,  1, "PSPHOT.BACKSUB")) {
            psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PSPHOT.");
            return false;
        }
    }
    // optionally save the PSF Model
    if (psMetadataLookupBool(NULL, recipe, "SAVE.PSF")) {
        if (!pmFPAfileDefineOutput (config, input->fpa, "PSPHOT.PSF.SAVE")) {
            psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for PSPHOT.PSF.SAVE");
            return false;
        }
    }

    // optionally save output plots
    // allow specific plots only
    if (psMetadataLookupBool(NULL, recipe, "SAVE.PLOTS")) {
        if (!pmFPAfileDefineOutput (config, input->fpa, "SOURCE.PLOT.MOMENTS")) {
            psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for SOURCE.PLOT.MOMENTS");
            return false;
        }
        if (!pmFPAfileDefineOutput (config, input->fpa, "SOURCE.PLOT.PSFMODEL")) {
            psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for SOURCE.PLOT.PSFMODEL");
            return false;
        }
        if (!pmFPAfileDefineOutput (config, input->fpa, "SOURCE.PLOT.APRESID")) {
            psError(PSPHOT_ERR_CONFIG, false, "Cannot find a rule for SOURCE.PLOT.APRESID");
            return false;
        }
    }

    if (psMetadataLookupPtr(NULL, config->arguments, "SRC")) {
        if (!pmFPAfileDefineFromArgs (&status, config, "PSPHOT.INPUT.CMF", "SRC")) {
            psError(PSPHOT_ERR_CONFIG, false, "Failed to find/build PSPHOT.INPUT.CMF");
            return status;
        }
    }

    if (psMetadataLookupPtr(NULL, config->arguments, "PSPHOT.PSF")) {
        pmFPAfileBindFromArgs(&status, input, config, "PSPHOT.PSF.LOAD", "PSPHOT.PSF");
        if (!status) {
            psError(PSPHOT_ERR_CONFIG, false, "Failed to find/build PSPHOT.PSF.LOAD");
            return status;
        }
    }

    // XXX add in example PSF image thumbnails
    // pmFPAfileConstruct (config->files, format, config->camera, "PSPHOT.PSF_SAMPLE");

    return true;
}
