      SUBROUTINE DELTA(DM,NDM,REF,NREF,REC)

C This routine reconstructs 12 bit predicted words from 2 bit delta 
C modulated words.  Predicted words must be processed by shift and pivot
C routines to fully recover 12 bit full words that have been delta
C modulated.  See JPL Voyager software document FP35-14-4 for delta
C reconstruction algorithm.

C Written by Sandy Kramer, HSTX, Code 692, NASA GSFC 
C Original code - 02/16/96
C Debugged and verified - 05/03/96

      INTEGER*2 DM(3,NDM),REC(3,NDM),REF(3,NREF)
      INTEGER*4 COMPARE,DMSTORE,DMSUM,DMW

C Input Data:

C DM       2 bit delta modulated word
C NDM      Number of delta modulated words
C REF      Reference words
C NREF     Number of reference words

C Output Data:

C REC      Reconstructed predict words

C Local Variables:

C COMPARE  Previous DM's MSB
C DMSTORE  Reconstructed difference from previous predicted 12 bit word
C DMSUM    Predicted 12 bit word
C IRES     Ratio of DM words to reference words
C LSB      Least significant bit of DM
C MSB      Most significant bit of DM

      IF ( MOD(NDM,NREF).NE.0 ) THEN
       WRITE(6,'(1X,''*DELTA*  INVALID DM/REF RATIO!'')')
       STOP
      END IF
      IRES = NDM/NREF
      DO IAX = 1,3

C Set initial values

       COMPARE = 1
       DMSTORE = 1
       DMSUM = 2048
       DO IDM = 1,NDM

C Skip missing minor frames
 
        IREF = (IDM-1)/IRES + 1
        IF ( REF(IAX,IREF).LE.0 ) THEN
         COMPARE = 1   ! reinitialize 
         DMSTORE = 1   ! reinitialize  
         DMSUM = 2048  ! reinitialize 
         GO TO 100
        END IF

        DMW = DM(IAX,IDM)
        MSB = 0
        LSB = 0
        CALL MOVBIT(DMW, 1, 1, MSB, 0)
        CALL MOVBIT(DMW, 0, 1, LSB, 0)
        DO ISTEP = 1,2
         IF ( ISTEP.EQ.1 ) THEN  ! process MSB of 2 bit DM word (step 1)
          IF ( MSB.EQ.COMPARE ) THEN  ! present compare = previous compare
           DMSTORE = ABS(DMSTORE) * 2
           IF ( DMSTORE.GT.64 ) DMSTORE = 64
          ELSE                        ! present compare <> previous compare
           DMSTORE = ABS(DMSTORE) / 2
           IF ( DMSTORE.LT.2 ) DMSTORE = 2
          END IF
          IF ( MSB.NE.0 ) DMSTORE = -DMSTORE  ! sign rule
          COMPARE = MSB          ! save current DM's MSB
         ELSE                    ! process LSB of 2 bit DM word (step 2)
          IF ( LSB.NE.MSB ) DMSTORE = - DMSTORE / 2 
         END IF
         DMSUM = DMSUM + DMSTORE  ! add DMSTORE to accumulating sum
        END DO
        REC(IAX,IDM) = DMSUM

  100  END DO

      END DO

      RETURN 
      END
