******************************************************************
*
*	TITLE:  UNPACK VOYAGER 814 WORD CR-6 EDR MAG SCIENCE BLOCK
*
*	FILE NAME:  MAGCR6.FOR
*
*	PURPOSE:  TO UNPACK THE MAG SCIENCE DATA
*
*	HISTORY:
*
*	AUTHOR            DATE            CHANGE
*	--------          ----            -------
*       S. B. KRAMER     03/20/95         ORIGINAL CODE
*                                         (MODE CR-6)
*
*	CALLING SEQUENCE:  SUBROUTINE MAGCR6()
*
*	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 ref	12 bits each
*				S ref	12 bits each
*                               P dm     2 bits each
*                               S dm     2 bits each
*
*	PDL:
*
*		CALL MOVBIT TO RECONSTRUCT THE MAG SCIENCE DATA INTO 
*		 I*2 VARIABLES
*		RETURN
*
******************************************************************* 
      SUBROUTINE MAGCR6()
C
C EACH CR-6 SCIENCE BLOCK CONTAINS 75 MINOR FRAMES (MF'S) EACH OF 152 
C BIT SIZE.  THERE IS ONE PRIMARY AND ONE SECONDARY REFERENCE WORD FROM
C ONE MAG AXIS PER MF.  THREE MINOR FRAMES ARE NEEDED TO ACQUIRE ONE
C SET OF X,Y,Z MAG VALUES.
C
      INTEGER*2 PDMIND(10),SDMIND(10),TIME(6)
      LOGICAL*1 DQFLAG,DPFLAG,RECTEST
C
C BIT OFFSET OF PRIMARY DELTA MODULATED WORD OCCURANCES 
C
      DATA PDMIND/ 32, 40, 56, 64, 80, 88,104,112,128,136/
C
C BIT OFFSET OF PRIMARY DELTA MODULATED WORD OCCURANCES 
C
      DATA SDMIND/ 48, 72, 96,120,144, -1, -1, -1, -1, -1/
C
C DATA PRESENCE SWITCH FLAG
C
      DATA DPFLAG/.FALSE./
C
C DATA QUALITY SWITCH FLAG
C
      DATA DQFLAG/.FALSE./
C
C DATA OUTPUT FLAG
C
      DATA RECTEST/.FALSE./ ! for development testing only
C
      DATA ICALL/0/
C
      INCLUDE 'UNPACK.INC'
C
      IF ( DRSDAT.NE.35 ) RETURN
C
      ICALL = ICALL + 1
      IF ( .NOT.SYS2(10) ) DPFLAG = .TRUE.
      IF ( .NOT.SYS2(11) ) DQFLAG = .TRUE.
C
C MAG WORD FORMAT        MAG DELTA WORD FORMAT
C
C BIT  7       0          BIT  7       0
C      |_______|               |_______|
C      |       |               |       |
C      +-------+               +-------+
C
C BITS 7-6 ARE FILL           BITS 5-4 = X DM
C BITS 5-0 CONTAIN            BITS 3-2 = Y DM
C EITHER HALF OF              BITS 1-0 = Z DM
C REFERENCE WORD
C OR X,Y,Z DM WORD
C
C 12 BIT REFERENCE WORDS SPLIT INTO TWO 8 BIT MAG WORDS.
C
C READ 75 MINOR FRAMES (19 BYTES EACH) OF CR-6 MAG BLOCK (3256 BYTES). 
C IGNORE TRAILING SPARE BYTES (1831).
C
      IPRX = 0
      IPRY = 0
      IPRZ = 0
      ISRX = 0
      ISRY = 0
      ISRZ = 0
      IPDM = 0
      ISDM = 0
      IMAG = -152
      DO 200 J = 1,75,3
C
C NEED 3 MF TO GET X,Y,Z AXIS (PRIMARY AND SECONDARY REFS)
C
       DO 100 K = 1,3
C
C BIT OFFSET (152 BITS PER CR-6 MF)
C
        IMAG = IMAG + 152
C
C MINOR FRAME COUNTER (75 MF PER CR-6 SCIENCE BLOCK)
C
        IFRM = J + K - 1
C
C DATA QUALITY STATUS WORD/INDICATOR COUNTER (ONE DQI PER 15 CR-6 MF)
C
        IDQ = (IFRM-1)/15 + 1
C
C PRIMARY X LFM REFERENCE WORD 
C
        IF ( K.EQ.1 ) THEN
         IPRX = IPRX + 1
         PREF(1,IPRX) = 0
         CALL MOVBIT(MAG, IMAG  , 6, PREF(1,IPRX),  6)
         CALL MOVBIT(MAG, IMAG+8, 6, 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
C PRIMARY Y LFM REFERENCE WORD
C
        IF ( K.EQ.2 ) THEN
         IPRY = IPRY + 1
         PREF(2,IPRY) = 0
         CALL MOVBIT(MAG, IMAG  , 6, PREF(2,IPRY),  6)
         CALL MOVBIT(MAG, IMAG+8, 6, 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
C PRIMARY Z LFM REFERENCE WORD
C
        IF ( K.EQ.3 ) THEN
         IPRZ = IPRZ + 1
         PREF(3,IPRZ) = 0
         CALL MOVBIT(MAG, IMAG  , 6, PREF(3,IPRZ),  6)
         CALL MOVBIT(MAG, IMAG+8, 6, PREF(3,IPRZ),  0)
c         write(6,'(1x,''primary x,y,z ref  :  '',3(i4,3x))') 
c     &    pref(1,iprx),pref(2,ipry),pref(3,iprz)
         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 SECONDARY X LFM REFERENCE WORD
C
        IF ( K.EQ.1 ) THEN
         ISRX = ISRX + 1
         SREF(1,ISRX) = 0
         CALL MOVBIT(MAG, IMAG+16, 6, SREF(1,ISRX),  6)
         CALL MOVBIT(MAG, IMAG+24, 6, 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
C SECONDARY Y LFM REFERENCE WORD
C
        IF ( K.EQ.2 ) THEN
         ISRY = ISRY + 1
         SREF(2,ISRY) = 0
         CALL MOVBIT(MAG, IMAG+16, 6, SREF(2,ISRY),  6)
         CALL MOVBIT(MAG, IMAG+24, 6, 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
C SECONDARY Z LFM REFERENCE WORD
C
        IF ( K.EQ.3 ) THEN
         ISRZ = ISRZ + 1
         SREF(3,ISRZ) = 0
         CALL MOVBIT(MAG, IMAG+16, 6, SREF(3,ISRZ),  6)
         CALL MOVBIT(MAG, IMAG+24, 6, SREF(3,ISRZ),  0)
c         write(6,'(1x,''secondary x,y,z ref:  '',3(i4,3x))') 
c     &    sref(1,isrx),sref(2,isry),sref(3,isrz)
         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 UNPACK DELTA MODULATED WORDS
C
        DO L = 1,10
C
C PRIMARY LFM DELTA MODULATED WORDS
C
         IF ( PDMIND(L).NE.-1 ) THEN
          IPDM = IPDM + 1
          PDELTA(1,IPDM) = 0
          PDELTA(2,IPDM) = 0
          PDELTA(3,IPDM) = 0
          CALL MOVBIT(MAG, IMAG+PDMIND(L)+4, 2, PDELTA(1,IPDM),  0)
          CALL MOVBIT(MAG, IMAG+PDMIND(L)+2, 2, PDELTA(2,IPDM),  0)
          CALL MOVBIT(MAG, IMAG+PDMIND(L)  , 2, PDELTA(3,IPDM),  0)
c          write(6,'(1x,''primary x,y,z DM:  '',3(i2,3x))')
c     &     pdelta(1,ipdm),pdelta(2,ipdm),pdelta(3,ipdm)
         END IF
C
C SECONDARY LFM DELTA MODULATED WORDS
C
         IF ( SDMIND(L).NE.-1 ) THEN
          ISDM = ISDM + 1
          SDELTA(1,ISDM) = 0
          SDELTA(2,ISDM) = 0
          SDELTA(3,ISDM) = 0
          CALL MOVBIT(MAG, IMAG+SDMIND(L)+4, 2, SDELTA(1,ISDM),  0)
          CALL MOVBIT(MAG, IMAG+SDMIND(L)+2, 2, SDELTA(2,ISDM),  0)
          CALL MOVBIT(MAG, IMAG+SDMIND(L)  , 2, SDELTA(3,ISDM),  0)
c          write(6,'(1x,''secondary x,y,z DM:  '',3(i2,3x))')
c     &     sdelta(1,isdm),sdelta(2,isdm),sdelta(3,isdm)
         END IF
C
        END DO
C
  100  CONTINUE
C
  200 CONTINUE
C
C OUTPUT COUNTS AND STATUS VALUES
C
      IF ( RECTEST ) THEN
       IF ( ICALL.EQ.1 ) OPEN(80,FILE='COUNTS.DAT',STATUS='NEW',
     & FORM='FORMATTED',CARRIAGECONTROL='LIST')
       DO IPDM = 1,750
        ISDM = (IPDM-1)/2 + 1
        IP = (IPDM-1)/30 + 1
        IS = (IPDM-1)/30 + 1
        ICYC = (IPDM-1)/75 + 1
        IF ( IPDM.EQ.1 ) THEN
         TIME(1) = YEAR3
         CALL CONHOUR(SCETHR,TIME)
         CALL CONSEC(SCETSC,TIME)
         TIME(6) = SCETML
         WRITE(80,'(''RECORD NUMBER: '',I5.5,''   TIME: '',
     &         I2,1X,I3,3(1X,I2.2),1X,I3.3)') RECNUM,TIME
        END IF
        WRITE(80,'(17(1X,I4),2(1X,Z4.4))')
     &  IPDM, PDELTA(1,IPDM),PDELTA(2,IPDM),PDELTA(3,IPDM),
     &  IP, PREF(1,IP), PREF(2,IP), PREF(3,IP),
     &  ISDM,SDELTA(1,ISDM),SDELTA(2,ISDM),SDELTA(3,ISDM),
     &  IS, SREF(1,IS), SREF(2,IS), SREF(3,IS),
     &  ICYC,STAT1(ICYC),STAT2(ICYC)
       END DO
      END IF
C
      RETURN
      END
