# include "dvo.h"
RegImage *load_imreg (char *DataBase, int *nimage);

# define NVALUE 22
enum {V_NONE, V_EXPTIME, V_CCDN, V_SKY, V_BIAS, V_FILTER, V_FWHM, V_AIRM, V_TIME, V_TEMP0, V_TEMP1, V_TEMP2, V_TEMP3, V_TELFOCUS, V_XPROBE, V_YPROBE, V_ZPROBE, V_RA, V_DEC, V_DETTEMP, V_ROTANGLE, V_REGTIME};
static char valuename[NVALUE][32] = {"none", "exptime", "ccd", "sky", "bias", "filter", "fwhm", "airmass", "time", "temp0", "temp1", "temp2", "temp3", "telfocus", "xprobe", "yprobe", "zprobe", "ra", "dec", "dettemp", "rotangle", "regtime"};

# define NTYPE 9
enum {T_NONE, T_OBJECT, T_DARK, T_BIAS, T_FLAT, T_MASK, T_FRINGE, T_SCATTER, T_MODES};
static char typename[NTYPE][32] = {"none", "object", "dark", "bias", "flat", "mask", "fringe", "scatter", "modes"};

# define NMODE 6
enum {M_NONE, M_MEG, M_SPLIT, M_SINGLE, M_CUBE, M_SLICE};
static char modename[NMODE][32] = {"none", "MEF", "SPLIT", "SINGLE", "CUBE", "SLICE"};

imrough (int argc, char **argv) {
 
  int i, N, Nimage, TimeSelect;
  int ModeSelect, TypeSelect, CCDSelect, FilterSelect, TimeFormat;
  int type, value, mode, CCD, NVEC;
  char DataBase[256], *Filter;
  double trange;
  float *Vec;
  unsigned long tzero, tend, TimeReference;
  RegImage *image;
  Vector *vec;

  VarConfig ("REGISTRATION_DATABASE", "%s", DataBase);

  TimeSelect = FALSE;
  if (N = get_argument (argc, argv, "-time")) {
    remove_argument (N, &argc, argv);
    if (!str_to_time (argv[N], &tzero)) { 
      fprintf (stderr, "syntax error\n");
      return (FALSE);
    }
    remove_argument (N, &argc, argv);
    if (!str_to_dtime (argv[N], &trange)) { 
      fprintf (stderr, "syntax error\n");
      return (FALSE);
    }
    remove_argument (N, &argc, argv);
    if (trange < 0) {
      trange = fabs (trange);
      tzero -= trange;
    }
    TimeSelect = TRUE;
  }
  if (N = get_argument (argc, argv, "-trange")) {
    remove_argument (N, &argc, argv);
    if (!str_to_time (argv[N], &tzero)) { 
      fprintf (stderr, "syntax error\n");
      return (FALSE);
    }
    remove_argument (N, &argc, argv);
    if (!str_to_time (argv[N], &tend)) { 
      fprintf (stderr, "syntax error\n");
      return (FALSE);
    }
    remove_argument (N, &argc, argv);
    trange = tend - tzero;
    if (trange < 0) {
      trange = fabs (trange);
      tzero -= trange;
    }
    TimeSelect = TRUE;
  }
 
  type = T_NONE;
  TypeSelect = FALSE;
  if (N = get_argument (argc, argv, "-type")) {
    TypeSelect = TRUE;
    remove_argument (N, &argc, argv);
    for (i = 0; (i < NTYPE) && (type == T_NONE); i++) {
      if (!strncasecmp (argv[N], typename[i], strlen(argv[N]))) type = i;
    }
    if (type == T_NONE) {
      fprintf (stderr, "ERROR: invalid image type %s\n", argv[N]);
      return (FALSE);
    }
    remove_argument (N, &argc, argv);
  }

  mode = M_NONE;
  ModeSelect = FALSE;
  if (N = get_argument (argc, argv, "-mode")) {
    ModeSelect = TRUE;
    remove_argument (N, &argc, argv);
    for (i = 0; (i < NMODE) && (mode == M_NONE); i++) {
      if (!strncasecmp (argv[N], modename[i], strlen(argv[N]))) mode = i;
    }
    if (mode == M_NONE) {
      fprintf (stderr, "ERROR: invalid image mode %s\n", argv[N]);
      return (FALSE);
    }
    remove_argument (N, &argc, argv);
  }

  CCDSelect = FALSE;
  if (N = get_argument (argc, argv, "-ccd")) {
    remove_argument (N, &argc, argv);
    CCD = atoi (argv[N]);
    remove_argument (N, &argc, argv);
    CCDSelect = TRUE;
  }
 
  FilterSelect = FALSE;
  if (N = get_argument (argc, argv, "-filter")) {
    remove_argument (N, &argc, argv);
    Filter = strcreate (argv[N]);
    remove_argument (N, &argc, argv);
    FilterSelect = TRUE;
    if (!strcasecmp (Filter, "X")) {
      FilterSelect = FALSE;
    }
  }

  if (argc != 2) {
    fprintf (stderr, "USAGE: imrough (value)\n");
    fprintf (stderr, "       value options:\n");
    for (i = 1; i < NVALUE; i++) {
      fprintf (stderr, "%s\n", valuename[i]);
    }
    return (FALSE);
  }
 
  /* identify selection */
  value = V_NONE;
  for (i = 0; (i < NVALUE) && (value == V_NONE); i++) {
    if (!strncasecmp (argv[1], valuename[i], strlen(argv[1]))) value = i;
  }
  if (value == V_NONE) {
    fprintf (stderr, "ERROR: invalid image value %s\n", argv[1]);
    return (FALSE);
  }

  if ((vec = SelectVector (argv[1], ANYVECTOR, TRUE)) == NULL) return (FALSE);

  image = load_imreg (DataBase, &Nimage);
  if (image == (RegImage *) NULL) return (FALSE);

  N = 0;
  NVEC = 1000;
  REALLOCATE (vec[0].elements, float, NVEC);
  Vec = vec[0].elements;

  GetTimeFormat (&TimeReference, &TimeFormat);

  /* get data */
  for (i = 0; i < Nimage; i++) {
    /* skip unmatched selections */
    if (TimeSelect && ((image[i].obstime < tzero) || (image[i].obstime > tzero + trange))) continue;
    if (TimeSelect && ((image[i].obstime < tzero) || (image[i].obstime > tzero + trange))) continue;
    if (FilterSelect && (strcasecmp (image[i].filter, Filter))) continue;
    if (CCDSelect && (image[i].ccd != CCD)) continue;
    if (TypeSelect && (image[i].type != type)) continue;
    if (ModeSelect && (image[i].mode != mode)) continue;

    /* assign correct value */
    switch (value) {
    case (V_EXPTIME):
      Vec[N] = image[i].exptime;
      break;
    case (V_CCDN):
      Vec[N] = image[i].ccd;
      break;
    case (V_SKY):
      Vec[N] = image[i].sky;
      break;
    case (V_BIAS):
      Vec[N] = image[i].bias;
      break;
    case (V_FILTER):
      Vec[N] = image[i].filter[0];
      break;
    case (V_FWHM):
      Vec[N] = image[i].fwhm;
      break;
    case (V_AIRM):
      Vec[N] = image[i].airmass;
      break;
    case (V_TIME):
      Vec[N] = TimeValue (image[i].obstime, TimeReference, TimeFormat);
      break;
    case (V_TEMP0):
      Vec[N] = image[i].teltemp[0];
      break;
    case (V_TEMP1):
      Vec[N] = image[i].teltemp[1];
      break;
    case (V_TEMP2):
      Vec[N] = image[i].teltemp[2];
      break;
    case (V_TEMP3):
      Vec[N] = image[i].teltemp[3];
      break;
    case (V_TELFOCUS):
      Vec[N] = image[i].telfocus;
      break;
    case (V_XPROBE):
      Vec[N] = image[i].xprobe;
      break;
    case (V_YPROBE):
      Vec[N] = image[i].yprobe;
      break;
    case (V_ZPROBE):
      Vec[N] = image[i].zprobe;
      break;
    case (V_RA):
      Vec[N] = image[i].ra;
      break;
    case (V_DEC):
      Vec[N] = image[i].dec;
      break;
    case (V_DETTEMP):
      Vec[N] = image[i].dettemp;
      break;
    case (V_ROTANGLE):
      Vec[N] = image[i].rotangle;
      break;
    case (V_REGTIME):
      Vec[N] = TimeValue (image[i].regtime, TimeReference, TimeFormat);
      break;
    }
    N++;
    if (N >= NVEC - 1) {
      NVEC += 1000;
      REALLOCATE (vec[0].elements, float, NVEC);
      Vec = vec[0].elements;
    }
  }

  REALLOCATE (vec[0].elements, float, MAX (1,N));
  vec[0].Nelements = N;

  free (image);
  return (TRUE);

}

RegImage *load_imreg (char *DataBase, int *nimage) {

  int Nimage, status;
  char line[80];
  FILE *f;
  Header header, theader;
  Matrix matrix;
  FTable table;
  RegImage *image;

  *nimage = 0;

  /* open database */
  f = fopen (DataBase, "r");
  if (f == (FILE *) NULL) {
    fprintf (stderr, "ERROR: can't open Registration Database\n");
    return ((RegImage *) NULL);
  }

  /* load in database header */
  if (!fits_fread_header (f, &header)) {
    fclose (f);
    fits_free_header (&header);
    fprintf (stderr, "ERROR: trouble reading database header\n");
    return ((RegImage *) NULL);
  }

  /* check for database v1, v2 */
  fits_scan (&header, "ORIGIN", "%s", 1, line);
  if (!strcmp (line, "MDM Observatory")) {

    fseek (f, header.size, SEEK_SET);
    
    /* load existing data from database */
    fits_scan (&header, "NIMAGES", "%d", 1, &Nimage);
    ALLOCATE (image, RegImage, Nimage);
    status = Fread (image, sizeof(RegImage), Nimage, f, "regimage");
    fclose (f);
    
    if (status != Nimage) {
      fprintf (stderr, "ERROR: header and data in dB don't match (%d vs %d)\n", Nimage, status);
      fits_free_header (&header);
      free (image);
      return ((RegImage *) NULL);
    }

    *nimage = Nimage;
    return (image);
  }

  /* we probably have v3 */
  if (!fits_fread_matrix (f, &matrix, &header)) {
    fclose (f);
    fits_free_header (&header);
    fits_free_matrix (&matrix);
    fprintf (stderr, "ERROR: trouble reading database matrix\n");
    return ((RegImage *) NULL);
  }

  table.header = &theader;
  if (!fits_fread_ftable  (f, &table, "IMAGE_DATABASE")) {
    fclose (f);
    fits_free_header (&header);
    fits_free_matrix (&matrix);
    fprintf (stderr, "ERROR: trouble reading database table\n");
    return ((RegImage *) NULL);
  }

  /* convert to internal format */
  image = (RegImage *) table.buffer;
  fits_scan (table.header, "NAXIS2", "%d", 1, &Nimage);
  ConvertStruct ((char *) image, sizeof (RegImage), Nimage, "regimage");

  fits_free_header (&header);
  fits_free_matrix (&matrix);

  *nimage = Nimage;
  return (image);
}
