# include "psastroInternal.h"

bool psastroAnalysis (pmConfig *config) {

    int nStars;

    // interpret the available initial astrometric information
    // apply the initial guess
    if (!psastroAstromGuess (&nStars, config)) {
	psError (PSASTRO_ERR_UNKNOWN, false, "failed to determine initial astrometry guess\n");
	return false;
    }
    if (nStars == 0) {
	psLogMsg ("psastro", 2, "skipping astrometry analysis : no stars\n");
	return true;
    }

    // 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;
}
