      SUBROUTINE TRACE                                                  TRAC001 
	implicit real*8 (a-h,o-z)
	character*8 id,ndate,modx,mody,modz,modrin,koll,modsav
C          CALCULATES THE RAY PATH                                      TRAC002 
      DIMENSION ROLD(20),DROLD(20)                                      TRAC003 
      COMMON /RK/ N,STEP,MODE,E1MAX,E1MIN,E2MAX,E2MIN,FACT,RSTART       TRAC004 
      COMMON /FLG/ NTYP,NEWWR,NEWWP,PENET,LINES,IHOP,HPUNCH             TRAC005 
      COMMON /TRAC/ GROUND,PERIGE,THERE,MINDIS,NEWRAY,SMT               TRAC006 
      COMMON /RIN/ MODRIN(3),COLL,FIELD,SPACE,N2,PNP(10),POLAR,LPOLAR   TRAC007 
      COMMON /XX/ MODX(2),X,PXPR,PXPTH,PXPPH,PXPT,HMAX                  TRAC008 
      COMMON R(20),T,STP,DRDT(20) /WW/ ID(10),W0,W(400)                 TRAC009 
	common / leo / apogee, range
      LOGICAL SPACE,HOME,WASNT,UNDRGD,GROUND,PERIGE,THERE,MINDIS,NEWWR, TRAC010 
     1        NEWWP,PENET,NEWRAY,WAS                                    TRAC011 
      REAL*8 MAXSTP                                                     TRAC012 
      COMPLEX*16 N2,PNP,POLAR,LPOLAR                                    TRAC013 
      EQUIVALENCE (EARTHR,W(2)),(RCVRH,W(20)),(HOP,W(22)),(MAXSTP,W(23))TRAC014 
     1,(SKIP,W(71)),(RAYSET,W(72)),(PLT,W(81))                          TRAC015 
      NHOP=HOP + 0.5d0                                                  TRAC016 
      MAX=MAXSTP                                                        TRAC017 
      NSKIP=SKIP                                                        TRAC018 
      RSTART=1.d0                                                       TRAC019 
      CALL HAMLTN                                                       TRAC020 
      HOME=DRDT(1)*(R(1)-EARTHR-RCVRH).GE.0.d0                          TRAC021 
C********* IHOP=0 TELLS PRINTR TO PRINT HEADING AND PUNCH A TRANSMITTER TRAC022 
C********* RAYSET AND TELLS RAYPLT TO START A NEW RAY                   TRAC023 
      IHOP=0                                                            TRAC024 
      CALL PRINTR (8HXMTR    ,0.d0)                                     TRAC025 
      IF (PLT.NE.0.d0) CALL RAYPLT                                      TRAC026 
      HTMAX=0.d0                                                        TRAC027 
      NEWRAY=.TRUE.                                                     TRAC028 
      THERE=R(1)-EARTHR.EQ.RCVRH                                        TRAC029 
C********* LOOP ON NUMBER OF HOPS                                       TRAC030 
   10 IHOP=IHOP+1                                                       TRAC031 
      IF (IHOP.GT.NHOP) RETURN                                          TRAC032 
      PENET=.FALSE.                                                     TRAC033 
      APHT=RCVRH                                                        TRAC034 
C********* LOOP ON MAXIMUM NUMBER OF STEPS PER HOP                      TRAC035 
      DO 79 J=1,MAX                                                     TRAC036 
      H=R(1)-EARTHR                                                     TRAC037 
      IF (dABS(H-RCVRH).GT.dABS(APHT-RCVRH)) APHT=H                     TRAC038 
      HTMAX=dMAX1(H,HTMAX)                                              TRAC039 
      IF (.NOT.SPACE) GO TO 12                                          TRAC040 
      CALL REACH                                                        TRAC041 
      RSTART=1.d0                                                       TRAC042 
      H=R(1)-EARTHR                                                     TRAC043 
      IF (dABS(H-RCVRH).GT.dABS(APHT-RCVRH)) APHT=H                     TRAC044 
      HTMAX=dMAX1(H,HTMAX)                                              TRAC045 
      IF (.NOT.SPACE) GO TO 12                                          TRAC046 
      IF (PERIGE) CALL PRINTR (8HPERIGEE ,0.)                           TRAC047 
      IF (THERE) GO TO 51                                               TRAC048 
      IF (MINDIS) GO TO 40                                              TRAC049 
      IF (GROUND) GO TO 60                                              TRAC050 
      IF (PLT.NE.0.) CALL RAYPLT                                        TRAC051 
      IF (PERIGE) GO TO 79                                              TRAC052 
   12 DO 13 L=1,N                                                       TRAC053 
      ROLD(L)=R(L)                                                      TRAC054 
   13 DROLD(L)=DRDT(L)                                                  TRAC055 
      TOLD=T                                                            TRAC056 
      WAS=THERE                                                         TRAC057 
      CALL RKAM                                                         TRAC058 
      H=R(1)-EARTHR                                                     TRAC059 
      THERE=.FALSE.                                                     TRAC060 
      WASNT=.NOT.HOME                                                   TRAC061 
      HOME=DRDT(1)*(H-RCVRH).GE.0.d0                                    TRAC062 
      TMP=(DRDT(1)-DROLD(1))*(T-TOLD)                                   TRAC063 
      SMT=0.d0                                                          TRAC064 
      IF (TMP.NE.0.d0) SMT=0.5d0*(R(1)-ROLD(1)+0.5d0*TMP)**2/dABS(TMP)  TRAC065 
      IF (((H-RCVRH)*(ROLD(1)-EARTHR-RCVRH).LT.0.d0.AND..NOT.WAS).OR.   TRAC066 
     1 (WAS.AND.DRDT(1)*DROLD(1).LT.0.d0.AND.HOME)) GO TO 50            TRAC067 
      IF (HOME.AND.WASNT) GO TO 30                                      TRAC068 
      IF (H.LT.0.d0.OR.DRDT(1).GT.0.d0.AND.DROLD(1).LT.0.d0.AND.
     a SMT.GT.H)                                                        TRAC069 
     1 GO TO 20                                                         TRAC070 
      IF (DROLD(1).LT.0.d0.AND.DRDT(1).GT.0.d0) CALL PRINTR
     a (8HPERIGEE ,0.d0)                                                TRAC071 
      IF (DROLD(1).GT.0.d0.AND.DRDT(1).LT.0.d0) CALL PRINTR
     a (8HAPOGEE  ,0.d0)                                                TRAC072 
	if(drold(1).gt.0.d0.and.drdt(1).lt.0.d0) apogee = r(1) - w(2)
      IF (DROLD(2)*DRDT(2).LT.0.d0) CALL PRINTR(8HMAX LAT ,0.d0)        TRAC073 
      IF (DROLD(3)*DRDT(3).LT.0.d0) CALL PRINTR(8HMAX LONG,0.d0)        TRAC074 
      DO 14 I=4,6                                                       TRAC075 
      IF (ROLD(I)*R(I).LT.0.d0) CALL PRINTR(8HWAVE REV,0.d0)            TRAC076 
   14 CONTINUE                                                          TRAC077 
      GO TO 75                                                          TRAC078 
C********* RAY WENT UNDERGROUND                                         TRAC079 
   20 CALL BACK UP(0.d0)                                                TRAC080 
      GO TO 60                                                          TRAC081 
C********* RAY MAY HAVE MADE A CLOSEST APPROACH                         TRAC082 
   30 CALL GRAZE(RCVRH)                                                 TRAC083 
      IF (THERE) GO TO 51                                               TRAC084 
   40 DRDT(1)=0.d0                                                      TRAC085 
      HPUNCH=R(1)-EARTHR                                                TRAC086 
      CALL PRINTR(8HMIN DIST,RAYSET)                                    TRAC087 
      IF (PLT.NE.0.d0)CALL RAYPLT                                       TRAC088 
      IF (IHOP.GE.NHOP) RETURN                                          TRAC089 
      IHOP=IHOP+1                                                       TRAC090 
      CALL PRINTR (8HMIN DIST,RAYSET)                                   TRAC091 
      GO TO 89                                                          TRAC092 
C********* RAY CROSSED RECEIVER HEIGHT                                  TRAC093 
   50 CALL BACK UP(RCVRH)                                               TRAC094 
      THERE=.TRUE.                                                      TRAC095 
   51 R(1)=EARTHR+RCVRH                                                 TRAC096 
      HTMAX=dMAX1(RCVRH,HTMAX)                                          TRAC097 
      HPUNCH=APHT                                                       TRAC098 
      CALL PRINTR(8HRCVR    ,RAYSET)                                    TRAC099 
      IF (PLT.NE.0.d0) CALL RAYPLT                                      TRAC100 
      IF (RCVRH.NE.0.d0) GO TO 89                                       TRAC101 
      IF (IHOP.GE.NHOP) RETURN                                          TRAC102 
      IHOP=IHOP+1                                                       TRAC103 
      APHT=RCVRH                                                        TRAC104 
C********* GROUND REFLECT                                               TRAC105 
   60 R(1)=EARTHR                                                       TRAC106 
      IF (dABS(RCVRH).GT.dABS(APHT-RCVRH)) APHT=0.d0                    TRAC107 
      R(4)=dABS (R(4))                                                  TRAC108 
      DRDT(1)=dABS (DRDT(1))                                            TRAC109 
      RSTART=1.d0                                                       TRAC110 
      HPUNCH=HTMAX                                                      TRAC111 
      CALL PRINTR(8HGRND REF,RAYSET)                                    TRAC112 
      HTMAX=0.d0                                                        TRAC113 
      IF (RCVRH.NE.0.d0) GO TO 65                                       TRAC114 
      THERE=.TRUE.                                                      TRAC115 
      HPUNCH=APHT                                                       TRAC116 
      CALL PRINTR (8HRCVR    ,RAYSET)                                   TRAC117 
      GO TO 89                                                          TRAC118 
   65 H=0.d0                                                            TRAC119 
      THERE=.FALSE.                                                     TRAC120 
C*********                                                              TRAC121 
   75 IF (PLT.NE.0.d0) CALL RAYPLT                                      TRAC122 
      IF (H.GT.HMAX.AND.H.GT.RCVRH.AND.DRDT(1).GT.0.d0) GO TO 90        TRAC123 
      IF (MOD(J,NSKIP).EQ.0) CALL PRINTR(8H        ,0.d0)               TRAC124 
   79 CONTINUE                                                          TRAC125 
C********* EXCEEDED MAXIMUM NUMBER OF STEPS                             TRAC126 
      HPUNCH=H                                                          TRAC127 
      CALL PRINTR(8HSTEP MAX,RAYSET)                                    TRAC128 
      RETURN                                                            TRAC129 
C*********                                                              TRAC130 
   89 HOME=.TRUE.                                                       TRAC131 
      GO TO 10                                                          TRAC132 
C********* RAY PENETRATED                                               TRAC133 
   90 PENET=.TRUE.                                                      TRAC134 
      HPUNCH=H                                                          TRAC135 
      CALL PRINTR(8HPENETRAT,RAYSET)                                    TRAC136 
      RETURN                                                            TRAC137 
         END                                                            TRAC138 
