******************************************************************
*
*	TITLE:  CONVERT RECONSTRUCTED DIGITAL VALUES INTO GAMMAS
*
*	FILE NAME:  CR4LFM.FOR
*
*	PURPOSE:  TO CONVERT DIGITAL CR-4 MAG SCIENCE DATA
*
*	HISTORY:
*
*	AUTHOR            DATE            CHANGE
*	--------          ----            -------
*       S. B. KRAMER     03/25/96         ORIGINAL CODE
*                                         (MODE CR-4)
*
*
*	COMMON AREAS:
*
*		SEE UNPACK.INC FOR COMMON AREA DEFINITIONS
*
*	NOTES:
*
*
*	PDL:
*
*	   CALL COMMAND AND STATUS WORD EXTRACTION ROUTINE
*          CALL DIFFERENCE WORD RECONTRUCTION ROUTINE
*	   CALL COUNTS TO GAMMAS CONVERSION ROUTINE
*	   CALL SENSOR ALIGNMENT ROUTINE
*	   CALL BOOM ALIGNMENT ROUTINE
*
*	RETURN
*
******************************************************************
      SUBROUTINE CR4LFM(GAMMA,GAMMA2,BAD,TIME)

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

      INCLUDE 'UNPACK.INC'

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

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,20,4)

C RECONSTRUCT PRIMARY FULL WORDS FROM DIFFERENCE WORDS

      CALL DIFFERENCE(PDIFF,640,PREF,40,PREC)

C RECONSTRUCT SECONDARY FULL WORDS FROM DIFFERENCE WORDS

      CALL DIFFERENCE(SDIFF,80,SREF,20,SREC)

C CONVERT COUNTS INTO GAMMAS (MODE SENSITIVE)

      CALL MAKEGAMMAS(GAMMA,640,GAMMA2,80,20,4,BAD,TIME)

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

      IF ( SROTATE ) CALL SENSOR_ALIGN(GAMMA,GAMMA2,640,80,4,BAD)

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

      IF ( BROTATE ) CALL BOOM_ALIGN(GAMMA,GAMMA2,640,80,4,BAD)
C
      RETURN
      END
