
	/* given a UT date and a value for the altitude of the sun above
	   the horizon, print out the LST times at which the sun

	             1. sinks to the altitude after noon
	             2. rises up to the altitude after midnight

	   thus, called like so:
	  
	             alttimes 01/06/1991 -6

	   where
	             date is June 1, 1991 (note date form DD/MM/YYYY)
	             critical alt is six degrees BELOW the horizon

	   there will be output that looks like this:

	             00:31:00  11:03:04  

	   output is in the form HH:MM:SS.  

	   these numbers can be used to schedule twilight sky flatfield
	   exposures.

	   this program is used by the gardener cshell script, which needs to
	   know the times that all the requests for a multi-observation-per-night
	   target are. 

	   if there's some problem with the command-line arguments, or with 
	   the calculations, the program prints an error message to stderr
	   and exits with a non-zero code. */

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

char *progname = "alttimes";

main(argc, argv)
int argc;
char *argv[];
{
	int day, month, year, h1, m1, h2, m2, err_flag;
	double ut, jd, jds, jde, lsts, lste, alt; 
	double s1, s2; 

	err_flag = 0;
	if (argc != 3)
		err_flag = 1;
	else if (is_date(argv[1]) != 0)
		err_flag = 1;
	else if (sscanf(argv[1], "%02d/%02d/%04d", &day, &month, &year) != 3) 
		err_flag = 1;
	else if (sscanf(argv[2], "%lf", &alt) != 1)
		err_flag = 1;

	if (err_flag) {
		fprintf(stderr, "usage: alttimes DD/MM/YYYY altitude \n");
		exit(1);
	}

	ut = 0.0;
	get_jd(day, month, year, ut, &jd);
	night_limits(jd, alt, &jds, &jde);
	if (get_lst(jds, &lsts) < 0)
		exit(1);
	if (get_lst(jde, &lste) < 0)
		exit(1);
	if (lste < lsts)
		lste += 24.0;
	
	hhhtohms(lsts, &h1, &m1, &s1);
	hhhtohms(lste, &h2, &m2, &s2);
	printf("%02d:%02d:%02.0lf %02d:%02d:%02.0lf\n", 
				h1, m1, s1, h2, m2, s2);

	exit(0);
}
