      SUBROUTINE TGLOC00(NTARGS,KTYPE,TARGNAME,NPARMS,PARMS,
     *     KSYS,KORIGIN,TARGVEC,
     *     KTIME,TSEC50, KPOS,SCPOS, KVEL,SCVEL, KATT,ATT, KTOP,TOPCEN,
     *     LUERR,IERR)
      IMPLICIT REAL*8 (A-H,O-Z)
C
C THIS ROUTINE IS PART OF THE TOSS TARGET LOCATION PACKAGE, TGLOC.
C IT CALLS APPROPRIATE ROUTINES TO COMPUTE TARGET LOCATIONS IN THE
C 'NATURAL' SYSTEM AND, IF NECESSARY, CALLS A TRANSFORMATION PACKAGE
C TO PUT THEM IN THE DESIRED OUTPUT SYSTEM.  ON ANY ONE CALL TO TGLOC00,
C TARGETS ARE ALL OF THE SAME TYPE.
C
C VAR     DIM    TYPE   I/O  DESCRIPTION
C ---     ---    ----   ---  -----------
C
C -->> ARGUMENTS HAVE THE SAME DESCRIPTION AS THOSE IN TGLOC.  NOTE,
C      HOWEVER THAT TGLOC CALLS TGLOC00 WITH INDEXED ARRAYS SO THAT,
C      FOR EXAMPLE, TARGNAME(1) IN THIS ROUTINE MAY CORRESPOND TO
C      TARGNAME(23) IN TGLOC, THE ROUTINE THAT CALLS THIS ONE.
C
C***********************************************************************
C
C BY C PETRUZZO, 8/85.
C     MODIFIED....
C
C***********************************************************************
C
C
C CALLING SEQUENCE ARRAYS:
C
      REAL*8 PARMS(NPARMS,1)              ! ACTUALLY, (NPARMS,NTARGS)
      REAL*8 TARGVEC(3,1)                 ! ACTUALLY, (3,NTARGS)
      CHARACTER*16 TARGNAME(1)            ! ACTUALLY, (NTARGS)
      INTEGER KTYPE(1)                    ! ACTUALLY, (NTARGS)
      REAL*8 SCPOS(3),SCVEL(3),ATT(4),TOPCEN(4)
C
C INTERNAL VARIABLES:
      REAL*8 IDENT(3,3)/1.D0,3*0.D0,1.D0,3*0.D0,1.D0/
      REAL*8 TEMP6(6),DUMTOP(5)/5*9999.D0/,TOPTEMP(5)
      INTEGER KSPEC1(3)/999,999,1/,KSPEC2(3)/999,999,1/
      INTEGER KOUNTERR/0/
      LOGICAL NEEDPACK
C
      IERR = 0
      IF(NTARGS.LE.0) GO TO 9999
C
      IBUG = 0
      LUBUG = 19
C
      IF(IBUG.NE.0) THEN
        WRITE(LUBUG,8502) NTARGS,KSYS,KORIGIN,NPARMS
 8502   FORMAT(' TGLOC00 DEBUG 8502. NTARGS=',I4,
     *            '   KSYS,KORIGIN=',2I3,'  NPARMS=',I3)
        NP = MIN(NPARMS,5)
        IF(NTARGS.GT.0) WRITE(LUBUG,8501)
     *      (I,KTYPE(I),TARGNAME(I),(PARMS(J,I),J=1,NP),I=1,NTARGS)
 8501   FORMAT(<NTARGS>('    ITARG=',I3,'  KTYPE=',I3,'  TARGNAME=',A/,
     *                  '       PARMS(1-..)=',<NP>G13.5/))
        END IF
C
C CHECK FOR ERRONEOUS CALL. ALL KTYPE(I)'S SHOULD BE THE SAME. IF NOT
C AN ERROR HAS CREPT IN.
C
      KTEST = KTYPE(1)
      DO I=1,NTARGS
        IF(KTYPE(I).NE.KTEST) THEN
          TYPE *,' TGLOC00. ERROR END. PROGRAMMER ERROR. SEE SOURCE.'
          STOP   ' TGLOC00. ERROR END. PROGRAMMER ERROR. SEE SOURCE.'
          END IF
        END DO
C
C
C ***************************************
C *  ERROR CHECKS FOR THIS TARGET TYPE  *
C ***************************************
C
      CALL TGLOC00A(KTYPE(1),NPARMS, KTIME,KPOS,KVEL,KATT,KTOP,ATT(1),
     *        KSYS,KORIGIN, LUERR,IERR1)
      IF(IERR1.NE.0) THEN
        IERR = 1
        DO I=1,NTARGS
          TARGVEC(1,I) = 'ERROR'
          TARGVEC(2,I) = 'ERROR'
          TARGVEC(3,I) = 'ERROR'
          END DO
        GO TO 9999
        END IF
C
C
C  *********************************************
C  *  GET TARGET LOCATION IN 'NATURAL' SYSTEM  *
C  *********************************************
C
      GO TO (100,200,300,400,500,600,700,800),KTYPE(1)
C
C---> MOVING CELESTIAL TARGET.
  100 CONTINUE
      CALL TGLOC01(NTARGS,TARGNAME,TSEC50,KSYSNAT,KCENNAT,TARGVEC,
     *       LUERR,IERR1)
      GO TO 5000
C
C---> EARTH-FIXED TARGET.
  200 CONTINUE
      CALL TGLOC02(NTARGS,TARGNAME,NPARMS,PARMS,KSYSNAT,KCENNAT,TARGVEC,
     *        LUERR,IERR1)
      GO TO 5000
C
C---> FIXED CELESTIAL TARGET.
  300 CONTINUE
      CALL TGLOC03(NTARGS,TARGNAME,NPARMS,PARMS,KSYSNAT,KCENNAT,TARGVEC,
     *        LUERR,IERR1)
      GO TO 5000
C
C---> FIXED TO LOCAL ORBITAL COORDINATES.
  400 CONTINUE
      CALL TGLOC04(NTARGS,TARGNAME,NPARMS,PARMS,KSYSNAT,KCENNAT,TARGVEC,
     *        LUERR,IERR1)
      GO TO 5000
C
C---> NOON OR MIDNIGHT ZENITH.
  500 CONTINUE
      CALL TGLOC05(NTARGS,KSYSNAT,KCENNAT,TARGNAME,
     *       TSEC50,SCPOS,SCVEL,TARGVEC,LUERR,IERR1)
      GO TO 5000
C
C---> HORIZON.
  600 CONTINUE
      CALL TGLOC06(NTARGS,TARGNAME,NPARMS,PARMS,SCPOS,
     *       KSYSNAT,KCENNAT,TARGVEC,LUERR,IERR1)
      GO TO 5000
C
C---> A SATELLITE.
  700 CONTINUE
      CALL TGLOC07(NTARGS,TARGNAME,NPARMS,PARMS,TSEC50,
     *         KSYSNAT,KCENNAT,TARGVEC,LUERR,IERR1)
      GO TO 5000
C
C---> NON-SPECIFIC.
  800 CONTINUE
      CALL TGLOC08(NTARGS,KSYSNAT,KCENNAT,TARGVEC)
      GO TO 5000
C
C
 5000 CONTINUE
C
      IF(IERR1.NE.0) THEN
        IERR=1
        IF(LUERR.GT.0) WRITE(LUERR,5001) KTYPE(1)
 5001   FORMAT(/,' TGLOC00. ERROR CONDITION. TARGET TYPE=',I4)
        GO TO 9999
        END IF
C
C
C  ******************************************
C  *  GET TARGET LOCATION IN OUTPUT SYSTEM  *
C  ******************************************
C
      IF(KSYSNAT.EQ.9999) KSYSNAT = KSYS    ! NATRL SYSTEM UNIMPORTANT
      IF(KCENNAT.EQ.9999) KCENNAT = KORIGIN ! NATRL ORIGIN UNIMPORTANT
C
C SET FLAG INDICATING NEED TO CALL CTPAK.
      NEEDPACK = KSYSNAT.NE.KSYS .OR. KCENNAT.NE.KORIGIN
C
      IF(IBUG.NE.0)  WRITE(LUBUG,8507)
     *   KTYPE(1),KSYSNAT,KCENNAT,KSYS,KORIGIN,NEEDPACK,
     *      (TARGNAME(J),(TARGVEC(I,J),I=1,3),J=1,NTARGS)
 8507 FORMAT(' TGLOC DEBUG 8507. KTYPE=',I3,
     *               ' NATL KSYSNAT,KCENNAT=',2I3/,
     *       '    CALLER KSYS,KORIGIN=',2I3,'   NEEDPACK=',L2/,
     *       '    NATURAL TARGVEC='/,(7X,A,3G16.8) )
C
C
      IF(NEEDPACK) THEN
        KSPEC1(1)=KSYSNAT
        KSPEC1(2)=KCENNAT
        KSPEC2(1)=KSYS
        KSPEC2(2)=KORIGIN
        IF(KTOP.NE.0) THEN
          TOPTEMP(1) = TOPCEN(1)
          TOPTEMP(2) = TOPCEN(2)
          TOPTEMP(3) = TOPCEN(3)
          TOPTEMP(4) = TOPCEN(4)
          IF(TOPTEMP(4).NE.0.D0) TOPTEMP(4) = CONST(59)
          TOPTEMP(5) = CONST(53)
          END IF
        CALL CTPAK(NTARGS,
     *    TARGVEC, KSPEC1,
     *     KTIME,TSEC50, KATT,ATT, KPOS,SCPOS, KVEL,SCVEL, KTOP,TOPTEMP,
     *    TARGVEC, KSPEC2,
     *     KTIME,TSEC50, KATT,ATT, KPOS,SCPOS, KVEL,SCVEL, KTOP,TOPTEMP,
     *    0,DUM,DUM, LUERR,IERR1)
        IF(IERR1.NE.0) THEN
          IERR=1
          DO I=1,NTARGS
            TARGVEC(1,I) = 'ERROR'
            TARGVEC(2,I) = 'ERROR'
            TARGVEC(3,I) = 'ERROR'
            END DO
          IF(LUERR.GT.0) WRITE(LUERR,9015)
 9015     FORMAT(' TGLOC00. ERROR RETURN FROM CTPACK.')
          END IF
        END IF
C
 9999 CONTINUE
C
      RETURN
      END
