      SUBROUTINE VIM5DIFF(TIME)
C
C VIM5 ROUTINE
C
C THIS SUBROUTINE RECONSTRUCTS PRIMARY FULL WORDS FROM PRIMARY REFERENCE 
C AND PRIMARY DIFFERENCE WORDS.  RECONSTRUCTION IS VERIFIED AGAINST PRIMARY 
C REFERENCE WORDS OCCURRING EVERY 25TH COUNT.  AN INCORRECT RECONSTRUCTION 
C RESULTS IN ALL RECONSTRUCTED PRIMARY FULL WORDS FOR A 25 COUNT CYCLE TO BE 
C FLAGGED IN  ERROR AND REPLACED WITH THE FILL VALUE ZERO.
C
C     PREF   PRIMARY REFERENCE FULL WORD        (FILL = 0)
C     PDIFF  PRIMARY DIFFERENCE HALF WORD       (FILL = 255)
C     PREC   PRIMARY RECONSTRUCTED FULL WORD    (FILL = 0)
C     PERR   PRIMARY RECONSTRUCTION ERROR FLAG  (0 = FALSE)
C                                               (1 = TRUE)
C
      INTEGER*2 PSUM(3),PERR(3,16),TIME(6)
      LOGICAL*1 RECTEST
C
      INCLUDE 'UNPACK.INC'
C
C OUTPUT PRIMARY AND SECONDARY REFERENCE WORDS, PRIMARY DIFFERENCE WORDS,
C RECONSTRUCTED PRIMARY FULL WORDS, MODES AND RANGES.  COUNTS ARE NOT 
C ROTATED BY SENSOR, BOOM ALIGNMENT OR SEDR MATRICES.
C
      IF ( SYS2(31) ) RECTEST = .TRUE.
C
C RECONSTRUCT PRIMARY FULL WORDS FROM PRIMARY DIFFERENCES.
C TEST FOR BAD DIFFERENCE WORDS AND NULL DATA.
C
      IP = 1
      PSUM(1) = 0
      PSUM(2) = 0
      PSUM(3) = 0
      PERR(1,16) = 0
      PERR(2,16) = 0
      PERR(3,16) = 0
C
C LOAD FIRST PRIMARY REFERENCE WORDS
C
      DO IAX = 1,3
       IF ( PREF(IAX,1).NE.0 ) THEN
        PREC(IAX,1) = PREF(IAX,1)
        PERR(IAX,1) = 0
       ELSE
        PREC(IAX,1) = 0
        PERR(IAX,1) = 1
       END IF
      END DO
C
C RECONSTRUCT 12 BIT FULL WORDS FROM 6 BIT DIFFERENCE WORDS.  USE
C PRIMARY REFERENCE WORDS TO TEST FOR ERRORS IN RECONSTRUCTION OF
C FULL WORDS FROM DIFFERENCE WORDS.
C
      DO IWRD=1,400
C
C VERIFY RECONSTRUCTED FULL WORDS
C
       IF ( MOD(IWRD-1,25).EQ.0 .AND. IWRD.GT.1 ) THEN
        IP = IP + 1
        DO IAX = 1,3
         PERR(IAX,IP) = 0
         PREC(IAX,IWRD) = 0
         IF (PREF(IAX,IP-1).NE.0.AND.
     &       PDIFF(IAX,IWRD).NE.255.AND.
     &       PERR(IAX,IP-1).NE.1) THEN 
          PSUM(IAX) = PSUM(IAX) + PDIFF(IAX,IWRD)
          PREC(IAX,IWRD) = PREF(IAX,IP-1) + PSUM(IAX)
         ELSE 
          PERR(IAX,IP-1) = 1
         END IF
         IF (PREC(IAX,IWRD).NE.PREF(IAX,IP)) THEN
          PREC(IAX,IWRD) = PREF(IAX,IP)
          PERR(IAX,IP-1) = 1
         END IF
         PSUM(IAX) = 0
        END DO
C
C OPERATE ON PRIMARY DIFFERENCE WORDS
C
       ELSE
        DO IAX = 1,3
         PREC(IAX,IWRD) = 0
         IF (PDIFF(IAX,IWRD).NE.255.AND.
     &       PERR(IAX,IP).NE.1) THEN 
          PSUM(IAX) = PSUM(IAX) + PDIFF(IAX,IWRD)
          PREC(IAX,IWRD) = PREF(IAX,IP) + PSUM(IAX)
         ELSE
          PERR(IAX,IP) = 1
         END IF
        END DO
       END IF
C
C END PRIMARY FULL WORD RECONTRUCTION
C
      END DO
C
C REMOVE ERROR FLAGGED RECONSTRUCTED FULL WORDS
C
      DO IWRD = 1,400
C
C PRIMARY REFERENCE WORD COUNTER
C
       IREF = INT((IWRD-1)/25) + 1
C
C 5 MF CYCLE COUNTER (20 MF / VIM-5 RECORD)
C
       ICYC = INT((IWRD-1)/100) + 1
C
       DO IAX = 1,3
        IF (PERR(IAX,IREF).EQ.1) THEN
         PREC(IAX,IWRD) = 0
        END IF
       END DO
C
       IP = IREF
C
C SECONDARY REFERENCE WORD COUNTER
C
       IS = INT((IWRD-1)/50) + 1
C
       IF (RECTEST) THEN
        IF (IWRD.EQ.1) WRITE(80,'(1X,''EDR TIME: '',6(1X,I3))') TIME
        WRITE(80,'(15(1X,I4),4(1X,I1),2(1X,Z4.4))') 
     &  IWRD, PDIFF(1,IWRD),PDIFF(2,IWRD),PDIFF(3,IWRD),
     &  IP, PREF(1,IP), PREF(2,IP), PREF(3,IP),
     &  PREC(1,IWRD),PREC(2,IWRD),PREC(3,IWRD),
     &  IS, SREF(1,IS), SREF(2,IS), SREF(3,IS), 
     &  OBMODE(ICYC),OBRNG(ICYC),IBMODE(ICYC),IBRNG(ICYC),
     &  STAT1(ICYC),STAT2(ICYC)
       END IF
C
      END DO
C
      RETURN
      END
