# include "psastroInternal.h"

bool psastroAnalysis (pmConfig *config) {

    // interpret the available initial astrometric information
    // apply the initial guess
    if (!psastroAstromGuess (config)) {
	psError (PSASTRO_ERR_UNKNOWN, false, "failed to determine initial astrometry guess\n");
	return false;
    }

    // load the reference stars overlapping the data stars
    psArray *refs = psastroLoadRefstars(config);
    if (!refs) {
	psError (PSASTRO_ERR_UNKNOWN, false, "failed to load reference data\n");
	return false;
    }

    if (!psastroChooseRefstars (config, refs)) {
	psError (PSASTRO_ERR_UNKNOWN, false, "failed to select reference data for chips\n");
	return false;
    }

    // XXX does this check the recipe??
    bool chipastro = psMetadataLookupBool (NULL, config->arguments, "PSASTRO.CHIP.MODE");
    bool mosastro  = psMetadataLookupBool (NULL, config->arguments, "PSASTRO.MOSAIC.MODE");
    if (!chipastro && !mosastro) {
	psLogMsg ("psastro", 3, "no astrometry mode selected, assuming chip mode\n");
	chipastro = true;
    }

    if (chipastro) {
      if (!psastroChipAstrom (config)) {
	    psError (PSASTRO_ERR_UNKNOWN, false, "failed to perform single chip astrometry\n");
	    return false;
	}
    } 
    if (mosastro) {
	if (!psastroMosaicAstrom (config)) {
	    psError (PSASTRO_ERR_UNKNOWN, false, "failed to perform mosaic camera astrometry\n");
	    return false;
	}
    }

    // XXX how do we specify stack astrometry?
    // psastroStackAstrom (config, refs);

    psFree (refs);
    return true;
}
