* SGP8 14 NOV 80

	SUBROUTINE SGP8(IFLAG,TSINCE)

	DOUBLE PRECISION EPOCH, DS50

	COMMON/E1/XMO,XNODEO,OMEGAO,EO,XINCL,XNO,XNDT2O,XNDD6O,BSTAR,X,Y,Z,XDOT,YDOT,ZDOT,EPOCH,DS50
	COMMON/C1/CK2,CK4,E6A,QOMS2T,S,TOTHRD,XJ3,XKE,XKMPER,XMNPDA,AE
	COMMON/C2/DE2RA,PI,PIO2,TWOPI,X3PIO2

	DATA RHO/0.15696615/

	SAVE COSI, THETA2, TTHMUN, XNODP, ISIMP, SINI, SINIO2, COSIO2 
	SAVE UNM5TH, UNMTH2, A3COF, XMDT1, XGDT1, XHDT1, XLLDOT, OMGDT
	SAVE XNODOT, XNDT, EDOT, PP, GAMMA, XND, QQ, ED, OVGPP

	IF (IFLAG .NE. 0) THEN

	    ! Recover original mean motion (xnodp) and semimajor axis (aodp)
	    ! from input elements --------- calculate ballistic coefficient
	    ! (B term) from input B* drag term

	    A1=(XKE/XNO)**TOTHRD
	    COSI=COS(XINCL)
	    THETA2=COSI*COSI
	    TTHMUN=3.*THETA2-1.
	    EOSQ=EO*EO
	    BETAO2=1.-EOSQ
	    BETAO=SQRT(BETAO2)
	    DEL1=1.5*CK2*TTHMUN/(A1*A1*BETAO*BETAO2)
	    AO=A1*(1.-DEL1*(.5*TOTHRD+DEL1*(1.+134./81.*DEL1)))
	    DELO=1.5*CK2*TTHMUN/(AO*AO*BETAO*BETAO2)
	    AODP=AO/(1.-DELO)
	    XNODP=XNO/(1.+DELO)
	    B=2.*BSTAR/RHO

	    ! Initialization

	    ISIMP=0
	    PO=AODP*BETAO2
	    POM2=1./(PO*PO)
	    SINI=SIN(XINCL)
	    SING=SIN(OMEGAO)
	    COSG=COS(OMEGAO)
	    TEMP=.5*XINCL
	    SINIO2=SIN(TEMP)
	    COSIO2=COS(TEMP)
	    THETA4=THETA2*THETA2
	    UNM5TH=1.-5.*THETA2
	    UNMTH2=1.-THETA2
	    A3COF=-XJ3/CK2*AE*AE*AE
	    PARDT1=3.*CK2*POM2*XNODP
	    PARDT2=PARDT1*CK2*POM2
	    PARDT4=1.25*CK4*POM2*POM2*XNODP
	    XMDT1=.5*PARDT1*BETAO*TTHMUN
	    XGDT1=-.5*PARDT1*UNM5TH
	    XHDT1=-PARDT1*COSI
	    XLLDOT=XNODP+XMDT1+.0625*PARDT2*BETAO*(13.-78.*THETA2+137.*THETA4)
	    OMGDT=XGDT1+.0625*PARDT2*(7.-114.*THETA2+395.*THETA4)+PARDT4*(3.-36.*THETA2+49.*THETA4)
	    XNODOT=XHDT1+(.5*PARDT2*(4.-19.*THETA2)+2.*PARDT4*(3.-7.*THETA2))*COSI
	    TSI=1./(PO-S)
	    ETA=EO*S*TSI
	    ETA2=ETA*ETA
	    PSIM2=ABS(1./(1.-ETA2))
	    ALPHA2=1.+EOSQ
	    EETA=EO*ETA
	    COS2G=2.*COSG*COSG-1.
	    D5=TSI*PSIM2
	    D1=D5/PO
	    D2=12.+ETA2*(36.+4.5*ETA2)
	    D3=ETA2*(15.+2.5*ETA2)
	    D4=ETA*(5.+3.75*ETA2)
	    B1=CK2*TTHMUN
	    B2=-CK2*UNMTH2
	    B3=A3COF*SINI
	    C0=.5*B*RHO*QOMS2T*XNODP*AODP*TSI**4*PSIM2**3.5/SQRT(ALPHA2)
	    C1=1.5*XNODP*ALPHA2*ALPHA2*C0
	    C4=D1*D3*B2
	    C5=D5*D4*B3
	    XNDT=C1*((2.+ETA2*(3.+34.*EOSQ)+5.*EETA*(4.+ETA2)+8.5*EOSQ)+D1*D2*B1+C4*COS2G+C5*SING)
	    XNDTN=XNDT/XNODP

	    ! If drag is very small, the isimp flag is set and the
	    ! equations are truncated to linear variation in mean
	    ! motion and quadratic variation in mean anomaly

	    IF (ABS(XNDTN*XMNPDA) .GE. 2.16E-3) THEN

		D6=ETA*(30.+22.5*ETA2)
		D7=ETA*(5.+12.5*ETA2)
		D8=1.+ETA2*(6.75+ETA2)
		C8=D1*D7*B2
		C9=D5*D8*B3
		EDOT=-C0*(ETA*(4.+ETA2+EOSQ*(15.5+7.*ETA2))+EO*(5.+15.*ETA2)+D1*D6*B1 +C8*COS2G+C9*SING)
		D20=.5*TOTHRD*XNDTN
		ALDTAL=EO*EDOT/ALPHA2
		TSDTTS=2.*AODP*TSI*(D20*BETAO2+EO*EDOT)
		ETDT=(EDOT+EO*TSDTTS)*TSI*S
		PSDTPS=-ETA*ETDT*PSIM2
		SIN2G=2.*SING*COSG
		C0DTC0=D20+4.*TSDTTS-ALDTAL-7.*PSDTPS
		C1DTC1=XNDTN+4.*ALDTAL+C0DTC0
		D9=ETA*(6.+68.*EOSQ)+EO*(20.+15.*ETA2)
		D10=5.*ETA*(4.+ETA2)+EO*(17.+68.*ETA2)
		D11=ETA*(72.+18.*ETA2)
		D12=ETA*(30.+10.*ETA2)
		D13=5.+11.25*ETA2
		D14=TSDTTS-2.*PSDTPS
		D15=2.*(D20+EO*EDOT/BETAO2)
		D1DT=D1*(D14+D15)
		D2DT=ETDT*D11
		D3DT=ETDT*D12
		D4DT=ETDT*D13
		D5DT=D5*D14
		C4DT=B2*(D1DT*D3+D1*D3DT)
		C5DT=B3*(D5DT*D4+D5*D4DT)
		D16= D9*ETDT+D10*EDOT +B1*(D1DT*D2+D1*D2DT) +C4DT*COS2G+C5DT*SING+XGDT1*(C5*COSG-2.*C4*SIN2G)
		XNDDT=C1DTC1*XNDT+C1*D16
		EDDOT=C0DTC0*EDOT-C0*(
     &		    (4.+3.*ETA2+30.*EETA+EOSQ*(15.5+21.*ETA2))*ETDT+(5.+15.*ETA2+EETA*(31.+14.*ETA2))*EDOT +
     &		    B1*(D1DT*D6+D1*ETDT*(30.+67.5*ETA2)) +B2*(D1DT*D7+D1*ETDT*(5.+37.5*ETA2))*COS2G+
     &		    B3*(D5DT*D8+D5*ETDT*ETA*(13.5+4.*ETA2))*SING+XGDT1*(C9*COSG-2.*C8*SIN2G))
		D25=EDOT*EDOT
		D17=XNDDT/XNODP-XNDTN*XNDTN
		TSDDTS=2.*TSDTTS*(TSDTTS-D20)+AODP*TSI*(TOTHRD*BETAO2*D17-4.*D20*EO*EDOT+2.*(D25+EO*EDDOT))
		ETDDT =(EDDOT+2.*EDOT*TSDTTS)*TSI*S+TSDDTS*ETA
		D18=TSDDTS-TSDTTS*TSDTTS
		D19=-PSDTPS*PSDTPS/ETA2-ETA*ETDDT*PSIM2-PSDTPS*PSDTPS
		D23=ETDT*ETDT
		D1DDT=D1DT*(D14+D15)+D1*(D18-2.*D19+TOTHRD*D17+2.*(ALPHA2*D25/BETAO2+EO*EDDOT)/BETAO2)
		XNTRDT=XNDT*(2.*TOTHRD*D17+3.*(D25+EO*EDDOT)/ALPHA2-6.*ALDTAL*ALDTAL +4.*D18-7.*D19 ) +
     &		    C1DTC1*XNDDT+C1*(C1DTC1*D16+D9*ETDDT+D10*EDDOT+D23*(6.+30.*EETA+68.*EOSQ)+
     &		    ETDT*EDOT*(40.+30.*ETA2+272.*EETA)+D25*(17.+68.*ETA2) +
     &		    B1*(D1DDT*D2+2.*D1DT*D2DT+D1*(ETDDT*D11+D23*(72.+54.*ETA2))) +
     &		    B2*(D1DDT*D3+2.*D1DT*D3DT+D1*(ETDDT*D12+D23*(30.+30.*ETA2))) * COS2G+
     &		    B3*((D5DT*D14+D5*(D18-2.*D19)) *D4+2.*D4DT*D5DT+D5*(ETDDT*D13+22.5*ETA*D23)) *SING+XGDT1*
     &		    ((7.*D20+4.*EO*EDOT/BETAO2)*(C5*COSG-2.*C4*SIN2G)+((2.*C5DT*COSG-4.*C4DT*SIN2G)-
     &		    XGDT1*(C5*SING+4.*C4*COS2G))))
		TMNDDT=XNDDT*1.E9
		TEMP=TMNDDT*TMNDDT-XNDT*1.E18*XNTRDT
		PP=(TEMP+TMNDDT*TMNDDT)/TEMP
		GAMMA=-XNTRDT/(XNDDT*(PP-2.))
		XND=XNDT/(PP*GAMMA)
		QQ=1.-EDDOT/(EDOT*GAMMA)
		ED=EDOT/(QQ*GAMMA)
		OVGPP=1./(GAMMA*(PP+1.))

	    ELSE

		ISIMP=1
		EDOT=-TOTHRD*XNDTN*(1.-EO)

	    END IF

	    IFLAG=0

	END IF

	! Update for secular gravity and atmospheric drag

	XMAM=mod(mod(XMO+XLLDOT*TSINCE,TWOPI)+TWOPI,TWOPI)
 	OMGASM=OMEGAO+OMGDT*TSINCE
	XNODES=XNODEO+XNODOT*TSINCE
	IF (ISIMP .NE. 1) THEN
	    TEMP=1.-GAMMA*TSINCE
	    TEMP1=TEMP**PP
	    XN=XNODP+XND*(1.-TEMP1)
	    EM=EO+ED*(1.-TEMP**QQ)
	    Z1=XND*(TSINCE+OVGPP*(TEMP*TEMP1-1.))
	ELSE
	    XN=XNODP+XNDT*TSINCE
 	    EM=EO+EDOT*TSINCE
	    Z1=.5*XNDT*TSINCE*TSINCE
	END IF
	Z7=3.5*TOTHRD*Z1/XNODP
 	XMAM=mod(mod(XMAM+Z1+Z7*XMDT1,TWOPI)+TWOPI,TWOPI)
	OMGASM=OMGASM+Z7*XGDT1
	XNODES=XNODES+Z7*XHDT1

	! Solve keplers equation

	ZC2=XMAM+EM*SIN(XMAM)*(1.+EM*COS(XMAM))
	DO I=1,10
	    SINE=SIN(ZC2)
	    COSE=COS(ZC2)
	    ZC5=1./(1.-EM*COSE)
	    CAPE=(XMAM+EM*SINE-ZC2)*ZC5+ZC2
	    IF(ABS(CAPE-ZC2) .LE. E6A) GO TO 140
	    ZC2=CAPE
	END DO

	! Short period preliminary quantities

 140	AM=(XKE/XN)**TOTHRD
 	BETA2M=1.-EM*EM
	SINOS=SIN(OMGASM)
	COSOS=COS(OMGASM)
	AXNM=EM*COSOS
	AYNM=EM*SINOS
	PM=AM*BETA2M
	G1=1./PM
	G2=.5*CK2*G1
	G3=G2*G1
	BETA=SQRT(BETA2M)
	G4=.25*A3COF*SINI
	G5=.25*A3COF*G1
	SNF=BETA*SINE*ZC5
	CSF=(COSE-EM)*ZC5
	FM=mod(atan2(SNF,CSF)+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