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

#include "dvoMakeCorr.h"

bool dvoMakeCorrLoop (pmConfig *config, dvoMakeCorrOptions *options) {

    bool status;
    pmChip *chip;
    pmCell *cell;
    pmReadout *readout;

    // psMetadata *recipe = psMetadataLookupMetadata(&status, config->recipes, RECIPE_NAME);

    pmFPAfile *input = psMetadataLookupPtr(&status, config->files, "DVOCORR.INPUT");
    if (!status) {
        psErrorStackPrint(stderr, "Can't find input data!\n");
        exit(EXIT_FAILURE);
    }

    pmFPAview *view = pmFPAviewAlloc(0);// View for level of interest

    // load data at FPA leve
    if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
	psError(PS_ERR_UNKNOWN, false, "failed IO for fpa in psphot\n");
	psFree(view);
        return false;
    }

    // process each chip in the FPA
    while ((chip = pmFPAviewNextChip(view, input->fpa, 1)) != NULL) {
        psLogMsg ("dvoMakeCorrLoop", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
        if (!chip->process || !chip->file_exists) continue;
	if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
            psError(PS_ERR_UNKNOWN, false, "failed IO for chip %d in dvoMakeCorr\n", view->chip);
	    psFree (view);
	    return false;
	}

	// process each cell in the Chip
        while ((cell = pmFPAviewNextCell(view, input->fpa, 1)) != NULL) {
            psLogMsg ("dvoMakeCorrLoop", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
            if (!cell->process || !cell->file_exists) continue;
            if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
		psError(PS_ERR_UNKNOWN, false, "failed IO for chip %d, cell %d in dvoMakeCorr\n", view->chip, view->cell);
		psFree (view);
		return false;
	    }

	    // process each of the readouts
	    while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
		if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
		    psError(PS_ERR_UNKNOWN, false, "failed IO for chip %d, cell %d, readout %d in dvoMakeCorr\n", view->chip, view->cell, view->readout);
		    psFree (view);
		    return false;
		}
		if (!readout->data_exists) continue;

		// XXX put the function here which unbins the input image
		dvoMakeCorrUnbin (config, view, "DVOCORR.OUTPUT", "DVOCORR.INPUT", "DVOCORR.REFHEAD");

		if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) {
		    psError(PS_ERR_UNKNOWN, false, "failed IO for chip %d, cell %d, readout %d in dvoMakeCorr\n", view->chip, view->cell, view->readout);
		    psFree (view);
		    return false;
		}
	    }
	    if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) {
		psError(PS_ERR_UNKNOWN, false, "failed IO for chip %d, cell %d in dvoMakeCorr\n", view->chip, view->cell);
		psFree (view);
		return false;
	    }
	}
	if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) {
	    psError(PS_ERR_UNKNOWN, false, "failed IO for chip %d in dvoMakeCorr\n", view->chip);
	    psFree (view);
	    return false;
	}
    }
    if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) {
	psError(PS_ERR_UNKNOWN, false, "failed IO for fpa in dvoMakeCorr\n");
	psFree (view);
	return false;
    }

    psFree (view);

    // fail if we failed to handle an error
    if (psErrorCodeLast() != PS_ERR_NONE) return false;

    return true;
}
