
	/* given a UT date and two numbers, i and j, print out the LST times
	   of the j-th window out of i windows which fill up the night. Thus,
	   called like this:

	             lsttimes 01/04/1991 4 2 

	   we have
	             UT date is April 1, 1991 (note form DD/MM/YYYY)
	             fit 4 windows of equal size into the night
	             print out the LST times of the second window

	   so, for example, if the LST of nightfall is 2:00, and that of
	   dawn is 10:00, then the program would print out this:

	              4:00:00 6:00:00
	   where
	              4:00:00   is the LST of window start (HH:MM:SS)
	              6:00:00   is the LST of window end   

	   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. 


	   added 12/10/1991 MWR: an option to specify an RA, Dec and Epoch
	   on the command line; the rise and set times for the object thus
	   specified will be used together with the sun's rise and set times
	   to define the beginning and ending times for the windows of
	   observation.  Not specifying RA, Dec and Epoch simply uses the
	   sun's rise and set times alone. usage:

	             lsttimes 01/04/1991 4 2 01:02:03 +23:32:23 1950

	   where arg 5 is RA, arg 6 Dec and arg 7 epoch.  If they are not
	   ALL specified, then the sun's values alone are used.


	   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"

#define MINALT   20.0		/* minimum altitude above horizon for an object */

char *progname = "lsttimes";

main(argc, argv)
int argc;
char *argv[];
{
	int day, month, year, num_of_win, n, h1, m1, h2, m2, err_flag, visflag;
	double ut, jd, jds, jde, lsts, lste, dlst, lst1, lst2, s1, s2;
	double oldra, olddec, ra, dec, epoch, ojds[2], ojde[2];

	err_flag = 0;
	if ((argc != 4) && (argc != 7))
		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], "%d", &num_of_win) != 1)
		err_flag = 1;
	else if (sscanf(argv[3], "%d", &n) != 1)
		err_flag = 1;
	if (argc == 7) {
		if (sscanf(argv[4], "%d:%d:%lf", &h1, &m1, &s1) != 3) 
			err_flag = 1;
		ratodeg(h1, m1, s1, &oldra);
		if (sscanf(argv[5], "%d:%d:%lf", &h1, &m1, &s1) != 3)
			err_flag = 1;
		hmstohhh(h1, m1, s1, &olddec);
		if (sscanf(argv[6], "%lf", &epoch) != 1)
			err_flag = 1;
		to_j2000(epoch, oldra, olddec, &ra, &dec); 
	}

	if (err_flag) {
		fprintf(stderr, "usage: lsttimes DD/MM/YYYY num_of_win n [ HH:MM:SS DD:MM:SS epoch ]\n");
		exit(1);
	}

	ut = 0.0;
	get_jd(day, month, year, ut, &jd);
	night_limits(jd, -TWI_DEGREES, &jds, &jde);

	/* and now combine the sun's rise/set times with that of the object whose
	   coordinates were given by the user (if he did) */
	if (argc == 7) {
		if (obj_jdlimits(jd, ra, dec, MINALT, &visflag, &(ojds[0]), &(ojde[0]),
					&(ojds[1]), &(ojde[1])) != 0) {
			fprintf(stderr, "lsttimes: error in obj_jdlimits\n");
			exit(1);
		}
		if (visflag == 0) {
			fprintf(stderr, "lsttimes: object never above %.2lf degrees altitude tonight\n", 
					MINALT);
			exit(1);
		}
		/* just use the larger observing window, if there are two */
		if (visflag == 2) {
			if ((ojde[1] - ojds[1]) > (ojde[0] - ojds[0])) {
				ojds[0] = ojds[1];
				ojde[0] = ojde[1];
			}
		}
		/* now change jds and jde so that they reflect the union of the
		   sun's window and that of the object */
		if (jds < ojds[0])
			jds = ojds[0];
		if (jde > ojde[0])
			jde = ojde[0];
	}
	
	if (get_lst(jds, &lsts) < 0)
		exit(1);
	if (get_lst(jde, &lste) < 0)
		exit(1);
	if (lste < lsts)
		lste += 24.0;

	dlst = (lste - lsts)/num_of_win;
	if ((lst1 = lsts + dlst*(n - 1)) > 24.0)
		lst1 -= 24.0;
	if ((lst2 = lsts + dlst*n) > 24.0)
		lst2 -= 24.0;
	
	hhhtohms(lst1, &h1, &m1, &s1);
	hhhtohms(lst2, &h2, &m2, &s2);
	printf("%02d:%02d:%02.0lf %02d:%02d:%02.0lf\n", h1, m1, s1, h2, m2, s2);

	exit(0);
}
