# include "psphotInternal.h"

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

    psTimerStart ("psphot");

    // find the currently selected readout
    pmReadout *readout = pmFPAfileThisReadout (config->files, view, "PSPHOT.INPUT");
    if (readout == NULL) psAbort("input not defined");

    // 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;
    psU8  **mask  = readout->mask->data.U8;
    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"));
    return true;
}

