# include "uniphot.h"

int main (int argc, char **argv) {

  int i, j, Nimage, Ntgroup, Nsgroup;
  Group *tgroup, *sgroup;
  Image *image;
  ImageLink *imlinks;

  /* add a mode to check / force consistency between image.Mcal and
     measure.Mcal use the method in delstar to match image with measure
     (gregions (image), etc) 
  */

  /* get configuration info, args, lockfile */
  initialize (argc, argv);

  /* load images filtered by selection */
  image = load_images (&Nimage);
  ALLOCATE (imlinks, ImageLink, Nimage);

  tgroup = find_image_tgroups (image, imlinks, Nimage, &Ntgroup);
  sgroup = find_image_sgroups (image, imlinks, Nimage, &Nsgroup);

  /* determine fit values */
  for (i = 0; i < NLOOP; i++) {

    fit_tgroup (tgroup, Ntgroup);
    fit_sgroup (sgroup, Nsgroup); 

  }    

  fprintf (stdout, "# uniphot results for filter %s\n", photcode[0].name);
  fprintf (stdout, "# STATMODE: %s\n", STATMODE);
  fprintf (stdout, "# NLOOP: %d\n", NLOOP);
  fprintf (stdout, "# time groups : %d\n", Ntgroup);
  for (i = 0; i < Ntgroup; i++) {
    fprintf (stdout, "%s %5d %5d %7.4f  %7.4f %7.4f\n", tgroup[i].label, 
	     tgroup[i].Nimage, tgroup[i].Ngood, 0.001*tgroup[i].M, 0.001*tgroup[i].dM, 0.001*tgroup[i].dMsub);
  }
  fprintf (stdout, "\n");

  fprintf (stdout, "# space groups : %d\n", Nsgroup);
  for (i = 0; i < Nsgroup; i++) {
    fprintf (stdout, "%s %5d %5d %7.4f  %7.4f %7.4f\n", sgroup[i].label, 
	     sgroup[i].Nimage, sgroup[i].Ngood, 0.001*sgroup[i].M, 0.001*sgroup[i].dM, 0.001*sgroup[i].dMsub);
  }

  if (!UPDATE) exit (0);

  update (image, Nimage, sgroup, Nsgroup);
  exit (0);
}

