******************************************************************
*
*	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)

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

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

C RECONSTRUCT PRIMARY FULL WORDS FROM DIFFERENCE WORDS

      CALL DIFFERENCE(PDIFF,400,PREF,16,PREC)

C ASSIGN SECONDARY REFERENCE WORDS TO SECONDARY FULL WORDS

      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 CONVERT COUNTS INTO GAMMAS (MODE SENSITIVE)

      CALL MAKEGAMMAS(GAMMA,400,GAMMA2,8,4,4,BAD,TIME)

C OUTPUT COUNTS

      IF ( SYS2(31) ) CALL WRITE_DIFF_COUNTS(TIME,16,400,8,0)

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

      IF ( SROTATE ) CALL SENSOR_ALIGN(GAMMA,GAMMA2,400,8,4,BAD)

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

      IF ( BROTATE ) CALL BOOM_ALIGN(GAMMA,GAMMA2,400,8,4,BAD)

      RETURN
      END
