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

#include "ppImage.h"

bool ppImageRebinChip (pmConfig *config, pmFPAview *view, ppImageOptions *options, char *outName) {

    pmCell *cell;
    pmReadout *inReadout, *outReadout;

    pmFPAfile *outFile = psMetadataLookupPtr(NULL, config->files, outName);
    if (outFile == NULL) return false;

    // XXX double check that chip != -1?

    pmChip *inChip  = pmFPAviewThisChip (view, outFile->src);
    pmChip *outChip = pmFPAviewThisChip (view, outFile->fpa);
    if (!pmChipCopyStructure (outChip, inChip, outFile->xBin, outFile->yBin)) {
        psError(PS_ERR_UNKNOWN, false, "Unable to copy chip structure.");
        return false;
    }

    while ((cell = pmFPAviewNextCell (view, outFile->src, 1)) != NULL) {
        psLogMsg ("ppImageRebinChip", 5, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
        if (! cell->process || ! cell->file_exists) { continue; }

        // process each of the readouts
        while ((inReadout = pmFPAviewNextReadout (view, outFile->src, 1)) != NULL) {
            if (! inReadout->data_exists) { continue; }

            outReadout = pmFPAviewThisReadout (view, outFile->fpa);

            // run the rebin code
            if (!pmReadoutRebin(outReadout, inReadout, options->maskValue, outFile->xBin, outFile->yBin)) {
                psError(PS_ERR_UNKNOWN, false, "Unable to rebin readout.");
                return false;
            }
        }
    }

    return true;
}

