******************************************************************
*
*	TITLE:  UNPACK VOYAGER 750 WORD CR-3 EDR MAG SCIENCE BLOCK
*
*	FILE NAME:  MAGCR3.FOR
*
*	PURPOSE:  TO UNPACK THE MAG SCIENCE DATA
*
*	HISTORY:
*
*	AUTHOR            DATE            CHANGE
*	--------          ----            -------
*       S. B. KRAMER     02/05/96         ORIGINAL CODE
*                                         (MODE CR-3)
*
*	CALLING SEQUENCE:  SUBROUTINE MAGCR3()
*
*	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 MAGCR3()
C
C CR-3 MAG DATA REPRESENTS AN AVERAGED READOUT OF THE MAG INSTRUMENTS.
C BOTH INSTRUMENTS ARE SAMPLED AT A .06 SEC/SAMPLE RATE.  THE CR-3
C PRIMARY DATA STREAM CONSISTS OF .24 SECOND AVERAGES.  THE CR-3 SECONDARY
C DATA STREAM CONSISTS OF .96 SECOND AVERAGES.  THE CR-3 SCIENCE BLOCK IS 
C DIVIDED INTO 40 MINOR FRAMES (MF) COVERING A 96 SECOND PERIOD.  ODD 
C NUMBERED MF'S CONTAIN 10 PRIMARY MAG FULL WORD (12 BIT) TRIPLETS (X,Y,Z)
C AND 2 SECONDARY MAG FULL WORD (12 BIT) TRIPLETS (X,Y,Z). EVEN NUMBERED 
C MF'S CONTAIN 10 PRIMARY MAG FULL WORD (12 BIT) TRIPLETS (X,Y,Z) AND 
C 3 SECONDARY MAG FULL WORD (12 BIT) TRIPLETS (X,Y,Z). 
C
C MAG REFERENCE WORD FORMAT      
C
C BIT  15             0          
C      |______________|          
C      |              |          
C      +--------------+          
C
C BITS 15-12 ARE FILL            
C UNSIGNED INTEGER               
C
C READ 40 MINOR FRAMES OF CR-3 RECORD SCIENCE BLOCK.
C
      INTEGER*2 TIME(6)
      INTEGER*4 PRXINDO(10),PRYINDO(10),PRZINDO(10),
     &          PRXINDE(10),PRYINDE(10),PRZINDE(10),
     &          SRXINDO(10),SRYINDO(10),SRZINDO(10),
     &          SRXINDE(10),SRYINDE(10),SRZINDE(10),
     &          IMAG(40)
      LOGICAL*1 DQFLAG,DPFLAG,RECTEST
C
C BIT OFFSET OF MF'S
C
      DATA IMAG/    0,  576, 1200, 1776, 2400, 2976, 3600, 4176, 4800,
     &           5376, 6000, 6576, 7200, 7776, 8400, 8976, 9600,10176,
     &          10800,11376,12000,12576,13200,13776,14400,14976,15600,
     &          16176,16800,17376,18000,18576,19200,19776,20400,17376,
     &          21600,22176,22800,23376 /
C
C BIT OFFSET OF PRIMARY REFERENCE WORD OCCURANCES WITHIN ODD MF'S
C
      DATA PRXINDO/ 0,48, 96,144,240,288,336,384,480,528/  
      DATA PRYINDO/16,64,112,160,256,304,352,400,496,544/ 
      DATA PRZINDO/32,80,128,176,272,320,368,416,512,560/ 
C
C BIT OFFSET OF PRIMARY REFERENCE WORD OCCURANCES WITHIN EVEN MF'S
C
      DATA PRXINDE/ 0,48,144,192,240,288,384,432,480,528/
      DATA PRYINDE/16,64,160,208,256,304,400,448,496,544/
      DATA PRZINDE/32,80,176,224,272,320,416,464,512,560/ 
C
C BIT OFFSET OF SECONDARY REFERENCE WORD OCCURANCES WITHIN ODD MF'S
C
      DATA SRXINDO/192,432, -1, -1, -1, -1, -1, -1, -1, -1/
      DATA SRYINDO/208,448, -1, -1, -1, -1, -1, -1, -1, -1/
      DATA SRZINDO/224,464, -1, -1, -1, -1, -1, -1, -1, -1/
C
C BIT OFFSET OF SECONDARY REFERENCE WORD OCCURANCES WITHIN EVEN MF'S
C
      DATA SRXINDE/ 96,336,576, -1, -1, -1, -1, -1, -1, -1/
      DATA SRYINDE/112,352,592, -1, -1, -1, -1, -1, -1, -1/
      DATA SRZINDE/128,368,608, -1, -1, -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.
      IF ( SYS2(31) ) RECTEST = .TRUE.
C
C READ 40 MINOR FRAMES OF CR-3 RECORD SCIENCE BLOCK (3000 BYTES). 
C
      IPRX = 0
      IPRY = 0
      IPRZ = 0
      ISRX = 0
      ISRY = 0
      ISRZ = 0
      DO 200 J = 1,40
C
C STATUS CYCLE COUNTER (ONE STATUS CYCLE = 4 MF)
C
       ICYC = INT((J-1)/4) + 1
C
C DATA QUALITY STATUS WORD/INDICATOR COUNTER (ONE DQI PER 4 CR-3 MF)
C
       IDQ = INT((J-1)/4) + 1
C
       DO 100 K = 1,10
C
C EXTRACT PRIMARY REFERENCE WORDS (12 BIT) USING A PREDETERMINED OFFSET
C TABLE.
C
C PRIMARY X LFM REFERENCE WORD
C
        IPRX = IPRX + 1
        PREF(1,IPRX) = 0
        IF ( MOD(J,2).NE.0 ) THEN
         CALL MOVBIT(MAG, IMAG(J)+PRXINDO(K)  , 8, PREF(1,IPRX),  8)
         CALL MOVBIT(MAG, IMAG(J)+PRXINDO(K)+8, 8, PREF(1,IPRX),  0)
        ELSE
         CALL MOVBIT(MAG, IMAG(J)+PRXINDE(K)  , 8, PREF(1,IPRX),  8)
         CALL MOVBIT(MAG, IMAG(J)+PRXINDE(K)+8, 8, PREF(1,IPRX),  0)
        END IF
        IF ( DPFLAG .AND. DPI(J).NE.0 ) PREF(1,IPRX) = 0
        IF ( DQFLAG .AND. DQI(IDQ).NE.0 ) PREF(1,IPRX) = 0
C
C PRIMARY Y LFM REFERENCE WORD
C
        IPRY = IPRY + 1
        PREF(2,IPRY) = 0
        IF ( MOD(J,2).NE.0 ) THEN
         CALL MOVBIT(MAG, IMAG(J)+PRYINDO(K)  , 8, PREF(2,IPRY),  8)
         CALL MOVBIT(MAG, IMAG(J)+PRYINDO(K)+8, 8, PREF(2,IPRY),  0)
        ELSE
         CALL MOVBIT(MAG, IMAG(J)+PRYINDE(K)  , 8, PREF(2,IPRY),  8)
         CALL MOVBIT(MAG, IMAG(J)+PRYINDE(K)+8, 8, PREF(2,IPRY),  0)
        END IF
        IF ( DPFLAG .AND. DPI(J).NE.0 ) PREF(2,IPRY) = 0
        IF ( DQFLAG .AND. DQI(IDQ).NE.0 ) PREF(2,IPRY) = 0
C
C PRIMARY Z LFM REFERENCE WORD
C
        IPRZ = IPRZ + 1
        PREF(3,IPRZ) = 0
        IF ( MOD(J,2).NE.0 ) THEN
         CALL MOVBIT(MAG, IMAG(J)+PRZINDO(K)  , 8, PREF(3,IPRZ),  8)
         CALL MOVBIT(MAG, IMAG(J)+PRZINDO(K)+8, 8, PREF(3,IPRZ),  0)
        ELSE
         CALL MOVBIT(MAG, IMAG(J)+PRZINDE(K)  , 8, PREF(3,IPRZ),  8)
         CALL MOVBIT(MAG, IMAG(J)+PRZINDE(K)+8, 8, PREF(3,IPRZ),  0)
        END IF
        IF ( DPFLAG .AND. DPI(J).NE.0 ) PREF(3,IPRZ) = 0
        IF ( DQFLAG .AND. DQI(IDQ).NE.0 ) PREF(3,IPRZ) = 0
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 ( MOD(J,2).NE.0 .AND. SRXINDO(K).NE.-1 ) THEN
         ISRX = ISRX + 1
         SREF(1,ISRX) = 0
         CALL MOVBIT(MAG, IMAG(J)+SRXINDO(K)  , 8, SREF(1,ISRX),  8)
         CALL MOVBIT(MAG, IMAG(J)+SRXINDO(K)+8, 8, 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
        ELSE IF ( MOD(J,2).EQ.0 .AND. SRXINDE(K).NE.-1 ) THEN
         ISRX = ISRX + 1
         SREF(1,ISRX) = 0
         CALL MOVBIT(MAG, IMAG(J)+SRXINDE(K)  , 8, SREF(1,ISRX),  8)
         CALL MOVBIT(MAG, IMAG(J)+SRXINDE(K)+8, 8, 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 ( MOD(J,2).NE.0 .AND. SRYINDO(K).NE.-1 ) THEN
         ISRY = ISRY + 1
         SREF(2,ISRY) = 0
         CALL MOVBIT(MAG, IMAG(J)+SRYINDO(K)  , 8, SREF(2,ISRY),  8)
         CALL MOVBIT(MAG, IMAG(J)+SRYINDO(K)+8, 8, 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
        ELSE IF ( MOD(J,2).EQ.0 .AND. SRYINDE(K).NE.-1 ) THEN
         ISRY = ISRY + 1
         SREF(2,ISRY) = 0
         CALL MOVBIT(MAG, IMAG(J)+SRYINDE(K)  , 8, SREF(2,ISRY),  8)
         CALL MOVBIT(MAG, IMAG(J)+SRYINDE(K)+8, 8, 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 ( MOD(J,2).NE.0 .AND. SRZINDO(K).NE.-1 ) THEN
         ISRZ = ISRZ + 1
         SREF(3,ISRZ) = 0
         CALL MOVBIT(MAG, IMAG(J)+SRZINDO(K)  , 8, SREF(3,ISRZ),  8)
         CALL MOVBIT(MAG, IMAG(J)+SRZINDO(K)+8, 8, 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
        ELSE IF ( MOD(J,2).EQ.0 .AND. SRZINDE(K).NE.-1 ) THEN
         ISRZ = ISRZ + 1
         SREF(3,ISRZ) = 0
         CALL MOVBIT(MAG, IMAG(J)+SRZINDE(K)  , 8, SREF(3,ISRZ),  8)
         CALL MOVBIT(MAG, IMAG(J)+SRZINDE(K)+8, 8, 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
  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 I = 1,400
        IP = I
        IS = (I-1)/4 + 1
        ICYC = (I-1)/40 + 1
        IF ( I.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
