# include "uniphot.h"

Group *find_image_tgroups (FITS_DB *db, ImageLink **Imlink, int *Ntgroup) {

  char *start, *stop;
  int i, j, Nimage, Ngroup, NGROUP, Nentry, NENTRY;
  unsigned int *time, *tmin, *tmax;
  Group *group;
  Image *image;
  ImageLink *imlink;

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

  /* sort time list (use only valid images?) */
  ALLOCATE (time, unsigned int, Nimage);
  for (i = 0; i < Nimage; i++) {
    time[i] = image[i].tzero;
  }
  sort_time (time, Nimage);

  /* find groups with dt < TRANGE */
  Ngroup = 0;
  NGROUP = 100;
  ALLOCATE (tmin, unsigned int, NGROUP);
  ALLOCATE (tmax, unsigned int, NGROUP);
  tmin[Ngroup] = time[0];

  /* generate tgroups */
  for (i = 0; i < Nimage - 1; i++) {
    if (time[i+1] - time[i] < TRANGE) continue;
    
    tmax[Ngroup] = time[i];
    
    Ngroup ++;
    if (Ngroup == NGROUP) {
      NGROUP += 100;
      REALLOCATE (tmin, unsigned int, NGROUP);
      REALLOCATE (tmax, unsigned int, NGROUP);
    }
    tmin[Ngroup] = time[i + 1];
  }
  tmax[Ngroup] = time[Nimage - 1];
  Ngroup ++;
  ALLOCATE (group, Group, Ngroup);

  /* assign images to groups */
  for (i = 0; i < Ngroup; i++) {
    Nentry = 0;
    NENTRY = 100;
    ALLOCATE (group[i].image, Image *, NENTRY);
    ALLOCATE (group[i].imlink, ImageLink *, NENTRY);
    group[i].M = 0;
    group[i].dM = 0;

    start = ohana_sec_to_date (tmin[i]);
    stop = ohana_sec_to_date (tmax[i]);
    snprintf (group[i].label, 64, "%s - %s", start, stop);
    free (start);
    free (stop);

    for (j = 0; j < Nimage; j++) {
      if (image[j].tzero < tmin[i]) continue;
      if (image[j].tzero > tmax[i]) continue;
      if (image[j].flags & ID_IMAGE_NOCAL) continue;
      
      group[i].image[Nentry] = &image[j];
      group[i].imlink[Nentry] = &imlink[j];
      imlink[j].tgroup = &group[i];
      Nentry ++;
      if (Nentry == NENTRY) {
	NENTRY += 100;
	REALLOCATE (group[i].image, Image *, NENTRY);
	REALLOCATE (group[i].imlink, ImageLink *, NENTRY);
      }
    }
    group[i].Nimage = Nentry;
  }
  *Imlink = imlink;
  *Ntgroup = Ngroup;
  return (group);
}


