      SUBROUTINE GETSEDR(EDRTIME,TD,TN,TP,ESPV,ERANGE,EANG,
     &                   MAT1,MAT2,MAT3)
C
C EXTRACT POINTING VECTOR AND NAVIGATION WORDS NECESSARY FOR THE COMPUTATION
C OF MATRICES TO ROTATE FIELD DATA FROM PAYLOAD TO IHG AND HG COORDINATES.
C
C ORIGINAL CODE BY SANDY KRAMER, HUGHES STX, CODE 692 NASA GSFC, 10/25/93
C HANDLE ENCOUNTER MODES, 05/01/95 - SBK
C INTERPOLATE POINTING VECTOR AND NAVIGATION DATA, 07/31/95 - SBK
C DETECT HEADER RECORDS IN CONCATENATED NAV FILES, 08/05/96 - SBK
C DETECT HEADER RECORDS IN CONCATENATED PTG FILES, 08/06/96 - SBK
C DETECT BAD NAV REC TIME TAGS, 08/22/96 - SBK
C
      CHARACTER IHDR(45)*4,TYPE*4
      CHARACTER STAR*12,PVRD*77
      INTEGER*2 EDRTIME(6),PTIME(6),NTIME(6),LAUNCH(6)
      INTEGER*4 INAV(126,2)
      REAL*4 RNAV(126,2),NAV(252),PV(3,3),OPV(3,3),
     &       RH(3),VH(3),SPV(6),RANGE,ANG(2),OANG(2),EANG(2),
     &       OSPV(6),ESPV(6),MTB(3,3),MTB5(3,3),MHG(3,3),
     &       OMTB(3,3),OMTB5(3,3),OMHG(3,3),
     &       MAT1(3,3),MAT2(3,3),MAT3(3,3),PMAT(3,3)
      REAL*8 TD,TP,TN,REALTIME,DATATIME,
     &       PVTIME,NAVTIME,OTN,OTP
C
      DATA ICALL/0/,ARAD/1.495985E8/
C
      EQUIVALENCE ( IHDR(1), INAV(1,1) ),
     &            ( NAV(1), RNAV(1,1) )
C
      SAVE PVRD,PVTIME,PV,NAVTIME,INAV,NAV,ICALL
C
      INCLUDE 'UNPACK.INC'
C
C DIAGNOSTIC OUTPUT UNIT
C
      IUNIT = 68
C
C COUNT CALLS FOR SEDR DATA
C
      ICALL = ICALL + 1
C
C LAUNCH TIME FOR CALCULATION OF NAV AND PNTG EPIC TIMES
C
      LAUNCH(1) = 77
      LAUNCH(2) = 232
      LAUNCH(3) = 0
      LAUNCH(4) = 0
      LAUNCH(5) = 0
      LAUNCH(6) = 0
C
C DETERMINE DESIRED COORDINATE PROCESSING
C
      IF ( SYS2(17) ) THEN
       ICOORD = 4  ! JUPITER
      ELSE IF ( SYS2(18) ) THEN
       ICOORD = 3  ! SATURN
      ELSE IF ( SYS2(19) ) THEN
       ICOORD = 5  ! URANUS
      ELSE IF ( SYS2(20) ) THEN
       ICOORD = 6  ! NEPTUNE
      ELSE
       ICOORD = 2  ! HG (DEFAULT)
      END IF
C
C CONVERT CALENDAR DATA TIME TO DECIMAL YEAR.
C
      DATATIME = REALTIME(EDRTIME)
C
C COMPUTE ELAPSED EDR TIME
C
      CALL ELPSTIME(LAUNCH,EDRTIME,TD)
C
      IF ( ICALL.GT.1 .AND. DATATIME.GT.PVTIME ) GOTO 7  ! GET NEW PTG VECTOR
      IF ( ICALL.GT.1 .AND. DATATIME.LE.PVTIME ) GOTO 12 ! CHECK NAV DATA
C
C READ POINTING VECTOR FILE HEADER.  EXPECT $$VGR AT HEADER START.
C
      READ(40,'(A77)') PVRD
    4 CONTINUE
      IF ( PVRD(1:5).NE.'$$VGR' ) THEN 
       WRITE(IUNIT,804)
       STOP
      END IF
      WRITE(IUNIT,*)
      WRITE(IUNIT,*) 'POINTING VECTOR FILE INFO'
      DO WHILE ( PVRD(1:5).NE.'$$EOH' )
       READ(40,'(A77)') PVRD
       IF ( PVRD(1:1).NE.'*' ) WRITE(IUNIT,*) PVRD
      END DO
c      WRITE(IUNIT,*)
c      WRITE(IUNIT,*) 'POINTING VECTOR FILE INFO'
c      DO I = 1,8
c       READ(40,'(A77)') PVRD
c       IF ( I.GT.1 ) WRITE(IUNIT,*) PVRD 
c      END DO
c      READ(40,'(////)')
C
C READ POINTING VECTOR DATA
C
    5 CONTINUE
      READ(40,'(A77)',END=200) PVRD
      IF (PVRD(1:5).EQ.'$$VGR') GOTO 4
c      IF (PVRD(1:1).EQ.'*') THEN
c       WRITE(IUNIT,*) 
c       WRITE(IUNIT,*) 'POINTING VECTOR FILE INFO'
c       WRITE(IUNIT,'(A77)') PVRD 
c       GOTO 5
c      END IF
C
C GET POINTING VECTORS' TIME TAG
C
      CALL EXTRACTDATE(PTIME,PVRD)
      PVTIME = REALTIME(PTIME)
C
C COMPUTE ELAPSED POINTING VECTOR TIME
C
      CALL ELPSTIME(LAUNCH,PTIME,TP)
C
      IF (PVTIME.LT.DATATIME) THEN
C
C LOAD 3X3 SENSOR POINTING MATRIX.  INSTRUMENT AXIS VECTORS ARE COLUMN
C ORIENTED.
C
C X AXIS VECTOR TO COLUMN 1
C
       READ(PVRD,'(34X,2(E13.12,2X),E13.12)') (PV(I,1),I=1,3)
C
C Y AXIS VECTOR TO COLUMN 2
C
       READ(40,'(A77)',END=200) PVRD
       READ(PVRD,'(34X,2(E13.12,2X),E13.12)') (PV(I,2),I=1,3)
C
C Z AXIS VECTOR TO COLUMN 3
C
       READ(40,'(A77)',END=200) PVRD
       READ(PVRD,'(34X,2(E13.12,2X),E13.12)') (PV(I,3),I=1,3)
C
      END IF
    7 CONTINUE
      IF (PVTIME.LT.DATATIME) THEN
C
C SAVE POINTING VECTOR DATA BEFORE GETTING NEXT RECORD
C
       DO J = 1,3
        DO I = 1,3
         OPV(I,J) = PV(I,J)
        END DO
       END DO
       OTP = TP
       GOTO 5
      END IF
C
C GET POINTING VECTOR WITH TIME TAG FOLLOWING DATA TIME TAG
C
C LOAD 3X3 SENSOR POINTING MATRIX.  INSTRUMENT AXIS VECTORS ARE COLUMN
C ORIENTED.
C
C X AXIS VECTOR TO COLUMN 1
C
      READ(PVRD,'(34X,2(E13.12,2X),E13.12)') (PV(I,1),I=1,3)
C
C Y AXIS VECTOR TO COLUMN 2
C
      READ(40,'(A77)',END=200) PVRD
      READ(PVRD,'(34X,2(E13.12,2X),E13.12)') (PV(I,2),I=1,3)
C
C Z AXIS VECTOR TO COLUMN 3
C
      READ(40,'(A77)',END=200) PVRD
      READ(PVRD,'(34X,2(E13.12,2X),E13.12)') (PV(I,3),I=1,3)
C
C INTERPOLATE POINTING VECTOR DATA
C
      CALL SEDRIP(OTP,OPV,TP,PV,TD,PMAT,9,0)
C
      IF (ICALL.GT.1) GOTO 12
C
C GET NAVIGATION FILE HEADER RECORD.  CONTAINS CHARACTER AND INTEGER DATA.
C
      READ(41) IHDR
      IF ( IHDR(2).NE.'SEDR' ) THEN
       WRITE(IUNIT,805)
       STOP
      END IF
    9 CONTINUE
      WRITE(IUNIT,*)
      WRITE(IUNIT,*) 'NAVIGATION FILE INFO'
      WRITE(IUNIT,'(1X,''PROJECT ID:  '',A4)') IHDR(1) 
      WRITE(IUNIT,'(1X,''FILE TYPE:   '',A4)') IHDR(2)
      WRITE(IUNIT,'(1X,''FLIGHT:      '',I2.2)') INAV(3,1)
      WRITE(IUNIT,'(1X,''FILE ID:     '',2A4)') IHDR(4),IHDR(5)
      WRITE(IUNIT,'(1X,''FILE FORMAT: '',2A4)') IHDR(14),IHDR(15)
      TYPE = IHDR(14)
C
C GET NAVIGATION DATA RECORDS.  CONTAINS INTEGER AND FLOATING POINT DATA.
C FLOATING POINT VALUES ARE CONVERTED FROM VAXG TO IEEE REPRESENTATION BY
C DEC FORTRAN CONVERT PARAMETER IN OPEN STATEMENT WHEN SOURCE CODE IS 
C COMPILED USING THE FLOAT=IEEE FLAG.
C
   10 CONTINUE
       READ(41,END=100,ERR=11) (INAV(I,1),I=1,6),(RNAV(I,1),I=7,126),
     &                         (RNAV(I,2),I=1,126)
   11  CONTINUE
C
C GET NAVIGATION RECORD TIME TAG.  USE 2 DIGIT YEAR.
C
       IF ( IHDR(2).EQ.'SEDR' ) GOTO 9  ! DETECT HDR REC IN CONCAT SEDRS
       NTIME(1) = INAV(1,1) - 1900
       IF (NTIME(1).GT.99) NTIME(1) = NTIME(1) - 100
       NTIME(2) = INAV(2,1)
       NTIME(3) = INAV(3,1)
       NTIME(4) = INAV(4,1)
       NTIME(5) = INAV(5,1)
       NTIME(6) = INAV(6,1)
       NAVTIME = REALTIME(NTIME)
C
C COMPUTE ELAPSED NAVIGATION TIME
C
       CALL ELPSTIME(LAUNCH,NTIME,TN)
C
C DETECT BAD NAV REC BASED ON VALID TIME TAG
C
       IF ( TN.EQ.-1.0D0 ) GOTO 10
C
C STORE CONTENTS OF NAVIGATION RECORD WHILE NAVIGATION TIME IS LESS THAN
C EDR TIME
C
       IF (NAVTIME.LT.DATATIME) THEN
C
C CALL SEDR PROCESSING ROUTINE 
C
        IF ( ICOORD.EQ.2 ) THEN
         CALL SEDRCRU(NTIME,NAV,PMAT,SPV,ANG,MTB,MHG,MTB5,RANGE)
        ELSE IF ( ICOORD.GE.3 ) THEN
         CALL SEDRENC(NTIME,NAV,PMAT,SPV,ANG,MTB,MHG,MTB5,RANGE)
        END IF
C
       END IF
   12  CONTINUE
C
C SAVE PROCESSED SEDR VALUES BEFORE GETTING NEW NAV DATA
C
       IF (NAVTIME.LT.DATATIME) THEN
        DO J = 1,3
         DO I = 1,3
          OMTB(I,J) = MTB(I,J)
          OMTB5(I,J) = MTB5(I,J)
          OMHG(I,J) = MHG(I,J)
         END DO
        END DO
        OTN = TN
        OSPV(1) = SPV(1)
        OSPV(2) = SPV(2)
        OSPV(3) = SPV(3)
        OSPV(4) = SPV(4)
        OSPV(5) = SPV(5)
        OSPV(6) = SPV(6)
        ORANGE = RANGE
        OANG(1) = ANG(1)
        OANG(2) = ANG(2)
        GOTO 10  ! CHECK NEXT NAV RECORD
       END IF
C
C CALL SEDR PROCESSING ROUTINE 
C
       IF ( ICOORD.EQ.2 ) THEN
        CALL SEDRCRU(NTIME,NAV,PMAT,SPV,ANG,MTB,MHG,MTB5,RANGE)
       ELSE IF ( ICOORD.GE.3 ) THEN
        CALL SEDRENC(NTIME,NAV,PMAT,SPV,ANG,MTB,MHG,MTB5,RANGE)
       END IF
C
C INTERPOLATE ROTATION MATRICES, ANGLES, POSITION AND VELOCITY
C
       CALL SEDRIP(OTN,OMTB,TN,MTB,TD,MAT1,9,0) ! ROTATION MATRIX 1
       CALL SEDRIP(OTN,OMTB5,TN,MTB5,TD,MAT2,9,0) ! ROTATION MATRIX 2
       CALL SEDRIP(OTN,OMHG,TN,MHG,TD,MAT3,9,0) ! ROTATION MATRIX 3
       CALL SEDRIP(OTN,ORANGE,TN,RANGE,TD,ERANGE,1,0) ! RANGE
       CALL SEDRIP(OTN,OSPV,TN,SPV,TD,ESPV,6,0) ! S/C POSITION/VELOCITY
       CALL SEDRIP(OTN,OANG(1),TN,ANG(1),TD,EANG(1),1,3) ! LONGITUDE
       CALL SEDRIP(OTN,OANG(2),TN,ANG(2),TD,EANG(2),1,4) ! LATITUDE
       GOTO 900
  100 CONTINUE
      WRITE(IUNIT,800)
      WRITE(IUNIT,801) NTIME
      GOTO 900
  200 CONTINUE
      WRITE(IUNIT,802)
      WRITE(IUNIT,803) PTIME
  900 CONTINUE
C
      RETURN
  800 FORMAT(1X,'*GETSEDR*  WARNING!  ',
     &       'END OF NAVIGATION FILE REACHED.')
  801 FORMAT(1X,'*GETSEDR*  LAST NAVIGATION TIME TAG AT',
     &       1X,I2,1X,I3.3,3(1X,I2.2),1X,I3.3) 
  802 FORMAT(1X,'*GETSEDR*  WARNING!  '
     &       'END OF POINTING VECTOR FILE REACHED.')
  803 FORMAT(1X,'*GETSEDR*  LAST POINTING VECTOR TIME TAG AT',
     &       1X,I2,1X,I3.3,3(1X,I2.2),1X,I3.3) 
  804 FORMAT(1X,'*GETSEDR*  ERROR!  ',
     &       'INVALID POINTING VECTOR FILE HEADER.')
  805 FORMAT(1X,'*GETSEDR*  ERROR!  ',
     &       'INVALID NAVIGATION FILE HEADER.')
      END
