******************************************************************
*
*	TITLE:  UNPACK VOYAGER 1508 WORD CR-2 EDR MAG SCIENCE BLOCK
*
*	FILE NAME:  MAGCR2.FOR
*
*	PURPOSE:  TO UNPACK THE MAG SCIENCE DATA
*
*	HISTORY:
*
*	AUTHOR            DATE            CHANGE
*	--------          ----            -------
*       S. B. KRAMER     01/22/96         ORIGINAL CODE
*                                         (MODE CR-2)
*
*	CALLING SEQUENCE:  SUBROUTINE MAGCR2()
*
*	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
*
*	PDL:
*
*		CALL MOVBIT TO RECONSTRUCT THE MAG SCIENCE DATA INTO 
*		 I*2 VARIABLES
*		RETURN
*
******************************************************************* 
      SUBROUTINE MAGCR2()
C
C CR-2 MAG DATA REPRESENTS AN AVERAGED READOUT OF THE MAG INSTRUMENTS.
C BOTH INSTRUMENTS ARE SAMPLED AT A .06 SEC/SAMPLE RATE.  THE CR-2
C PRIMARY DATA STREAM CONSISTS OF .12 SECOND AVERAGES.  THE CR-2 SECONDARY
C DATA STREAM CONSISTS OF .24 SECOND AVERAGES.  THE CR-2 SCIENCE BLOCK 
C IS DIVIDED INTO 40 MINOR FRAMES (MF).  EACH MF CONTAINS 10 PRIMARY MAG 
C FULL WORD (12 BIT) TRIPLETS (X,Y,Z) AND 5 SECONDARY MAG FULL WORD (12 BIT) 
C TRIPLETS (X,Y,Z).  NOTE THAT EACH BYTE OF THE SCIENCE BLOCK CONTAINS TWO 
C FILL BITS AT THE MSB'S.  THE CR-2 EDR CONTAINS 5 STATUS WORDS.
C
      INTEGER*2 TIME(6)
      INTEGER*4 PRXIND(10),PRYIND(10),PRZIND(10)
      INTEGER*4 SRXIND(10),SRYIND(10),SRZIND(10)
      LOGICAL*1 DQFLAG,DPFLAG,RECTEST
C
C BIT OFFSET OF PRIMARY REFERENCE WORD OCCURANCES WITHIN 1 MF CYCLE
C
      DATA PRXIND/  0, 48,144,192,288,336,432,480,576,624/
      DATA PRYIND/ 16, 64,160,208,304,352,448,496,592,640/
      DATA PRZIND/ 32, 80,176,224,320,368,464,512,608,656/
C
C BIT OFFSET OF SECONDARY REFERENCE WORD OCCURANCES WITHIN 1 MF CYCLE
C
      DATA SRXIND/ 96,240,384,528,672, -1, -1, -1, -1, -1/
      DATA SRYIND/112,256,400,544,688, -1, -1, -1, -1, -1/
      DATA SRZIND/128,272,416,560,704, -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./
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 READ 40 MINOR FRAMES (90 BYTE EACH) OF CR-2 RECORD 
C SCIENCE BLOCK (6032 BYTES). 
C
      IPR = 0
      ISR = 0
      IPRX = 0
      IPRY = 0
      IPRZ = 0
      ISRX = 0
      ISRY = 0
      ISRZ = 0
      IMAG = -720
      DO 200 J = 1,40
C
C BIT OFFSET (720 BITS PER CR-2 MF)
C
       IMAG = IMAG + 720
C
       DO 100 K = 1,10
C
C STATUS CYCLE COUNTER (ONE STATUS CYCLE = 8 MF)
C
        ICYC = INT((J-1)/8) + 1
C
C DATA QUALITY STATUS WORD/INDICATOR COUNTER (10 - ONE DQI PER 4 CR-1 MF)
C
        IDQ = INT((J-1)/4) + 1
C
C EXTRACT PRIMARY REFERENCE WORDS (12 BIT) USING A PREDETERMINED OFFSET
C TABLE.
C
C PRIMARY X LFM REFERENCE WORD
C
        IF ( PRXIND(K).NE.-1 ) THEN
         IPRX = IPRX + 1
         PREF(1,IPRX) = 0
         CALL MOVBIT(MAG, IMAG+PRXIND(K)  , 6, PREF(1,IPRX),  6)
         CALL MOVBIT(MAG, IMAG+PRXIND(K)+8, 6, PREF(1,IPRX),  0)
         IF ( DPFLAG .AND. DPI(J).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 ( PRYIND(K).NE.-1 ) THEN
         IPRY = IPRY + 1
         PREF(2,IPRY) = 0
         CALL MOVBIT(MAG, IMAG+PRYIND(K)  , 6, PREF(2,IPRY),  6)
         CALL MOVBIT(MAG, IMAG+PRYIND(K)+8, 6, PREF(2,IPRY),  0)
         IF ( DPFLAG .AND. DPI(J).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 ( PRZIND(K).NE.-1 ) THEN
         IPRZ = IPRZ + 1
         PREF(3,IPRZ) = 0
         CALL MOVBIT(MAG, IMAG+PRZIND(K)  , 6, PREF(3,IPRZ),  6)
         CALL MOVBIT(MAG, IMAG+PRZIND(K)+8, 6, PREF(3,IPRZ),  0)
         IF ( DPFLAG .AND. DPI(J).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
C SECONDARY X LFM REFERENCE WORD
C
        IF ( SRXIND(K).NE.-1 ) THEN
         ISRX = ISRX + 1
         SREF(1,ISRX) = 0
         CALL MOVBIT(MAG, IMAG+SRXIND(K)  , 6, SREF(1,ISRX),  6)
         CALL MOVBIT(MAG, IMAG+SRXIND(K)+8, 6, SREF(1,ISRX),  0)
         IF ( DPFLAG .AND. DPI(J).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 ( SRYIND(K).NE.-1 ) THEN
         ISRY = ISRY + 1
         SREF(2,ISRY) = 0
         CALL MOVBIT(MAG, IMAG+SRYIND(K)  , 6, SREF(2,ISRY),  6)
         CALL MOVBIT(MAG, IMAG+SRYIND(K)+8, 6, SREF(2,ISRY),  0)
         IF ( DPFLAG .AND. DPI(J).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 ( SRZIND(K).NE.-1 ) THEN
         ISRZ = ISRZ + 1
         SREF(3,ISRZ) = 0
         CALL MOVBIT(MAG, IMAG+SRZIND(K)  , 6, SREF(3,ISRZ),  6)
         CALL MOVBIT(MAG, IMAG+SRZIND(K)+8, 6, SREF(3,ISRZ),  0)
         IF ( DPFLAG .AND. DPI(J).NE.0 ) SREF(3,ISRZ) = 0
         IF ( DQFLAG .AND. DQI(IDQ).NE.0 ) SREF(3,ISRZ) = 0
        END IF
C
C THE FIRST SECONDARY Z AXIS COUNT OF EVERY EIGHTH MINOR FRAME IS
C ACTUALLY A MAG STAT1 WORD.  REMOVE
C
        IF ( K.EQ.1 .AND. MOD(J-1,8).EQ.0 ) SREF(3,ISRZ) = 0
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 IP = 1,400
        IS = (IP-1)/2 + 1
        ICYC = (IP-1)/80 + 1
        IF ( IP.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,'(9(1X,I4),2(1X,Z4.4))')
     &  IP, PREF(1,IP), PREF(2,IP), PREF(3,IP),
     &  IS, SREF(1,IS), SREF(2,IS), SREF(3,IS),
     &  ICYC, STAT1(ICYC), STAT2(ICYC)
       END DO
      END IF
C
      RETURN
      END
