# include "dvo.h"

int imphot (int argc, char **argv) {
  
  unsigned long int tzero;
  double trange;
  int N, GreyScale;
  int i, j, Nimage, Nsubset, *subset;
  char bufname[64];
  float *p;
  double fx, fy, x, y, applyMcal();
  Image *image;
  Buffer *buf;

  GreyScale = FALSE;
  if (N = get_argument (argc, argv, "-g")) {
    remove_argument (N, &argc, argv);
    strcpy (bufname, argv[N]);
    remove_argument (N, &argc, argv);
    GreyScale = TRUE;
  }

  if (argc != 3) {
    fprintf (stderr, "USAGE: imphot tzero trange [-g buffer]\n");
    return (FALSE);
  }

  if (GreyScale) {
    if ((buf = SelectBuffer (bufname, ANYBUFFER, TRUE)) == NULL) return (FALSE);
    buf[0].header.simple   = TRUE;
    buf[0].header.unsign   = FALSE;
    buf[0].header.extend   = FALSE;
    buf[0].header.bitpix   = -32;
    buf[0].header.Naxes    = 2;
    buf[0].header.Naxis[0] = 100;
    buf[0].header.Naxis[1] = 200;
    buf[0].header.bzero    = 0;
    buf[0].header.bscale   = 1;
    CreateBuffer (&buf[0]);
  }

  /* load image(s) in time range given */
  if (!str_to_time (argv[1], &tzero)) { 
    fprintf (stderr, "syntax error\n");
    return (FALSE);
  }
  if (!str_to_dtime (argv[2], &trange)) { 
    fprintf (stderr, "syntax error\n");
    return (FALSE);
  }    
  fprintf (stderr, "searching in range %ds - %ds (%f seconds)\n", (int)tzero, (int)(tzero + trange), trange);
  
  image = LoadImages (&Nimage);
  if (Nimage == 0) {
    return (FALSE);
  }
  image_subset (image, Nimage, &subset, &Nsubset, (Graphdata *) NULL, FALSE, tzero, trange, TRUE);

  if ((Nsubset > 1) && GreyScale) {
    fprintf (stderr, "more than one image selected, making GreyScale of first only\n");
  }

  if (GreyScale && Nsubset) {
    fx = image[subset[0]].NX / 100;
    fy = image[subset[0]].NY / 200;
    p = (float *) buf[0].matrix.buffer;
    for (y = 0; y < 200; y+=1.0) {
      for (x = 0; x < 100; x+=1.0, p++) {
	*p = applyMcal (&image[subset[0]], (fx*x), (fy*y));
      }
    }
  }

  for (j = 0; j < Nsubset; j++) {
    i = subset[j];
    switch (image[i].order) {
    case 0:
      fprintf (stderr, "%s: %d - %d\n", image[i].name, image[i].order, image[i].Mcal);
      break;
    case 1:
      fprintf (stderr, "%s: %d - %d, %d %d\n", image[i].name, image[i].order, image[i].Mcal, image[i].Mx, image[i].My);
      break;
    case 2:
      fprintf (stderr, "%s: %d - %d, %d %d, %d %d %d\n", image[i].name, image[i].order, image[i].Mcal, image[i].Mx, image[i].My, image[i].Mxx, image[i].Mxy, image[i].Myy);
      break;
    case 3:
      fprintf (stderr, "%s: %d - %d, %d %d, %d %d %d, %d %d %d %d\n", image[i].name, image[i].order, image[i].Mcal, image[i].Mx, image[i].My, 
	       image[i].Mxx, image[i].Mxy, image[i].Myy, image[i].Mxxx, image[i].Mxxy, image[i].Mxyy, image[i].Myyy);
      break;
    case 4:
      fprintf (stderr, "%s: %d - %d, %d %d, %d %d %d, %d %d %d %d, %d %d %d %d %d\n", image[i].name, image[i].order, image[i].Mcal, image[i].Mx, image[i].My, 
	       image[i].Mxx, image[i].Mxy, image[i].Myy, image[i].Mxxx, image[i].Mxxy, image[i].Mxyy, image[i].Myyy,
	       image[i].Mxxxx, image[i].Mxxxy, image[i].Mxxyy, image[i].Mxyyy, image[i].Myyyy);
      break;
    }
  }

  free (image);
  free (subset);
  return (TRUE);
}

