******************************************************************
*
*	TITLE:  CONVERT RECONSTRUCTED DIGITAL VALUES INTO GAMMAS
*
*	FILE NAME:  CR5LFM.FOR
*
*	PURPOSE:  TO CONVERT DIGITAL CR-5 MAG SCIENCE DATA
*
*	HISTORY:
*
*	AUTHOR            DATE            CHANGE
*	--------          ----            -------
*       S. B. KRAMER     11/02/92         ORIGINAL CODE
*                                         (MODE CR-5)
*       S. B. KRAMER     02/21/96         ADD DELTA RECONSTRUCTION
*
*
*	COMMON AREAS:
*
*		SEE UNPACK.INC FOR COMMON AREA DEFINITIONS
*
*	NOTES:
*
*
*	PDL:
*
*	   CALL COMMAND AND STATUS WORD EXTRACTION ROUTINE
*          CALL DELTA MODULATION RECONSTRUCT ROUTINES
*	   CALL COUNTS TO GAMMAS CONVERSION ROUTINE
*	   CALL SENSOR ALIGNMENT ROUTINE
*	   CALL BOOM ALIGNMENT ROUTINE
*
*	RETURN
*
******************************************************************
      SUBROUTINE CR5LFM(GAMMA,GAMMA2,BAD,TIME)
C
C CR-5 MASTER ROUTINE TO PRODUCE GAMMAS FOR BIGAVE
C
      INTEGER*2 TIME(6)
      LOGICAL*1 SROTATE,BROTATE,INBOARD,BEFORE
      REAL*4 GAMMA(3,2400),GAMMA2(3,600)
C
      INCLUDE 'UNPACK.INC'
C
      DATA SROTATE/.TRUE./,
     &     BROTATE/.TRUE./,
     &     ICALL/0/
C
      ICALL = ICALL + 1
C
C SENSOR ROTATION SWITCH
C
      IF ( SYS2(13) ) SROTATE = .FALSE.
C
C BOOM ALIGNMENT ROTATION SWITCH
C
      IF ( SYS2(23) ) BROTATE = .FALSE.
C
C EXTRACT INSTRUMENT STATUS FROM STAT WORDS
C
      CALL CR5COMM(TIME)
C
C CHECK PRIME LFM STATUS
C
      IF ( PRIME(1).NE.PRIME(2) .OR.
     &     PRIME(2).NE.PRIME(3) .OR.
     &     PRIME(3).NE.PRIME(4) .OR.
     &     PRIME(4).NE.PRIME(5) .OR.
     &     PRIME(5).NE.PRIME(6) .OR.
     &     PRIME(6).NE.PRIME(7) .OR.
     &     PRIME(7).NE.PRIME(8) .OR.
     &     PRIME(8).NE.PRIME(9) .OR.
     &     PRIME(9).NE.PRIME(10) .OR.
     &     PRIME(10).NE.PRIME(11) .OR.
     &     PRIME(11).NE.PRIME(12) ) THEN
C
       WRITE(6,800) RECNUM
C
C CHANGE IN PRIMARY LFM STATUS DETECTED.  USE USER-SUPPLIED DEFAULT.
C
       INBOARD = PRIME(0)
C
      ELSE IF ( PRIME(1) .AND. PRIME(2) .AND. PRIME(3) .AND.
     &          PRIME(4) .AND. PRIME(5) .AND. PRIME(6) .AND.
     &          PRIME(7) .AND. PRIME(8) .AND. PRIME(9) .AND.
     &          PRIME(10) .AND. PRIME(11) .AND. PRIME(12) ) THEN
C
C INBOARD MAG IS PRIME
C
       INBOARD = .TRUE.
C
      ELSE
C
C OUTBOARD MAG IS PRIME
C
       INBOARD = .FALSE.
C
      END IF
C
C OVERRIDE PRIME LFM STATUS
C
      IF ( SYS2(24) ) INBOARD = .TRUE.
C
C RECOVER 12 BIT PREDICTED WORDS
C
      CALL DELTA(PDELTA,2400,PREC,PREF,120)
      CALL DELTA(SDELTA,600,SREC,SREF,60)
      CALL SHIVOT(PREF,120,PREC,2400)
      CALL SHIVOT(SREF,60,SREC,600)
C
      IF ( SYS2(31) ) THEN
       IF ( ICALL.EQ.1 )
     &  OPEN(80,FILE='COUNTS.DAT',STATUS='NEW',FORM='FORMATTED',
     &  CARRIAGECONTROL='LIST',RECORDTYPE='VARIABLE',RECL=8191)
       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,'(1X,''EDR TIME: '',6(1X,I3))') TIME
        END IF
        WRITE(80,'(25(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),
     &  IPDM, PREC(1,IPDM), PREC(2,IPDM), PREC(3,IPDM),
     &  ISDM, SDELTA(1,ISDM), SDELTA(2,ISDM), SDELTA(3,ISDM),
     &  IS, SREF(1,IS), SREF(2,IS), SREF(3,IS),
     &  ISDM, SREC(1,ISDM), SREC(2,ISDM), SREC(3,ISDM),
     &  ICYC,STAT1(ICYC),STAT2(ICYC)
       END DO
      END IF
C
C CONVERT COUNTS INTO GAMMAS (MODE SENSITIVE)
C
      CALL CR5CNT(GAMMA,GAMMA2,BAD,TIME,INBOARD)
C
      IF ( SROTATE ) THEN
C
C ORTHOGONALIZE DATA WITH SENSOR MATRIX AFTER GAMMAS (MODE SENSITIVE)
C
       IF ( .NOT.INBOARD ) THEN
        CALL TRANS31(GAMMA,3,2400,VSLOB,BAD)
        CALL TRANS31(GAMMA2,3,600,VSLIB,BAD)
       ELSE 
        CALL TRANS31(GAMMA,3,2400,VSLIB,BAD)
        CALL TRANS31(GAMMA2,3,600,VSLOB,BAD)
       END IF
C
      END IF
C
      IF ( BROTATE ) THEN
C
C ROTATE DATA WITH BOOM ALIGNMENT MATRIX AFTER GAMMAS (MODE SENSITIVE)
C
       IF ( .NOT.INBOARD ) THEN
        CALL TRANS31(GAMMA,3,2400,VBLOB,BAD)
        CALL TRANS31(GAMMA2,3,600,VBLIB,BAD)
       ELSE
        CALL TRANS31(GAMMA,3,2400,VBLIB,BAD)
        CALL TRANS31(GAMMA2,3,600,VBLOB,BAD)
       END IF
C
      END IF
C
      RETURN
  800 FORMAT(1X,'*CR5LFM*  PRIME LFM STATUS CHANGE IN RECORD ',I5)
      END
