      SUBROUTINE CR4DIFF(TIME)
C
C CR-4 ROUTINE
C
C THIS SUBROUTINE RECONSTRUCTS FULL WORDS FROM 12 BIT REFERENCE AND 6 BIT
C DIFFERENCE WORDS.  RECONSTRUCTION IS VERIFIED AGAINST PRIMARY REFERENCE 
C WORDS OCCURRING EVERY 16TH COUNT AND SECONDARY REFERENCE WORDS OCCURRING
C EVERY 4TH COUNT.  AN INCORRECT RECONSTRUCTION RESULTS IN ALL RECONSTRUCTED 
C FULL WORDS FOR A REFERENCE CYCLE TO BE FLAGGED AS ERRED AND REPLACED WITH 
C 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     SREF   SECONDARY REFERENCE FULL WORD        (FILL = 0)
C     SDIFF  SECONDARY DIFFERENCE HALF WORD       (FILL = 255)
C     SREC   SECONDARY RECONSTRUCTED FULL WORD    (FILL = 0)
C     SERR   SECONDARY RECONSTRUCTION ERROR FLAG  (0 = FALSE)
C                                                 (1 = TRUE)
C
C ORIGINAL SOURCE WRITTEN BY SANDY KRAMER, HUGHES STX, CODE 692  03/26/96
C
      INTEGER*2 PSUM(3),PERR(3,40),TIME(6),
     &          SSUM(3),SERR(3,20)
      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,40) = 0
      PERR(2,40) = 0
      PERR(3,40) = 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,640
C
C VERIFY RECONSTRUCTED FULL WORDS
C
       IF ( MOD(IWRD-1,16).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).EQ.0 ) 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  ! REINITIALIZE DIFFERENCE WORD ACCUMULATORS
        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).EQ.0 ) 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 RECONSTRUCTION
C
      END DO
C
C RECONSTRUCT SECONDARY FULL WORDS FROM SECONDARY DIFFERENCES.
C TEST FOR BAD DIFFERENCE WORDS AND NULL DATA.
C
      IS = 1
      SSUM(1) = 0
      SSUM(2) = 0
      SSUM(3) = 0
      SERR(1,20) = 0
      SERR(2,20) = 0
      SERR(3,20) = 0
C
C LOAD FIRST SECONDARY REFERENCE WORDS
C
      DO IAX = 1,3
       IF ( SREF(IAX,1).NE.0 ) THEN
        SREC(IAX,1) = SREF(IAX,1)
        SERR(IAX,1) = 0
       ELSE
        SREC(IAX,1) = 0
        SERR(IAX,1) = 1
       END IF
      END DO
C
C RECONSTRUCT 12 BIT FULL WORDS FROM 6 BIT DIFFERENCE WORDS.  USE
C SECONDARY REFERENCE WORDS TO TEST FOR ERRORS IN RECONSTRUCTION OF 
C FULL WORDS FROM DIFFERENCE WORDS.
C
      DO IWRD = 1,80
C
C VERIFY RECONSTRUCTED FULL WORDS
C
       IF ( MOD(IWRD-1,4).EQ.0 .AND. IWRD.GT.1 ) THEN
        IS = IS + 1
        DO IAX = 1,3
         SERR(IAX,IS) = 0
         SREC(IAX,IWRD) = 0
         IF (SREF(IAX,IS-1).NE.0.AND.
     &       SDIFF(IAX,IWRD).NE.255.AND.
     &       SERR(IAX,IS-1).NE.1) THEN 
          SSUM(IAX) = SSUM(IAX) + SDIFF(IAX,IWRD)
          SREC(IAX,IWRD) = SREF(IAX,IS-1) + SSUM(IAX)
         ELSE 
          SREC(IAX,IWRD) = 0
          SERR(IAX,IS-1) = 1
         END IF
         IF (SREC(IAX,IWRD).NE.SREF(IAX,IS)) THEN
          SREC(IAX,IWRD) = SREF(IAX,IS)
          SERR(IAX,IS-1) = 1
         END IF
         SSUM(IAX) = 0  ! REINITIALIZE DIFFERENCE WORD ACCUMULATORS
        END DO
C
C OPERATE ON SECONDARY DIFFERENCE WORDS
C
       ELSE
        DO IAX = 1,3
         SREC(IAX,IWRD) = 0
         IF (SDIFF(IAX,IWRD).NE.255.AND.
     &       SERR(IAX,IS).NE.1) THEN 
          SSUM(IAX) = SSUM(IAX) + SDIFF(IAX,IWRD)
          SREC(IAX,IWRD) = SREF(IAX,IS) + SSUM(IAX)
         ELSE
          SREC(IAX,IWRD) = 0
          SERR(IAX,IS) = 1
         END IF
        END DO
       END IF
C
C END SECONDARY FULL WORD RECONSTRUCTION
C
      END DO
C
C REMOVE ERROR FLAGGED RECONSTRUCTED FULL WORDS
C
      DO IWRD = 1,640
C
C PRIMARY REFERENCE WORD COUNTER
C
       IP = INT((IWRD-1)/16) + 1
C
C CHECK FOR PRIMARY FULL WORD RECONSTRUCTION ERRORS
C
       DO IAX = 1,3
        IF ( PERR(IAX,IP).EQ.1 ) PREC(IAX,IWRD) = 0
       END DO
C
C SECONDARY REFERENCE WORD COUNTER
C
       IS = INT((IWRD-1)/32) + 1
C
C CHECK FOR SECONDARY FULL WORD RECONSTRUCTION ERRORS
C
       IF (MOD(IWRD,2).NE.0) THEN
        IWRD2 = (IWRD-1)/8 + 1
        DO IAX = 1,3
         IF (SERR(IAX,IS).EQ.1) SREC(IAX,IWRD2) = 0
        END DO
       END IF
C
C 2 MF CYCLE COUNTER (40 MF / CR-4 RECORD)
C
       ICYC = INT((IWRD-1)/32) + 1
C
       IF (RECTEST) THEN
C
        IF (IWRD.EQ.1) WRITE(80,'(1X,''EDR TIME: '',6(1X,I3))') TIME
C
        WRITE(80,'(22(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),
     &  IWRD2,SDIFF(1,IWRD2),SDIFF(2,IWRD2),SDIFF(3,IWRD2),
     &  IS, SREF(1,IS), SREF(2,IS), SREF(3,IS), 
     &  SREC(1,IWRD2),SREC(2,IWRD2),SREC(3,IWRD2),
     &  OBMODE(ICYC),OBRNG(ICYC),IBMODE(ICYC),IBRNG(ICYC),
     &  STAT1(ICYC),STAT2(ICYC)
C
       END IF
C
      END DO
C
      RETURN
      END
