# include "psastroInternal.h"
# define NONLIN_TOL 0.001 /* tolerance in pixels */

bool psastroMosaicChipAstrom (pmFPA *fpa, psMetadata *recipe, int iteration) {

    pmChip *chip = NULL;
    pmCell *cell = NULL;
    pmReadout *readout = NULL;
    pmFPAview *view = pmFPAviewAlloc (0);

    // this loop selects the matched stars for all chips
    while ((chip = pmFPAviewNextChip (view, fpa, 1)) != NULL) {
        psTrace ("psastro", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
        if (!chip->process || !chip->file_exists) { continue; }
        if (!chip->toFPA) { continue; }

        while ((cell = pmFPAviewNextCell (view, fpa, 1)) != NULL) {
            psTrace ("psastro", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
            if (!cell->process || !cell->file_exists) { continue; }

            // process each of the readouts
            // XXX there can only be one readout per chip, right?
            while ((readout = pmFPAviewNextReadout (view, fpa, 1)) != NULL) {
                if (! readout->data_exists) { continue; }

                // save WCS and analysis metadata in update header
                psMetadata *updates = psMetadataAlloc();

                if (!psastroMosaicOneChip (chip, readout, recipe, updates, iteration)) {
                    readout->data_exists = false;
                    psError(PS_ERR_UNKNOWN, false, "failed to find a solution for %d,%d,%d\n",
                            view->chip, view->cell, view->readout);
                    psFree(updates);
                    psFree(view);
                    return false;
                }

                // create the header keywords to descripe the results
                pmAstromWriteBilevelChip (updates, chip, NONLIN_TOL);
                psMetadataAddMetadata (readout->analysis, PS_LIST_TAIL, "PSASTRO.HEADER",  PS_META_REPLACE, "psastro header stats", updates);
                psFree (updates);
            }
        }
    }
    psFree (view);
    return true;
}
