
	/* read in the FWHM data from a .coo file that matches
	   the given .pht file, and set the fwhm field in each
	   star structure from the .pht file accordingly. */


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

struct s_coo {
	int id;
	double xc;
	double yc;
	double peak;
	double fwhm;
	double round;
	double sharp;
};

static void set_coo_defaults( /* struct s_image *im */ );
static void get_name( /* char *phtname, *cooname */ );
static int slurp_coo_data( /* char *file, struct s_image *im */ );
static int read_coo_line( /* char *line; struct s_coo *coo */ );

static char progname[] = "multipht";


slurp_coo(file, im)
char *file;
struct s_image *im;
{
	char cooname[LINELEN];

	set_coo_defaults(im);
	get_name(file, cooname);	
	slurp_coo_data(cooname, im);
	
	return(0);
}

	/* set all the .coo fields in every star in the given image to
	   default values that will allow us to proceed even if there
	   is no .coo file for this image */

static void
set_coo_defaults(im)
struct s_image *im;
{
	int i;
	struct s_star *s;

	for (i = 0; i < im->num_star; i++) {
		s = &(im->stars[i]);
		s->peak = -1;
		s->fwhm = -1;
		s->sharp = -1;
		s->round = NODATA;
	}
}

	/* convert a file name from ending in ".pht" to ".coo" */

static void
get_name(phtname, cooname)
char *phtname, *cooname;
{
	int len;

	len = strlen(phtname);
	if (strncmp(phtname + (len - 4), ".pht", 4) != 0) {
		fprintf(stderr, "%s.get_name: pht file ..%s.. doesn't end in .pht?! \n",
						progname, phtname);
		exit(-1);
	}
	strcpy(cooname, phtname);
	cooname[len - 3] = 'c';		
	cooname[len - 2] = 'o';		
	cooname[len - 1] = 'o';		

}

	/* read in the data from a .coo file, sticking the peak, FWHM, etc.
	   into the given star structure. return 0 if all OK, or -1 if there's
	   a problem. */

static int 
slurp_coo_data(file, im)
char *file;
struct s_image *im;
{
	int i, found, naper;
	char line[LINELEN];
	FILE *fp;
	struct s_coo coo;
	struct s_star *s;

	/* first, skip past all lines in the file until we come to the
	   one that consists of 'END' - leave us just past that line */
	if ((fp = fopen(file, "r")) == NULL) {
		fprintf(stderr, "%s.slurp_coo_data: can't open file %s\n",
					progname, file);
		return(-1);
	}
	found = 0;
	while (fgets(line, LINELEN, fp) != NULL) {
		if (strcmp(line, "END\n") == 0) {
			found = 1;
			break;
		}
	}
	if (found == 0) {
		fclose(fp);
		fprintf(stderr, "%s.slurp_coo_data: file %s has no END\n",
					progname, file);
		return(-1);
	}

	/* now that we're here, read each line in and set the values of 
	   the corresponding 'star' structure from the .pht file. */
	while (fgets(line, LINELEN, fp) != NULL) {
		if (read_coo_line(line, &coo) != 0) {
			fprintf(stderr, "%s.slurp_coo_data: bad data line\n", progname);
		}
		if (coo.id > im->num_star)
			break;
		s = &(im->stars[coo.id - 1]);
		s->peak = coo.peak;
		s->fwhm = coo.fwhm;
		s->round = coo.round;
		s->sharp = coo.sharp;
	}
	fclose(fp);

	return(0);
}
		
	/* read in one line's worth of .coo data. 
	   return 0 if all is well, or -1 if there's a problem.  */
	
static int 
read_coo_line(line, coo)
char *line;
struct s_coo *coo;
{
	if (sscanf(line, "%d %lf %lf %lf %lf %lf %lf ", &(coo->id), &(coo->xc),
			&(coo->yc), &(coo->peak), &(coo->fwhm), &(coo->round), 
			&(coo->sharp)) != 7) {
		fprintf(stderr, "%s.read_coo_line: bad line ..%s..\n",
				progname, line);
		return(-1);
	}
	return(0);
}

