******************************************************************
*
*	TITLE:  UNPACK VOYAGER 2400 WORD CR-1 EDR MAG SCIENCE BLOCK
*
*	FILE NAME:  MAGCR1.FOR
*
*	PURPOSE:  TO UNPACK THE MAG SCIENCE DATA
*
*	HISTORY:
*
*	AUTHOR            DATE            CHANGE
*	--------          ----            -------
*       S. B. KRAMER     10/03/95         ORIGINAL CODE
*                                         (MODE CR-1)
*
*	CALLING SEQUENCE:  SUBROUTINE MAGCR1()
*
*	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 MAGCR1()
C
C CR-1 MAG DATA REPRESENTS A DIRECT READOUT OF THE MAG INSTRUMENTS.
C BOTH INSTRUMENTS ARE SAMPLED AT A .06 SEC/SAMPLE RATE.  THE CR-1 
C SCIENCE BLOCK IS DIVIDED INTO 80 MINOR FRAMES (MF).  EACH MF 
C CONTAINS 10 PRIMARY MAG FULL WORD (12 BIT) TRIPLETS (X,Y,Z) AND 
C 10 SECONDARY MAG FULL WORD (12 BIT) TRIPLETS (X,Y,Z).  NOTE THAT 
C EACH BYTE OF THE SCIENCE BLOCK CONTAINS TWO FILL BITS AT THE MSB'S.
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, 96,192,288,384,480,576,672,768,864/
      DATA PRYIND/ 16,112,208,304,400,496,592,688,784,880/
      DATA PRZIND/ 32,128,224,320,416,512,608,704,800,896/
C
C BIT OFFSET OF SECONDARY REFERENCE WORD OCCURANCES WITHIN 1 MF CYCLE
C
      DATA SRXIND/ 48,144,240,336,432,528,624,720,816,912/
      DATA SRYIND/ 64,160,256,352,448,544,640,736,832,928/
      DATA SRZIND/ 80,176,272,368,464,560,656,752,848,944/
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 80 MINOR FRAMES (120 BYTE EACH) OF CR-1 RECORD 
C SCIENCE BLOCK (9600 BYTES). 
C
      IPR = 0
      ISR = 0
      IPRX = 0
      IPRY = 0
      IPRZ = 0
      ISRX = 0
      ISRY = 0
      ISRZ = 0
      IMAG = -960
      DO 200 J = 1,80
C
C BIT OFFSET ( 960 BITS PER CR-1 MF)
C
       IMAG = IMAG + 960
C
       DO 100 K = 1,10
C
C STATUS CYCLE COUNTER (ONE STATUS CYCLE = 10 MF)
C
        ICYC = INT((J-1)/10) + 1
C
C DATA QUALITY STATUS WORD/INDICATOR COUNTER (ONE DQI PER 8 CR-1 MF)
C
        IDQ = INT((J-1)/8) + 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
  100  CONTINUE
C
  200 CONTINUE
C
C OUTPUT COUNTS AND STATUS VALUES
C
      IF ( RECTEST ) THEN
       DO IP = 1,800
        IS = IP
        ICYC = (IP-1)/100 + 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
