# include "dvo.h"

/* qualities to be extracted */
enum {ZERO, DTIME, SKY, BIAS, FWHM, AIRM, TIME, TEMP};

detrend (int argc, char **argv) {
 
  FILE *f;
  int i, Nimage, status, N, TimeSelect;
  char DataBase[256];
  unsigned long int tzero, tend;
  double trange;
  int TypeSelect, CCDSelect, FilterSelect;
  char *Filter, *Type;
  int type, mode, CCD;
  int NVALUE;
  float *value;
  time_t TimeReference;
  int TimeFormat;
  Header header;
  RegImage *pimage;
  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;
  }
 
  TypeSelect = FALSE;
  if (N = get_argument (argc, argv, "-type")) {
    remove_argument (N, &argc, argv);
    Type = strcreate (argv[N]);
    remove_argument (N, &argc, argv);
    TypeSelect = TRUE;
  }
  type = TYPE_NONE;
  if (TypeSelect) {
    if (!strncasecmp (Type, "obj", 3)) {
      type = TYPE_OBJECT;
    }
    if (!strncasecmp (Type, "bias", 4)) {
      type = TYPE_BIAS;
    }
    if (!strncasecmp (Type, "dark", 4)) {
      type = TYPE_DARK;
    }
    if (!strncasecmp (Type, "flat", 4)) {
      type = TYPE_FLAT;
    }
    if (type == TYPE_NONE) {
      fprintf (stderr, "invalid image type %s\n", Type);
      return (FALSE);
    }
  }

  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");
    return (FALSE);
  }
  
  /* identify selection */
  mode = ZERO;
  if (!strcasecmp (argv[1], "exptime")) mode = DTIME;
  if (!strcasecmp (argv[1], "sky")) mode = SKY;
  if (!strcasecmp (argv[1], "bias")) mode = BIAS;
  if (!strcasecmp (argv[1], "fwhm")) mode = FWHM;
  if (!strcasecmp (argv[1], "airmass")) mode = AIRM;
  if (!strcasecmp (argv[1], "time")) mode = TIME;
  if (!strcasecmp (argv[1], "temp")) mode = TEMP;
  if (mode == ZERO) {
    fprintf (stderr, "value may be one of the following:\n");
    fprintf (stderr, " exptime, sky, bias, fwhm, airmass, time\n");
    return (FALSE);
  }

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

  /* load in database header */
  if (!fits_read_header (DataBase, &header)) {
    fprintf (stderr, "ERROR: trouble reading database header\n");
    return (FALSE);
  }

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

  /* load existing data from database */
  fits_scan (&header, "NIMAGES", "%d", 1, &Nimage);
  ALLOCATE (pimage, RegImage, Nimage);
  status = Fread (pimage, 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 (pimage);
    return (FALSE);
  }

  N = 0;
  NVALUE = 1000;
  REALLOCATE (vec[0].elements, float, NVALUE);
  value = vec[0].elements;

  GetTimeFormat (&TimeReference, &TimeFormat);
  fprintf (stderr, "%d %d\n", TimeReference, TimeFormat);

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

    /* assign correct value */
    switch (mode) {
    case (DTIME):
      value[N] = pimage[i].exptime;
      break;
    case (TIME):
      value[N] = TimeValue (pimage[i].obstime, TimeReference, TimeFormat);
      break;
    case (SKY):
      value[N] = pimage[i].sky;
      break;
    case (BIAS):
      value[N] = pimage[i].bias;
      break;
    case (FWHM):
      value[N] = pimage[i].fwhm;
      break;
    case (AIRM):
      value[N] = pimage[i].airmass;
      break;
    case (TEMP):
      value[N] = pimage[i].teltemp[0];
      break;
    }
    N++;
    if (N >= NVALUE - 1) {
      NVALUE += 1000;
      REALLOCATE (vec[0].elements, float, NVALUE);
      value = vec[0].elements;
    }
  }

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

  free (pimage);
  fits_free_header (&header);
  return (TRUE);

}
