

	/* assign weights to all star measurements, either automatically or
	   interactively. */

#include <stdio.h>
#include <math.h>
#include "bait.h"
#include "pf.h"
#include "multipht.h"

#define IMAGE_CUT      0.25		/* if an image has < IMAGE_CUT times the */
                        		/*   max num of stars in an image, kill it */
#define STAR_CUT       0.25		/* if a star appears in fewer than */
                        		/*   STAR_CUT*num_images images, kill it */

static void image_weights( /* int flags */ );
static void star_weights( /* int flags */ );
static void stat_weights( /* int flags */ );


	/* assign weights to all the star measurements.
	   use the zeroth aperture to calculate statistical weights - 
	   we have already (in pick_aper()) placed the 'best' aperture
	   measurement into the zeroth element of the array. */

do_weights(flags)
int flags;
{

	image_weights(flags);
	star_weights(flags);
	stat_weights(flags);

}

	/* eliminate all images from the solution that either
	           a. are selected by the user (if AUTO not specified)
	           b. have fewer than IMAGE_CUT times the number of stars in 
	                   the best image 
	 */

static void
image_weights(flags)
int flags;
{
	int i, max, cutoff;

	if (flags & AUTO) {
		if (debug_flag)
			print_image_hist();
		max = 0;
		for (i = 0; i < num_images; i++) {
			if (image_arr[i].num_star > max)
				max = image_arr[i].num_star;
		}
		cutoff = (int) (IMAGE_CUT*max);
	}
	else {
		printf("List of #stars,  #images with that many stars \n");
		print_image_hist();
		cutoff = 0;
		printf("Enter cutoff for number of stars an image must have: ");
		scanf("%d", &cutoff);
	}

	for (i = 0; i < num_images; i++) {
		if (image_arr[i].num_star < cutoff)	{
			if (debug_flag) {
				printf("about to eliminate image %s: only %d stars\n",
							image_arr[i].name, image_arr[i].num_star);
			}
			image_arr[i].include = 0;
		}
	}
}


	/* eliminate all stars from the solution that either
	           a. are selected by the user (if AUTO not specified)
	           b. appear in fewer than STAR_CUT*num_images images 
	 */

static void
star_weights(flags)
int flags;
{
	int i, cutoff;
	struct s_hashp *hp;


	if (flags & AUTO) {
		if (debug_flag)
			print_star_hist();
		cutoff = (int) (STAR_CUT*num_images);
	}
	else {
		printf("List of #images,  #stars that appear in that many images\n");
		print_star_hist();
		cutoff = 0;
		printf("Enter cutoff for number of images in which a star must appear: ");
		scanf("%d", &cutoff);
	}

	for (hp = star_list; hp != NULL; hp = hp->next) {
		if (hp->hash->num < cutoff)	{
			if (debug_flag) {
				printf("about to eliminate star %d: only %d images\n",
							hp->hash->id, hp->hash->num);
			}
			hp->hash->include = 0;
		}
	}
}


	/* assign a weight to the 'w4' field in every star structure 
	   which is inversely proportional to the square of the estimated
	   uncertainty in the instrumental magnitude in the zeroth aperture. 
	   if the 'err' field has the 'NODATA' value, give the star
	   a weight of 0.0 */

	/* note that this function should run after 'image_weights()' 
	   for increased efficiency, but it will work fine otherwise */

static void
stat_weights(flags)
int flags;
{
	int i, j, aper;
	double err, mag;

	aper = 0;
	for (i = 0; i < num_images; i++) {
		if (image_arr[i].include == 1) {
			for (j = 0; j < image_arr[i].num_star; j++) {
				mag = image_arr[i].stars[j].mag[aper];
				err = image_arr[i].stars[j].err[aper];
				if ((mag == NODATA) || (err == NODATA))
					image_arr[i].stars[j].w4 = 0.0;
				else
					image_arr[i].stars[j].w4 = 1.0/(err*err);
			}
		}
	}
	
}


