# include "psphotInternal.h"

bool psphotSkyReplace (pmConfig *config, const pmFPAview *view)
{
    bool status = true;

    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
    psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");

    // skip the chisq image (optionally?)
    int chisqNum = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.CHISQ.NUM");
    if (!status) chisqNum = -1;

    // loop over the available readouts
    for (int i = 0; i < num; i++) {
	if (i == chisqNum) continue; // skip chisq image
	if (!psphotSkyReplaceReadout (config, view, "PSPHOT.INPUT", i)) {
            psError (PSPHOT_ERR_CONFIG, false, "failed to replace sky for PSPHOT.INPUT entry %d", i);
	    return false;
	}
    }
    return true;
}

// XXX make this an option?
// in order to  successfully replace the sky, we must define a corresponding file...
bool psphotSkyReplaceReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index) {

    psTimerStart ("psphot.skyreplace");

    // find the currently selected readout
    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
    psAssert (file, "missing file?");

    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
    psAssert (readout, "missing readout?");

    // select background pixels, from output background file, or create
    pmReadout *background = pmFPAfileThisReadout (config->files, view, "PSPHOT.BACKGND");
    if (background == NULL) psAbort("background not defined");

    // select the corresponding images
    psF32 **image = readout->image->data.F32;
    psImageMaskType  **mask  = readout->mask->data.PS_TYPE_IMAGE_MASK_DATA;
    psF32 **back  = background->image->data.F32;

    // replace the background model
    for (int j = 0; j < readout->image->numRows; j++) {
        for (int i = 0; i < readout->image->numCols; i++) {
            if (!mask[j][i]) {
                image[j][i] += back[j][i];
            }
        }
    }
    psLogMsg ("psphot.sky", PS_LOG_DETAIL, "replace background flux : %f sec\n", psTimerMark ("psphot.skyreplace"));
    return true;
}

