******************************************************************
*
*	TITLE:  UNPACK VOYAGER 260 WORD VIM-5 EDR MAG SCIENCE BLOCK
*
*	FILE NAME:  MAGVIM5.FOR
*
*	PURPOSE:  TO UNPACK THE MAG SCIENCE DATA
*
*	HISTORY:
*
*	AUTHOR            DATE            CHANGE
*	--------          ----            -------
*       S. B. KRAMER     10/26/92         ORIGINAL CODE
*                                         (MODE VIM-5)
*
*	CALLING SEQUENCE:  SUBROUTINE MAGVIM5()
*
*	MODULES REFERENCED:
*
*		MOVBIT
*
*	COMMON AREAS:
*
*		SEE UNPACK.INC FOR COMMON AREA DEFINITIONS
*
*	NOTES:
*
*		SCIENCE DATA IS RECONSTRUCTED INTO I*2 VARIABLES FROM THE
*                FOLLOWING:
*				P diff	6 bits each
*				P ref	12 bits each
*				S ref	12 bits each
*
*	PDL:
*
*		CALL MOVBIT TO RECONSTRUCT THE MAG SCIENCE DATA INTO 
*		 I*2 VARIABLES
*		RETURN
*
******************************************************************* 
      SUBROUTINE MAGVIM5()
C
      INTEGER*4 PDXIND(125),PDYIND(125),PDZIND(125)
      INTEGER*4 PRXIND(5),PRYIND(5),PRZIND(5)
      INTEGER*4 SRXIND(5),SRYIND(5),SRZIND(5)
      LOGICAL*1 DQFLAG,DPFLAG
C
C BIT OFFSET OF PRIMARY REFERENCE WORD OCCURANCES WITHIN 5 MF CYCLE
C
      DATA PRXIND/  6,120,198,312, -1/
      DATA PRYIND/ 24,138,216,330, -1/
      DATA PRZIND/ 42,156,234,348, -1/
C
C BIT OFFSET OF SECONDARY REFERENCE WORD OCCURANCES WITHIN 5 MF CYCLE
C
      DATA SRXIND/ 54, -1,246, -1, -1/
      DATA SRYIND/ 66, -1,258, -1, -1/
      DATA SRZIND/ 78, -1,270, -1, -1/
C
C BIT OFFSET OF DIFFERENCE WORD OCCURANCES WITHIN 5 MF CYCLE
C
      DATA PDXIND/  0, 90,108,126,144,162,180,198,216,234,
     &            252,270,288,306,324,342,360,378,396, -1,
     &             -1, -1, -1, -1 ,-1,
C
     &              6, 24, 42, 60, 78, 96,114,168,186,204,
     &            222,240,258,276,294,312,330,348,366,384,
     &            402, -1, -1, -1, -1,
C
     &             12, 30, 48, 66, 84,102,120,138,156,174,
     &            192,282,300,318,336,354,372,390, -1, -1,
     &             -1, -1, -1, -1, -1,
C
     &              0, 18, 36, 54, 72, 90,108,126,144,162,
     &            180,198,216,234,252,270,288,306,360,378,
     &            396, -1 ,-1, -1, -1,
C
     &              6, 24, 42, 60, 78, 96,114,132,150,168,
     &            186,204,222,240,258,276,294,312,330,348,
     &            366, -1, -1, -1, -1/
C
      DATA PDYIND/ 18, 96,114,132,150,168,186,204,222,240,
     &            258,276,294,312,330,348,366,384,402, -1,
     &             -1, -1, -1, -1, -1,
C
     &             12, 30, 48, 66, 84,102,132,174,192,210,
     &            228,246,264,282,300,318,336,354,372,390,
     &             -1, -1, -1, -1, -1,
C
     &              0, 18, 36, 54, 72, 90,108,126,144,162,
     &            180,210,288,306,324,342,360,378,396, -1,
     &             -1, -1, -1, -1, -1,
C
     &              6, 24, 42, 60, 78, 96,114,132,150,168,
     &            186,204,222,240,258,276,294,324,366,384,
     &            402, -1, -1, -1, -1,
C
     &             12, 30, 48, 66, 84,102,120,138,156,174,
     &            192,210,228,246,264,282,300,318,336,354,
     &            372, -1, -1, -1, -1/
C
      DATA PDZIND/ 36,102,120,138,156,174,192,210,228,246,
     &            264,282,300,318,336,354,372,390, -1, -1,
     &             -1, -1, -1, -1, -1,
C
     &              0, 18, 36, 54, 72, 90,108,150,180,198,
     &            216,234,252,270,288,306,324,342,360,378,
     &            396, -1, -1, -1, -1,
C
     &              6, 24, 42, 60, 78, 96,114,132,150,168,
     &            186,228,294,312,330,348,366,384,402, -1,
     &             -1, -1, -1, -1, -1,
C
     &             12, 30, 48, 66, 84,102,120,138,156,174,
     &            192,210,228,246,264,282,300,342,372,390,
     &             -1, -1, -1, -1, -1,
C
     &              0, 18, 36, 54, 72, 90,108,126,144,162,
     &            180,198,216,234,252,270,288,306,324,342,
     &            360,378, -1, -1, -1/
C
C DATA PRESENCE SWITCH FLAG
C
      DATA DPFLAG/.FALSE./
C
C DATA QUALITY SWITCH FLAG
C
      DATA DQFLAG/.FALSE./
C
      INCLUDE 'UNPACK.INC'
C
      IF (DRSDAT.NE.35) RETURN
C
      IF (.NOT.SYS2(10)) DPFLAG = .TRUE.
      IF (.NOT.SYS2(11)) DQFLAG = .TRUE.
C
C CONVERT IBM ARCHITECTURE BYTE INTO VAX BYTE ARCHITECTURE BY
C REVERSING BIT ORDER IN EACH BYTE, IE 0-7 -> 7-0
C
       DO J=1,1040
        MAG(J) = FLIP(ZEXT(MAG(J)))
       END DO
C
C READ 20 MINOR FRAMES (52 BYTE EACH) OF VIM-5 RECORD (1040 BYTES).  DATA 
C CYCLES EVERY 5 MINOR FRAMES.
C
      IPR = 0
      ISR = 0
      IPRX = 0
      IPRY = 0
      IPRZ = 0
      ISRX = 0
      ISRY = 0
      ISRZ = 0
      IPDX = 0
      IPDY = 0
      IPDZ = 0
      IMAG = -416
      DO 200 J = 1,4
C
       DO 100 K = 1,5
C
C BIT OFFSET (416 BITS PER VIM-5 MF)
C
        IMAG = IMAG + 416
C
C MINOR FRAME COUNTER (20 MF PER VIM-5 SCIENCE BLOCK)
C
        IFRM = (J-1)*5 + K
C
C DATA QUALITY STATUS WORD/INDICATOR COUNTER (ONE DQI PER 8 VIM-5 MF)
C
        IDQ = INT((IFRM-1)/8) + 1
C
C EXTRACT PRIMARY REFERENCE WORDS (12 BIT) USING A PREDETERMINED OFFSET
C TABLE.
C
        IF (PRXIND(K).NE.-1) THEN
         IPRX = IPRX + 1
         PREF(1,IPRX) = 0
         CALL MOVBIT(MAG, IMAG+PRXIND(K)   , 1, PREF(1,IPRX), 11)
         CALL MOVBIT(MAG, IMAG+PRXIND(K)+1 , 1, PREF(1,IPRX), 10)
         CALL MOVBIT(MAG, IMAG+PRXIND(K)+2 , 1, PREF(1,IPRX),  9)
         CALL MOVBIT(MAG, IMAG+PRXIND(K)+3 , 1, PREF(1,IPRX),  8)
         CALL MOVBIT(MAG, IMAG+PRXIND(K)+4 , 1, PREF(1,IPRX),  7)
         CALL MOVBIT(MAG, IMAG+PRXIND(K)+5 , 1, PREF(1,IPRX),  6)
         CALL MOVBIT(MAG, IMAG+PRXIND(K)+6 , 1, PREF(1,IPRX),  5)
         CALL MOVBIT(MAG, IMAG+PRXIND(K)+7 , 1, PREF(1,IPRX),  4)
         CALL MOVBIT(MAG, IMAG+PRXIND(K)+8 , 1, PREF(1,IPRX),  3)
         CALL MOVBIT(MAG, IMAG+PRXIND(K)+9 , 1, PREF(1,IPRX),  2)
         CALL MOVBIT(MAG, IMAG+PRXIND(K)+10, 1, PREF(1,IPRX),  1)
         CALL MOVBIT(MAG, IMAG+PRXIND(K)+11, 1, PREF(1,IPRX),  0)
         IF (DPFLAG.AND.DPI(IFRM).NE.0) PREF(1,IPRX) = 0
         IF (DQFLAG.AND.DQI(IDQ).NE.0) PREF(1,IPRX) = 0
        END IF
C
        IF (PRYIND(K).NE.-1) THEN
         IPRY = IPRY + 1
         PREF(2,IPRY) = 0
         CALL MOVBIT(MAG, IMAG+PRYIND(K)   , 1, PREF(2,IPRY), 11)
         CALL MOVBIT(MAG, IMAG+PRYIND(K)+1 , 1, PREF(2,IPRY), 10)
         CALL MOVBIT(MAG, IMAG+PRYIND(K)+2 , 1, PREF(2,IPRY),  9)
         CALL MOVBIT(MAG, IMAG+PRYIND(K)+3 , 1, PREF(2,IPRY),  8)
         CALL MOVBIT(MAG, IMAG+PRYIND(K)+4 , 1, PREF(2,IPRY),  7)
         CALL MOVBIT(MAG, IMAG+PRYIND(K)+5 , 1, PREF(2,IPRY),  6)
         CALL MOVBIT(MAG, IMAG+PRYIND(K)+6 , 1, PREF(2,IPRY),  5)
         CALL MOVBIT(MAG, IMAG+PRYIND(K)+7 , 1, PREF(2,IPRY),  4)
         CALL MOVBIT(MAG, IMAG+PRYIND(K)+8 , 1, PREF(2,IPRY),  3)
         CALL MOVBIT(MAG, IMAG+PRYIND(K)+9 , 1, PREF(2,IPRY),  2)
         CALL MOVBIT(MAG, IMAG+PRYIND(K)+10, 1, PREF(2,IPRY),  1)
         CALL MOVBIT(MAG, IMAG+PRYIND(K)+11, 1, PREF(2,IPRY),  0)
         IF (DPFLAG.AND.DPI(IFRM).NE.0) PREF(2,IPRY) = 0
         IF (DQFLAG.AND.DQI(IDQ).NE.0) PREF(2,IPRY) = 0
        END IF
C
        IF (PRZIND(K).NE.-1) THEN
         IPRZ = IPRZ + 1
         PREF(3,IPRZ) = 0
         CALL MOVBIT(MAG, IMAG+PRZIND(K)   , 1, PREF(3,IPRZ), 11)
         CALL MOVBIT(MAG, IMAG+PRZIND(K)+1 , 1, PREF(3,IPRZ), 10)
         CALL MOVBIT(MAG, IMAG+PRZIND(K)+2 , 1, PREF(3,IPRZ),  9)
         CALL MOVBIT(MAG, IMAG+PRZIND(K)+3 , 1, PREF(3,IPRZ),  8)
         CALL MOVBIT(MAG, IMAG+PRZIND(K)+4 , 1, PREF(3,IPRZ),  7)
         CALL MOVBIT(MAG, IMAG+PRZIND(K)+5 , 1, PREF(3,IPRZ),  6)
         CALL MOVBIT(MAG, IMAG+PRZIND(K)+6 , 1, PREF(3,IPRZ),  5)
         CALL MOVBIT(MAG, IMAG+PRZIND(K)+7 , 1, PREF(3,IPRZ),  4)
         CALL MOVBIT(MAG, IMAG+PRZIND(K)+8 , 1, PREF(3,IPRZ),  3)
         CALL MOVBIT(MAG, IMAG+PRZIND(K)+9 , 1, PREF(3,IPRZ),  2)
         CALL MOVBIT(MAG, IMAG+PRZIND(K)+10, 1, PREF(3,IPRZ),  1)
         CALL MOVBIT(MAG, IMAG+PRZIND(K)+11, 1, PREF(3,IPRZ),  0)
         IF (DPFLAG.AND.DPI(IFRM).NE.0) PREF(3,IPRZ) = 0
         IF (DQFLAG.AND.DQI(IDQ).NE.0) PREF(3,IPRZ) = 0
        END IF
C
C EXTRACT SECONDARY REFERENCE WORDS (12 BIT) USING A PREDETERMINED OFFSET
C TABLE AND ASSIGN TO I*2 WORD.
C
        IF (SRXIND(K).NE.-1) THEN
         ISRX = ISRX + 1
         SREF(1,ISRX) = 0
         CALL MOVBIT(MAG, IMAG+SRXIND(K)   , 1, SREF(1,ISRX), 11)
         CALL MOVBIT(MAG, IMAG+SRXIND(K)+1 , 1, SREF(1,ISRX), 10)
         CALL MOVBIT(MAG, IMAG+SRXIND(K)+2 , 1, SREF(1,ISRX),  9)
         CALL MOVBIT(MAG, IMAG+SRXIND(K)+3 , 1, SREF(1,ISRX),  8)
         CALL MOVBIT(MAG, IMAG+SRXIND(K)+4 , 1, SREF(1,ISRX),  7)
         CALL MOVBIT(MAG, IMAG+SRXIND(K)+5 , 1, SREF(1,ISRX),  6)
         CALL MOVBIT(MAG, IMAG+SRXIND(K)+6 , 1, SREF(1,ISRX),  5)
         CALL MOVBIT(MAG, IMAG+SRXIND(K)+7 , 1, SREF(1,ISRX),  4)
         CALL MOVBIT(MAG, IMAG+SRXIND(K)+8 , 1, SREF(1,ISRX),  3)
         CALL MOVBIT(MAG, IMAG+SRXIND(K)+9 , 1, SREF(1,ISRX),  2)
         CALL MOVBIT(MAG, IMAG+SRXIND(K)+10, 1, SREF(1,ISRX),  1)
         CALL MOVBIT(MAG, IMAG+SRXIND(K)+11, 1, SREF(1,ISRX),  0)
         IF (DPFLAG.AND.DPI(IFRM).NE.0) SREF(1,ISRX) = 0
         IF (DQFLAG.AND.DQI(IDQ).NE.0) SREF(1,ISRX) = 0
        END IF
C
        IF (SRYIND(K).NE.-1) THEN
         ISRY = ISRY + 1
         SREF(2,ISRY) = 0
         CALL MOVBIT(MAG, IMAG+SRYIND(K)   , 1, SREF(2,ISRY), 11)
         CALL MOVBIT(MAG, IMAG+SRYIND(K)+1 , 1, SREF(2,ISRY), 10)
         CALL MOVBIT(MAG, IMAG+SRYIND(K)+2 , 1, SREF(2,ISRY),  9)
         CALL MOVBIT(MAG, IMAG+SRYIND(K)+3 , 1, SREF(2,ISRY),  8)
         CALL MOVBIT(MAG, IMAG+SRYIND(K)+4 , 1, SREF(2,ISRY),  7)
         CALL MOVBIT(MAG, IMAG+SRYIND(K)+5 , 1, SREF(2,ISRY),  6)
         CALL MOVBIT(MAG, IMAG+SRYIND(K)+6 , 1, SREF(2,ISRY),  5)
         CALL MOVBIT(MAG, IMAG+SRYIND(K)+7 , 1, SREF(2,ISRY),  4)
         CALL MOVBIT(MAG, IMAG+SRYIND(K)+8 , 1, SREF(2,ISRY),  3)
         CALL MOVBIT(MAG, IMAG+SRYIND(K)+9 , 1, SREF(2,ISRY),  2)
         CALL MOVBIT(MAG, IMAG+SRYIND(K)+10, 1, SREF(2,ISRY),  1)
         CALL MOVBIT(MAG, IMAG+SRYIND(K)+11, 1, SREF(2,ISRY),  0)
         IF (DPFLAG.AND.DPI(IFRM).NE.0) SREF(2,ISRY) = 0
         IF (DQFLAG.AND.DQI(IDQ).NE.0) SREF(2,ISRY) = 0
        END IF
C
        IF (SRZIND(K).NE.-1) THEN
         ISRZ = ISRZ + 1
         SREF(3,ISRZ) = 0
         CALL MOVBIT(MAG, IMAG+SRZIND(K)   , 1, SREF(3,ISRZ), 11)
         CALL MOVBIT(MAG, IMAG+SRZIND(K)+1 , 1, SREF(3,ISRZ), 10)
         CALL MOVBIT(MAG, IMAG+SRZIND(K)+2 , 1, SREF(3,ISRZ),  9)
         CALL MOVBIT(MAG, IMAG+SRZIND(K)+3 , 1, SREF(3,ISRZ),  8)
         CALL MOVBIT(MAG, IMAG+SRZIND(K)+4 , 1, SREF(3,ISRZ),  7)
         CALL MOVBIT(MAG, IMAG+SRZIND(K)+5 , 1, SREF(3,ISRZ),  6)
         CALL MOVBIT(MAG, IMAG+SRZIND(K)+6 , 1, SREF(3,ISRZ),  5)
         CALL MOVBIT(MAG, IMAG+SRZIND(K)+7 , 1, SREF(3,ISRZ),  4)
         CALL MOVBIT(MAG, IMAG+SRZIND(K)+8 , 1, SREF(3,ISRZ),  3)
         CALL MOVBIT(MAG, IMAG+SRZIND(K)+9 , 1, SREF(3,ISRZ),  2)
         CALL MOVBIT(MAG, IMAG+SRZIND(K)+10, 1, SREF(3,ISRZ),  1)
         CALL MOVBIT(MAG, IMAG+SRZIND(K)+11, 1, SREF(3,ISRZ),  0)
         IF (DPFLAG.AND.DPI(IFRM).NE.0) SREF(3,ISRZ) = 0
         IF (DQFLAG.AND.DQI(IDQ).NE.0) SREF(3,ISRZ) = 0
        END IF
C
C EXTRACT 6 BIT DIFFERENCE WORDS FROM MINOR FRAME ACCORDING TO PREDETERMINED
C INDEX IN DATA STATEMENT AND ASSIGN TO I*2 WORD.
C
        DO LL = 1,25
         IOFF = LL + (K-1)*25
         IF (PDXIND(IOFF).NE.-1) THEN
          IPDX = IPDX + 1
          PDIFF(1,IPDX) = 0
          CALL MOVBIT(MAG, IMAG+PDXIND(IOFF)  , 1, PDIFF(1,IPDX),  5)
          CALL MOVBIT(MAG, IMAG+PDXIND(IOFF)+1, 1, PDIFF(1,IPDX),  4)
          CALL MOVBIT(MAG, IMAG+PDXIND(IOFF)+2, 1, PDIFF(1,IPDX),  3)
          CALL MOVBIT(MAG, IMAG+PDXIND(IOFF)+3, 1, PDIFF(1,IPDX),  2)
          CALL MOVBIT(MAG, IMAG+PDXIND(IOFF)+4, 1, PDIFF(1,IPDX),  1)
          CALL MOVBIT(MAG, IMAG+PDXIND(IOFF)+5, 1, PDIFF(1,IPDX),  0)
          IF (PDIFF(1,IPDX).GT.31) PDIFF(1,IPDX) = PDIFF(1,IPDX) - 64
          IF (DPFLAG.AND.DPI(IFRM).NE.0) PDIFF(1,IPDX) = 255
          IF (DQFLAG.AND.DQI(IDQ).NE.0) PDIFF(1,IPDX) = 255
         END IF
C
         IF (PDYIND(IOFF).NE.-1) THEN
          IPDY = IPDY + 1
          PDIFF(2,IPDY) = 0
          CALL MOVBIT(MAG, IMAG+PDYIND(IOFF)  , 1, PDIFF(2,IPDY),  5)
          CALL MOVBIT(MAG, IMAG+PDYIND(IOFF)+1, 1, PDIFF(2,IPDY),  4)
          CALL MOVBIT(MAG, IMAG+PDYIND(IOFF)+2, 1, PDIFF(2,IPDY),  3)
          CALL MOVBIT(MAG, IMAG+PDYIND(IOFF)+3, 1, PDIFF(2,IPDY),  2)
          CALL MOVBIT(MAG, IMAG+PDYIND(IOFF)+4, 1, PDIFF(2,IPDY),  1)
          CALL MOVBIT(MAG, IMAG+PDYIND(IOFF)+5, 1, PDIFF(2,IPDY),  0)
          IF (PDIFF(2,IPDY).GT.31) PDIFF(2,IPDY) = PDIFF(2,IPDY) - 64
          IF (DPFLAG.AND.DPI(IFRM).NE.0) PDIFF(2,IPDY) = 255
          IF (DQFLAG.AND.DQI(IDQ).NE.0) PDIFF(2,IPDY) = 255
         END IF
C
         IF (PDZIND(IOFF).NE.-1) THEN
          IPDZ = IPDZ + 1
          PDIFF(3,IPDZ) = 0
          CALL MOVBIT(MAG, IMAG+PDZIND(IOFF)  , 1, PDIFF(3,IPDZ),  5)
          CALL MOVBIT(MAG, IMAG+PDZIND(IOFF)+1, 1, PDIFF(3,IPDZ),  4)
          CALL MOVBIT(MAG, IMAG+PDZIND(IOFF)+2, 1, PDIFF(3,IPDZ),  3)
          CALL MOVBIT(MAG, IMAG+PDZIND(IOFF)+3, 1, PDIFF(3,IPDZ),  2)
          CALL MOVBIT(MAG, IMAG+PDZIND(IOFF)+4, 1, PDIFF(3,IPDZ),  1)
          CALL MOVBIT(MAG, IMAG+PDZIND(IOFF)+5, 1, PDIFF(3,IPDZ),  0)
          IF (PDIFF(3,IPDZ).GT.31) PDIFF(3,IPDZ) = PDIFF(3,IPDZ) - 64
          IF (DPFLAG.AND.DPI(IFRM).NE.0) PDIFF(3,IPDZ) = 255
          IF (DQFLAG.AND.DQI(IDQ).NE.0) PDIFF(3,IPDZ) = 255
         END IF
        END DO
C
  100  CONTINUE
C
  200 CONTINUE
C
      RETURN
      END
