# include "uniphot.h"
# include <glob.h>

void update (FITS_DB *db, Group *sgroup, int Nsgroup) {

  int i, j, status, Nmin, Nimage;
  char line[256];
  glob_t pglob;
  double Rmin, Rmax, Dmin, Dmax, x, y, radius;
  Image *image;
  Catalog catalog;
  Coords coords;

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

  /* clear the NOCAL flags */
  for (i = 0; i < Nimage; i++) {
    image[i].code &= ~ID_IMAGE_NOCAL;
  }

  /* apply calculated space-group offset to image Mcal values */
  for (i = 0; i < Nsgroup; i++) {
    for (j = 0; j < sgroup[i].Nimage; j++) {
      sgroup[i].image[j][0].Mcal -= sgroup[i].M;
    }
  }

  /** write image table **/
  dvo_image_update (db, VERBOSE);

  // XXX this process uses the existence of the file to perform the update
  // XXX convert this to an examination of the SkyTable
  /** update catalog tables **/
  pglob.gl_offs = 0;
  sprintf (line, "%s/*/*.cpt", CATDIR);
  status = glob (line, 0, NULL, &pglob);

  coords.crpix1 = coords.crpix2 = 0.0;
  coords.cdelt1 = coords.cdelt2 = 1.0; /* scale is degrees, radius in degrees */
  coords.pc1_1 = coords.pc2_2 = 1.0;
  coords.pc1_2 = coords.pc2_1 = 0.0;
  strcpy (coords.ctype, "RA---TAN");
  
  for (i = 0; i < pglob.gl_pathc; i++) {
    catalog.filename = pglob.gl_pathv[i];
    catalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
    catalog.Nsecfilt  = GetPhotcodeNsecfilt ();

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

    gfits_scan (&catalog.header, "RA0", "%lf",  1, &Rmin);
    gfits_scan (&catalog.header, "RA1", "%lf",  1, &Rmax);
    gfits_scan (&catalog.header, "DEC0", "%lf", 1, &Dmin);
    gfits_scan (&catalog.header, "DEC1", "%lf", 1, &Dmax);
    while (Rmin <   0.0) { Rmin += 360.0; }
    while (Rmin > 360.0) { Rmin -= 360.0; }
    while (Rmax <   0.0) { Rmax += 360.0; }
    while (Rmax > 360.0) { Rmax -= 360.0; }

    coords.crval1 = 0.5*(Rmin + Rmax);
    coords.crval2 = 0.5*(Dmin + Dmax);

    Nmin = 0;
    Rmin = 1000;
    /* primitive version: match catalog with closest sgroup */
    for (j = 0; j < Nsgroup; j++) {
      if (!RD_to_XY (&x, &y, sgroup[j].v1, sgroup[j].v2, &coords)) continue;
      radius = hypot (x, y);
      if ((j == 0) || (radius < Rmin)) {
	Rmin = radius;
	Nmin = j;
      }
    }

    fprintf (stderr, "catalog: %s sgroup: %d %s %f\n", catalog.filename, Nmin, sgroup[Nmin].label, Rmin);
    update_dvo_catalog (&catalog, &sgroup[Nmin], (Rmin > 2*RADIUS)); 
    dvo_catalog_save (&catalog, VERBOSE);
    dvo_catalog_unlock (&catalog);
    dvo_catalog_free (&catalog);
  }
}      

/* loop over all average 

   if (source equiv photcode)

   loop over sgroups

   if (radius < RADIUS) 
      
   average.M (+/-) source.M
*/

/* loop over all measure 

   if (source equiv photcode)

   loop over sgroups

   if (radius < RADIUS) 
      
   measure.Mcal (+/-) source.M
*/

/* alternative (slower, more robust)

   loop over all average
   loop over all average.Nm
   select measure with source equiv photcode
   match measure to image, get Mcal & offset
   if any offset for average[i] is different, give error
   apply offset to average.M
*/
