
	/* deal with groups of targets. Groups are initially defined as sets of 
	   targets whose "ideal" observations - made when they cross the meridian -
	   overlap. However, targets can later be moved from one group to another,
	   so that their "ideal" observations no longer overlap. */

#include <stdio.h>
#include <math.h>
#include "listgen.h"

static int num_groups = 0;		/* number of groups */

	/* create groups out of the initial list of targets. returns the number
	   of groups created. */

create_groups(num_list)
int num_list;
{
	int i;
	double last_lst, new_lst;

	/* first, sort all targets by RA */
	groups[0].start = 0;
	groups[0].end = num_list - 1;
	sort_group(&(groups[0]));

	/* assign target ID numbers to each target, based on this initial
	   sort by RA */
	target_ids(num_list);

	/* now, go through the list, putting all targets which overlap with their
	   neighboring targets into the same group. Note that we keep track of 
	   LST in units that range from 0-360 (degrees) rather than 0-24 (hours). 
	   Therefore, one LST unit = 1 degree = 240 seconds of time. */

	/* doesn't handle the artificial boundary between ra=24 and ra=0 correctly 
	   yet - needs more work. */
	num_groups = 1;
	groups[0].group_id = 0;
	groups[0].start = 0;
	groups[0].end = 0;
	groups[0].start_lst = list[0].ra;
	last_lst = list[0].ra + list[0].exp_time/240.0;
	groups[0].end_lst = last_lst;
	for (i = 1; i < num_list; i++) {
		if (list[i].ra <= last_lst) {
			groups[num_groups - 1].end = i;
			if ((new_lst = list[i].ra + list[i].exp_time/240.0) > last_lst) {
				last_lst = new_lst;
				groups[num_groups - 1].end_lst = last_lst;
			}
		}
		else {
			groups[num_groups].group_id = num_groups;
			groups[num_groups].start = i;
			groups[num_groups].end = i;
			groups[num_groups].start_lst = list[i].ra;
			last_lst = list[i].ra + list[i].exp_time/240.0;
			groups[num_groups].end_lst = last_lst;
			num_groups++;
		}
	}
	for (i = 0; i < num_groups; i++) {
		groups[i].mid_lst = (groups[i].start_lst + groups[i].end_lst)/2.0;
	}

	/* now set up the initial boundaries for each group, at the midway points
	   between it and its two neighboring groups */
	groups[0].start_bound = groups[0].mid_lst - MAX_INIT_SHIFT;
	if (groups[0].start_bound < 0.0)
		groups[0].start_bound = 0.0;
	for (i = 1; i < num_groups; i++) {
		groups[i].start_bound = (groups[i - 1].mid_lst + groups[i].mid_lst)/2.0;
		groups[i - 1].end_bound = groups[i].start_bound;
	}
	groups[i - 1].end_bound = groups[i - 1].mid_lst + MAX_INIT_SHIFT;
	if (groups[i - 1].end_bound > 360.0)
		groups[i - 1].end_bound = 360.0;

	return(num_groups);	
}

	/* print out information for each target inside a group */

print_group(grp, verbose)
struct group *grp;
int verbose;
{
	int i;

	printf("--- Group %5d --- %10.6lf - %10.6lf %10.6lf - %10.6lf ---- \n", 
			grp->group_id, grp->start_bound, grp->start_lst, grp->end_lst,
			grp->end_bound);
	for (i = grp->start; i <= grp->end; i++) {
		print_target(&(list[i]), verbose);
	}
}

	/* print out information on all groups */

print_all_groups(verbose)
int verbose;
{
	int i;

	for (i = 0; i < num_groups; i++) {
		print_group(&(groups[i]), verbose);
	}
}
