/*******************************************************************************
*$ Component_name:
*	XKep_MeanAnom
*$ Abstract:
*	Calculates the mean anomaly of a satellite based on its true anomaly,
*	for internal use by other Kepler Library routines.
*$ Keywords:
*	KEPLER, ORBITAL_MOTION
*	C, INTERNAL_ROUTINE
*$ Declarations:
*	double		XKep_MeanAnom( e, eccRatio, trueAnom )
*	double		e, eccRatio, trueAnom;
*$ Inputs:
*	e		orbital eccentricity.
*	eccRatio	value of sqrt((1+e)/(1-e)).
*	trueAnom	true anomaly in radians.
*$ Outputs:
*	none
*$ Returns:
*	mean anomaly in radians, between -pi and pi.
*$ Detailed_description:
*	This function returns the mean anomaly of a satellite based on its given
*	eccentricity and true anomaly.  It is intended for internal use by other
*	Kepler Library routines, specifically Kep_MeanAnom().
*$ External_references:
*	none
*$ Examples:
*	none
*$ Error_handling:
*	none
*$ Limitations:
*	Accuracy will be reduced when eccentricity is close to unity.
*$ Author_and_institution:
*	Mark R. Showalter
*	NASA/Ames Research Center
*$ Version_and_date:
*	1991 June 18
*$ Change_history:
*	none
*******************************************************************************/
#include <math.h>
#include "kepler.h"

double		XKep_MeanAnom( e, eccRatio, trueAnom )
double		e, eccRatio, trueAnom;
{
double		tanHalfPsi, psi, meanAnom;

/*******************************************************************************
* Calculate the eccentric anomaly psi:
*		tan(psi/2) = sqrt((1-e)/(1+e)) tan(trueAnom/2)
*******************************************************************************/

	tanHalfPsi = tan(0.5*trueAnom) / eccRatio;
	psi = 2. * atan(tanHalfPsi);

/*******************************************************************************
* Calculate the mean anomaly M:
*		M = psi - e * sin(psi)
*******************************************************************************/

	meanAnom = psi - e * sin(psi);

	return (meanAnom);
}

/******************************************************************************/
