
	/* given an input file from pcygni's "boxofstars" program,
	   which gives columns for

	        RA    Dec  epoch  mag  dist-from-center

	   calculate the RA and Dec components of the distance of each 
	   star from the center in arcseconds, and append those two
	   numbers to the end of each line.

	   Usage is 

			finderbit ra dec epoch
	 */


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

#define DEGTORAD 0.0174532925  

char *progname = "finderbit";

main(argc, argv)
int argc;
char *argv[];
{
	char line[200];
	int rah, ram, decd, decm;
	double cra, cram, cras, cdec, cdecm, cdecs, ras, decs, epoch, mag;
	double dist, dist2, ra, dec, ddec, dra, newra, newdec;

	/* first, get the RA, Dec and epoch of the center of the field
	   from the command line */
	if (argc != 4) {
		fprintf("usage: finderbit RA Dec epoch\n");
		exit(1);
	}
	if (sscanf(argv[1], "%lf:%lf:%lf", &cra, &cram, &cras) != 3) {
		fprintf("bad RA argument ..%s..\n", argv[1]);
		exit(1);
	}
	cra = (cra + cram/60.0 + cras/3600.0)*15.0;
	if (sscanf(argv[2], "%lf:%lf:%lf", &cdec, &cdecm, &cdecs) != 3) {
		fprintf("bad Dec argument ..%s..\n", argv[2]);
		exit(1);
	}
	cdec = (cdec + cdecm/60.0 + cdecs/3600.0);
	if (sscanf(argv[3], "%lf", &epoch) != 1) {
		fprintf(stderr, "bad epoch argument ..%s..\n", argv[3]);
		exit(1);
	}

	/* next, precess center to J2000.0 coords */
	to_j2000(epoch, cra, cdec, &newra, &newdec);
	cra = newra;
	cdec = newdec;
	
	
	while (gets(line) != NULL) {
		if (line[0] == '#')
			continue;
		if (sscanf(line, "%02d:%02d:%lf %03d:%02d:%lf %lf %lf %lf\n",
				&rah, &ram, &ras, &decd, &decm, &decs, &epoch, 
				&mag, &dist) != 9) {
			fprintf(stderr, "bad input format ..%s..\n", line);
			exit(1);
		}
		ra = (rah + ram/60.0 + ras/3600.0)*15.0;
		dec = (decd + decm/60.0 + decs/3600.0);

		/* calculate difference in position in arcsec */
		ddec = (dec - cdec)*3600.0;
		dra = (ra - cra)*cos(cdec*DEGTORAD)*3600.0;
		dist2 = sqrt(ddec*ddec + dra*dra);
		if (fabs(dist - dist2) > 1.0) {
			fprintf(stderr, "warning: distances disagree for star ..%s..\n", line);
		}

		printf("%02d:%02d:%05.2lf %+02d:%02d:%05.2lf %6.1lf %5.2lf %8.2lf %8.2lf %8.2lf\n", 
				rah, ram, ras, decd, decm, decs, epoch, mag, dist,
				dra, ddec);
	}

}
		
	
