# include "relphot.h"
void usage (void);

int args (int argc, char **argv) {

  int N;
  double trange;

  /* define time */
  TimeSelect = FALSE;
  if ((N = get_argument (argc, argv, "-time"))) {
    remove_argument (N, &argc, argv);
    if (!ohana_str_to_time (argv[N], &TSTART)) { 
      fprintf (stderr, "ERROR: syntax error\n");
      return (FALSE);
    }
    remove_argument (N, &argc, argv);
    if (!ohana_str_to_dtime (argv[N], &trange)) { 
      if (!ohana_str_to_time (argv[N], &TSTOP)) { 
	fprintf (stderr, "ERROR: syntax error\n");
	return (FALSE);
      }
    } else {
      if (trange < 0) {
	trange = fabs (trange);
	TSTOP = TSTART;
	TSTART -= trange;
      } else {
	TSTOP = TSTART + trange;
      }
    }
    remove_argument (N, &argc, argv);
    TimeSelect = TRUE;
  }

  /* specify portion of the sky */
  UserPatch.Rmin = 0;
  UserPatch.Rmax = 360;
  UserPatch.Dmin = -90;
  UserPatch.Dmax = +90;
  UserPatchSelect = FALSE;
  if ((N = get_argument (argc, argv, "-region"))) {
    remove_argument (N, &argc, argv);
    UserPatch.Rmin = atof (argv[N]);
    remove_argument (N, &argc, argv);
    UserPatch.Rmax = atof (argv[N]);
    remove_argument (N, &argc, argv);
    UserPatch.Dmin = atof (argv[N]);
    remove_argument (N, &argc, argv);
    UserPatch.Dmax = atof (argv[N]);
    remove_argument (N, &argc, argv);
    UserPatchSelect = TRUE;
  }

  VERBOSE = FALSE;
  if ((N = get_argument (argc, argv, "-v"))) {
    VERBOSE = TRUE;
    remove_argument (N, &argc, argv);
  }

  PLOTSTUFF = FALSE;
  if ((N = get_argument (argc, argv, "-plot"))) {
    PLOTSTUFF = TRUE;
    remove_argument (N, &argc, argv);
  }

  PLOTDELAY = 500000;
  if ((N = get_argument (argc, argv, "-plotdelay"))) {
    remove_argument (N, &argc, argv);
    PLOTDELAY = 1e6*atof(argv[N]);
    remove_argument (N, &argc, argv);
  }

  if ((N = get_argument (argc, argv, "-outroot"))) {
    remove_argument (N, &argc, argv);
    OUTROOT = strcreate (argv[N]);
    remove_argument (N, &argc, argv);
  } else {
    OUTROOT = strcreate ("relphot");
  }      

  strcpy (STATMODE, "CHI_INNER_WTMEAN");
  if ((N = get_argument (argc, argv, "-statmode"))) {
    remove_argument (N, &argc, argv);
    strcpy (STATMODE, argv[N]);
    remove_argument (N, &argc, argv);
  }

  NLOOP = 8;
  if ((N = get_argument (argc, argv, "-n"))) {
    remove_argument (N, &argc, argv);
    NLOOP = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }
  if ((N = get_argument (argc, argv, "-nloop"))) {
    remove_argument (N, &argc, argv);
    NLOOP = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }

  NGRID = 8;
  if ((N = get_argument (argc, argv, "-ngrid"))) {
    remove_argument (N, &argc, argv);
    NGRID = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }

  RESET = FALSE;
  if ((N = get_argument (argc, argv, "-reset"))) {
    remove_argument (N, &argc, argv);
    RESET = TRUE;
  }

  UPDATE = FALSE;
  if ((N = get_argument (argc, argv, "-update"))) {
    remove_argument (N, &argc, argv);
    UPDATE = TRUE;
  }

  SHOW_PARAMS = FALSE;
  if ((N = get_argument (argc, argv, "-params"))) {
    remove_argument (N, &argc, argv);
    SHOW_PARAMS = TRUE;
  }

  PlotMmin = 10.0; PlotMmax = 20.0; PlotdMmin = -1.0; PlotdMmax = 1.0;
  if ((N = get_argument (argc, argv, "-plrange"))) {
    remove_argument (N, &argc, argv);
    PlotMmin = atof (argv[N]);
    remove_argument (N, &argc, argv);
    PlotMmax = atof (argv[N]);
    remove_argument (N, &argc, argv);
    PlotdMmin = atof (argv[N]);
    remove_argument (N, &argc, argv);
    PlotdMmax = atof (argv[N]);
    remove_argument (N, &argc, argv);
  }

  /* XXX this argument used to do two things: specify the camera name and tell the analysis to
     calculate a common zero point for a single mosaic.  I've moved the MOSAICNAME concept into the
     config system, but need to use this argument to specify that the mosaic zeropoints should be
     calculated. */
  MOSAIC_ZEROPT = FALSE;
  if ((N = get_argument (argc, argv, "-mosaic"))) {
    remove_argument (N, &argc, argv);
    MOSAIC_ZEROPT = TRUE;
  }

  FREEZE_IMAGES = FALSE;
  if ((N = get_argument (argc, argv, "-imfreeze"))) {
    remove_argument (N, &argc, argv);
    FREEZE_IMAGES = TRUE;
  }

  FREEZE_MOSAICS = FALSE;
  if ((N = get_argument (argc, argv, "-mosfreeze"))) {
    remove_argument (N, &argc, argv);
    FREEZE_MOSAICS = TRUE;
  }

  USE_GRID = FALSE;
  if ((N = get_argument (argc, argv, "-grid"))) {
    remove_argument (N, &argc, argv);
    USE_GRID = TRUE;
  }

  MIN_ERROR = 0.001;
  if ((N = get_argument (argc, argv, "-minerror"))) {
    remove_argument (N, &argc, argv);
    MIN_ERROR = atof (argv[N]);
    remove_argument (N, &argc, argv);
    /* require MIN_ERROR > 0 */
  }  

  AreaSelect = FALSE;
  if ((N = get_argument (argc, argv, "-area"))) {
    remove_argument (N, &argc, argv);
    AreaXmin = atof (argv[N]);
    remove_argument (N, &argc, argv);
    AreaXmax = atof (argv[N]);
    remove_argument (N, &argc, argv);
    AreaYmin = atof (argv[N]);
    remove_argument (N, &argc, argv);
    AreaYmax = atof (argv[N]);
    remove_argument (N, &argc, argv);
    AreaSelect = TRUE;
  }

  ImagSelect = FALSE;
  if ((N = get_argument (argc, argv, "-instmag"))) {
    remove_argument (N, &argc, argv);
    ImagMin = atof (argv[N]);
    remove_argument (N, &argc, argv);
    ImagMax = atof (argv[N]);
    remove_argument (N, &argc, argv);
    ImagSelect = TRUE;
  }

  DophotSelect = FALSE;
  if ((N = get_argument (argc, argv, "-dophot"))) {
    remove_argument (N, &argc, argv);
    DophotValue = atof (argv[N]);
    remove_argument (N, &argc, argv);
    DophotSelect = TRUE;
  }

  UpdateAverages = FALSE;
  if ((N = get_argument (argc, argv, "-averages"))) {
    remove_argument (N, &argc, argv);
    UpdateAverages = TRUE;
  }

  if (UpdateAverages && (argc == 1)) return TRUE;
  if (UserPatchSelect && (argc == 2)) return TRUE;
  if (argc != 3) usage ();

  return TRUE;
}

void usage () {
  fprintf (stderr, "ERROR: USAGE: relphot (region) (photcode)\n");
  fprintf (stderr, "       or:    relphot (photcode) -region RA RA DEC DEC\n");
  fprintf (stderr, "       or:    relphot -averages -region RA RA DEC DEC\n");
  fprintf (stderr, "  options: \n");
  fprintf (stderr, "  -time (start) (stop)\n");
  fprintf (stderr, "  -v\n");
  fprintf (stderr, "  -outroot (outroot)\n");
  fprintf (stderr, "  -plot\n");
  fprintf (stderr, "  -plotdelay (seconds)\n");
  fprintf (stderr, "  -statmode (mode)\n");
  fprintf (stderr, "  -n (nloop)\n");
  fprintf (stderr, "  -reset\n");
  fprintf (stderr, "  -update\n");
  fprintf (stderr, "  -params\n");
  fprintf (stderr, "  -mosaic (mosaic)\n");
  fprintf (stderr, "  -imfreeze\n");
  fprintf (stderr, "  -grid\n");
  fprintf (stderr, "  -area Xmin Xmax Ymin Ymax\n");
  fprintf (stderr, "  -instmag min max\n");
  fprintf (stderr, "  \n");
  exit (2);
} 
