# include "dvoshell.h"

int imphot (int argc, char **argv) {
  
  time_t 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) {
    gprint (GP_ERR, "USAGE: imphot tzero trange [-g buffer]\n");
    return (FALSE);
  }

  buf = NULL;
  if (GreyScale) {
    if ((buf = SelectBuffer (bufname, ANYBUFFER, TRUE)) == NULL) return (FALSE);
    CreateBuffer (buf, 100, 200, -32, 0.0, 1.0);
  }

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

  if ((Nsubset > 1) && GreyScale) {
    gprint (GP_ERR, "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:
      gprint (GP_ERR, "%s: %d - %f\n", image[i].name, image[i].order, image[i].Mcal);
      break;
    case 1:
      gprint (GP_ERR, "%s: %d - %f, %d %d\n", image[i].name, image[i].order, image[i].Mcal, image[i].Mx, image[i].My);
      break;
    case 2:
      gprint (GP_ERR, "%s: %d - %f, %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:
      gprint (GP_ERR, "%s: %d - %f, %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:
      gprint (GP_ERR, "%s: %d - %f, %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);
}

