# include "pswarp.h"

static void usage (void) {
    fprintf(stderr, "USAGE: pswarp [-file image(s)] [-list imagelist] [options] (output) (skycell)\n");
    fprintf(stderr, "  options:\n");
    fprintf(stderr, "    [-astrom astrom.cmp] : provide an alternative astrometry calibration\n");
    fprintf(stderr, "    [-mask mask.fits] : provide a corresponding mask image\n");
    fprintf(stderr, "    [-weight weight.fits] : provide a corresponding weight image (pixel variance)\n");
    psErrorStackPrint(stderr, "\n");
    exit (2);
}

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

    psLibInit(NULL);

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

    // init various psphot pieces (errors, models, threads)
    psphotInit();

    pmConfig *config = pswarpArguments(argc, argv);
    if (!config) usage();

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

    if (!pswarpOptions(config)) {
        psErrorStackPrint(stderr, "error parsing options\n");
        exit(PS_EXIT_SYS_ERROR);
    }

    // load the skycell layout information
    if (!pswarpDefine(config)) {
        psErrorStackPrint(stderr, "error loading output definition\n");
        exit(PS_EXIT_CONFIG_ERROR);
    }

    // load and warp
    if (!pswarpLoop(config)) {
        psErrorStackPrint(stderr, "error warping data\n");
        exit(PS_EXIT_DATA_ERROR);
    }

    psLogMsg("pswarp", 3, "complete pswarp run: %f sec\n", psTimerMark("pswarp"));
    pswarpCleanup(config);
    psLibFinalize();
    exit(PS_EXIT_SUCCESS);
}
