# include "imregister.h"
# include "photreg.h"

void DeleteSubset (FITS_DB *db, PhotPars *photdata, int Nphotdata, int *match, int Nmatch) {

  int i, j;
  int *keep, Ndel, Nsubset;
  PhotPars *subset;

  ALLOCATE (keep, int, MAX (Nphotdata, 1));
  for (i = 0; i < Nphotdata; i++) keep[i] = TRUE;
  fprintf (stderr, "total of %d photdata\n", Nphotdata);

  Ndel = 0;
  for (i = 0; i < Nmatch; i++) {
    j = match[i];
    if (j == -1) continue;
    keep[j] = FALSE;
    Ndel ++;
  }
  fprintf (stderr, "delete %d photdata\n", Ndel);

  if (Ndel == 0) { 
    fprintf (stderr, "SUCCESS\n");
    gfits_db_close (db);
    gfits_db_free (db);
    exit (0);
  }

  /* create new data list */
  Nsubset = Nphotdata - Ndel;
  ALLOCATE (subset, PhotPars, MAX (1, Nsubset));
  fprintf (stderr, "keeping %d photdata\n", Nsubset);
  for (j = i = 0; i < Nphotdata; i++) {
    if (!keep[i]) continue;
    subset[j] = photdata[i];
    j++;
  }

  gfits_table_set_PhotPars (&db[0].ftable, subset, Nsubset);
  gfits_db_save (db);
  gfits_db_close (db);
  gfits_db_free (db);

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