# include "pswarp.h"

bool pswarpParseCamera (pmConfig *config) {

    bool status;
    pmFPAfile *skycell = NULL;
    pmConfig *skyConfig = NULL;

    // the input image(s) are required arguments; they define the camera
    status = false;
    pmFPAfile *input = pmFPAfileDefineFromArgs (&status, config, "PSWARP.INPUT", "INPUT");
    if (!input) {
        psError(PSWARP_ERR_CONFIG, false, "Failed to build FPA from PSWARP.INPUT");
        return false;
    }

    // the input image(s) are required arguments; they define the camera
    status = false;
    pmFPAfileDefineFromArgs (&status, config, "PSWARP.ASTROM", "ASTROM");
    if (status) {
        psLogMsg ("pswarp", 3, "using supplied astrometry\n");
    } else {
        psLogMsg ("pswarp", 3, "using header astrometry\n");
    }

    // the mask is not required - but must conform to input camera
    pmFPAfile *inMask = pmFPAfileBindFromArgs(NULL, input, config, "PSWARP.MASK", "MASK");
    if (!inMask) {
        psLogMsg ("pswarp", 3, "no mask supplied\n");
    }

    pmFPAfile *inWeight = pmFPAfileBindFromArgs(NULL, input, config, "PSWARP.WEIGHT", "WEIGHT");
    if (!inWeight) {
        psLogMsg ("pswarp", 3, "no weight supplied\n");
    }

    // the input skycell is a required argument: it defines the output image
    // XXX we may need a different skycell structure here
    status = pswarpDefineSkycell (&skycell, &skyConfig, config, "PSWARP.SKYCELL", "SKYCELL");
    if (!status) {
        psError(PSWARP_ERR_CONFIG, false, "Failed to build FPA from PSWARP.SKYCELL");
        return false;
    }

    // these calls bind the I/O handle to the specified fpa
    // NOTE: output file could be binned relative to sky cell...
    // if (!pmFPAfileDefineFromFPA (skyConfig, skycell->fpa, 1, 1, "PSWARP.OUTPUT")) {
    if (!pmFPAfileDefineOutput (skyConfig, skycell->fpa, "PSWARP.OUTPUT")) {
        psError(PSWARP_ERR_CONFIG, false, "Failed to build FPA from PSWARP.OUTPUT");
        return false;
    }
    if (inMask) {
        if (!pmFPAfileDefineOutput (skyConfig, skycell->fpa, "PSWARP.OUTPUT.MASK")) {
            psError(PSWARP_ERR_CONFIG, false, "Failed to build FPA from PSWARP.OUTPUT.MASK");
            return false;
        }
    }
    if (inWeight) {
        if (!pmFPAfileDefineOutput (skyConfig, skycell->fpa, "PSWARP.OUTPUT.WEIGHT")) {
            psError(PSWARP_ERR_CONFIG, false, "Failed to build FPA from PSWARP.OUTPUT.WEIGHT");
            return false;
        }
    }

    // Chip selection: turn on only the chips specified
    char *chipLine = psMetadataLookupStr(NULL, config->arguments, "CHIP_SELECTIONS");
    psArray *chips = psStringSplitArray (chipLine, ",", false);
    if (chips->n > 0) {
        pmFPASelectChip (input->fpa, -1, true); // deselect all chips
        for (int i = 0; i < chips->n; i++) {
            int chipNum = atoi(chips->data[i]);
            if (! pmFPASelectChip(input->fpa, chipNum, false)) {
                psError(PSWARP_ERR_CONFIG, true, "Chip number %d doesn't exist in camera.\n", chipNum);
                return false;
            }
        }
    }
    psFree (chips);
    psFree (skyConfig);

    psTrace("pswarp", 1, "Done with pswarpParseCamera...\n");
    return true;
}
