******************************************************************
*
*	TITLE:  UNPACK VOYAGER 1500 WORD EDR MAG DATA
*
*	FILE NAME:  MAGGS3.FOR
*
*	PURPOSE:  TO UNPACK THE MAG SCIENCE DATA
*
*	HISTORY:
*
*	AUTHOR            DATE            CHANGE
*	--------          ----            -------
*	S. J. KEMPLER     8/12/85         ORIGINAL CODE
*       S. B. KRAMER     01/06/93         MODIFIED FOR NEW VOY PROD PGM
*
*	CALLING SEQUENCE:  SUBROUTINE MAGGS3()
*
*	ARGUMENT LIST:
*
*	NAME		TYPE	USED	PURPOSE
*	-----		-----	-----	--------
*
*	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 diff	6 bits each
*				S ref	12 bits each
*				OB HFM	12 bits each
*				IB HFM	12 bits each
*
*	PDL:
*
*		CALL MOVBIT TO RECONSTRUCT THE MAG SCIENCE DATA INTO I*2 VARIABLES
*		RETURN
*
******************************************************************* 
      SUBROUTINE MAGGS3()
C
      INTEGER*4 PDXIND(10), PDYIND(10), PDZIND(10),
     I          PRXIND(2),  PRYIND(2),  PRZIND(2),
     I          SDXIND(5),  SDYIND(5),  SDZIND(5)
C
      LOGICAL*1 DPFLAG,DQFLAG,RECTEST/.FALSE./
C
      INCLUDE 'UNPACK.INC'
C
C BIT OFFSET OF PRIMARY DIIFERENCE WORDS
C
      DATA PDXIND/ 0,168,264,288,336,360,456,480,528,552/
      DATA PDYIND/24,176,272,296,344,384,464,488,536,560/
      DATA PDZIND/48,184,280,304,352,408,472,496,544,568/
C
C BIT OFFSET OF PRIMARY REFERENCE WORDS
C
      DATA PRXIND/ 8,368/
      DATA PRYIND/32,392/
      DATA PRZIND/56,416/
C
C BIT OFFSET OF SECONDARY REFERENCE WORDS
C
      DATA SDXIND/192,312,432,504,576/
      DATA SDYIND/216,320,440,512,584/
      DATA SDZIND/240,328,448,520,592/
C
C DATA PRESENCE SWITCH FLAG
C
      DATA DPFLAG/.FALSE./
C
C DATA QUALITY SWITCH FLAG
C
      DATA DQFLAG/.FALSE./
C
      IF (DRSDAT.NE.35) RETURN
C
      IF (.NOT.SYS2(10)) DPFLAG = .TRUE.
      IF (.NOT.SYS2(11)) DQFLAG = .TRUE.
C
      IPD = 0
      IPR = 0
      ISD = 0
      ISR = 0
      IB  = 0
      IMAG=-600
      IFRM = 0
C
      DO 90 I=1,80
C
C BIT OFFSET (600 BITS PER GS-3 FRAME)
C
         IMAG=IMAG+600
C
C MINOR FRAME COUNTER (80 MF PER GS-3 SCIENCE BLOCK)
C
         IFRM = I
C
C DATA QUALITY STATUS WORD/INDICATOR COUNTER (ONE DQI PER 8 GS-3 MF)
C
         IDQ = INT((IFRM-1)/8) + 1
C
         DO 10 J=1,10
C
C EXTRACT PRIMARY DIFFERENCE WORDS
C
            IPD=IPD+1
C
            PDIFF(1,IPD) = 0
            CALL MOVBIT( MAG, PDXIND(J)+IMAG  , 6, PDIFF(1,IPD),  0)
            IF ( PDIFF(1,IPD).GE.32 ) PDIFF(1,IPD) = PDIFF(1,IPD) - 64
C
C CHECK DATA PRESENCE INDICATOR
C
            IF (DPFLAG.AND.DPI(IFRM).NE.0) PDIFF(1,IPD) = 255
C
C CHECK DATA QUALITY INDICATOR
C
            IF (DQFLAG.AND.DQI(IDQ).NE.0) PDIFF(1,IPD) = 255
C
            PDIFF(2,IPD) = 0
            CALL MOVBIT( MAG, PDYIND(J)+IMAG  , 6, PDIFF(2,IPD),   0)
            IF ( PDIFF(2,IPD).GE.32 ) PDIFF(2,IPD) = PDIFF(2,IPD) - 64
C
C CHECK DATA PRESENCE INDICATOR
C
            IF (DPFLAG.AND.DPI(IFRM).NE.0) PDIFF(2,IPD) = 255
C
C CHECK DATA QUALITY INDICATOR
C
            IF (DQFLAG.AND.DQI(IDQ).NE.0) PDIFF(2,IPD) = 255
C
            PDIFF(3,IPD) = 0
            CALL MOVBIT( MAG, PDZIND(J)+IMAG  , 6, PDIFF(3,IPD),   0)
            IF ( PDIFF(3,IPD).GE.32 ) PDIFF(3,IPD) = PDIFF(3,IPD) - 64
C
C CHECK DATA PRESENCE INDICATOR
C
            IF (DPFLAG.AND.DPI(IFRM).NE.0) PDIFF(3,IPD) = 255
C
C CHECK DATA QUALITY INDICATOR
C
            IF (DQFLAG.AND.DQI(IDQ).NE.0) PDIFF(3,IPD) = 255
C
  10     CONTINUE
C
         DO 20 J=1,2
C
C EXTRACT PRIMARY REFERENCE WORDS
C
            IPR=IPR+1
C
            CALL MOVBIT( MAG, PRXIND(J)+IMAG  , 6, PREF(1,IPR), 6)
            CALL MOVBIT( MAG, PRXIND(J)+IMAG+8, 6, PREF(1,IPR), 0)
C
C CHECK DATA PRESENCE INDICATOR
C
            IF (DPFLAG.AND.DPI(IFRM).NE.0) PREF(1,IPR) = 0
C
C CHECK DATA QUALITY INDICATOR
C
            IF (DQFLAG.AND.DQI(IDQ).NE.0) PREF(1,IPR) = 0
C
            CALL MOVBIT( MAG, PRYIND(J)+IMAG  , 6, PREF(2,IPR), 6)
            CALL MOVBIT( MAG, PRYIND(J)+IMAG+8, 6, PREF(2,IPR), 0)
C
C CHECK DATA PRESENCE INDICATOR
C
            IF (DPFLAG.AND.DPI(IFRM).NE.0) PREF(2,IPR) = 0
C
C CHECK DATA QUALITY INDICATOR
C
            IF (DQFLAG.AND.DQI(IDQ).NE.0) PREF(2,IPR) = 0
C
            CALL MOVBIT( MAG, PRZIND(J)+IMAG  , 6, PREF(3,IPR), 6)
            CALL MOVBIT( MAG, PRZIND(J)+IMAG+8, 6, PREF(3,IPR), 0)
C
C CHECK DATA PRESENCE INDICATOR
C
            IF (DPFLAG.AND.DPI(IFRM).NE.0) PREF(3,IPR) = 0
C
C CHECK DATA QUALITY INDICATOR
C
            IF (DQFLAG.AND.DQI(IDQ).NE.0) PREF(3,IPR) = 0
C
  20     CONTINUE
C
         DO 30 J=1,5
C
C EXTRACT SECONDARY DIFFERENCE WORDS
C
            ISD=ISD+1
C     
            SDIFF(1,ISD) = 0
            CALL MOVBIT( MAG, SDXIND(J)+IMAG  , 6, SDIFF(1,ISD),   0)
            IF ( SDIFF(1,ISD).GE.32 ) SDIFF(1,ISD) = SDIFF(1,ISD) - 64
C
C CHECK DATA PRESENCE INDICATOR
C
            IF (DPFLAG.AND.DPI(IFRM).NE.0) SDIFF(1,ISD) = 255
C
C CHECK DATA QUALITY INDICATOR
C
            IF (DQFLAG.AND.DQI(IDQ).NE.0) SDIFF(1,ISD) = 255
C
            SDIFF(2,ISD) = 0
            CALL MOVBIT( MAG, SDYIND(J)+IMAG  , 6, SDIFF(2,ISD),   0)
            IF ( SDIFF(2,ISD).GE.32 ) SDIFF(2,ISD) = SDIFF(2,ISD) - 64
C
C CHECK DATA PRESENCE INDICATOR
C
            IF (DPFLAG.AND.DPI(IFRM).NE.0) SDIFF(2,ISD) = 255
C
C CHECK DATA QUALITY INDICATOR
C
            IF (DQFLAG.AND.DQI(IDQ).NE.0) SDIFF(2,ISD) = 255
C
            SDIFF(3,ISD) = 0
            CALL MOVBIT( MAG, SDZIND(J)+IMAG  , 6, SDIFF(3,ISD),   0)
            IF ( SDIFF(3,ISD).GE.32 ) SDIFF(3,ISD) = SDIFF(3,ISD) - 64
C
C CHECK DATA PRESENCE INDICATOR
C
            IF (DPFLAG.AND.DPI(IFRM).NE.0) SDIFF(3,ISD) = 255
C
C CHECK DATA QUALITY INDICATOR
C
            IF (DQFLAG.AND.DQI(IDQ).NE.0) SDIFF(3,ISD) = 255
C
  30     CONTINUE
C
C EXTRACT SECONDARY REFERENCE WORDS
C
            ISR=ISR+1
C
            CALL MOVBIT( MAG, 200+IMAG  , 6, SREF(1,ISR), 6)
            CALL MOVBIT( MAG, 200+IMAG+8, 6, SREF(1,ISR), 0)
C
C CHECK DATA PRESENCE INDICATOR
C
            IF (DPFLAG.AND.DPI(IFRM).NE.0) SREF(1,ISR) = 0
C
C CHECK DATA QUALITY INDICATOR
C
            IF (DQFLAG.AND.DQI(IDQ).NE.0) SREF(1,ISR) = 0
C
            CALL MOVBIT( MAG, 224+IMAG  , 6, SREF(2,ISR), 6)
            CALL MOVBIT( MAG, 224+IMAG+8, 6, SREF(2,ISR), 0)
C
C CHECK DATA PRESENCE INDICATOR
C
            IF (DPFLAG.AND.DPI(IFRM).NE.0) SREF(2,ISR) = 0
C
C CHECK DATA QUALITY INDICATOR
C
            IF (DQFLAG.AND.DQI(IDQ).NE.0) SREF(2,ISR) = 0
C
            CALL MOVBIT( MAG, 248+IMAG  , 6, SREF(3,ISR), 6)
            CALL MOVBIT( MAG, 248+IMAG+8, 6, SREF(3,ISR), 0)
C
C CHECK DATA PRESENCE INDICATOR
C
            IF (DPFLAG.AND.DPI(IFRM).NE.0) SREF(3,ISR) = 0
C
C CHECK DATA QUALITY INDICATOR
C
            IF (DQFLAG.AND.DQI(IDQ).NE.0) SREF(3,ISR) = 0
C
C EXTRACT HIGH FIELD MAG WORDS
C
            IB=IB+1
C
C OUTBOARD HIGH FIELD MAG
C
            CALL MOVBIT( MAG, 72 +IMAG  , 6, OBHFM(1,IB), 6)
            CALL MOVBIT( MAG, 72 +IMAG+8, 6, OBHFM(1,IB), 0)
C
            CALL MOVBIT( MAG, 88 +IMAG  , 6, OBHFM(2,IB), 6)
            CALL MOVBIT( MAG, 88 +IMAG+8, 6, OBHFM(2,IB), 0)
C
            CALL MOVBIT( MAG, 104+IMAG  , 6, OBHFM(3,IB), 6)
            CALL MOVBIT( MAG, 104+IMAG+8, 6, OBHFM(3,IB), 0)
C
C INBOARD HIGH FIELD MAG
C
            CALL MOVBIT( MAG, 120+IMAG  , 6, IBHFM(1,IB), 6)
            CALL MOVBIT( MAG, 120+IMAG+8, 6, IBHFM(1,IB), 0)
C
            CALL MOVBIT( MAG, 136+IMAG  , 6, IBHFM(2,IB), 6)
            CALL MOVBIT( MAG, 136+IMAG+8, 6, IBHFM(2,IB), 0)
C
            CALL MOVBIT( MAG, 152+IMAG  , 6, IBHFM(3,IB), 6)
            CALL MOVBIT( MAG, 152+IMAG+8, 6, IBHFM(3,IB), 0)
C
  90  CONTINUE
C
C OUTPUT COUNTS
C
      IF (RECTEST) THEN
       DO I = 1,800
        K = (I-1)/2 + 1
        L = (I-1)/5 + 1
        MM = (I-1)/10 + 1
        WRITE(99,800) RECNUM,L,(PREF(J,L),J=1,3),I,(PDIFF(J,I),J=1,3),
     &                MM,(SREF(J,MM),J=1,3),K,(SDIFF(J,K),J=1,3)
       END DO
      END IF
C
      RETURN
  800 FORMAT(1X,I5,4(1X,I3,3(1X,I5)))
      END
