#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "dvoApplyCorr.h"

// this function is operating on each readout, assuming there is a correspondence between
// readouts in the input (raw) flat-field image and the correction frame.  this is not really
// true

bool dvoApplyCorrReadout (pmConfig *config, pmFPAview *view, char *inName, char *corrName) {
    
    bool status;

    pmFPAfile *inFile = psMetadataLookupPtr (&status, config->files, inName);
    pmFPAfile *corrFile = psMetadataLookupPtr (&status, config->files, corrName);
    assert (inFile);
    assert (corrFile);

    pmReadout *inData = pmFPAviewThisReadout (view, inFile->fpa);
    pmReadout *corrData = pmFPAviewThisReadout (view, corrFile->fpa);
    assert (inData);
    assert (corrData);

    // dimensions of input image:
    assert (inData->image->numCols == corrData->image->numCols);
    assert (inData->image->numRows == corrData->image->numRows);

    // NOTE : the output pmFPA points at the same pixels as the input pmFPA

    // the corr image is a multiplicative factor
    for (int j = 0; j < inData->image->numRows; j++) {
	for (int i = 0; i < inData->image->numCols; i++) {
	    inData->image->data.F32[j][i] *= corrData->image->data.F32[j][i]; 
	}
    }

    // psFits *fits = psFitsOpen ("tmp.fits", "w");
    // psFitsWriteImage (fits, NULL, inData->image, 0, NULL);
    // psFitsClose (fits);
    // exit (0);

    return true;
}
