      SUBROUTINE SHIVOT(REF,NREF,REC,NDM,PASSALL)

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 ORIGINAL CODE WRITTEN BY SANDY KRAMER, HUGHES STX, 02/20/96
C ERROR CHECKING ADDED BY SBK - 05/03/96
C REMOVAL OF INCLUDE FILE DEPENDENCE - SBK 12/26/96

C INPUT:

C    REF = 12 BIT REFERENCE FULL WORDS
C    NREF = NUMBER OF REFERENCE FULL WORDS
C    REC = RECONSTRUCTED DELTA MODULATED (DM) WORDS
C    NDM = NUMBER OF RECONSTRUCTED DELTA MODULATED (DM) WORDS
C    PASSALL = FLAG CONTROLING THE INCLUSION/EXCLUSION OF 
C              RECONSTRUCTED PREDICT WORDS

C OUPUT:

C    REC = ADJUSTED RECONSTRUCTED DELTA MODULATED (DM) WORDS

C LOCAL VARIABLES:

C    IRES = RATIO OF DM WORDS TO FULL WORDS

C NOTES:

C   CR-5
C        IRES = 20 PRIMARY
C             = 10 SECONDARY
C   
C   CR-6 
C        IRES = 30 PRIMARY
C             = 15 SECONDARY

C ALGORITHM:

C     Given n reconstructed delta words (DM) per reference word (R),

C     R(1) and R(2) are two full twelve bit reference words.
C     DM(i) is the ith reconstructed full word between R(1) and R(2).
C     DM(n) immediately precedes R(2) in the raw telemetry stream.
C     DM(n+1) immediately follows R(2) in the raw telemetry stream.
C     x(i) is the ith adjusted reconstructed full word.

C     LEFT = R(1) - DM(1)
C     RITE = R(2) - ( DM(n+1) + LEFT )
C     x(i) = DM(i) + LEFT + (i-1)*(RITE/n), where i=1,n+1

C     x(1) = R(1)
C     x(n+1) = R(2)

C     Align reference and reconstructed full words as follows:

C     R                   R                      R
C     1                   2                   ...N=NREF
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     1 2 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . .K=NDM 
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

      INTEGER*2 REF(3,NREF),REC(3,NDM)
      INTEGER*4 LEFT,RITE,ERR
      LOGICAL*1 PASSALL

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

      IF ( PASSALL ) GOTO 10 

C PASS ONLY REFERENCE WORDS

      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 RECOVER DELTA MODULATED WORDS

   10 CONTINUE 

C PROCESS ALL THREE AXES

      DO IAX = 1,3

C SHIFT AND PIVOT DATA

       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 FINAL SHIFT, NO PIVOT, NO RECONSTRUCTION ERROR CHECK 

       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

      END DO    

      RETURN
      END       
