******************************************************************
*
*	TITLE:  UNPACK VOYAGER 720 WORD CR-4 EDR MAG SCIENCE BLOCK
*
*	FILE NAME:  MAGCR4.FOR
*
*	PURPOSE:  TO UNPACK THE MAG SCIENCE DATA
*
*	HISTORY:
*
*	AUTHOR            DATE            CHANGE
*	--------          ----            -------
*       S. B. KRAMER     01/25/96         ORIGINAL CODE
*                                         (MODE CR-4)
*
*	CALLING SEQUENCE:  SUBROUTINE MAGCR4()
*
*	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 diff   6 bits each
*				S diff   6 bits each
*
*	PDL:
*
*		CALL MOVBIT TO RECONSTRUCT THE MAG SCIENCE DATA INTO 
*		 I*2 VARIABLES
*		RETURN
*
******************************************************************* 
      SUBROUTINE MAGCR4()
C
C CR-4 MAG DATA REPRESENTS AN AVERAGED READOUT OF THE MAG INSTRUMENTS.
C BOTH INSTRUMENTS ARE SAMPLED AT A .06 SEC/SAMPLE RATE.  THE CR-4
C PRIMARY DATA STREAM CONSISTS OF .30 SECOND AVERAGES.  THE CR-4 SECONDARY
C DATA STREAM CONSISTS OF 2.4 SECOND AVERAGES.  THE CR-4 SCIENCE BLOCK 
C IS DIVIDED INTO 40 MINOR FRAMES (MF).  THE FIRST 2 MF'S CONSTITUTE A
C CYCLE THAT OCCURS 20 TIMES.  NOTE THAT EACH BYTE OF THE SCIENCE 
C BLOCK CONTAINS TWO FILL BITS AT THE MSB'S.  THE CR-4 EDR CONTAINS 20 
C STATUS WORDS.
C
      INTEGER*2 TIME(6)
C
      INTEGER*4 PRXIND(32),PRYIND(32),PRZIND(32),
     &          SRXIND(32),SRYIND(32),SRZIND(32),
     &          PDXIND(32),PDYIND(32),PDZIND(32),
     &          SDXIND(32),SDYIND(32),SDZIND(32)
C
      LOGICAL*1 DQFLAG,DPFLAG,RECTEST
C
C BIT OFFSET OF PRIMARY REFERENCE WORD OCCURANCES WITHIN A 2 MF CYCLE
C
      DATA PRXIND/ 40,616, -1, -1, -1, -1, -1, -1, -1, -1,
     &             -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &             -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &             -1, -1 /
      DATA PRYIND/ 64,640, -1, -1, -1, -1, -1, -1, -1, -1,
     &             -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &             -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &             -1, -1 /
      DATA PRZIND/ 96,672, -1, -1, -1, -1, -1, -1, -1, -1,
     &             -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &             -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &             -1, -1 /
C
C BIT OFFSET OF PRIMARY REFERENCE WORD OCCURANCES WITHIN A 2 MF CYCLE
C
      DATA SRXIND/ 288, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &              -1, -1 /
      DATA SRYIND/ 320, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &              -1, -1 /
      DATA SRZIND/ 352, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &              -1, -1 /
C
C BIT OFFSET OF PRIMARY DIFFERENCE WORD OCCURANCES WITHIN A 2 MF CYCLE
C
      DATA PDXIND/  32, 112, 136, 160, 184, 208, 232, 256, 368, 392,
     &             416, 440, 464, 488, 512, 536, 608, 688, 712, 736,
     &             760, 784, 808, 832, 880, 904, 928, 952, 976,1000,
     &            1024,1048 /
      DATA PDYIND/  56, 120, 144, 168, 192, 216, 240, 264, 376, 400,
     &             424, 448, 472, 496, 520, 544, 632, 696, 720, 744,
     &             768, 792, 816, 840, 888, 912, 936, 960, 984,1008,
     &            1032,1056 /
      DATA PDZIND/  80, 128, 152, 176, 200, 224, 248, 272, 384, 408,
     &             432, 456, 480, 504, 528, 576, 656, 704, 728, 752,
     &             776, 800, 824, 848, 896, 920, 944, 968, 992,1016,
     &            1040,1064 /
C
C BIT OFFSET OF SECONDARY DIFFERENCE WORD OCCURANCES WITHIN A 2 MF CYCLE
C
      DATA SDXIND/ 280,584,856,1072,-1, -1, -1, -1, -1, -1,
     &              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &              -1, -1 /
      DATA SDYIND/ 304,592,864,1080,-1, -1, -1, -1, -1, -1,
     &              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &              -1, -1 /
      DATA SDZIND/ 336,600,872,1088,-1, -1, -1, -1, -1, -1,
     &              -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
     &              -1, -1, -1, -1, -1, -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./ ! 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 REFERENCE WORD FORMAT      MAG DIFFERENCE WORD FORMAT
C
C BIT  15             0          BIT  7       0
C      |______________|               |_______|
C      |              |               |       |
C      +--------------+               +-------+
C 
C BITS 15-12 ARE FILL            BITS 7 AND 6 ARE FILL           
C UNSIGNED INTEGER               BIT 5 IS SIGN BIT
C
C READ 40 MINOR FRAMES OF CR-4 RECORD SCIENCE BLOCK. 
C
      IPRX = 0
      IPRY = 0
      IPRZ = 0
      IPDX = 0
      IPDY = 0
      IPDZ = 0
      ISRX = 0
      ISRY = 0
      ISRZ = 0
      ISDX = 0
      ISDY = 0
      ISDZ = 0
      IMAG = -1152
C
C READ 2 MF CYCLE FOR 20 OCCURANCES
C
      DO 200 J = 1,20
C
C BIT OFFSET ( 1152 BITS PER CR-4 MF)
C
       IMAG = IMAG + 1152
C
C EXTRACT VALUES FROM 1152 BIT 2 MF CYCLE
C
       DO 100 K = 1,32
C
C STATUS CYCLE COUNTER (20 TOTAL - ONE STATUS CYCLE = 2 MF)
C
        ICYC = J
C
C DATA QUALITY STATUS WORD/INDICATOR COUNTER (10 - ONE DQI PER 4 CR-1 MF)
C
        IDQ = (J-1)/2 + 1
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
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)  , 8, PREF(1,IPRX),  8)
         CALL MOVBIT(MAG, IMAG+PRXIND(K)+8, 8, 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)  , 8, PREF(2,IPRY),  8)
         CALL MOVBIT(MAG, IMAG+PRYIND(K)+8, 8, 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)  , 8, PREF(3,IPRZ),  8)
         CALL MOVBIT(MAG, IMAG+PRZIND(K)+8, 8, 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
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C PRIMARY X LFM DIFFERENCE 
C
        IF ( PDXIND(K).NE.-1 ) THEN
         IPDX = IPDX + 1
         PDIFF(1,IPDX) = 0
         CALL MOVBIT(MAG, IMAG+PDXIND(K), 6, PDIFF(1,IPDX), 0)
         IF ( PDIFF(1,IPDX).GE.32 ) PDIFF(1,IPDX) = PDIFF(1,IPDX) - 64
         IF ( DPFLAG .AND. DPI(J).NE.0 ) PDIFF(1,IPDX) = 255
         IF ( DQFLAG .AND. DQI(IDQ).NE.0 ) PDIFF(1,IPDX) = 255
        END IF
C
C PRIMARY Y LFM DIFFERENCE WORD
C
        IF ( PDYIND(K).NE.-1 ) THEN
         IPDY = IPDY + 1
         PDIFF(2,IPDY) = 0
         CALL MOVBIT(MAG, IMAG+PDYIND(K), 6, PDIFF(2,IPDY), 0)
         IF ( PDIFF(2,IPDY).GE.32 ) PDIFF(2,IPDY) = PDIFF(2,IPDY) - 64
         IF ( DPFLAG .AND. DPI(J).NE.0 ) PDIFF(2,IPDY) = 255
         IF ( DQFLAG .AND. DQI(IDQ).NE.0 ) PDIFF(2,IPDY) = 255
        END IF
C
C PRIMARY Z LFM DIFFERENCE WORD
C
        IF ( PDZIND(K).NE.-1 ) THEN
         IPDZ = IPDZ + 1
         PDIFF(3,IPDZ) = 0
         CALL MOVBIT(MAG, IMAG+PDZIND(K), 6, PDIFF(3,IPDZ), 0)
         IF ( PDIFF(3,IPDZ).GE.32 ) PDIFF(3,IPDZ) = PDIFF(3,IPDZ) - 64
         IF ( DPFLAG .AND. DPI(J).NE.0 ) PDIFF(3,IPDZ) = 255
         IF ( DQFLAG .AND. DQI(IDQ).NE.0 ) PDIFF(3,IPDZ) = 255
        END IF
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
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)  , 8, SREF(1,ISRX),  8)
         CALL MOVBIT(MAG, IMAG+SRXIND(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 ( SRYIND(K).NE.-1 ) THEN
         ISRY = ISRY + 1
         SREF(2,ISRY) = 0
         CALL MOVBIT(MAG, IMAG+SRYIND(K)  , 8, SREF(2,ISRY),  8)
         CALL MOVBIT(MAG, IMAG+SRYIND(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 ( SRZIND(K).NE.-1 ) THEN
         ISRZ = ISRZ + 1
         SREF(3,ISRZ) = 0
         CALL MOVBIT(MAG, IMAG+SRZIND(K)  , 8, SREF(3,ISRZ),  8)
         CALL MOVBIT(MAG, IMAG+SRZIND(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
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C SECONDARY X LFM DIFFERENCE 
C
        IF ( SDXIND(K).NE.-1 ) THEN
         ISDX = ISDX + 1
         SDIFF(1,ISDX) = 0
         CALL MOVBIT(MAG, IMAG+SDXIND(K), 6, SDIFF(1,ISDX), 0)
         IF ( SDIFF(1,ISDX).GE.32 ) SDIFF(1,ISDX) = SDIFF(1,ISDX) - 64
         IF ( DPFLAG .AND. DPI(J).NE.0 ) SDIFF(1,ISDX) = 255
         IF ( DQFLAG .AND. DQI(IDQ).NE.0 ) SDIFF(1,ISDX) = 255
        END IF
C
C SECONDARY Y LFM DIFFERENCE WORD
C
        IF ( SDYIND(K).NE.-1 ) THEN
         ISDY = ISDY + 1
         SDIFF(2,ISDY) = 0
         CALL MOVBIT(MAG, IMAG+SDYIND(K), 6, SDIFF(2,ISDY), 0)
         IF ( SDIFF(2,ISDY).GE.32 ) SDIFF(2,ISDY) = SDIFF(2,ISDY) - 64
         IF ( DPFLAG .AND. DPI(J).NE.0 ) SDIFF(2,ISDY) = 255
         IF ( DQFLAG .AND. DQI(IDQ).NE.0 ) SDIFF(2,ISDY) = 255
        END IF
C
C SECONDARY Z LFM DIFFERENCE WORD
C
        IF ( SDZIND(K).NE.-1 ) THEN
         ISDZ = ISDZ + 1
         SDIFF(3,ISDZ) = 0
         CALL MOVBIT(MAG, IMAG+SDZIND(K), 6, SDIFF(3,ISDZ), 0)
         IF ( SDIFF(3,ISDZ).GE.32 ) SDIFF(3,ISDZ) = SDIFF(3,ISDZ) - 64
         IF ( DPFLAG .AND. DPI(J).NE.0 ) SDIFF(3,ISDZ) = 255
         IF ( DQFLAG .AND. DQI(IDQ).NE.0 ) SDIFF(3,ISDZ) = 255
        END IF
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
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,640
        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
        IPR = (I-1)/16 + 1
        ISR = (I-1)/32 + 1
        ISD = (I-1)/8 + 1
        ICYC = ISR
        WRITE(80,'(16(1X,I4),2(1X,Z4.4))')
     &  I, PDIFF(1,I), PDIFF(2,I), PDIFF(3,I),
     &  IPR, PREF(1,IPR), PREF(2,IPR), PREF(3,IPR),
     &  ISD, SDIFF(1,ISD),SDIFF(2,ISD),SDIFF(3,ISD),
     &  ISR, SREF(1,ISR), SREF(2,ISR), SREF(3,ISR),
     &  STAT1(ICYC),STAT2(ICYC)
       END DO
      END IF
C
      RETURN
      END
