
	/* "make" an observation of the given target; this means
	         1. fill in the "tonight_status" field with OBSERVED
	         2. fill in appropriate UT- or LST-of-observation fields
	         3. put airmass at middle of observation into airmass field
	         4. advance clocks by exposure time, plus any 'setup' time
	                (for slewing, readout, etc.)
	         5. if desired, print a line of info to stdout
	*/

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

#define TINY   1.0e-6			/* bug in printf requires check of small num */

static int print_it();


observe(tar)
struct target *tar;
{
	double jd, ut, lst;
	double alt, az, air1, air2, air3;

	now(&jd, &ut, &lst);


	tar->tonight_status |= OBSERVED; 
	tar->last_jd = (long) jd;
	tar->obs_ut = ut;
	tar->obs_exp_time = tar->exp_time;

	/* calculating the observed HA is tricky, when LST rolls over from
	   24 hours (= 360 degrees) to 0 hours (0 degrees) */
	tar->obs_ha = lst - tar->ra;
	if (tar->obs_ha < -180.0)
		tar->obs_ha += 180.0;
	if (tar->obs_ha > 180.0)
		tar->obs_ha -= 180.0;
	
	/* now calculate an "average" airmass of the observation, from the
	   values of airmass at beginning, middle (doubly weighted) and end. */
	altaz(tar->obs_ha, tar->dec, &alt, &az, &air1);
	altaz(tar->obs_ha + tar->exp_time/480.0, tar->dec, &alt, &az, &air2);
	altaz(tar->obs_ha + tar->exp_time/240.0, tar->dec, &alt, &az, &air3);
	tar->obs_airmass = (air1 + 2*air2 + air3)/4.0;

	advance_clocks(tar->exp_time + tar->setup_time);

	print_it(ut, lst, tar);

}

	/* prints UT, LST, RA, Dec, HA, airmass */

static int
print_it(ut, lst, tar)
double ut, lst;
struct target *tar;
{
	char sign;
	int hours, deg, hmin, dmin;
	double hsec, dsec;

	sign = '+';
	hhhtohms(tar->dec, &deg, &dmin, &dsec);
	if (deg < 0) {
		sign = '-';
		deg = -deg;
	}
	else if (dmin < 0) {
		sign = '-';
		dmin = -dmin;
	}
	else if (dsec < 0) {
		sign = '-';
		dsec = -dsec;
	}
	degtora(tar->ra, &hours, &hmin, &hsec);

	/* there is a bug in the 'printf' routine which prints out too many
	   characters if 'dsec' has a very small, but non-zero, value. */
	if (dsec < TINY)
		dsec = 0.0;
	if (hsec < TINY)
		hsec = 0.0;

	printf("%8.4lf %8.4lf %02d:%02d:%05.2lf %c%02d:%02d:%05.2lf %8.4lf %4.2lf \n",
			ut, lst, hours, hmin, hsec, sign, deg, dmin, dsec, tar->obs_ha,
			tar->obs_airmass);
}
