# include "relphot.h"

RelphotMode initialize (int argc, char **argv) {

  relphot_help (argc, argv);
  ConfigInit (&argc, argv);
  RelphotMode mode = args (argc, argv);
  if (!mode) exit (2);

  // UPDATE_AVERAGES always operates on all photcodes? 
  if (mode == UPDATE_AVERAGES) {
    char tmpline1[256];
    int Ns;
    Nphotcodes = GetPhotcodeNsecfilt ();
    ALLOCATE (photcodes, PhotCode *, Nphotcodes);
    ALLOCATE (PhotcodeList, char, 256);
    for (Ns = 0; Ns < Nphotcodes; Ns++) {
      photcodes[Ns] = GetPhotcodebyNsec (Ns);
      if (Ns > 0) {
	snprintf (tmpline1, 256, "%s,%s", PhotcodeList, photcodes[Ns][0].name);
      } else {
	snprintf (tmpline1, 256, "%s", photcodes[Ns][0].name);
      }
      strcpy (PhotcodeList, tmpline1);
    }
  }    
    
  if (USE_GRID && (Nphotcodes > 1)) {
    fprintf (stderr, "grid correction analysis currently can only operate on a single photcode\n");
    exit (1);
  }

  IMAGE_BAD = ID_IMAGE_PHOTOM_POOR | ID_IMAGE_PHOTOM_FEW | ID_IMAGE_PHOTOM_SKIP;
  STAR_BAD  = ID_STAR_POOR | ID_STAR_FEW;
  MEAS_BAD  = ID_MEAS_NOCAL | ID_MEAS_POOR_PHOTOM | ID_MEAS_SKIP_PHOTOM | ID_MEAS_AREA;

  if (SHOW_PARAMS) {
    fprintf (stderr, "current parameter settings:\n");
    if (TimeSelect) {
      fprintf (stderr, "TimeSelect: TRUE (%s - %s)\n", ohana_sec_to_date (TSTART), ohana_sec_to_date (TSTOP));
    } else {
      fprintf (stderr, "TimeSelect: FALSE\n");
    }
    fprintf (stderr, "VERBOSE: %d, PLOTSTUFF: %d\n", VERBOSE, PLOTSTUFF);
    fprintf (stderr, "GRID_X: %d, GRID_Y: %d, BINNING: %d == Nmx: %d, Nmy: %d\n", 
	     RELPHOT_GRID_X, 
	     RELPHOT_GRID_Y, 
	     RELPHOT_GRID_BINNING, 
	     (int)(RELPHOT_GRID_X/RELPHOT_GRID_BINNING), (int)(RELPHOT_GRID_Y/RELPHOT_GRID_BINNING));

    fprintf (stderr, "MAG_LIM                %lf\n", MAG_LIM);
    fprintf (stderr, "STAR_SCATTER           %lf\n", STAR_SCATTER);
    fprintf (stderr, "IMAGE_SCATTER          %lf\n", IMAGE_SCATTER);
    fprintf (stderr, "IMAGE_OFFSET           %lf\n", IMAGE_OFFSET);
    fprintf (stderr, "IMAGE_CATALOG          %s\n",  ImageCat);
    fprintf (stderr, "GSCFILE                %s\n",  GSCFILE);
    fprintf (stderr, "CATDIR                 %s\n",  CATDIR);
    exit (0);
  }

  // init the random seed
  long A, B;
  A = time(NULL);
  for (B = 0; A == time(NULL); B++);
  srand48(B);

  return mode;
}

void initialize_client (int argc, char **argv) {

  // XXX need to determine which globals can affect relphot_client in either mode and pass appropriately

  relphot_client_help (argc, argv);
  ConfigInit (&argc, argv);
  args_client (argc, argv);

  if (MODE == MODE_UPDATE_OBJECTS) {
    char tmpline1[256];
    int Ns;
    Nphotcodes = GetPhotcodeNsecfilt ();
    ALLOCATE (photcodes, PhotCode *, Nphotcodes);
    ALLOCATE (PhotcodeList, char, 256);
    for (Ns = 0; Ns < Nphotcodes; Ns++) {
      photcodes[Ns] = GetPhotcodebyNsec (Ns);
      if (Ns > 0) {
	snprintf (tmpline1, 256, "%s,%s", PhotcodeList, photcodes[Ns][0].name);
      } else {
	snprintf (tmpline1, 256, "%s", photcodes[Ns][0].name);
      }
      strcpy (PhotcodeList, tmpline1);
    }
    return;
  }

  // load the list of photcodes into the globals (photcodes, Nphotcodes)
  PhotcodeList = strcreate (argv[1]);
  photcodes = ParsePhotcodeList (PhotcodeList, &Nphotcodes, TRUE); // require SEC photcodes
}

void ParsePhotcodeList_old (char *word) {

  Nphotcodes = 0;
  photcodes = NULL;
  int NPHOTCODES = 10;
  ALLOCATE (photcodes, PhotCode *, NPHOTCODES);

  /* parse the comma-separated list of photcodesKeep */
  char *myList = strcreate(word);
  char *list = myList;
  char *codename = NULL;
  char *ptr = NULL;
  while ((codename = strtok_r (list, ",", &ptr)) != NULL) {
    list = NULL; // pass NULL on successive strtok_r calls
    if ((photcodes[Nphotcodes] = GetPhotcodebyName (codename)) == NULL) {
      fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", codename);
      exit (1);
    }
    if (photcodes[Nphotcodes][0].type != PHOT_SEC) {
      fprintf (stderr, "photcode %s is not an filter type (SEC)\n", codename);
      exit (1);
    }
    Nphotcodes ++;
    CHECK_REALLOCATE (photcodes, PhotCode *, NPHOTCODES, Nphotcodes, 10);
  }
}

