# include "dvoshell.h"

int gimages (int argc, char **argv) {
  
  int i, j, N, Nimage, Nfound, *subset, Nsubset, status;
  double ra, dec, Ra, Dec, X, Y;
  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)) { 
      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;
  }
  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)) { 
      gprint (GP_ERR, "syntax error\n");
      return (FALSE);
    }
    remove_argument (N, &argc, argv);
  }

  code = NULL;
  PhotCodeSelect = FALSE;
  if ((N = get_argument (argc, argv, "-photcode"))) {
    remove_argument (N, &argc, argv);
    if ((code = GetPhotcodebyName (argv[N])) == NULL) {
      gprint (GP_ERR, "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) {
    gprint (GP_ERR, "USAGE: gimages RA DEC [-time t dt] [-pix]\n");
    return (FALSE);
  }

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

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

  Nfound = 0;
  for (j = 0; j < Nsubset; j++) {
    i = subset[j];
    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 (!FindMosaicForImage (image, Nimage, i)) continue;

    status = RD_to_XY (&X, &Y, Ra, Dec, &image[i].coords);
    if (!status) continue;
    if (X < 0) continue;
    if (Y < 0) continue;
    if (X > image[i].NX) continue;
    if (Y > image[i].NY) continue;

    /*** XXX we need to re-introduce the use of applyMcal
    Mcal = applyMcal (&image[i], 2048.0, 2048.0);
    ***/

    date = sec_to_date (image[i].tzero);

    if (PixelCoords) {
      gprint (GP_LOG, "%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, image[i].secz_PS, image[i].Mcal_PS, image[i].dMcal_PS, image[i].exptime, image[i].code);
    } else {
      XY_to_RD (&ra, &dec, 0.5*image[i].NX, 0.5*image[i].NY, &image[i].coords);
      gprint (GP_LOG, "%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, image[i].secz_PS, image[i].Mcal_PS, image[i].dMcal_PS, 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);

}
