******************************************************************
*
*	TITLE:  CONVERT RECONSTRUCTED DIGITAL VALUES INTO GAMMAS
*
*	FILE NAME:  VIM5LFM.FOR
*
*	PURPOSE:  TO CONVERT DIGITAL VIM-5 MAG SCIENCE DATA
*
*	HISTORY:
*
*	AUTHOR            DATE            CHANGE
*	--------          ----            -------
*       S. B. KRAMER     11/02/92         ORIGINAL CODE
*                                         (MODE VIM-5)
*                        04/26/95         MOD TO ACCEPT LFM FLIP MATRICES
*
*	COMMON AREAS:
*
*		SEE UNPACK.INC FOR COMMON AREA DEFINITIONS
*
*	NOTES:
*
*
*	PDL:
*
*	   CALL COMMAND AND STATUS WORD EXTRACTION ROUTINE
*	   CALL FULL WORD RECONSTRUCTION ROUTINE
*	   CALL COUNTS TO GAMMAS CONVERSION ROUTINE
*	   CALL SENSOR ALIGNMENT ROUTINE
*	   CALL BOOM ALIGNMENT ROUTINE
*
*	RETURN
*
******************************************************************
      SUBROUTINE VIM5LFM(GAMMA,GAMMA2,BAD,TIME)
C
      INTEGER*2 TIME(6)
      LOGICAL*1 SROTATE,BROTATE
      REAL*4 GAMMA(3,400),GAMMA2(3,8)
C
      INCLUDE 'UNPACK.INC'
C
      DATA SROTATE/.TRUE./,
     &     BROTATE/.TRUE./
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 MAGSTATUS(TIME,4,4)
C
C RECONSTRUCT PRIMARY FULL WORDS FROM DIFFERENCE WORDS
C
      CALL DIFFERENCE(PDIFF,400,PREF,16,PREC)
C
C ASSIGN SECONDARY REFERENCE WORDS TO SECONDARY FULL WORDS
C
      DO I = 1,8
       SREC(1,I) = SREF(1,I)
       SREC(2,I) = SREF(2,I)
       SREC(3,I) = SREF(3,I)
      END DO
C
C CONVERT COUNTS INTO GAMMAS (MODE SENSITIVE)
C
      CALL MAKEGAMMAS(GAMMA,400,GAMMA2,8,4,4,BAD,TIME)
C
C ORTHOGONALIZE DATA WITH SENSOR MATRIX AFTER GAMMAS (MODE SENSITIVE)
C
      IF ( SROTATE ) THEN
       CALL SENSOR_ALIGN(GAMMA,GAMMA2,400,8,4,BAD)
      END IF
C
C ROTATE DATA WITH BOOM ALIGNMENT MATRIX AFTER GAMMAS (MODE SENSITIVE)
C
      IF ( BROTATE ) THEN
       CALL BOOM_ALIGN(GAMMA,GAMMA2,400,8,4,BAD)
      END IF
C
      RETURN
      END
