# include "dvo.h"

int gimages (int argc, char **argv) {
  
  int i, j, N, Nimage, Nfound, *subset, Nsubset, status;
  double ra, dec, Ra, Dec, X, Y, Mcal;
  double trange, t;
  int TimeSelect, PixelCoords, TimeFormat, PhotCodeSelect;
  time_t tzero, TimeReference;
  char name[64], *date;

  PhotCode *code;
  Image *image;

  if (!InitPhotcodes ()) return (FALSE);

  GetTimeFormat (&TimeReference, &TimeFormat);

  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);
    TimeSelect = TRUE;
  }
  if (N = get_argument (argc, argv, "-tref")) {
    remove_argument (N, &argc, argv);
    TimeSelect = TRUE;

    t = atof (argv[N]);
    tzero = TimeRef (t, TimeReference, TimeFormat);
    remove_argument (N, &argc, argv);

    if (!str_to_dtime (argv[N], &trange)) { 
      fprintf (stderr, "syntax error\n");
      return (FALSE);
    }
    remove_argument (N, &argc, argv);
  }

  PhotCodeSelect = FALSE;
  if (N = get_argument (argc, argv, "-photcode")) {
    remove_argument (N, &argc, argv);
    if ((code = GetPhotcodebyName (argv[N])) == NULL) {
      fprintf (stderr, "ERROR: photcode not found in photcode table\n");
      return (FALSE);
    }
    PhotCodeSelect = TRUE;
    remove_argument (N, &argc, argv);
  }

  PixelCoords = FALSE;
  if (N = get_argument (argc, argv, "-pix")) {
    remove_argument (N, &argc, argv);
    PixelCoords = TRUE;
  }

  if (argc != 3) {
    fprintf (stderr, "USAGE: gimages RA DEC [-time t dt] [-pix]\n");
    return (FALSE);
  }

  if (!str_to_radec (&Ra, &Dec, argv[1], argv[2])) return (FALSE);

  image = LoadImages (&Nimage);
  if (Nimage == 0) {
    return (FALSE);
  }
  image_subset (image, Nimage, &subset, &Nsubset, (Graphdata *) NULL, FALSE, tzero, trange, TimeSelect);

  Nfound = 0;
  for (j = 0; j < Nsubset; j++) {
    i = subset[j];
    status = RD_to_XY (&X, &Y, Ra, Dec, &image[i].coords);
    if (!status || (X < 0) || (X > image[i].NX) || (Y < 0) || (Y > image[i].NY)) continue;
    XY_to_RD (&ra, &dec, 0.5*image[i].NX, 0.5*image[i].NY, &image[i].coords);
    Mcal = applyMcal (&image[i], 2048.0, 2048.0);
    /* t = TimeValue (image[i].tzero, TimeReference, TimeFormat); */
    date = sec_to_date (image[i].tzero);
    if (PhotCodeSelect) {
      if ((code[0].type == PHOT_REF) || (code[0].type == PHOT_DEP)) {
	if (code[0].code != image[i].source) continue;
      } 
      if ((code[0].type == PHOT_PRI) || (code[0].type == PHOT_SEC)) {
	if (code[0].code != GetPhotcodeEquivCodebyCode (image[i].source)) continue;
      } 
    }      
    if (PixelCoords) {
      fprintf (GetOutfile(), "%3d %s %6.1f %6.1f %20s %5d %2d %4.2f %6.3f %5.3f %5.3f %4x\n", 
	       Nfound, image[i].name, X, Y, date, image[i].nstar, image[i].source, 0.001*image[i].secz, 0.001*Mcal, 0.001*image[i].dMcal, image[i].exptime, image[i].code);
    } else {
      fprintf (GetOutfile(), "%3d %s %8.4f %8.4f %20s %5d %2d %4.2f %6.3f %5.3f %5.3f %4x\n", 
	       Nfound, image[i].name, ra, dec, date, image[i].nstar, image[i].source, 0.001*image[i].secz, 0.001*Mcal, 0.001*image[i].dMcal, image[i].exptime, image[i].code);
    }
    sprintf (name, "IMAGEx:%d", Nfound);
    set_variable     (name, X);
    sprintf (name, "IMAGEy:%d", Nfound);
    set_variable     (name, Y);
    sprintf (name, "IMAGEt:%d", Nfound);
    set_str_variable (name, date);
    sprintf (name, "IMAGEccd:%d", Nfound);
    set_int_variable (name, image[i].ccdnum);
    sprintf (name, "IMAGEname:%d", Nfound);
    set_str_variable (name, image[i].name);
    Nfound ++;
    free (date);
  }
  set_int_variable ("IMAGEx:n", Nfound);
  set_int_variable ("IMAGEy:n", Nfound);
  set_int_variable ("IMAGEt:n", Nfound);
  set_int_variable ("IMAGEccd:n", Nfound);
  set_int_variable ("IMAGEname:n", Nfound);

  free (image);
  free (subset);

  return (TRUE);

}
