// sattool - visual satellite tracking and prediction tool.
// Copyright 2000 Tom Rothamel <tom-idbg@onegeek.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  

#include "sattool.h"

// This is used by the passes and step commands to print a single
// encounter. It prints the time, azimuth, elevation, and range.
void print_encounter(FILE *of, Pass *p, char *label, double t) {
	char buf[40];
	char mag[10];
	Encounter *e;
		
	e = new Encounter(p->sat, p->site, t);
	e->calcmore();

	if (p->mmt != HUGE_VAL) {
		if (!isLit(e->objp)) {
			strncpy(mag, " N/L", 10);
		} else {
			snprintf(mag, 10,"%+ 3.1f", p->sat->calcmag(e));
		}

	} else {
		mag[0] = 0;
	}
	
	strfsgptime(buf, 40, "%Y%m%d %H:%M:%S", t);

	fprintf(of, "%s\%s % 6.1f %s % 5.1f %s %.0f\n", label, buf,
		e->azimuth, CompassDir(e->azimuth), e->elevation,
		mag, e->range);

	delete e;
}

// This prints information about the list of passes. (It's the passes
// command.)
int passes(int argc, char **argv) {
	List *l;
	Thing *t;
	Pass *p;
	Sat *s;
	Options *o;
	FILE *of;
	
	o = ParseGlobalOptions(argc, argv, NULL);
	l = ReadInput(o);

	of = OutputFile(o);
	if (!of) return -1;
	
	while (t = l->next()) {
		p = getPass(t);
		if (!p) continue;
		s = p->sat;
		
		fprintf(of, "%05d %s %s\n", s->norad, s->desig, s->name);
		print_encounter(of, p, "      Pass Start: ", p->min0); 
		print_encounter(of, p, "   Pass Max Elev: ", p->max); 
		if (p->mmt != HUGE_VAL /* && p->minmag != HUGE_VAL */)
			print_encounter(of, p, "    Pass Min Mag: ", p->mmt); 
		print_encounter(of, p, "        Pass End: ", p->min1); 
		fprintf(of, "\n");
	}

	fclose(of);
	return 0;
}
	
	
	
	
