      SUBROUTINE SHIVOT(REF,NREF,REC,NDM)
C
C THIS ROUTINE ADJUSTS 12 BIT PREDICT WORDS RECONSTRUCTED FROM DELTA
C MODULATED WORDS TO TRACK 12 BIT REFERENCE WORDS. ALGORITHM IS BASED 
C UPON REFERENCES IN JPL VOYAGER SOFTWARE DOC FP35-14-4
C
C ORIGINAL CODE WRITTEN BY SANDY KRAMER, HUGHES STX, 02/20/96
C ERROR CHECKING ADDED BY SBK - 05/03/96
C
C     INPUT:
C        REF = 12 BIT FULL WORDS
C        NREF = NUMBER OF FULL WORDS
C        REC = RECONSTRUCTED DELTA MODULATED (DM) WORDS
C        NDM = NUMBER OF RECONSTRUCTED DELTA MODULATED (DM)  WORDS
C        IRES = RATIO OF DM WORDS TO FULL WORDS
C
C   CR-5
C        IRES = 20 PRIMARY
C             = 10 SECONDARY
C   
C   CR-6 
C        IRES = 30 PRIMARY
C             = 15 SECONDARY
C
C     R                   R                   R
C     |                   |                   |
C     x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
C        
C     D D D D D D D D D D D D D D D D D D D D D D D D D D D D D D
C     M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M 
C     1 2 3 . . . . . . n n
C                         +
C                         1
C
C     LEFT = R(1) - DM(1)
C     RITE = R(2) - ( DM(n+1) + LEFT )
C
C
      INTEGER*2 REF(3,NREF),REC(3,NDM)
      INTEGER*4 LEFT,RITE,ERR
C
      INCLUDE 'UNPACK.INC'
C
      IF ( MOD(NDM,NREF).NE.0 ) THEN
       WRITE(6,'(1X,''*SHIVOT*  INVALID DM/REF RATIO!'')')
       STOP
      END IF
      IRES = NDM/NREF
C
C PASS ONLY REFERENCE WORDS
C
      IF ( SYS2(8) ) GOTO 10 
      DO IAX = 1,3
       DO I = 1,NDM
        J = (I-1)/IRES + 1
        REC(IAX,I) = 0
        IF ( MOD(I-1,IRES).EQ.0 ) REC(IAX,I) = REF(IAX,J)
       END DO
      END DO
      RETURN
C
   10 CONTINUE 
C
C PROCESS ALL THREE AXES
C
      DO IAX = 1,3
C
C SHIFT AND PIVOT DATA
C
       DO I = 1,NREF-1
        J = (I-1)*IRES + 1
        LEFT = REF(IAX,I) - REC(IAX,J)
        RITE = REF(IAX,I+1) - ( REC(IAX,J+IRES) + LEFT )
        ERR = ABS(RITE)
        DO IDM = 0,IRES-1
         REC(IAX,J+IDM) = REC(IAX,J+IDM) + LEFT  ! shift
         REC(IAX,J+IDM) = REC(IAX,J+IDM) + IDM*(RITE/IRES)  ! pivot
         IF ( REF(IAX,I).EQ.0 ) REC(IAX,J+IDM) = 0  ! null data check
         IF ( ERR.GE.5 ) REC(IAX,J+IDM) = 0  ! error check
        END DO
       END DO
C
C FINAL SHIFT, NO PIVOT, NO RECONSTRUCTION ERROR CHECK 
C
       J = (NREF-1)*IRES + 1
       LEFT = REF(IAX,NREF) - REC(IAX,J)
       DO IDM = 0,IRES-1
        REC(IAX,J+IDM) = REC(IAX,J+IDM) + LEFT  ! shift
        IF ( REF(IAX,NREF).EQ.0 ) REC(IAX,J+IDM) = 0  ! null data check
       END DO
C
      END DO    
C
      RETURN
      END       
