////////////////////////////////////////////////////////////////////////////////
// This file holds all routines to calculate a satellite position.
// Please bear in mind that this 50kBytes of code need some processing time.
// So don't expect a fluent animation. For this purpose you got GPS, and LinSat.
// Author Varol Okan. 
//
// For updates and news, please check :
// http://www.MovingSatellites.com
//
// Javascript help from : http://www.js-examples.com/
////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////
// BOOL SGP4(double tsince,int *iflag, VECTOR *pos, VECTOR *vel)
// Use global variables now rather then passing 

// And we initialize the static variables here ...
SGP4.a1		= 0.0;SGP4.a3ovk2	= 0.0;SGP4.ao		= 0.0;
SGP4.aodp	= 0.0;SGP4.aycof	= 0.0;SGP4.betao	= 0.0;
SGP4.betao2	= 0.0;SGP4.c1		= 0.0;SGP4.c1sq		= 0.0;
SGP4.c2		= 0.0;SGP4.c3		= 0.0;SGP4.c4		= 0.0;
SGP4.c5		= 0.0;SGP4.coef		= 0.0;SGP4.coef1	= 0.0;
SGP4.cosio	= 0.0;SGP4.d2		= 0.0;SGP4.d3		= 0.0;
SGP4.d4		= 0.0;SGP4.del1		= 0.0;SGP4.delmo	= 0.0;
SGP4.delo	= 0.0;SGP4.eeta		= 0.0;SGP4.eosq		= 0.0;
SGP4.eta	= 0.0;SGP4.etasq	= 0.0;SGP4.isimp	= 0;
SGP4.omgcof	= 0.0;SGP4.omgdot	= 0.0;SGP4.perige	= 0.0;
SGP4.pinvsq	= 0.0;SGP4.psisq	= 0.0;SGP4.qoms24	= 0.0;
SGP4.s4		= 0.0;SGP4.sinio	= 0.0;SGP4.sinmo	= 0.0;
SGP4.t2cof	= 0.0;SGP4.t3cof	= 0.0;SGP4.t4cof	= 0.0;
SGP4.t5cof	= 0.0;SGP4.temp		= 0.0;SGP4.temp1	= 0.0;
SGP4.temp2	= 0.0;SGP4.temp3	= 0.0;SGP4.theta2	= 0.0;
SGP4.theta4	= 0.0;SGP4.tsi		= 0.0;SGP4.x1m5th	= 0.0;
SGP4.x1mth2	= 0.0;SGP4.x3thm1	= 0.0;SGP4.x7thm1	= 0.0;
SGP4.xhdot1	= 0.0;SGP4.xlcof	= 0.0;SGP4.xmcof	= 0.0;
SGP4.xmdot	= 0.0;SGP4.xnodcf	= 0.0;SGP4.xnodot	= 0.0;
SGP4.xnodp	= 0.0;
function SGP4( tsince )
{
// variables
    var i; // int
    var cosuk,sinuk,rfdotk,vx,vy,vz,ux,uy,uz,xmy,xmx;
    var cosnok,sinnok,cosik,sinik,rdotk,xinck,xnodek,uk,rk;
    var cos2u,sin2u,u,sinu,cosu,betal,rfdot,rdot,r,pl,elsq;
    var esine,ecose,epw,temp6,temp5,temp4,cosepw,sinepw;
    var capu,ayn,xlt,aynl,xll,axn,xn,beta,xl,e,a,tfour;
    var tcube,delm,delomg,templ,tempe,tempa,xnode,tsq,xmp;
    var omega,xnoddf,omgadf,xmdf,x,y,z,xdot,ydot,zdot;
	var ee;
// Recover original mean motion (xnodp) and semimajor axis (aodp) 
// from input elements. 
	if (m_iFlag != 0)  {
		SGP4.a1 = Math.pow(m_xke/m_Sat.fMeanMotion,m_tothrd);
		SGP4.cosio = Math.cos(m_Sat.fInclination);
		SGP4.theta2 = SGP4.cosio*SGP4.cosio;
		SGP4.x3thm1 = 3.0*SGP4.theta2 - 1.0;
		SGP4.eosq = m_Sat.fEccentricity*m_Sat.fEccentricity;
		SGP4.betao2 = 1.0 - SGP4.eosq;
		SGP4.betao = Math.sqrt(SGP4.betao2);
		SGP4.del1 = 1.5*m_ck2*SGP4.x3thm1/(SGP4.a1*SGP4.a1*SGP4.betao*SGP4.betao2);
		SGP4.ao = SGP4.a1*(1.0 - SGP4.del1*(0.5*m_tothrd + SGP4.del1*(1.0 + 134.0/81.0*SGP4.del1)));
		SGP4.delo = 1.5*m_ck2*SGP4.x3thm1/(SGP4.ao*SGP4.ao*SGP4.betao*SGP4.betao2);
		SGP4.xnodp = m_Sat.fMeanMotion/(1.0 + SGP4.delo);
		SGP4.aodp = SGP4.ao/(1.0 - SGP4.delo);
// Initialization 
// For perigee less than 220 kilometers, the isimp flag is set and
//  the equations are truncated to linear variation in sqrt a and
//  quadratic variation in mean anomaly.  Also, the c3 term, the
//  delta omega term, and the delta m term are dropped.
		SGP4.isimp = 0;
		if ((SGP4.aodp*(1.0 - m_Sat.fEccentricity)/m_ae) < (220.0/m_xkmper + m_ae))
			SGP4.isimp = 1;
// For perigee below 156 km, the values of s and qoms2t are altered. 
		SGP4.s4 = m_s;
		SGP4.qoms24 = m_qoms2t;
		SGP4.perige = m_xkmper*(SGP4.aodp*(1.0 - m_Sat.fEccentricity) - m_ae);
		if (SGP4.perige < 156.0)	{
			SGP4.s4 = SGP4.perige - 78.0;
			if (SGP4.perige <= 98.0)
				SGP4.s4 = 20.0;
			SGP4.qoms24 = Math.pow((120.0 - SGP4.s4)*m_ae/m_xkmper,4.0);
			SGP4.s4 = SGP4.s4/m_xkmper + m_ae;
		}
		SGP4.pinvsq = 1.0/(SGP4.aodp*SGP4.aodp*SGP4.betao2*SGP4.betao2); 
		SGP4.tsi = 1.0/(SGP4.aodp - SGP4.s4);
		SGP4.eta = SGP4.aodp*m_Sat.fEccentricity*SGP4.tsi;
		SGP4.etasq = SGP4.eta*SGP4.eta;
		SGP4.eeta = m_Sat.fEccentricity*SGP4.eta;
		SGP4.psisq = Math.abs(1.0 - SGP4.etasq);
		SGP4.coef = SGP4.qoms24*Math.pow(SGP4.tsi,4.0);
		SGP4.coef1 = SGP4.coef/Math.pow(SGP4.psisq,3.5);
		SGP4.c2 = SGP4.coef1*SGP4.xnodp*(SGP4.aodp*(1.0 + 1.5*SGP4.etasq + SGP4.eeta*(4.0 + SGP4.etasq))
			+ 0.75*m_ck2*SGP4.tsi/SGP4.psisq*SGP4.x3thm1*(8.0 + 3.0*SGP4.etasq*(8.0 + SGP4.etasq)));
		SGP4.c1 = m_Sat.fRadiationCoefficient*SGP4.c2;
		SGP4.sinio = Math.sin(m_Sat.fInclination);
		SGP4.a3ovk2 = -m_xj3/m_ck2*Math.pow(m_ae,3.0);
		SGP4.c3 = SGP4.coef*SGP4.tsi*SGP4.a3ovk2*SGP4.xnodp*m_ae*SGP4.sinio/m_Sat.fEccentricity;
		SGP4.x1mth2 = 1.0 - SGP4.theta2;
		SGP4.c4 = 2.0*SGP4.xnodp*SGP4.coef1*SGP4.aodp*SGP4.betao2*(SGP4.eta*(2.0 + 0.5*SGP4.etasq)
			+ m_Sat.fEccentricity*(0.5 + 2.0*SGP4.etasq) - 2.0*m_ck2*SGP4.tsi/(SGP4.aodp*SGP4.psisq)
			*(-3.0*SGP4.x3thm1*(1.0 - 2.0*SGP4.eeta + SGP4.etasq*(1.5 - 0.5*SGP4.eeta))
			+ 0.75*SGP4.x1mth2*(2.0*SGP4.etasq - SGP4.eeta*(1.0 + SGP4.etasq))*Math.cos(2.0*m_Sat.fPeregee)));
		SGP4.c5 = 2.0*SGP4.coef1*SGP4.aodp*SGP4.betao2*(1.0 + 2.75*(SGP4.etasq + SGP4.eeta) + SGP4.eeta*SGP4.etasq);
		SGP4.theta4 = SGP4.theta2*SGP4.theta2;
		SGP4.temp1 = 3.0*m_ck2*SGP4.pinvsq*SGP4.xnodp;
		SGP4.temp2 = SGP4.temp1*m_ck2*SGP4.pinvsq;
		SGP4.temp3 = 1.25*m_ck4*SGP4.pinvsq*SGP4.pinvsq*SGP4.xnodp;
		SGP4.xmdot = SGP4.xnodp + 0.5*SGP4.temp1*SGP4.betao*SGP4.x3thm1
			+ 0.0625*SGP4.temp2*SGP4.betao*(13.0 - 78.0*SGP4.theta2 + 137.0*SGP4.theta4);
		SGP4.x1m5th = 1.0 - 5.0*SGP4.theta2;
		SGP4.omgdot = -0.5*SGP4.temp1*SGP4.x1m5th + 0.0625*SGP4.temp2*(7.0 - 114.0*SGP4.theta2 +395.0*SGP4.theta4)
			+ SGP4.temp3*(3.0 - 36.0*SGP4.theta2 + 49.0*SGP4.theta4);
		SGP4.xhdot1 = -SGP4.temp1*SGP4.cosio;
		SGP4.xnodot = SGP4.xhdot1 + (0.5*SGP4.temp2*(4.0 - 19.0*SGP4.theta2)
			+ 2.0*SGP4.temp3*(3.0 - 7.0*SGP4.theta2))*SGP4.cosio;
		SGP4.omgcof = m_Sat.fRadiationCoefficient*SGP4.c3*Math.cos(m_Sat.fPeregee);
		SGP4.xmcof = -m_tothrd*SGP4.coef*m_Sat.fRadiationCoefficient*m_ae/SGP4.eeta;
		SGP4.xnodcf = 3.5*SGP4.betao2*SGP4.xhdot1*SGP4.c1;
		SGP4.t2cof = 1.5*SGP4.c1;
		SGP4.xlcof = 0.125*SGP4.a3ovk2*SGP4.sinio*(3.0 + 5.0*SGP4.cosio)/(1.0 + SGP4.cosio);
		SGP4.aycof = 0.25*SGP4.a3ovk2*SGP4.sinio;
		SGP4.delmo = Math.pow(1.0 + SGP4.eta*Math.cos(m_Sat.fMeanAnomaly),3.0);
		SGP4.sinmo = Math.sin(m_Sat.fMeanAnomaly);
		SGP4.x7thm1 = 7.0*SGP4.theta2 - 1.0;
		if (SGP4.isimp != 1)	{
			SGP4.c1sq = SGP4.c1*SGP4.c1;
			SGP4.d2 = 4.0*SGP4.aodp*SGP4.tsi*SGP4.c1sq;
			SGP4.temp = SGP4.d2*SGP4.tsi*SGP4.c1/3.0;
			SGP4.d3 = (17.0*SGP4.aodp + SGP4.s4)*SGP4.temp;
			SGP4.d4 = 0.5*SGP4.temp*SGP4.aodp*SGP4.tsi*(221.0*SGP4.aodp + 31.0*SGP4.s4)*SGP4.c1;
			SGP4.t3cof = SGP4.d2 + 2.0*SGP4.c1sq;
			SGP4.t4cof = 0.25*(3.0*SGP4.d3 + SGP4.c1*(12.0*SGP4.d2 + 10.0*SGP4.c1sq));
			SGP4.t5cof = 0.2*(3.0*SGP4.d4 + 12.0*SGP4.c1*SGP4.d3 + 6.0*SGP4.d2*SGP4.d2 + 15.0*SGP4.c1sq*(2.0*SGP4.d2 + SGP4.c1sq));
		} 
		m_iFlag = 0;
	}
// Update for secular gravity and atmospheric drag. 
	xmdf = m_Sat.fMeanAnomaly + SGP4.xmdot*tsince;
	omgadf = m_Sat.fPeregee + SGP4.omgdot*tsince;
	xnoddf = m_Sat.fRightAscending + SGP4.xnodot*tsince;
	omega = omgadf;
	xmp = xmdf;
	tsq = tsince*tsince;
	xnode = xnoddf + SGP4.xnodcf*tsq;
	tempa = 1.0 - SGP4.c1*tsince;
	tempe = m_Sat.fRadiationCoefficient*SGP4.c4*tsince;
	templ = SGP4.t2cof*tsq;
	if (SGP4.isimp != 1) {
		delomg = SGP4.omgcof*tsince;
		delm = SGP4.xmcof*(Math.pow(1.0 + SGP4.eta*Math.cos(xmdf),3.0) - SGP4.delmo);
		SGP4.temp = delomg + delm;
		xmp = xmdf + SGP4.temp;
		omega = omgadf - SGP4.temp;
		tcube = tsq*tsince;
		tfour = tsince*tcube;
		tempa = tempa - SGP4.d2*tsq - SGP4.d3*tcube - SGP4.d4*tfour;
		tempe = tempe + m_Sat.fRadiationCoefficient*SGP4.c5*(Math.sin(xmp) - SGP4.sinmo);
		templ = templ + SGP4.t3cof*tcube + tfour*(SGP4.t4cof + tsince*SGP4.t5cof);
	}
	a = SGP4.aodp*sqr(tempa);
	e = m_Sat.fEccentricity - tempe;
	ee = e*e;
	if ( ee > 1.0) return false;	// error, no good satellite datas ...
	xl = xmp + omega + xnode + SGP4.xnodp*templ;
	beta = Math.sqrt(1.0 - ee);
	xn = m_xke/Math.pow(a,1.5);
// Long period periodics 
	axn = e*Math.cos(omega);
	SGP4.temp = 1.0/(a*beta*beta);
	xll = SGP4.temp*SGP4.xlcof*axn;
	aynl = SGP4.temp*SGP4.aycof;
	xlt = xl + xll;
	ayn = e*Math.sin(omega) + aynl;
// Solve Kepler's Equation 
	capu = Fmod2p(xlt - xnode);
	SGP4.temp2 = capu;
	for (i = 1;i <= 10;i++)	{
		sinepw = Math.sin(SGP4.temp2);
		cosepw = Math.cos(SGP4.temp2);
		SGP4.temp3 = axn*sinepw;
		temp4 = ayn*cosepw;
		temp5 = axn*cosepw;
		temp6 = ayn*sinepw;
		epw = (capu - temp4 + SGP4.temp3 - SGP4.temp2)/(1.0 - temp5 - temp6) + SGP4.temp2;
		if (Math.abs(epw - SGP4.temp2) <= m_e6a)
			break;
		SGP4.temp2 = epw;
	}; //for i
// Short period preliminary quantities 
	ecose = temp5 + temp6;
	esine = SGP4.temp3 - temp4;
	elsq = axn*axn + ayn*ayn;
	SGP4.temp = 1.0 - elsq;
	pl = a*SGP4.temp;
	r = a*(1.0 - ecose);
	SGP4.temp1 = 1.0/r;
	rdot = m_xke*Math.sqrt(a)*esine*SGP4.temp1;
	rfdot = m_xke*Math.sqrt(pl)*SGP4.temp1;
	SGP4.temp2 = a*SGP4.temp1;
	betal = Math.sqrt(SGP4.temp);
	SGP4.temp3 = 1.0/(1.0 + betal);
	cosu = SGP4.temp2*(cosepw - axn + ayn*esine*SGP4.temp3);
	sinu = SGP4.temp2*(sinepw - ayn - axn*esine*SGP4.temp3);
	u = AcTan(sinu,cosu);
	sin2u = 2*sinu*cosu;
	cos2u = 2*cosu*cosu - 1.0;
	SGP4.temp = 1.0/pl;
	SGP4.temp1 = m_ck2*SGP4.temp;
	SGP4.temp2 = SGP4.temp1*SGP4.temp;
// Update for short periodics 
	rk = r*(1.0 - 1.5*SGP4.temp2*betal*SGP4.x3thm1) + 0.5*SGP4.temp1*SGP4.x1mth2*cos2u;
	uk = u - 0.25*SGP4.temp2*SGP4.x7thm1*sin2u;
	xnodek = xnode + 1.5*SGP4.temp2*SGP4.cosio*sin2u;
	xinck = m_Sat.fInclination + 1.5*SGP4.temp2*SGP4.cosio*SGP4.sinio*cos2u;
	rdotk = rdot - xn*SGP4.temp1*SGP4.x1mth2*sin2u;
	rfdotk = rfdot + xn*SGP4.temp1*(SGP4.x1mth2*cos2u + 1.5*SGP4.x3thm1);
// Orientation vectors 
	sinuk = Math.sin(uk);
	cosuk = Math.cos(uk);
	sinik = Math.sin(xinck);
	cosik = Math.cos(xinck);
	sinnok = Math.sin(xnodek);
	cosnok = Math.cos(xnodek);
	xmx = -sinnok*cosik;
	xmy = cosnok*cosik;
	ux = xmx*sinuk + cosnok*cosuk;
	uy = xmy*sinuk + sinnok*cosuk;
	uz = sinik*sinuk;
	vx = xmx*cosuk - cosnok*sinuk;
	vy = xmy*cosuk - sinnok*sinuk;
	vz = sinik*cosuk;
// Position and velocity 
	x = rk*ux;  m_pos[0] = x;
	y = rk*uy;  m_pos[1] = y;
	z = rk*uz;  m_pos[2] = z;
////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
	xdot = rdotk*ux + rfdotk*vx;  m_vel[0] = xdot;
	ydot = rdotk*uy + rfdotk*vy;  m_vel[1] = ydot;
	zdot = rdotk*uz + rfdotk*vz;  m_vel[2] = zdot;
	return true;
}; //Procedure SGP4


// Init the static variables ...
Deep.zns    =  1.19459E-5;     Deep.c1ss   =  2.9864797E-6;   Deep.zes    =  0.01675;
Deep.znl    =  1.5835218E-4;   Deep.c1l    =  4.7968065E-7;   Deep.zel    =  0.05490;
Deep.zcosis =  0.91744867;     Deep.zsinis =  0.39785416;     Deep.zsings = -0.98088458;
Deep.zcosgs =  0.1945905;/*    Deep.zcoshs =  1;              Deep.zsinhs =  0; never used ...*/
Deep.q22    =  1.7891679E-6;   Deep.q31    =  2.1460748E-6;   Deep.q33    =  2.2123015E-7;
Deep.g22    =  5.7686396;      Deep.g32    =  0.95240898;     Deep.g44    =  1.8014998;
Deep.g52    =  1.0508330;      Deep.g54    =  4.4108898;      Deep.root22 =  1.7891679E-6;
Deep.root32 =  3.7393792E-7;   Deep.root44 =  7.3636953E-9;   Deep.root52 =  1.1428639E-7;
Deep.root54 =  2.1765803E-9;   Deep.thdt   =  4.3752691E-3;
//const  Typed constants to retain values between ENTRY calls 
Deep.iresfl	 = 0;Deep.isynfl	= 0;
Deep.iret    = 0;Deep.iretn	= 0;
Deep.ls		 = 0;
Deep.a1      = 0.0;Deep.a2      = 0.0;Deep.a3      = 0.0;
Deep.a4      = 0.0;Deep.a5      = 0.0;Deep.a6      = 0.0;
Deep.a7      = 0.0;Deep.a8      = 0.0;Deep.a9      = 0.0;
Deep.a10     = 0.0;Deep.ainv2   = 0.0;Deep.alfdp   = 0.0;
Deep.aqnv    = 0.0;Deep.atime   = 0.0;Deep.betdp   = 0.0;
Deep.bfact   = 0.0;Deep.c       = 0.0;Deep.cc      = 0.0;
Deep.cosis   = 0.0;Deep.cosok   = 0.0;Deep.cosq    = 0.0;
Deep.ctem    = 0.0;Deep.d2201   = 0.0;Deep.d2211   = 0.0;
Deep.d3210   = 0.0;Deep.d3222   = 0.0;Deep.d4410   = 0.0;
Deep.d4422   = 0.0;Deep.d5220   = 0.0;Deep.d5232   = 0.0;
Deep.d5421   = 0.0;Deep.d5433   = 0.0;Deep.dalf    = 0.0;
Deep.day     = 0.0;Deep.dbet    = 0.0;Deep.del1    = 0.0;
Deep.del2    = 0.0;Deep.del3    = 0.0;Deep.delt    = 0.0;
Deep.dls     = 0.0;Deep.e3      = 0.0;Deep.ee2     = 0.0;
Deep.eoc     = 0.0;Deep.eq      = 0.0;Deep.f2      = 0.0;
Deep.f220    = 0.0;Deep.f221    = 0.0;Deep.f3      = 0.0;
Deep.f311    = 0.0;Deep.f321    = 0.0;Deep.f322    = 0.0;
Deep.f330    = 0.0;Deep.f441    = 0.0;Deep.f442    = 0.0;
Deep.f522    = 0.0;Deep.f523    = 0.0;Deep.f542    = 0.0;
Deep.f543    = 0.0;Deep.fasx2   = 0.0;Deep.fasx4   = 0.0;
Deep.fasx6   = 0.0;Deep.ft      = 0.0;Deep.g200    = 0.0;
Deep.g201    = 0.0;Deep.g211    = 0.0;Deep.g300    = 0.0;
Deep.g310    = 0.0;Deep.g322    = 0.0;Deep.g410    = 0.0;
Deep.g422    = 0.0;Deep.g520    = 0.0;Deep.g521    = 0.0;
Deep.g532    = 0.0;Deep.g533    = 0.0;Deep.gam     = 0.0;
Deep.omegaq  = 0.0;Deep.pe      = 0.0;Deep.pgh     = 0.0;
Deep.ph      = 0.0;Deep.pinc    = 0.0;Deep.pl      = 0.0;
Deep.preep   = 0.0;Deep.s1      = 0.0;Deep.s2      = 0.0;
Deep.s3      = 0.0;Deep.s4      = 0.0;Deep.s5      = 0.0;
Deep.s6      = 0.0;Deep.s7      = 0.0;Deep.savtsn  = 0.0;
Deep.se      = 0.0;Deep.se2     = 0.0;Deep.se3     = 0.0;
Deep.sel     = 0.0;Deep.ses     = 0.0;Deep.sgh     = 0.0;
Deep.sgh2    = 0.0;Deep.sgh3    = 0.0;Deep.sgh4    = 0.0;
Deep.sghl    = 0.0;Deep.sghs    = 0.0;Deep.sh      = 0.0;
Deep.sh2     = 0.0;Deep.sh3     = 0.0;Deep.sh1     = 0.0;
Deep.shs     = 0.0;Deep.si      = 0.0;Deep.si2     = 0.0;
Deep.si3     = 0.0;Deep.sil     = 0.0;Deep.sini2   = 0.0;
Deep.sinis   = 0.0;Deep.sinok   = 0.0;Deep.sinq    = 0.0;
Deep.sinzf   = 0.0;Deep.sis     = 0.0;Deep.sl      = 0.0;
Deep.sl2     = 0.0;Deep.sl3     = 0.0;Deep.sl4     = 0.0;
Deep.sll     = 0.0;Deep.sls     = 0.0;Deep.sse     = 0.0;
Deep.ssg     = 0.0;Deep.ssh     = 0.0;Deep.ssi     = 0.0;
Deep.ssl     = 0.0;Deep.stem    = 0.0;Deep.step2   = 0.0;
Deep.stepn   = 0.0;Deep.stepp   = 0.0;Deep.temp    = 0.0;
Deep.temp1   = 0.0;Deep.thgr    = 0.0;Deep.x1      = 0.0;
Deep.x2      = 0.0;Deep.x2li    = 0.0;Deep.x2omi   = 0.0;
Deep.x3      = 0.0;Deep.x4      = 0.0;Deep.x5      = 0.0;
Deep.x6      = 0.0;Deep.x7      = 0.0;Deep.x8      = 0.0;
Deep.xfact   = 0.0;Deep.xgh2    = 0.0;Deep.xgh3    = 0.0;
Deep.xgh4    = 0.0;Deep.xh2     = 0.0;Deep.xh3     = 0.0;
Deep.xi2     = 0.0;Deep.xi3     = 0.0;Deep.xl      = 0.0;
Deep.xl2     = 0.0;Deep.xl3     = 0.0;Deep.xl4     = 0.0;
Deep.xlamo   = 0.0;Deep.xldot   = 0.0;Deep.xli     = 0.0;
Deep.xls     = 0.0;Deep.xmao    = 0.0;Deep.xnddt   = 0.0;
Deep.xndot   = 0.0;Deep.xni     = 0.0;Deep.xno2    = 0.0;
Deep.xnodce  = 0.0;Deep.xnoi    = 0.0;Deep.xnq     = 0.0;
Deep.xomi    = 0.0;Deep.xpidot  = 0.0;Deep.xqncl   = 0.0;
Deep.z1      = 0.0;Deep.z11     = 0.0;Deep.z12     = 0.0;
Deep.z13     = 0.0;Deep.z2      = 0.0;Deep.z21     = 0.0;
Deep.z22     = 0.0;Deep.z23     = 0.0;Deep.z3      = 0.0;
Deep.z31     = 0.0;Deep.z32     = 0.0;Deep.z33     = 0.0;
Deep.zcosg   = 0.0;Deep.zcosgl  = 0.0;Deep.zcosh   = 0.0;
Deep.zcoshl  = 0.0;Deep.zcosi   = 0.0;Deep.zcosil  = 0.0;
Deep.ze      = 0.0;Deep.zf      = 0.0;Deep.zm      = 0.0;
Deep.zmo     = 0.0;Deep.zmol    = 0.0;Deep.zmos    = 0.0;
Deep.zn      = 0.0;Deep.zsing   = 0.0;Deep.zsingl  = 0.0;
Deep.zsinh   = 0.0;Deep.zsinhl  = 0.0;Deep.zsini   = 0.0;
Deep.zsinil  = 0.0;Deep.zx      = 0.0;Deep.zy      = 0.0;

function Deep( ideep )
{
	var DEEP20, DEEP45, DEEP80, DEEP100, DEEP125, DEEP150;
	DEEP20 = false;
	DEEP45 = true;
	DEEP80 = false;
	DEEP100 = true;
	DEEP125 = false;
	DEEP150 = false;

	var	bStopInnerLoop = false;
	if (ideep == m_dpinit) { // Entrance for deep space initialization 
		Deep.thgr = ThetaG(m_Sat.fJulianEpoch);
		Deep.eq = m_Sat.fEccentricity;
		Deep.xnq = m_xnodp;
		Deep.aqnv = 1.0/m_ao;
		Deep.xqncl = m_Sat.fInclination;
		Deep.xmao = m_Sat.fMeanAnomaly;
		Deep.xpidot = m_omgdt + m_xnodot;
		Deep.sinq = Math.sin(m_Sat.fRightAscending);
		Deep.cosq = Math.cos(m_Sat.fRightAscending);
		Deep.omegaq = m_Sat.fPeregee;
		// Initialize lunar solar terms 
		Deep.day = m_Sat.fJulianEpoch - 2433281.5 + 18261.5;  //Days since 1900 Jan 0.5
		if (Deep.day != Deep.preep)	{
			Deep.preep = Deep.day;
			Deep.xnodce = 4.5236020 - 9.2422029E-4*Deep.day;
			Deep.stem = Math.sin(Deep.xnodce);
			Deep.ctem = Math.cos(Deep.xnodce);
			Deep.zcosil = 0.91375164 - 0.03568096*Deep.ctem;
			Deep.zsinil = Math.sqrt(1.0 - Deep.zcosil*Deep.zcosil);
			Deep.zsinhl = 0.089683511*Deep.stem/Deep.zsinil;
			Deep.zcoshl = Math.sqrt(1.0 - Deep.zsinhl*Deep.zsinhl);
			Deep.c = 4.7199672 + 0.22997150*Deep.day;
			Deep.gam = 5.8351514 + 0.0019443680*Deep.day;
			Deep.zmol = Fmod2p(Deep.c - Deep.gam);
			Deep.zx = 0.39785416*Deep.stem/Deep.zsinil;
			Deep.zy = Deep.zcoshl*Deep.ctem + 0.91744867*Deep.zsinhl*Deep.stem;
			Deep.zx = AcTan(Deep.zx,Deep.zy);
			Deep.zx = Deep.gam + Deep.zx - Deep.xnodce;
			Deep.zcosgl = Math.cos(Deep.zx);
			Deep.zsingl = Math.sin(Deep.zx);
			Deep.zmos = 6.2565837 + 0.017201977*Deep.day;
			Deep.zmos = Fmod2p(Deep.zmos);
		}
    // Do solar terms 
		Deep.savtsn = 1E20;
		Deep.zcosg = Deep.zcosgs;
		Deep.zsing = Deep.zsings;
		Deep.zcosi = Deep.zcosis;
		Deep.zsini = Deep.zsinis;
		Deep.zcosh = Deep.cosq;
		Deep.zsinh = Deep.sinq;
		Deep.cc = Deep.c1ss;
		Deep.zn = Deep.zns;
		Deep.ze = Deep.zes;
		Deep.zmo = Deep.zmos;
		Deep.xnoi = 1.0/Deep.xnq;
		Deep.ls = 30; //assign 30 to ls
		DEEP20 = true;
		while (DEEP20)	{
			DEEP20 = false;
			Deep.a1 = Deep.zcosg*Deep.zcosh + Deep.zsing*Deep.zcosi*Deep.zsinh;
			Deep.a3 = -Deep.zsing*Deep.zcosh + Deep.zcosg*Deep.zcosi*Deep.zsinh;
			Deep.a7 = -Deep.zcosg*Deep.zsinh + Deep.zsing*Deep.zcosi*Deep.zcosh;
			Deep.a8 = Deep.zsing*Deep.zsini;
			Deep.a9 = Deep.zsing*Deep.zsinh + Deep.zcosg*Deep.zcosi*Deep.zcosh;
			Deep.a10 = Deep.zcosg*Deep.zsini;
			Deep.a2 = m_cosiq*Deep.a7 +  m_siniq*Deep.a8;
			Deep.a4 = m_cosiq*Deep.a9 +  m_siniq*Deep.a10;
			Deep.a5 = -m_siniq*Deep.a7 +  m_cosiq*Deep.a8;
			Deep.a6 = -m_siniq*Deep.a9 +  m_cosiq*Deep.a10;
			Deep.x1 = Deep.a1*m_cosomo + Deep.a2*m_sinomo;
			Deep.x2 = Deep.a3*m_cosomo + Deep.a4*m_sinomo;
			Deep.x3 = -Deep.a1*m_sinomo + Deep.a2*m_cosomo;
			Deep.x4 = -Deep.a3*m_sinomo + Deep.a4*m_cosomo;
			Deep.x5 = Deep.a5*m_sinomo;
			Deep.x6 = Deep.a6*m_sinomo;
			Deep.x7 = Deep.a5*m_cosomo;
			Deep.x8 = Deep.a6*m_cosomo;
			Deep.z31 = 12.0*Deep.x1*Deep.x1 - 3.0*Deep.x3*Deep.x3;
			Deep.z32 = 24.0*Deep.x1*Deep.x2 - 6.0*Deep.x3*Deep.x4;
			Deep.z33 = 12.0*Deep.x2*Deep.x2 - 3.0*Deep.x4*Deep.x4;
			Deep.z1 = 3.0*(Deep.a1*Deep.a1 + Deep.a2*Deep.a2) + Deep.z31*m_eqsq;
			Deep.z2 = 6.0*(Deep.a1*Deep.a3 + Deep.a2*Deep.a4) + Deep.z32*m_eqsq;
			Deep.z3 = 3.0*(Deep.a3*Deep.a3 + Deep.a4*Deep.a4) + Deep.z33*m_eqsq;
			Deep.z11 = -6.0*Deep.a1*Deep.a5 + m_eqsq*(-24.0*Deep.x1*Deep.x7 - 6.0*Deep.x3*Deep.x5);
			Deep.z12 = -6.0*(Deep.a1*Deep.a6 + Deep.a3*Deep.a5)
				+ m_eqsq*(-24.0*(Deep.x2*Deep.x7 + Deep.x1*Deep.x8) - 6.0*(Deep.x3*Deep.x6 + Deep.x4*Deep.x5));
			Deep.z13 = -6.0*Deep.a3*Deep.a6 + m_eqsq*(-24.0*Deep.x2*Deep.x8 - 6.0*Deep.x4*Deep.x6);
			Deep.z21 = 6.0*Deep.a2*Deep.a5 + m_eqsq*(24.0*Deep.x1*Deep.x5 - 6.0*Deep.x3*Deep.x7);
			Deep.z22 = 6.0*(Deep.a4*Deep.a5 + Deep.a2*Deep.a6)
				+ m_eqsq*(24.0*(Deep.x2*Deep.x5 + Deep.x1*Deep.x6) - 6.0*(Deep.x4*Deep.x7 + Deep.x3*Deep.x8));
			Deep.z23 = 6.0*Deep.a4*Deep.a6 + m_eqsq*(24.0*Deep.x2*Deep.x6 - 6.0*Deep.x4*Deep.x8);
			Deep.z1 = Deep.z1 + Deep.z1 + m_bsq*Deep.z31;
			Deep.z2 = Deep.z2 + Deep.z2 + m_bsq*Deep.z32;
			Deep.z3 = Deep.z3 + Deep.z3 + m_bsq*Deep.z33;
			Deep.s3 = Deep.cc*Deep.xnoi;
			Deep.s2 = -0.5*Deep.s3/m_rteqsq;
			Deep.s4 = Deep.s3*m_rteqsq;
			Deep.s1 = -15.0*Deep.eq*Deep.s4;
			Deep.s5 = Deep.x1*Deep.x3 + Deep.x2*Deep.x4;
			Deep.s6 = Deep.x2*Deep.x3 + Deep.x1*Deep.x4;
			Deep.s7 = Deep.x2*Deep.x4 - Deep.x1*Deep.x3;
			Deep.se = Deep.s1*Deep.zn*Deep.s5;
			Deep.si = Deep.s2*Deep.zn*(Deep.z11 + Deep.z13);
			Deep.sl = -Deep.zn*Deep.s3*(Deep.z1 + Deep.z3 - 14.0 - 6.0*m_eqsq);
			Deep.sgh = Deep.s4*Deep.zn*(Deep.z31 + Deep.z33 - 6.0);
			Deep.sh = -Deep.zn*Deep.s2*(Deep.z21 + Deep.z23);
			if (Deep.xqncl < 5.2359877E-2)
				Deep.sh = 0.0;
			Deep.ee2 = 2.0*Deep.s1*Deep.s6;
			Deep.e3 = 2.0*Deep.s1*Deep.s7;
			Deep.xi2 = 2.0*Deep.s2*Deep.z12;
			Deep.xi3 = 2.0*Deep.s2*(Deep.z13 - Deep.z11);
			Deep.xl2 = -2.0*Deep.s3*Deep.z2;
			Deep.xl3 = -2.0*Deep.s3*(Deep.z3 - Deep.z1);
			Deep.xl4 = -2.0*Deep.s3*(-21.0 - 9.0*m_eqsq)*Deep.ze;
			Deep.xgh2 = 2.0*Deep.s4*Deep.z32;
			Deep.xgh3 = 2.0*Deep.s4*(Deep.z33 - Deep.z31);
			Deep.xgh4 = -18.0*Deep.s4*Deep.ze;
			Deep.xh2 = -2.0*Deep.s2*Deep.z22;
			Deep.xh3 = -2.0*Deep.s2*(Deep.z23 - Deep.z21);
    // Do lunar terms 
			if (Deep.ls == 30) 	{
				Deep.sse = Deep.se;
				Deep.ssi = Deep.si;
				Deep.ssl = Deep.sl;
				Deep.ssh = Deep.sh/m_siniq;
				Deep.ssg = Deep.sgh - m_cosiq*Deep.ssh;
				Deep.se2 = Deep.ee2;
				Deep.si2 = Deep.xi2;
				Deep.sl2 = Deep.xl2;
				Deep.sgh2 = Deep.xgh2;
				Deep.sh2 = Deep.xh2;
				Deep.se3 = Deep.e3;
				Deep.si3 = Deep.xi3;
				Deep.sl3 = Deep.xl3;
				Deep.sgh3 = Deep.xgh3;
				Deep.sh3 = Deep.xh3;
				Deep.sl4 = Deep.xl4; 
				Deep.sgh4 = Deep.xgh4;
				Deep.zcosg = Deep.zcosgl;
				Deep.zsing = Deep.zsingl;
				Deep.zcosi = Deep.zcosil;
				Deep.zsini = Deep.zsinil;
				Deep.zcosh = Deep.zcoshl*Deep.cosq + Deep.zsinhl*Deep.sinq;
				Deep.zsinh = Deep.sinq*Deep.zcoshl - Deep.cosq*Deep.zsinhl;
				Deep.zn = Deep.znl;
				Deep.cc = Deep.c1l;
				Deep.ze = Deep.zel;
				Deep.zmo = Deep.zmol;
				Deep.ls = 40; //assign 40 to ls
				DEEP20 = true;
			}
			else if (Deep.ls == 40) 	{
				Deep.sse = Deep.sse + Deep.se;
				Deep.ssi = Deep.ssi + Deep.si;
				Deep.ssl = Deep.ssl + Deep.sl;
				Deep.ssg = Deep.ssg + Deep.sgh - m_cosiq/m_siniq*Deep.sh;
				Deep.ssh = Deep.ssh + Deep.sh/m_siniq;
    	// Geopotential resonance initialization for 12 hour orbits 
				Deep.iresfl = 0;
				Deep.isynfl = 0;
				DEEP80 = false;
//				if ((xnq < 0.0052359877) and (xnq > 0.0034906585))	{
				if (( Deep.xnq < 0.0052359877 ) && ( Deep.xnq > 0.0034906585 ))	{
					Deep.iresfl = 1;
					Deep.isynfl = 1;
					Deep.g200 = 1.0 + m_eqsq*(-2.5 + 0.8125*m_eqsq);
					Deep.g310 = 1.0 + 2.0*m_eqsq;
					Deep.g300 = 1.0 + m_eqsq*(-6.0 + 6.60937*m_eqsq);
					Deep.f220 = 0.75*(1.0 + m_cosiq)*(1.0 + m_cosiq);
					Deep.f311 = 0.9375*m_siniq*m_siniq*(1.0 + 3*m_cosiq) - 0.75*(1.0 + m_cosiq);
					Deep.f330 = 1.0 + m_cosiq;
					Deep.f330 = 1.875*Deep.f330*Deep.f330*Deep.f330;
					Deep.del1 = 3.0*Deep.xnq*Deep.xnq*Deep.aqnv*Deep.aqnv;
					Deep.del2 = 2.0*Deep.del1*Deep.f220*Deep.g200*Deep.q22;
					Deep.del3 = 3.0*Deep.del1*Deep.f330*Deep.g300*Deep.q33*Deep.aqnv;
					Deep.del1 = Deep.del1*Deep.f311*Deep.g310*Deep.q31*Deep.aqnv;
					Deep.fasx2 = 0.13130908;
					Deep.fasx4 = 2.8843198;
					Deep.fasx6 = 0.37448087;
					Deep.xlamo = Deep.xmao + m_Sat.fRightAscending + m_Sat.fPeregee - Deep.thgr;
					Deep.bfact = m_xlldot + Deep.xpidot - Deep.thdt;
					Deep.bfact = Deep.bfact + Deep.ssl + Deep.ssg + Deep.ssh;
					DEEP80 = true;
				}
				if (!DEEP80)	{
					if (( Deep.xnq < 8.26E-3 ) || ( Deep.xnq > 9.24E-3 ))
						return true;
					if (Deep.eq < 0.5)
						return true;
					Deep.iresfl = 1;
					Deep.eoc = Deep.eq*m_eqsq;
					Deep.g201 = -0.306 - (Deep.eq - 0.64)*0.440;
					DEEP45 = true;
					if (Deep.eq <= 0.65)	{
						Deep.g211 = 3.616 - 13.247*Deep.eq + 16.290*m_eqsq;
						Deep.g310 = -19.302 + 117.390*Deep.eq - 228.419*m_eqsq + 156.591*Deep.eoc;
						Deep.g322 = -18.9068 + 109.7927*Deep.eq - 214.6334*m_eqsq + 146.5816*Deep.eoc;
						Deep.g410 = -41.122 + 242.694*Deep.eq - 471.094*m_eqsq + 313.953*Deep.eoc;
						Deep.g422 = -146.407 + 841.880*Deep.eq - 1629.014*m_eqsq + 1083.435*Deep.eoc;
						Deep.g520 = -532.114 + 3017.977*Deep.eq - 5740.0*m_eqsq + 3708.276*Deep.eoc;
						DEEP45 = false;
					}
				} // DEEP80 not set ...
			}
			else	return true;
		} // return to DEEP20 untill DEEP2 == false ...
		if (!DEEP80)	{	 
			if (DEEP45)	{	
				Deep.g211 = -72.099 + 331.819*Deep.eq - 508.738*m_eqsq + 266.724*Deep.eoc;
				Deep.g310 = -346.844 + 1582.851*Deep.eq - 2415.925*m_eqsq + 1246.113*Deep.eoc;
				Deep.g322 = -342.585 + 1554.908*Deep.eq - 2366.899*m_eqsq + 1215.972*Deep.eoc;
				Deep.g410 = -1052.797 + 4758.686*Deep.eq - 7193.992*m_eqsq + 3651.957*Deep.eoc;
				Deep.g422 = -3581.69 + 16178.11*Deep.eq - 24462.77*m_eqsq + 12422.52*Deep.eoc;
			}
			if (Deep.eq > 0.715)
				Deep.g520 = -5149.66 + 29936.92*Deep.eq - 54087.36*m_eqsq + 31324.56*Deep.eoc;
			else
				Deep.g520 = 1464.74 - 4664.75*Deep.eq + 3763.64*m_eqsq;
			if (Deep.eq < (0.7))	{
				Deep.g533 = -919.2277 + 4988.61*Deep.eq - 9064.77*m_eqsq + 5542.21*Deep.eoc;
				Deep.g521 = -822.71072 + 4568.6173*Deep.eq - 8491.4146*m_eqsq + 5337.524*Deep.eoc;
				Deep.g532 = -853.666 + 4690.25*Deep.eq - 8624.77*m_eqsq + 5341.4*Deep.eoc;
			}
			else	{
				Deep.g533 = -37995.78 + 161616.52*Deep.eq - 229838.2*m_eqsq + 109377.94*Deep.eoc;
				Deep.g521 = -51752.104 + 218913.95*Deep.eq - 309468.16*m_eqsq + 146349.42*Deep.eoc;
				Deep.g532 = -40023.88 + 170470.89*Deep.eq - 242699.48*m_eqsq + 115605.82*Deep.eoc;
			}
			Deep.sini2 = m_siniq*m_siniq;
			Deep.f220 = 0.75*(1.0 + 2.0*m_cosiq + m_cosq2);
			Deep.f221 = 1.5*Deep.sini2;
			Deep.f321 = 1.875*m_siniq*(1.0 - 2.0*m_cosiq - 3.0*m_cosq2);
			Deep.f322 = -1.875*m_siniq*(1.0 + 2.0*m_cosiq - 3.0*m_cosq2);
			Deep.f441 = 35.0*Deep.sini2*Deep.f220;
			Deep.f442 = 39.3750*Deep.sini2*Deep.sini2;
			Deep.f522 = 9.84375*m_siniq*(Deep.sini2*(1.0 - 2.0*m_cosiq - 5.0*m_cosq2)
				+ 0.33333333*(-2.0 + 4.0*m_cosiq + 6.0*m_cosq2));
			Deep.f523 = m_siniq*(4.92187512*Deep.sini2*(-2.0 - 4.0*m_cosiq + 10.0*m_cosq2)
				+ 6.56250012*(1.0 + 2.0*m_cosiq - 3.0*m_cosq2));
			Deep.f542 = 29.53125*m_siniq*(2.0 - 8.0*m_cosiq + m_cosq2*(-12.0 + 8.0*m_cosiq + 10.0*m_cosq2));
			Deep.f543 = 29.53125*m_siniq*(-2.0 - 8.0*m_cosiq + m_cosq2*(12.0 + 8.0*m_cosiq - 10.0*m_cosq2));
			Deep.xno2 = Deep.xnq*Deep.xnq;
			Deep.ainv2 = Deep.aqnv*Deep.aqnv;
			Deep.temp1 = 3.0*Deep.xno2*Deep.ainv2;
			Deep.temp = Deep.temp1*Deep.root22;
			Deep.d2201 = Deep.temp*Deep.f220*Deep.g201;
			Deep.d2211 = Deep.temp*Deep.f221*Deep.g211;
			Deep.temp1 = Deep.temp1*Deep.aqnv;
			Deep.temp = Deep.temp1*Deep.root32;
			Deep.d3210 = Deep.temp*Deep.f321*Deep.g310;
			Deep.d3222 = Deep.temp*Deep.f322*Deep.g322;
			Deep.temp1 = Deep.temp1*Deep.aqnv;
			Deep.temp = 2.0*Deep.temp1*Deep.root44;
			Deep.d4410 = Deep.temp*Deep.f441*Deep.g410;
			Deep.d4422 = Deep.temp*Deep.f442*Deep.g422;
			Deep.temp1 = Deep.temp1*Deep.aqnv;
			Deep.temp = Deep.temp1*Deep.root52;
			Deep.d5220 = Deep.temp*Deep.f522*Deep.g520;
			Deep.d5232 = Deep.temp*Deep.f523*Deep.g532;
			Deep.temp = 2.0*Deep.temp1*Deep.root54;
			Deep.d5421 = Deep.temp*Deep.f542*Deep.g521;
			Deep.d5433 = Deep.temp*Deep.f543*Deep.g533;
			Deep.xlamo = Deep.xmao + m_Sat.fRightAscending + m_Sat.fRightAscending - Deep.thgr - Deep.thgr;
			Deep.bfact = m_xlldot + m_xnodot + m_xnodot - Deep.thdt - Deep.thdt;
			Deep.bfact = Deep.bfact + Deep.ssl + Deep.ssh + Deep.ssh;
    // Synchronous resonance terms initialization 
		} // end not DEEP80
		DEEP80 = false;
		Deep.xfact = Deep.bfact - Deep.xnq;
    // Initialize integrator 
		Deep.xli = Deep.xlamo;
		Deep.xni = Deep.xnq;
		Deep.atime = 0.0;
		Deep.stepp = 720.0;
		Deep.stepn = -720.0;
		Deep.step2 = 259200.0;
	} //dpinit
	else if (ideep == m_dpsec) { // Entrance for deep space secular effects 
		m_xll = m_xll + Deep.ssl*m_t;
		m_omgasm = m_omgasm + Deep.ssg*m_t;
		m_xnodes = m_xnodes + Deep.ssh*m_t;
		m__em = m_Sat.fEccentricity + Deep.sse*m_t;
		m_xinc = m_Sat.fInclination + Deep.ssi*m_t;
		if (m_xinc < 0.0)	{
			m_xinc = -m_xinc;
			m_xnodes = m_xnodes  +  m_PI;
			m_omgasm = m_omgasm - m_PI;
		}
		if (Deep.iresfl == 0) 
			return true;
		DEEP100 = true;
		while (DEEP100 || DEEP125)	{
			DEEP100 = false;
			DEEP125 = false;
			DEEP150 = false;
			if ((Deep.atime == 0.0) || 
				((m_t>= 0.0) && (Deep.atime <  0.0)) || 
				(( m_t<  0.0 ) && ( Deep.atime >= 0.0 )) )	{
				if (m_t< 0)	{
					Deep.delt = Deep.stepn;
				}
				else 	{
					Deep.delt = Deep.stepp;
				}
				Deep.atime = 0.0;
				Deep.xni = Deep.xnq;
				Deep.xli = Deep.xlamo;
			}
			else {
				if (Math.abs(m_t) < Math.abs(Deep.atime))	{
					Deep.delt = Deep.stepp;
					if (m_t>= 0.0)
						Deep.delt= Deep.stepn;
					Deep.iret = 100; //assign 100 to iret
					Deep.iretn = 165; //assign 165 to iretn
					DEEP150 = true;
				}
				else {
					Deep.delt = Deep.stepn;
					if (m_t> 0.0)
						Deep.delt = Deep.stepp;
				}
			}
			if (!DEEP150)	{
				if (Math.abs(m_t- Deep.atime) >= Deep.stepp) 	{
					Deep.iret = 125; //assign 125 to iret
					Deep.iretn = 165; //assign 165 to iretn
				}
				else {
					Deep.ft = m_t- Deep.atime;
					Deep.iretn = 140; //assign 140 to iretn
				}
			} // end if (!DEEP150)
    // Dot terms calculated 
			bStopInnerLoop = false;
			while (!bStopInnerLoop)	{ 
				if (Deep.isynfl != 0)	{
					Deep.xndot = Deep.del1*Math.sin(Deep.xli - Deep.fasx2) + Deep.del2*Math.sin(2.0*(Deep.xli - Deep.fasx4))
						+ Deep.del3*Math.sin(3.0*(Deep.xli - Deep.fasx6));
					Deep.xnddt = Deep.del1*Math.cos(Deep.xli - Deep.fasx2)
						+ 2.0*Deep.del2*Math.cos(2.0*(Deep.xli - Deep.fasx4))
						+ 3.0*Deep.del3*Math.cos(3.0*(Deep.xli - Deep.fasx6));
				}
				else {
					Deep.xomi = Deep.omegaq + m_omgdt * Deep.atime;
					Deep.x2omi = Deep.xomi + Deep.xomi;
					Deep.x2li = Deep.xli + Deep.xli;
					Deep.xndot = Deep.d2201*Math.sin(Deep.x2omi + Deep.xli - Deep.g22)
						+ Deep.d2211*Math.sin(Deep.xli - Deep.g22)
						+ Deep.d3210*Math.sin(Deep.xomi + Deep.xli - Deep.g32)
						+ Deep.d3222*Math.sin(-Deep.xomi + Deep.xli - Deep.g32)
						+ Deep.d4410*Math.sin(Deep.x2omi + Deep.x2li - Deep.g44)
						+ Deep.d4422*Math.sin(Deep.x2li - Deep.g44)
						+ Deep.d5220*Math.sin(Deep.xomi + Deep.xli - Deep.g52)
						+ Deep.d5232*Math.sin(-Deep.xomi + Deep.xli - Deep.g52)
						+ Deep.d5421*Math.sin(Deep.xomi + Deep.x2li - Deep.g54)
						+ Deep.d5433*Math.sin(-Deep.xomi + Deep.x2li - Deep.g54);
					xnddt = Deep.d2201*Math.cos(Deep.x2omi + Deep.xli - Deep.g22)
						+ Deep.d2211*Math.cos(Deep.xli - Deep.g22)
						+ Deep.d3210*Math.cos(Deep.xomi + Deep.xli - Deep.g32)
						+ Deep.d3222*Math.cos(-Deep.xomi + Deep.xli - Deep.g32)
						+ Deep.d5220*Math.cos(Deep.xomi + Deep.xli - Deep.g52)
						+ Deep.d5232*Math.cos(-Deep.xomi + Deep.xli - Deep.g52)
						+ 2.0*(Deep.d4410*Math.cos(Deep.x2omi + Deep.x2li - Deep.g44)
						+ Deep.d4422*Math.cos(Deep.x2li - Deep.g44)
						+ Deep.d5421*Math.cos(Deep.xomi + Deep.x2li - Deep.g54)
						+ Deep.d5433*Math.cos(-Deep.xomi + Deep.x2li - Deep.g54));
				}
				Deep.xldot = Deep.xni + Deep.xfact;
				Deep.xnddt = Deep.xnddt*Deep.xldot;
				if (Deep.iretn == 140) 	{
					m_xn = Deep.xni + Deep.xndot*Deep.ft + Deep.xnddt*Deep.ft*Deep.ft*0.5;
					Deep.xl = Deep.xli + Deep.xldot*Deep.ft + Deep.xndot*Deep.ft*Deep.ft*0.5;
					Deep.temp = -m_xnodes + Deep.thgr + m_t*Deep.thdt;
					m_xll = Deep.xl - m_omgasm + Deep.temp;
					if (Deep.isynfl == 0)
						m_xll = Deep.xl + Deep.temp + Deep.temp;
					return true;
				}
				else if (Deep.iretn == 165) 	{
					Deep.xli = Deep.xli + Deep.xldot*Deep.delt + Deep.xndot*Deep.step2;
					Deep.xni = Deep.xni + Deep.xndot*Deep.delt + Deep.xnddt*Deep.step2;
					Deep.atime = Deep.atime + Deep.delt;
					if(Deep.iret == 100)
						DEEP100 = true;
					else if(Deep.iret == 125) 
						DEEP125 = true;
					else 
						return true;
					// Got this far means I should loop back to DEEP100 or DEEP125
					bStopInnerLoop = true;
				}
				else 
					return true;
    // Integrator 
				if (!bStopInnerLoop)
					Deep.iretn = 165; //assign 165 to iretn
			}	// back to while (!bStopInnerLoop) ...
		} // while (DEEP100 || DEEP125) ...
	} //dpsec
    else if(ideep ==  m_dpper) { // Entrance for lunar-solar periodics 
		Deep.sinis = Math.sin(m_xinc);
		Deep.cosis = Math.cos(m_xinc);
		if (Math.abs(Deep.savtsn - m_t) >= 30.0)	{
			Deep.savtsn = m_t;
			Deep.zm = Deep.zmos + Deep.zns*m_t;
			Deep.zf = Deep.zm + 2.0*Deep.zes*Math.sin(Deep.zm);
			Deep.sinzf = Math.sin(Deep.zf);
			Deep.f2 = 0.5*Deep.sinzf*Deep.sinzf - 0.25;
			Deep.f3 = -0.5*Deep.sinzf*Math.cos(Deep.zf);
			Deep.ses = Deep.se2*Deep.f2 + Deep.se3*Deep.f3;
			Deep.sis = Deep.si2*Deep.f2 + Deep.si3*Deep.f3;
			Deep.sls = Deep.sl2*Deep.f2 + Deep.sl3*Deep.f3 + Deep.sl4*Deep.sinzf;
			Deep.sghs = Deep.sgh2*Deep.f2 + Deep.sgh3*Deep.f3 + Deep.sgh4*Deep.sinzf;
			Deep.shs = Deep.sh2*Deep.f2 + Deep.sh3*Deep.f3;
			Deep.zm = Deep.zmol + Deep.znl*m_t;
			Deep.zf = Deep.zm + 2.0*Deep.zel*Math.sin(Deep.zm);
			Deep.sinzf = Math.sin(Deep.zf);
			Deep.f2 = 0.5*Deep.sinzf*Deep.sinzf - 0.25;
			Deep.f3 = -0.5*Deep.sinzf*Math.cos(Deep.zf);
			Deep.sel = Deep.ee2*Deep.f2 + Deep.e3*Deep.f3;
			Deep.sil = Deep.xi2*Deep.f2 + Deep.xi3*Deep.f3;
			Deep.sll = Deep.xl2*Deep.f2 + Deep.xl3*Deep.f3 + Deep.xl4*Deep.sinzf;
			Deep.sghl = Deep.xgh2*Deep.f2 + Deep.xgh3*Deep.f3 + Deep.xgh4*Deep.sinzf;
			Deep.sh1 = Deep.xh2*Deep.f2 + Deep.xh3*Deep.f3;
			Deep.pe = Deep.ses + Deep.sel;
			Deep.pinc = Deep.sis + Deep.sil;
			Deep.pl = Deep.sls + Deep.sll;
		}
		Deep.pgh = Deep.sghs + Deep.sghl;
		Deep.ph = Deep.shs + Deep.sh1;
		m_xinc = m_xinc + Deep.pinc;
		m__em = m__em + Deep.pe;
		if (Deep.xqncl >= 0.2)	{
	    // Apply periodics directly 
			Deep.ph = Deep.ph/m_siniq;
			Deep.pgh = Deep.pgh - m_cosiq*Deep.ph;
			m_omgasm = m_omgasm + Deep.pgh;
			m_xnodes = m_xnodes + Deep.ph;
			m_xll = m_xll + Deep.pl;
		}
		else {
    // Apply periodics with Lyddane modification 
			Deep.sinok = Math.sin(m_xnodes);
			Deep.cosok = Math.cos(m_xnodes);
			Deep.alfdp = Deep.sinis*Deep.sinok;
			Deep.betdp = Deep.sinis*Deep.cosok;
			Deep.dalf = Deep.ph*Deep.cosok + Deep.pinc*Deep.cosis*Deep.sinok;
			Deep.dbet = -Deep.ph*Deep.sinok + Deep.pinc*Deep.cosis*Deep.cosok;
			Deep.alfdp = Deep.alfdp + Deep.dalf;
			Deep.betdp = Deep.betdp + Deep.dbet;
			Deep.xls = m_xll + m_omgasm + Deep.cosis*m_xnodes;
			Deep.dls = Deep.pl + Deep.pgh - Deep.pinc*m_xnodes*Deep.sinis;
			Deep.xls = Deep.xls + Deep.dls;
			m_xnodes = AcTan(Deep.alfdp, Deep.betdp);
			m_xll = m_xll + Deep.pl;
			m_omgasm = Deep.xls - m_xll - Math.cos(m_xinc)*m_xnodes;
		}
	}; //dpper
	return true;
}; //Procedure Deep

// void
function Call_dpinit(	eosq, sinio, cosio, betao, aodp,
						theta2, sing, cosg, betao2, xmdot,
						omgdot, xnodott, xnodpp)
{
	m_eqsq   = eosq;
	m_siniq  = sinio;
	m_cosiq  = cosio;
	m_rteqsq = betao;
	m_ao     = aodp;
	m_cosq2  = theta2;
	m_sinomo = sing;
	m_cosomo = cosg;
	m_bsq    = betao2;
	m_xlldot = xmdot;
	m_omgdt  = omgdot;
	m_xnodot = xnodott;
	m_xnodp  = xnodpp;
	Deep(1);
}; //Procedure Call_dpinit

// void
function Call_dpsec( xmdf, omgadf, xnode, emm, xincc, xnn, tsince)
{
	m_xll    = xmdf;
	m_omgasm = omgadf;
	m_xnodes = xnode;
	m_xn     = xnn;  
	m_t      = tsince;
	Deep(2);
}; // Procedure Call_dpsec 

// void
function Call_dpper( e, xincc, omgadf, xnode, xmam)
{
	m__em    = e;       
	m_xinc   = xincc;   
	m_omgasm = omgadf;  
	m_xnodes = xnode;
	m_xll    = xmam;
	Deep(3);
}; //Procedure Call_dpper

//////////////////////////////////////////////////////////////////////////////
// BOOL SDP4(double tsince, int *iflag, VECTOR *pos, VECTOR *vel)
//  Init static variables ...
    SDP4.a1       = 0.0;SDP4.a3ovk2   = 0.0;SDP4.ao       = 0.0;
    SDP4.aodp     = 0.0;SDP4.aycof    = 0.0;SDP4.betao    = 0.0;
    SDP4.betao2   = 0.0;SDP4.c1       = 0.0;SDP4.c2       = 0.0;
    SDP4.c4       = 0.0;SDP4.coef     = 0.0;SDP4.coef1    = 0.0;
    SDP4.cosg     = 0.0;SDP4.cosio    = 0.0;SDP4.del1     = 0.0;
    SDP4.delo     = 0.0;SDP4.eeta     = 0.0;SDP4.eosq     = 0.0;
    SDP4.eta      = 0.0;SDP4.etasq    = 0.0;SDP4.omgdot   = 0.0;
    SDP4.perige   = 0.0;SDP4.pinvsq   = 0.0;SDP4.psisq    = 0.0;
    SDP4.qoms24   = 0.0;SDP4.s4       = 0.0;SDP4.sing     = 0.0;
    SDP4.sinio    = 0.0;SDP4.t2cof    = 0.0;SDP4.temp1    = 0.0;
    SDP4.temp2    = 0.0;SDP4.temp3    = 0.0;SDP4.theta2   = 0.0;
    SDP4.theta4   = 0.0;SDP4.tsi      = 0.0;SDP4.x1m5th   = 0.0;
    SDP4.x1mth2   = 0.0;SDP4.x3thm1   = 0.0;SDP4.x7thm1   = 0.0;
    SDP4.xhdot1   = 0.0;SDP4.xlcof    = 0.0;SDP4.xmdot    = 0.0;
    SDP4.xnodcf   = 0.0;SDP4.xnodot   = 0.0;SDP4.xnodp    = 0.0;
function SDP4( tsince )
{
    var i;  // int
    var a,axn,ayn,aynl,beta,betal,capu,cos2u,cosepw,cosik;
    var cosnok,cosu,cosuk,e,ecose,elsq,em,epw,esine,omgadf;
    var pl,r,rdot,rdotk,rfdot,rfdotk,rk,sin2u,sinepw,sinik;
    var sinnok,sinu,sinuk,temp,temp4,temp5,temp6,tempa;
    var tempe,templ,tsq,u,uk,ux,uy,uz,vx,vy,vz,xinc,xinck;
    var xl,xll,xlt,xmam,xmdf,xmx,xmy,xn,xnoddf,xnode,xnodek;
    var x,y,z,xdot,ydot,zdot;
	var ee;

	if (m_iFlag != 0)	{
// Recover original mean motion (xnodp) and semimajor axis (aodp) 
// from input elements. 
		SDP4.a1 = Math.pow(m_xke/m_Sat.fMeanMotion,m_tothrd);
		SDP4.cosio = Math.cos(m_Sat.fInclination);
		SDP4.theta2 = SDP4.cosio*SDP4.cosio;
		SDP4.x3thm1 = 3.0*SDP4.theta2 - 1.0;
		SDP4.eosq = m_Sat.fEccentricity*m_Sat.fEccentricity;
		SDP4.betao2 = 1.0 - SDP4.eosq;
		SDP4.betao = Math.sqrt(SDP4.betao2);
		SDP4.del1 = 1.5*m_ck2*SDP4.x3thm1/(SDP4.a1*SDP4.a1*SDP4.betao*SDP4.betao2);
		SDP4.ao = SDP4.a1*(1.0 - SDP4.del1*(0.5*m_tothrd + SDP4.del1*(1.0 + 134.0/81.0*SDP4.del1)));
		SDP4.delo = 1.5*m_ck2*SDP4.x3thm1/(SDP4.ao*SDP4.ao*SDP4.betao*SDP4.betao2);
		SDP4.xnodp = m_Sat.fMeanMotion/(1.0 + SDP4.delo);
		SDP4.aodp = SDP4.ao/(1.0 - SDP4.delo);
// Initialization 
// For perigee below 156 km, the values of s and qoms2t are altered. 
		SDP4.s4 = m_s;
		SDP4.qoms24 = m_qoms2t;
		SDP4.perige = (SDP4.aodp*(1.0 - m_Sat.fEccentricity) - m_ae)*m_xkmper;
		if (SDP4.perige < 156.0)	{
			SDP4.s4 = SDP4.perige - 78.0;
			if (SDP4.perige <= 98.0) 
				SDP4.s4 = 20.0;
			SDP4.qoms24 = Math.pow((120.0 - SDP4.s4)*m_ae/m_xkmper,4.0);
			SDP4.s4 = SDP4.s4/m_xkmper + m_ae;
		}
		SDP4.pinvsq = 1.0/(SDP4.aodp*SDP4.aodp*SDP4.betao2*SDP4.betao2);
		SDP4.sing = Math.sin(m_Sat.fPeregee);
		SDP4.cosg = Math.cos(m_Sat.fPeregee);
		SDP4.tsi = 1.0/(SDP4.aodp - SDP4.s4);
		SDP4.eta = SDP4.aodp*m_Sat.fEccentricity*SDP4.tsi;
		SDP4.etasq = SDP4.eta*SDP4.eta;
		SDP4.eeta = m_Sat.fEccentricity*SDP4.eta;
		SDP4.psisq = Math.abs(1.0 - SDP4.etasq);
		SDP4.coef = SDP4.qoms24*Math.pow(SDP4.tsi,4.0);
		SDP4.coef1 = SDP4.coef/Math.pow(SDP4.psisq,3.5);
		SDP4.c2 = SDP4.coef1*SDP4.xnodp*(SDP4.aodp*(1.0 + 1.5*SDP4.etasq + SDP4.eeta*(4.0 + SDP4.etasq))
			+ 0.75*m_ck2*SDP4.tsi/SDP4.psisq*SDP4.x3thm1*(8.0 + 3.0*SDP4.etasq*(8.0 + SDP4.etasq)));
		SDP4.c1 = m_Sat.fRadiationCoefficient*SDP4.c2;
		SDP4.sinio = Math.sin(m_Sat.fInclination);
		SDP4.a3ovk2 = -m_xj3/m_ck2*Math.pow(m_ae,3.0);
		SDP4.x1mth2 = 1.0 - SDP4.theta2;
		SDP4.c4 = 2.0*SDP4.xnodp*SDP4.coef1*SDP4.aodp*SDP4.betao2*(SDP4.eta*(2.0 + 0.5*SDP4.etasq)
			+ m_Sat.fEccentricity*(0.5 + 2.0*SDP4.etasq) - 2.0*m_ck2*SDP4.tsi/(SDP4.aodp*SDP4.psisq)
			*(-3.0*SDP4.x3thm1*(1.0 - 2.0*SDP4.eeta + SDP4.etasq*(1.5 - 0.5*SDP4.eeta))
			+ 0.75*SDP4.x1mth2*(2.0*SDP4.etasq - SDP4.eeta*(1.0 + SDP4.etasq))*Math.cos(2.0*m_Sat.fPeregee)));
		SDP4.theta4 = SDP4.theta2*SDP4.theta2;
		SDP4.temp1 = 3.0*m_ck2*SDP4.pinvsq*SDP4.xnodp;
		SDP4.temp2 = SDP4.temp1*m_ck2*SDP4.pinvsq;
		SDP4.temp3 = 1.25*m_ck4*SDP4.pinvsq*SDP4.pinvsq*SDP4.xnodp;
		SDP4.xmdot = SDP4.xnodp + 0.5*SDP4.temp1*SDP4.betao*SDP4.x3thm1
			+ 0.0625*SDP4.temp2*SDP4.betao*(13.0 - 78.0*SDP4.theta2 + 137.0*SDP4.theta4);
		SDP4.x1m5th = 1.0 - 5.0*SDP4.theta2;
		SDP4.omgdot = -0.5*SDP4.temp1*SDP4.x1m5th + 0.0625*SDP4.temp2*(7.0 - 114.0*SDP4.theta2 + 395.0*SDP4.theta4)
			+ SDP4.temp3*(3.0 - 36.0*SDP4.theta2 + 49.0*SDP4.theta4);
		SDP4.xhdot1 = -SDP4.temp1*SDP4.cosio;
		SDP4.xnodot = SDP4.xhdot1 + (0.5*SDP4.temp2*(4.0 - 19.0*SDP4.theta2)
			+ 2.0*SDP4.temp3*(3.0 - 7.0*SDP4.theta2))*SDP4.cosio;
		SDP4.xnodcf = 3.5*SDP4.betao2*SDP4.xhdot1*SDP4.c1;
		SDP4.t2cof = 1.5*SDP4.c1;
		SDP4.xlcof = 0.125*SDP4.a3ovk2*SDP4.sinio*(3.0 + 5.0*SDP4.cosio)/(1.0 + SDP4.cosio);
		SDP4.aycof = 0.25*SDP4.a3ovk2*SDP4.sinio;
		SDP4.x7thm1 = 7.0*SDP4.theta2 - 1.0;
		m_iFlag = 0;
//		Call_dpinit(&eosq,&sinio,&cosio,&betao,&aodp,&theta2,&sing,&cosg,
//			&betao2,&xmdot,&omgdot,&xnodot,&xnodp);
		Call_dpinit(SDP4.eosq, SDP4.sinio, SDP4.cosio, SDP4.betao, SDP4.aodp, SDP4.theta2, SDP4.sing, SDP4.cosg, SDP4.betao2, SDP4.xmdot, SDP4.omgdot, SDP4.xnodot, SDP4.xnodp);
		// and set the local variables to the values returned (global values)
		SDP4.eosq 	= m_eqsq;
		SDP4.sinio 	= m_siniq;
		SDP4.cosio 	= m_cosiq;
		SDP4.betao 	= m_rteqsq;
		SDP4.aodp 	= m_ao;
		SDP4.theta2 	= m_cosq2;
		SDP4.sing 	= m_sinomo;
		SDP4.cosg 	= m_cosomo;
		SDP4.betao2 	= m_bsq;
		SDP4.xmdot 	= m_xlldot;
		SDP4.omgdot 	= m_omgdt;
		SDP4.xnodot = m_xnodot;
		SDP4.xnodp 	= m_xnodp;
// Update for secular gravity and atmospheric drag 
	}
	xmdf = m_Sat.fMeanAnomaly + SDP4.xmdot*tsince;
	omgadf = m_Sat.fPeregee + SDP4.omgdot*tsince;
	xnoddf = m_Sat.fRightAscending + SDP4.xnodot*tsince;
	tsq = tsince*tsince;
	xnode = xnoddf + SDP4.xnodcf*tsq;
	tempa = 1.0 - SDP4.c1*tsince;
	tempe = m_Sat.fRadiationCoefficient*SDP4.c4*tsince;
	templ = SDP4.t2cof*tsq;
	xn = SDP4.xnodp;
//	Call_dpsec(&xmdf,&omgadf,&xnode,&em,&xinc,&xn,&tsince);
	Call_dpsec(xmdf, omgadf, xnode, em, xinc, xn, tsince);
	// and set the local variables to the values returned (global values)
	xmdf	= m_xll;
	omgadf	= m_omgasm;
	xnode	= m_xnodes;
	em		= m__em;
	xinc	= m_xinc;
	xn		= m_xn;
	tsince	= m_t;
	a = Math.pow(m_xke/xn,m_tothrd)*sqr(tempa);
	e = em - tempe;
	ee = e*e;
	if ( ee > 1.0) 
		return false;	// wrong satellite datas
	xmam = xmdf + SDP4.xnodp*templ;
//	Call_dpper(&e,&xinc,&omgadf,&xnode,&xmam);
	Call_dpper( e, xinc, omgadf, xnode, xmam);
	// and set the local variables to the values returned (global values)
	e      = m__em;
	xincc  = m_xinc;
	omgadf = m_omgasm;
	xnode  = m_xnodes;
	xmam   = m_xll;
	xinc   = m_xinc;
	
	xl = xmam + omgadf + xnode;
	beta = Math.sqrt(1.0 - ee);
	xn = m_xke/Math.pow(a,1.5);
// Long period periodics 
	axn = e*Math.cos(omgadf);
	temp = 1.0/(a*beta*beta);
  	xll = temp*SDP4.xlcof*axn;
	aynl = temp*SDP4.aycof;
	xlt = xl + xll;
	ayn = e*Math.sin(omgadf) + aynl;
// Solve Kepler's Equation 
	capu = Fmod2p(xlt - xnode);
	SDP4.temp2 = capu;
	for (i = 1;i < 10;i++)	{
		sinepw = Math.sin(SDP4.temp2);
		cosepw = Math.cos(SDP4.temp2);
		SDP4.temp3 = axn*sinepw;
		temp4 = ayn*cosepw;
		temp5 = axn*cosepw;
		temp6 = ayn*sinepw;
		epw = (capu - temp4 + SDP4.temp3 - SDP4.temp2)/(1.0 - temp5 - temp6) + SDP4.temp2;
		if (Math.abs(epw - SDP4.temp2) <= m_e6a) 
			break;
		SDP4.temp2 = epw;
    }; //for i
// Short period preliminary quantities 
	ecose = temp5 + temp6;
	esine = SDP4.temp3 - temp4;
	elsq = axn*axn + ayn*ayn;
	temp = 1.0 - elsq;
	pl = a*temp;
	r = a*(1.0 - ecose);
	SDP4.temp1 = 1.0/r;
	rdot = m_xke*Math.sqrt(a)*esine*SDP4.temp1;
	rfdot = m_xke*Math.sqrt(pl)*SDP4.temp1;
	SDP4.temp2 = a*SDP4.temp1;
	betal = Math.sqrt(temp);
	SDP4.temp3 = 1.0/(1.0 + betal);
	cosu = SDP4.temp2*(cosepw - axn + ayn*esine*SDP4.temp3);
	sinu = SDP4.temp2*(sinepw - ayn - axn*esine*SDP4.temp3);
	u = AcTan(sinu,cosu);
	sin2u = 2.0*sinu*cosu;
	cos2u = 2.0*cosu*cosu - 1.0;
	temp = 1.0/pl;
	SDP4.temp1 = m_ck2*temp;
	SDP4.temp2 = SDP4.temp1*temp;
// Update for short periodics 
	rk = r*(1.0 - 1.5*SDP4.temp2*betal*SDP4.x3thm1) + 0.5*SDP4.temp1*SDP4.x1mth2*cos2u;
	uk = u - 0.25*SDP4.temp2*SDP4.x7thm1*sin2u;
	xnodek = xnode + 1.5*SDP4.temp2*SDP4.cosio*sin2u;
  	xinck = xinc + 1.5*SDP4.temp2*SDP4.cosio*SDP4.sinio*cos2u;
	rdotk = rdot - xn*SDP4.temp1*SDP4.x1mth2*sin2u;
	rfdotk = rfdot + xn*SDP4.temp1*(SDP4.x1mth2*cos2u + 1.5*SDP4.x3thm1);
// Orientation vectors 
	sinuk = Math.sin(uk);
	cosuk = Math.cos(uk);
	sinik = Math.sin(xinck);
	cosik = Math.cos(xinck);
	sinnok = Math.sin(xnodek);
	cosnok = Math.cos(xnodek);
	xmx = -sinnok*cosik;
	xmy = cosnok*cosik;
	ux = xmx*sinuk + cosnok*cosuk;
	uy = xmy*sinuk + sinnok*cosuk;
	uz = sinik*sinuk;
	vx = xmx*cosuk - cosnok*sinuk;
	vy = xmy*cosuk - sinnok*sinuk;
	vz = sinik*cosuk;
// Position and velocity 
	x = rk*ux;  m_pos[0] = x;
	y = rk*uy;  m_pos[1] = y;
	z = rk*uz;  m_pos[2] = z;
	
	xdot = rdotk*ux + rfdotk*vx;  m_vel[0] = xdot;
	ydot = rdotk*uy + rfdotk*vy;  m_vel[1] = ydot;
	zdot = rdotk*uz + rfdotk*vz;  m_vel[2] = zdot;
	return true;
};	//Procedure SDP4



