/** @file censor.c
 *
 *  @brief
 *
 *  @ingroup censor
 *
 *  @author IfA
 *  @version $Revision: 1.28 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2009-02-07 02:03:34 $
 *  Copyright 2009 Institute for Astronomy, University of Hawaii
 */

# include "censor.h"

static void usage(void) {
    fprintf(stderr, "USAGE: censorObjects -file (sources) -masklist (masklist) (output)\n");
    fprintf(stderr, "       censorObjects -file (sources) -mask (mask.fits) (output)\n");
    fprintf(stderr, "       censorObjects -list (sourceslist) -masklist (masklist) (output)\n");
    fprintf(stderr, "       censorObjects -list (sourceslist) -mask (mask.fits) (output)\n");
    exit(PS_EXIT_CONFIG_ERROR);
}

int main (int argc, char **argv)
{
    psTimerStart ("complete");

    censorErrorRegister();              // register our error codes/messages

    // model inits are needed in pmSourceIO
    // models defined in psphot/src/models are not available in censor
    pmModelClassInit();

    // load configuration information
    pmConfig *config = censorArguments(argc, argv);
    if (!config) {
        usage();
    }

    censorVersionPrint();

    // load identify the data sources
    if (!censorParseCamera (config)) {
        psErrorStackPrint(stderr, "error setting up the camera\n");
        psFree(config);
        exit(PS_EXIT_CONFIG_ERROR);
    }

    bool status;
    bool checkInputOnly = psMetadataLookupBool(&status, config->arguments, 
        "CHECK.INPUT.ONLY");

    if (!checkInputOnly) {
        // load the raw detection data (using PSPHOT.SOURCES filerule)
        // select subset of stars for astrometry
        if (!censorLoop (config)) {
            psErrorStackPrint(stderr, "error loading input data\n");
            psFree(config);
            exit(PS_EXIT_DATA_ERROR);
        }

        psLogMsg("censor", 3, "complete censor run: %f sec\n", psTimerMark ("complete"));
    } else {
        if (!checkInputLoop (config)) {
            psErrorStackPrint(stderr, "error loading input data\n");
            psFree(config);
            exit(PS_EXIT_DATA_ERROR);
        }
        psLogMsg("censor", 3, "complete check input run: %f sec\n", psTimerMark ("complete"));
    }

    censorCleanup(config);
    exit(PS_EXIT_SUCCESS);
}
