#ifndef lint
static char *sccsid = "@(#)power.c	1.1 (UKC) 9/12/87";
#endif  lint
#include <math.h>
#include <stdio.h>
/*
 * Power function.
 * Args are azimuth,elevation in radians.
 * 2 element end fire array of horizontal dipoles
 * over perfect earth.
 */
#define PI 3.141592653589793

double
power(a,e)
	double a,e;
{	double field;
	double fi,fd,fe;
	double sin_a,cos_a,sin_e,cos_e;
	double getdouble();
	static double spacing,phase,height;
	static int first = 1;

	if(first)
	{	height = getdouble("height = ")*2.0*PI;
		spacing = getdouble("S = ")*2.0*PI;
		phase = getdouble("phase shift = ")*PI/180.0;
		first = 0;
	}

	if(e < 0.0) return(0.0);	/* ignore -ve elevations */

	sin_e = sin(e);
	cos_e = cos(e);
	sin_a = sin(a);
	cos_a = cos(a);

	fi = 2.0 * cos((phase + spacing*cos_a*cos_e)/2.0); /* isotropic */
	fd = sin_a*cos_e;
	if(fd < 0.0) fd = -fd;
	fd = 1.0 - fd;	/* dipole */
	fe = 2.0 * cos((PI + 2.0*height*sin_e)/2.0);	/* perfect earth */

	field = fi * fd * fe;
	return(field*field);
}

/*
 * If input from a tty emit prompt on stderr
 * Read a floating point number from stdin
 */
double
getdouble(prompt)
	char *prompt;
{	double value;

	while(1)
	{	if(isatty(0))
		{	fprintf(stderr,prompt);
			fflush(stderr);
		}
		if(fscanf(stdin,"%F", &value) == 1) break;
		fprintf(stderr,"bad input\n");
		if(!isatty(0)) exit(1);
	}

	return(value);
}
