# include "dvoshell.h"

int imdata (int argc, char **argv) {
  
  int i, j, k, I;
  int Nimage, N, NPTS, found, mode, TimeSelect;
  int n, Nregions, NREGIONS, TimeFormat;
  int *subset, Nsubset;
  double trange;
  time_t tzero, start, stop, TimeReference;
  Image *image;
  Catalog catalog;
  SkyTable *sky;
  SkyList *skylist, *skyset;
  Vector *vec;

  start = stop = 0;
  TimeSelect = FALSE;
  if ((N = get_argument (argc, argv, "-time"))) {
    remove_argument (N, &argc, argv);
    if (!str_to_time (argv[N], &tzero)) { 
      gprint (GP_ERR, "syntax error\n");
      return (FALSE);
    }
    remove_argument (N, &argc, argv);
    if (!str_to_dtime (argv[N], &trange)) { 
      gprint (GP_ERR, "syntax error\n");
      return (FALSE);
    }
    remove_argument (N, &argc, argv);
    TimeSelect = TRUE;
    gprint (GP_ERR, "searching in range %ds - %ds (%f seconds)\n", (int)tzero, (int)(tzero + trange), trange);
    if (trange > 0) {
      start = tzero;
      stop  = tzero + trange;
    } else {
      stop = tzero;
      start  = tzero + trange;
    }      
  }

  gprint (GP_ERR, "function is poorly defined; disabled and may be removed\n");
  return (FALSE);

  if (argc != 4) {
    gprint (GP_ERR, "USAGE: imdata (value) [-time t dt]\n");
    return (FALSE);
  }

  /* identify selection */
  mode = 0;
  if (!strcasecmp (argv[1], "ra")) 
    mode = 1;
  if (!strcasecmp (argv[1], "dec")) 
    mode = 2;
  if (!strcasecmp (argv[1], "mag")) 
    mode = 3;
  if (!strcasecmp (argv[1], "dmag")) 
    mode = 4;
  if (!strcasecmp (argv[1], "Mcal")) 
    mode = 5;
  if (!strcasecmp (argv[1], "Mrel")) 
    mode = 6;
  if (!strcasecmp (argv[1], "source")) 
    mode = 7;
  if (!strcasecmp (argv[1], "x")) 
    mode = 8;
  if (!strcasecmp (argv[1], "y")) 
    mode = 9;
  if (!strcasecmp (argv[1], "time")) 
    mode = 10;
  if (mode == 0) {
    gprint (GP_ERR, "value may be one of the following:\n");
    gprint (GP_ERR, " ra dR dec dD mag dmag Mrel Mcal source time\n");
    return (FALSE);
  }
  if ((vec = SelectVector (argv[1], ANYVECTOR, TRUE)) == NULL) return (FALSE);

  if ((image = LoadImages (&Nimage)) == NULL) return (FALSE);
  image_subset (image, Nimage, &subset, &Nsubset, (Graphdata *) NULL, FALSE, tzero, trange, TimeSelect);
  BuildChipMatch (image, Nimage);
  GetTimeFormat (&TimeReference, &TimeFormat);

  /* load sky from correct table */
  sky = GetSkyTable ();

  Nregions = 0;
  NREGIONS = 10;
  ALLOCATE (skylist, SkyList, 1);
  ALLOCATE (skylist[0].regions, SkyRegion *, NREGIONS);

  /* for each image of interest, find the appropriate region files */
  for (i = 0; i < Nsubset; i++) {
    I = subset[i];

    if (!FindMosaicForImage (image, Nimage, I)) continue;
    skyset = SkyListByImage (sky, -1, &image[I]);

    for (j = 0; j < skyset[0].Nregions; j++) {
      found = FALSE;
      for (k = 0; (k < skylist[0].Nregions) && !found; k++) {
	found = !strcmp (skylist[0].regions[k][0].name, skyset[0].regions[j][0].name);
      }
      if (found) continue;
      skylist[0].regions[Nregions] = skyset[0].regions[j];
      Nregions ++;
      CHECK_REALLOCATE (skylist[0].regions, SkyRegion *, NREGIONS, Nregions, 10);
    }
    SkyListFree (skyset, FALSE);
  }	
  free (subset);
  for (i = 0; i < skylist[0].Nregions; i++) {
    gprint (GP_ERR, "try %s\n", skylist[0].regions[i][0].name);
  } 
  
  /* create output vector */
  NPTS = 1000;
  REALLOCATE (vec[0].elements, float, NPTS);
  vec[0].Nelements = N = 0;

  /* for each region file, extract the data of interest in the right time range */
  for (j = 0; j < skylist[0].Nregions; j++) {

    /* get file name and open */
    catalog.filename = skylist[0].filename[j];
    catalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_SECF;
    catalog.Nsecfilt = 0;

    // an error exit status here is a significant error
    if (!dvo_catalog_open (&catalog, NULL, FALSE, "r")) {
      fprintf (stderr, "ERROR: failure to open catalog file %s\n", catalog.filename);
      exit (2);
    }
    dvo_catalog_unlock (&catalog);

    /* assign vector values */
    switch (mode) {
      case (1):  /* ra */
	for (i = 0; i < catalog.Nmeasure; i++) {
	  if ((catalog.measure[i].t < start) || (catalog.measure[i].t > stop)) continue;
	  n = catalog.measure[i].averef;
	  vec[0].elements[N] = catalog.average[n].R - catalog.measure[i].dR / 3600.0;
	  N++;
	  CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 1000);
	}
	break;
      case (2):  /* dec */
	for (i = 0; i < catalog.Nmeasure; i++) {
	  if ((catalog.measure[i].t < start) || (catalog.measure[i].t > stop)) continue;
	  n = catalog.measure[i].averef;
	  vec[0].elements[N] = catalog.average[n].D - catalog.measure[i].dD / 3600.0;
	  N++;
	  CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 1000);
	}
	break;
      case (3):  /* mag */
	for (i = 0; i < catalog.Nmeasure; i++) {
	  if ((catalog.measure[i].t < start) || (catalog.measure[i].t > stop)) continue;
	  vec[0].elements[N] = catalog.measure[i].M;
	  N++;
	  CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 1000);
	}
	break;
      case (4):  /* dmag */
	for (i = 0; i < catalog.Nmeasure; i++) {
	  if ((catalog.measure[i].t < start) || (catalog.measure[i].t > stop)) continue;
	  vec[0].elements[N] = catalog.measure[i].dM;
	  N++;
	  CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 1000);
	}
	break;
      case (5):  /* Mcal */
	for (i = 0; i < catalog.Nmeasure; i++) {
	  if ((catalog.measure[i].t < start) || (catalog.measure[i].t > stop)) continue;
	  vec[0].elements[N] = catalog.measure[i].Mcal;
	  N++;
	  CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 1000);
	}
	break;
      case (6):  /* Mrel */
	for (i = 0; i < catalog.Nmeasure; i++) {
	  if ((catalog.measure[i].t < start) || (catalog.measure[i].t > stop)) continue;
	  n = catalog.measure[i].averef;
	  // vec[0].elements[N] = catalog.average[n].M;
	  N++;
	}
	break;
      case (7):  /* source */
	for (i = 0; i < catalog.Nmeasure; i++) {
	  if ((catalog.measure[i].t < start) || (catalog.measure[i].t > stop)) continue;
	  vec[0].elements[N] = catalog.measure[i].photcode;
	  N++;
	  CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 1000);
	}
	break;
      case (10):  /* time */
	for (i = 0; i < catalog.Nmeasure; i++) {
	  if ((catalog.measure[i].t < start) || (catalog.measure[i].t > stop)) continue;
	  vec[0].elements[N] = TimeValue (catalog.measure[i].t, TimeReference, TimeFormat);
	  N++;
	  CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 1000);
	}
	break;
    }
    dvo_catalog_free (&catalog);
  }
  
  vec[0].Nelements = N;
  REALLOCATE (vec[0].elements, float, MAX(1,N));
  return (TRUE);
}
