******************************************************************
*
*	TITLE:  UNPACK VOYAGER 1080 WORD CR-5 EDR MAG SCIENCE BLOCK
*
*	FILE NAME:  MAGCR5.FOR
*
*	PURPOSE:  TO UNPACK THE MAG SCIENCE DATA
*
*	HISTORY:
*
*	AUTHOR            DATE            CHANGE
*	--------          ----            -------
*       S. B. KRAMER     09/26/92         ORIGINAL CODE
*                                         (MODE CR-5)
*       S. B. KRAMER     11/17/95         ADD DELTA MODULATION PROCESSING
*
*	CALLING SEQUENCE:  SUBROUTINE MAGCR5()
*
*	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     6 bits each
*                               S dm     6 bits each
*
*	PDL:
*
*		CALL MOVBIT TO RECONSTRUCT THE MAG SCIENCE DATA INTO 
*		 I*2 VARIABLES
*		RETURN
*
******************************************************************* 
      SUBROUTINE MAGCR5()
C
      INTEGER*2 TIME(6)
      INTEGER*4 PDXIND(125),PDYIND(125),PDZIND(125),
     &          PRXIND(5),PRYIND(5),PRZIND(5),
     &          SRXIND(5),SRYIND(5),SRZIND(5),
     &          PDMIND(40),SDMIND(40)
      LOGICAL*1 DQFLAG,DPFLAG,RECTEST
C
C BIT OFFSET OF PRIMARY REFERENCE WORD OCCURANCES WITHIN 5 MF CYCLE
C
      DATA PRXIND/  0,304, -1, -1, -1/
      DATA PRYIND/ 16,320, -1, -1, -1/
      DATA PRZIND/ 32,336, -1, -1, -1/
C
C BIT OFFSET OF SECONDARY REFERENCE WORD OCCURANCES WITHIN 5 MF CYCLE
C
      DATA SRXIND/ 80, -1, -1, -1, -1/
      DATA SRYIND/ 96, -1, -1, -1, -1/
      DATA SRZIND/112, -1, -1, -1, -1/
C
C BIT OFFSET OF PRIMARY DELTA MODULATED WORD OCCURANCES WITHIN 5 MF CYCLE
C
      DATA PDMIND/ 48, 56, 64, 72,136,
     &            144,152,160,176,184,
     &            192,200,216,224,232,
     &            240,256,264,272,280,
     &            352,360,368,376,392,
     &            400,408,416,432,440,
     &            448,456,472,480,488,
     &            496,512,520,528,536/
C
C BIT OFFSET OF PRIMARY DELTA MODULATED WORD OCCURANCES WITHIN 5 MF CYCLE
C
      DATA SDMIND/128,168,208,248,288,
     &            384,424,464,504,544,
     &             -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 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 MAG REFERENCE WORD FORMAT      MAG DELTA WORD FORMAT
C
C BIT  15             0          BIT  7       0
C      |______________|               |_______|
C      |              |               |       |
C      +--------------+               +-------+
C
C BITS 15-12 ARE FILL            BITS 5-4 = X DM
C UNSIGNED INTEGER               BITS 3-2 = Y DM
C                                BITS 1-0 = Z DM
C
C
C READ 60 MINOR FRAMES (72 BYTE EACH) OF CR-5 RECORD (4320 BYTES). 
C
      IPR = 0
      ISR = 0
      IPRX = 0
      IPRY = 0
      IPRZ = 0
      ISRX = 0
      ISRY = 0
      ISRZ = 0
      IPDM = 0
      ISDM = 0
      IMAG = -576
      IFRM = 0
      DO 200 J = 1,60
C
C BIT OFFSET (576 BITS PER CR-5 MF)
C
       IMAG = IMAG + 576
C
C MINOR FRAME COUNTER (60 MF PER CR-5 SCIENCE BLOCK)
C
       IFRM = IFRM + 1
C
C STATUS CYCLE COUNTER (12 TOTAL - ONE STATUS CYCLE = 5 MF)
C
       ICYC = (IFRM-1)/5 + 1
C
C DATA QUALITY STATUS WORD/INDICATOR COUNTER (ONE DQI PER 30 CR-5 MF)
C
       IDQ = INT((IFRM-1)/30) + 1
C
C GET 12 BIT REFERENCE WORDS
C
       DO 100 K = 1,2
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)  , 8, PREF(1,IPRX),  8)
         CALL MOVBIT(MAG, IMAG+PRXIND(K)+8, 8, 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 ( 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(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 ( 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(IFRM).NE.0 ) PREF(3,IPRZ) = 0
         IF ( DQFLAG.AND.DQI(IDQ).NE.0 ) 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)
        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)  , 8, SREF(1,ISRX),  8)
         CALL MOVBIT(MAG, IMAG+SRXIND(K)+8, 8, 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 ( 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(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 ( 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(IFRM).NE.0 ) SREF(3,ISRZ) = 0
         IF ( DQFLAG.AND.DQI(IDQ).NE.0 ) 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)
        END IF
C
  100  CONTINUE
C
C GET 2 BIT DELTA MODULATED WORDS
C
       DO 150 L = 1,40
C
C EXTRACT PRIMARY DELTA MODULATED WORDS (2 BIT) USING A PREDETERMINED 
C OFFSET TABLE.
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
  150  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,2400
        ISDM = (IPDM-1)/4 + 1
        IP = (IPDM-1)/20 + 1
        IS = (IPDM-1)/40 + 1
        ICYC = (IPDM-1)/200 + 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
