# 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 || !status) {
        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;
    pmFPAfile *astrom = pmFPAfileBindFromArgs(&status, input, config, "PSWARP.ASTROM", "ASTROM");
    if (!status) {
        psError (PS_ERR_UNKNOWN, false, "failed to load find definition");
        return NULL;
    }
    if (astrom) {
        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(&status, input, config, "PSWARP.MASK", "MASK");
    if (!status) {
        psError (PS_ERR_UNKNOWN, false, "failed to load find definition");
        return NULL;
    }
    if (!inMask) {
        psLogMsg ("pswarp", 3, "no mask supplied\n");
    }

    pmFPAfile *inWeight = pmFPAfileBindFromArgs(&status, input, config, "PSWARP.WEIGHT", "WEIGHT");
    if (!status) {
        psError (PS_ERR_UNKNOWN, false, "failed to load find definition");
        return NULL;
    }
    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;
    }
    psFree(skyConfig);

    // The output skycell
    pmFPAfile *output = pmFPAfileDefineSkycell(config, NULL, "PSWARP.OUTPUT");
    if (!output) {
        psError(PSWARP_ERR_CONFIG, false, "Failed to build FPA from PSWARP.OUTPUT");
        return false;
    }
    if (!pmFPAfileDefineSkycell(config, output->fpa, "PSWARP.OUTPUT.MASK")) {
        psError(PSWARP_ERR_CONFIG, false, "Failed to build FPA from PSWARP.OUTPUT.MASK");
        return false;
    }
    if (inWeight) {
        if (!pmFPAfileDefineSkycell(config, output->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
    bool mdok;                          // Status of MD lookup
    char *chipLine = psMetadataLookupStr(&mdok, config->arguments, "CHIP_SELECTIONS");
    if (mdok) {
        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);
    }

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