******************************************************************
*
*	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 CR-5 MASTER ROUTINE TO PRODUCE GAMMAS FOR BIGAVE

      INTEGER*2 TIME(6)
      LOGICAL*1 SROTATE,BROTATE
      REAL*4 GAMMA(3,2400),GAMMA2(3,600)

      INCLUDE 'UNPACK.INC'

      DATA SROTATE/.TRUE./,
     &     BROTATE/.TRUE./,
     &     ICALL/0/

      ICALL = ICALL + 1

C SENSOR ROTATION SWITCH

      IF ( SYS2(13) ) SROTATE = .FALSE.

C BOOM ALIGNMENT ROTATION SWITCH

      IF ( SYS2(23) ) BROTATE = .FALSE.

C EXTRACT INSTRUMENT STATUS FROM STAT WORDS

      CALL MAGSTATUS(TIME,12,12)

C RECOVER 12 BIT PREDICTED WORDS

      CALL DELTA(PDELTA,2400,PREF,120,PREC)
      CALL DELTA(SDELTA,600,SREF,60,SREC)

C ADJUST LEVEL OF RECONSTRUCTED PREDICT WORDS

      CALL SHIVOT(PREF,120,PREC,2400,SYS2(8))
      CALL SHIVOT(SREF,60,SREC,600,SYS2(8))

C CONVERT COUNTS INTO GAMMAS (MODE SENSITIVE)

      CALL MAKEGAMMAS(GAMMA,2400,GAMMA2,600,12,12,BAD,TIME)

C ORTHOGONALIZE DATA WITH SENSOR MATRIX AFTER GAMMAS (MODE SENSITIVE)

      IF ( SROTATE ) CALL SENSOR_ALIGN(GAMMA,GAMMA2,2400,600,12,BAD)

C ROTATE DATA WITH BOOM ALIGNMENT MATRIX AFTER GAMMAS (MODE SENSITIVE)

      IF ( BROTATE ) CALL BOOM_ALIGN(GAMMA,GAMMA2,2400,600,12,BAD)
C
      RETURN
      END
