# 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.skyreplace");

    // 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;
    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;
}

