
	/* a program to convert 'insgen' output into a form that includes
	   HA and airmass, and prints angular measures in degrees instead
	   of HH:MM:SS form. reads from stdin, prints to stdout. */

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

#define NO_VAL    -999.0

char *progname = "calc_ha";

main(argc, argv)
int argc;
char *argv[];
{
	char line[256], name[256];
	double jd, ra, dec, ha, airmass, exp_time;
	
	while (gets(line) != NULL) {
		parse_line(line, &jd, &ra, &dec, &ha, &airmass, &exp_time, name);
		printf("%9.4lf %6.2lf %6.2lf %7.2lf %5.2lf %4.0lf %s\n",
				jd, ra, dec, ha, airmass, exp_time, name);
	}

}

	/* read the stuff from the line, and calculate HA and airmass.  If
	   RA and Dec are not given (for flats, for example) set the
	   dependent quantities all to NO_VAL.

	   note that the 'jd' parameter is really the MODIFIED Julian
	   Date, so we have to add 2,440,000 to it to get REAL JD.  */

parse_line(line, jd, ra, dec, ha, airmass, exp_time, name)
char *line, *name;
double *jd, *ra, *dec, *ha, *airmass, *exp_time;
{
	int h, m;
	double s, x, alt, az, lst, real_jd;

	if (sscanf(line, "%lf", jd) != 1) {
		fprintf(stderr, "can't read jd from line ..%s..\n", line);
		exit(1);
	}
	real_jd = 2440000.0 + *jd;

	if (sscanf(line + 19, "%02d:%02d:%lf", &h, &m, &s) != 3) {
		*ra = NO_VAL;
	}
	else {
		hmstohhh(h, m, s, &x);
		hhhtodeg(x, ra);
	}
	if (sscanf(line + 30, "%03d:%02d:%lf", &h, &m, &s) != 3) {
		*dec = NO_VAL;
	}
	else {
		hmstohhh(h, m, s, dec);
	}
	if (sscanf(line + 44, "%lf", exp_time) != 1) {	
		fprintf(stderr, "can't read exp_time from ..%s..\n", line + 44);
		exit(1);
	}
	strcpy(name, line + 52);

	/* calculate the additional quantities */
	get_lst(real_jd, &lst);
	hhhtodeg(lst, &x);
	*ha = x - *ra;

	degtohhh(*ha, &x);
	to_altaz(x, *dec, &alt, &az, airmass);
		
}



