# include "delstar.h"

void delete_times (FITS_DB *db) {

  int i, j, k, n;
  int Nimage, Nimlist, found, code;
  int Nregions, NREGIONS;
  int *imlist;
  SkyList *skylist, *skyset;
  SkyTable *sky;
  Image *image;
  Catalog catalog;

  code = (PHOTCODE == NULL) ? -1 : PHOTCODE[0].code;

  /* load sky from correct table */
  sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, SKY_DEPTH, VERBOSE);
  SkyTableSetFilenames (sky, CATDIR, "cpt");

  Nregions = 0;
  NREGIONS = 10;
  ALLOCATE (skylist, SkyList, 1);
  ALLOCATE (skylist[0].regions, SkyRegion *, NREGIONS);
  skylist[0].ownElements = FALSE; // free these elements when freeing the list

  image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);

  /* find images for time range, delete each image */ 
  imlist = find_images_time (db, START, END, PHOTCODE, &Nimlist);

  /* find all overlapping regions */
  for (n = 0; n < Nimlist; n++) {
    j = imlist[n];
    if (VERBOSE) fprintf (stderr, "finding regions for %s\n", image[j].name);

    // XXX EAM : need to handle failure
    FindMosaicForImage (image, Nimage, j);
    skyset = SkyListByImage (sky, -1, &image[j]);

    // tregion = gregion_image (&image[j], &Ntregions);
    for (i = 0; i < skyset[0].Nregions; i++) {
      found = FALSE;
      for (k = 0; (k < skylist[0].Nregions) && !found; k++) {
	found = !strcmp (skylist[0].regions[k][0].name, skyset[0].regions[i][0].name);
      }
      if (found) continue;
      skylist[0].regions[Nregions] = skyset[0].regions[i];
      Nregions ++;
      CHECK_REALLOCATE (skylist[0].regions, SkyRegion *, NREGIONS, Nregions, 10);
    }
    SkyListFree (skyset);
  }

  /* delete from all identified regions */
  for (i = 0; i < skylist[0].Nregions; i++) {
    if (VERBOSE) fprintf (stderr, "deleting from %s\n", skylist[0].filename[i]);
    catalog.filename = skylist[0].filename[i];  /* don't free region before catalog! */
    catalog.Nsecfilt  = GetPhotcodeNsecfilt ();
    catalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;

    // an error exit status here is a significant error
    if (!dvo_catalog_open (&catalog, skylist[0].regions[i], VERBOSE, "a")) {
      fprintf (stderr, "ERROR: failure to open catalog file %s\n", catalog.filename);
      exit (2);
    }
    if (!catalog.Naves_disk) {
      dvo_catalog_unlock (&catalog);
      dvo_catalog_free (&catalog);
      continue;
    }

    find_matches (&catalog, code, START, END);
    dvo_catalog_save (&catalog, VERBOSE);
    dvo_catalog_unlock (&catalog);
    dvo_catalog_free (&catalog);
  }

  /* delete the identified images */
  gfits_vtable_from_ftable (&db[0].ftable, &db[0].vtable, imlist, Nimlist);
  dvo_image_update (db, VERBOSE);
  dvo_image_unlock (db);

  fprintf (stderr, "SUCCESS\n");
  exit (0);
}
