      SUBROUTINE TGLOC05B(THISNAME,TSEC50,SCPOS,SCVEL,
     *            TIMTARG,TARG,LUERR,IERR)
      IMPLICIT REAL*8 (A-H,O-Z)
C
C THIS ROUTINE IS PART OF THE TOSS TARGET LOCATION PACKAGE, TGLOC.
C
C THIS IS A SEVICE ROUTINE FOR THE TARGET LOCATION PACKAGE.  IT FINDS
C THE NOON/MIDNIGHT ZENITH DIRECTION VECTOR FOR THE ALGORITHM WHERE
C NOON IS DEFINED AS OCCURRING WHEN THE S/C HAS THE SAME RIGHT ASCENSION
C AS THE SUN AND MIDNIGHT IS WHEN THE S/C HAS THE SAME RIGHT ASCENSION
C AS THE ANTI-SUN.
C
C VAR     DIM    TYPE I/O DESCRIPTION
C ---     ---    ---- --- -----------
C
C THISNAME 1     CH*16 I  NAME OF THE ZENITH TARGET.  THE ONLY VALID
C                         NAMES ARE 'NOONZENITH2' AND 'MIDZENITH2'.
C
C TSEC50   1      R*8  I  THE TIME ASSOCIATED WITH SCPOS AND SCVEL. TIME
C                         IS NEEDED BECAUSE THE SUN POSITION IS
C                         INVOLVED.  IN SECONDS SINCE 1/1/50, 0.0 HR
C
C SCPOS    3      R*8  I  THE SPACECRAFT POSITION VECTOR AT TIME TSEC50.
C                         THE S/C STATE IS NEEDED BECAUSE ZENITH DEPENDS
C                         ON THE TRAJECTORY.  MEAN OF 1950.0 IS ASSUMED.
C                         IN KM.
C
C SCVEL    3      R*8  I  THE SPACECRAFT VELOCITY VECTOR AT TIME TSEC50.
C                         MEAN OF 1950.0 IS ASSUMED.  IN KM/SEC.
C
C TIMTARG  1      R*8  O  THE TIME WHEN THE S/C ARRIVES AT THE NOON OR
C                         MIDNIGHT POSITION.  IN SECONDS SINCE
C                         1/1/50, 0.0 HRS
C
C TARG     3      R*8  O  TARG IS THE CARTESIAN UNIT VECTOR OF THE
C                         NOON/MIDNIGHT ZENITH TARGET. IN MEAN OF 1950.0
C
C LUERR    1      I*4  I  FORTRAN UNIT NUMBER FOR ERROR MESSAGES.
C                          = 0/NEGATIVE, NO MESSAGES POSSIBLE.
C
C IERR     1      I*4  O  ERROR RETURN FLAG.
C                          = 0, NO ERROR.
C                          = OTHERWISE, ERROR.
C
C***********************************************************************
C
C BY C PETRUZZO, GSFC/742, 8/85.
C        MODIFIED....
C
C***********************************************************************
C
      REAL*8 SUNPOS(3),STATE1(6),PLANE2(2),ELEMS(6),
     *       ORBNORM(3),MERIDNORM(3),SCPOS(3),SCVEL(3),TARG(3)
      REAL*8 HALFPI/ 1.570796326794897D0 /
      REAL*8 PI/ 3.141592653589793D0 /
      REAL*8 TWOPI/ 6.283185307179586D0 /
      REAL*8 DEGRAD/ 57.29577951308232D0 /
      LOGICAL WRONGSIDE,CONVERGED
      CHARACTER*16 THISNAME,NOONZ/'NOONZENITH2'/,MIDZ/'MIDZENITH2'/
C
      IBUG = 0
      LUBUG = 19
C
C SET INDICATOR TELLING THE ROUTINE WHICH ZENITH TARGET THIS IS
C
      KEY = 0
      IF(THISNAME.EQ.NOONZ) KEY = +1
      IF(THISNAME.EQ.MIDZ)  KEY = -1
C
C    ERROR CHECK. IF THIS OCCURS, ROUTINE SHOULD NOT HAVE BEEN CALLED.
      IF(KEY.EQ.0) THEN
        TYPE *,'TGLOC05B. CODING ERROR 1. RUN STOPPED.'
        STOP   'TGLOC05B. CODING ERROR 1. RUN STOPPED.'
        END IF
C
C INITIALIZE MISC PARAMS
C
      IERR = 0
      ERTHMU = CONST(56)
      ANGTEST = 0.001D0/DEGRAD
C
C METHOD: THE S/C CROSSES THE MERIDIAN CONTAINING THE SUN AT TWO PLACES,
C         ONE ON THE DAY SIDE AND ONE ON THE NIGHT SIDE. WE PROPAGATE
C         THE STATE TO THE NEAREST(IN TIME) CROSSING, SEE WHICH ONE IT
C         IS, AND PROPAGATE TO THE PROPER ONE IF NECESSARY. WE ITERATE
C         BECAUSE THE SUN IS MOVING.
C
C LOAD INPUT CONDS INTO ITERATION LOOP ARRAY FOR FIRST ITERATION
C
      TSTATE1 = TSEC50
      STATE1(1) = SCPOS(1)
      STATE1(2) = SCPOS(2)
      STATE1(3) = SCPOS(3)
      STATE1(4) = SCVEL(1)
      STATE1(5) = SCVEL(2)
      STATE1(6) = SCVEL(3)
C
C SET THE ORBITAL PERIOD.
C
      PD = PERIOD(ERTHMU,STATE1(1),STATE1(4))
C
C COMPUTE ORBIT NORMAL VECTOR.
C
      CALL UCROSS(STATE1(1),STATE1(4),ORBNORM)
      IF(IBUG.NE.0) THEN
        CALL XYZSPH(-1,ONRA,ONDEC,DUM,ORBNORM,1.D0)
        WRITE(LUBUG,5009) ONRA*DEGRAD,ONDEC*DEGRAD
 5009   FORMAT(' TGLOC05B DEBUG. ORBNORM  RA,DEC=',2G18.10)
        END IF
C
C
C SET PARAMETERS FOR ITERATION LOOPS.
C
      MAXITER = 10    ! MAX LOOPS TO ITERATE
      TIMEPS = 0.1D0  ! FOR CONVERGENCE TEST
      TLAST = -1.D10  ! FOR CONVERGENCE TEST
      CONVERGED = .FALSE.
      ITER = 0
C
C
      DO WHILE ( .NOT.CONVERGED .AND. ITER.LT.MAXITER )
        ITER = ITER + 1
        IF(IBUG.NE.0) WRITE(LUBUG,5002) ITER,PAKTIM50(TSTATE1)
 5002   FORMAT(/,' TGLOC05B DEBUG. START ITER=',I2,'  TIME=',F15.8)
C
C
C      FIND MERIDIAN PLANE CONTAINING THE SUN. SET ITS NORMAL VECTOR.
C
        CALL SOLM50(TSTATE1,SUNPOS,0)
        CALL XYZSPH(-1,SUNRA,SUNDEC,DUM,SUNPOS,1.D0)
        PLANE2(1) = HALFPI
        PLANE2(2) = SUNRA
        CALL XYZSPH(1,SUNRA+HALFPI,0.D0,1.D0,MERIDNORM,1.D0)
        IF(IBUG.NE.0) WRITE(LUBUG,5010) SUNRA*DEGRAD,SUNDEC*DEGRAD
 5010   FORMAT(' TGLOC05B DEBUG. SUN RA,DEC=',2G18.10)
C
C
C      FIND THE TIME UNTIL THE NEAREST CROSSING OF THE SUN MERIDIAN
C      PLANE BY THE S/C.
C
        CALL XPLANE(ERTHMU,STATE1,-1,PLANE2,0,KCROSS,DELTIM,LUERR,IERR1)
        IF(IBUG.NE.0) WRITE(LUBUG,5004)
     *     PLANE2(1)*DEGRAD,PLANE2(2)*DEGRAD,ICROSS,KCROSS,DELTIM,IERR1
 5004   FORMAT(' TGLOC05B DEBUG. '/,
     *         '    MERIDIAN PLANE INCL=',G12.5/,
     *         '    MERIDIAN PLANE NODE=',G12.5/,
     *         '    ICROSS, KCROSS =',2I5/,
     *         '    DELTIM=',G12.5,'  IERR1=',I3)
        IF(IERR1.NE.0) THEN
          IF(LUERR.GT.0) WRITE(LUERR,3001)
 3001     FORMAT(/,' TGLOC05B. ERROR CONDITION. ERROR RETURN FROM ',
     *      'XPLANE.'/,'    BAD NOON OR MIDNIGHT ZENITH LOCATION.')
          IERR = 1
          GO TO 9999
          END IF
C
C
C      PROPAGATE STATE1 TO THE NEAREST CROSSING.
C
        CALL TUBODY(ERTHMU,STATE1(1),STATE1(4),DELTIM,
     *                     STATE1(1),STATE1(4),LUERR,IERR1)
        TSTATE1 = TSTATE1 + DELTIM
C
C      FOR MIDNIGHT ZENITH, WE WANT THE S/C POSITION AT THE MERIDIAN
C      CROSSING ON THE BACK SIDE OF THE EARTH. FOR NOON ZENITH, WE WANT
C      THE FRONT SIDE. TEST THE LOCATION WE HAVE FOUND AND PROPAGATE IT
C      1/2 ORBIT IF NECESSARY.
C
        IF(IBUG.NE.0) WRITE(LUBUG,5005) ANGVEC(SUNPOS,STATE1)*DEGRAD
 5005   FORMAT(' TGLOC05B DEBUG. SUN/SC SEPARATION=',G12.5)
        TEST = DOT(SUNPOS,STATE1)
        WRONGSIDE = ( TEST.GT.0.D0 .AND. KEY.EQ.-1)     .OR.
     *              ( TEST.LT.0.D0 .AND. KEY.EQ.+1)
        IF(WRONGSIDE) THEN
          TPROP = PD/2.D0
          IF(DELTIM.GT.0.D0) TPROP = -TPROP  ! KEEP CLOSE TO EPOCH TIME
          CALL TUBODY(ERTHMU,STATE1(1),STATE1(4),TPROP,
     *                       STATE1(1),STATE1(4),LUERR,IERR1)
          TSTATE1 = TSTATE1 + TPROP
          IF(IBUG.NE.0) WRITE(LUBUG,5011) TPROP,PAKTIM50(TSTATE1)
 5011     FORMAT(' TGLOC05B DEBUG. FOUND CROSSING ON FRONT.'/,
     *           '    PROPAGATE ',G12.5,' SECONDS TO TIME ',G15.8)
          END IF
C
C      TEST FOR 'CLOSE ENOUGH' TIME. IF SO, EXIT THE ITERATION LOOP.
C
        IF(IBUG.NE.0) WRITE(LUBUG,5006) TSTATE1-TLAST
 5006   FORMAT(' TGLOC05B CONVERGENCE TEST. TSTATE1-TLAST=',G14.7)
        CONVERGED = DABS(TSTATE1-TLAST) .LT. TIMEPS
        TLAST = TSTATE1
C
        END DO
C
C
      IF(CONVERGED) THEN
        TIMTARG = TSTATE1
        TARG(1) = STATE1(1)
        TARG(2) = STATE1(2)
        TARG(3) = STATE1(3)
      ELSE
        IERR = 1
        IF(LUERR.GT.0) WRITE(LUERR,9001)
 9001   FORMAT(/,
     *   ' TGLOC05B. MAX ITERATIONS EXCEEDED. BAD NOON OR MIDNIGHT ',
     *       ' ZENITH LOCATION COMPUTED.')
        END IF
C
C
 9999 CONTINUE
      RETURN
      END
