******************************************************************
*
*	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,INBOARD,BEFORE
      REAL*4 GAMMA(3,400),GAMMA2(3,8),FIELD(3)
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 CHECK PRIME LFM STATUS
C
      IF ( PRIME(1).NE.PRIME(2) .OR.
     &     PRIME(2).NE.PRIME(3) .OR.
     &     PRIME(3).NE.PRIME(4) ) THEN
C
       WRITE(6,800) RECNUM
C
C CHANGE IN PRIMARY LFM STATUS DETECTED.  USE HARD-CODED DEFAULT.
C
       INBOARD = PRIME(0)
C
      ELSE IF ( PRIME(1) .AND. PRIME(2) .AND.
     &          PRIME(3) .AND. PRIME(4) ) 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 CONVERT COUNTS INTO GAMMAS (MODE SENSITIVE)
C
      CALL VIM5CNT(GAMMA,GAMMA2,BAD,TIME,INBOARD)
C
C ORTHOGONALIZE DATA WITH SENSOR MATRIX AFTER GAMMAS (MODE SENSITIVE)
C
      IF ( SROTATE ) THEN
C
C PRIMARY MAG SENSOR ALIGNMENT
C
       DO I = 1,400
C
        ISTAT = (I-1)/100 + 1
        IF ( GAMMA(1,I).NE.BAD .AND.
     &       GAMMA(2,I).NE.BAD .AND.
     &       GAMMA(3,I).NE.BAD ) THEN
C
C INBOARD MAG IS PRIMARY
C
         IF ( INBOARD ) THEN
          IF ( IBFLIP(ISTAT) ) THEN
           CALL MPRD31(FIELD,VSLIB180,GAMMA(1,I))  ! INBOARD LFM FLIP = 180
          ELSE
           CALL MPRD31(FIELD,VSLIB,GAMMA(1,I))     ! INBOARD LFM FLIP = 0
          END IF
          GAMMA(1,I) = FIELD(1)
          GAMMA(2,I) = FIELD(2)
          GAMMA(3,I) = FIELD(3) 
C
C OUTBOARD MAG IS PRIMARY
C
         ELSE 
          IF ( OBFLIP(ISTAT) ) THEN
           CALL MPRD31(FIELD,VSLOB180,GAMMA(1,I))  ! OUTBOARD LFM FLIP = 180
          ELSE
           CALL MPRD31(FIELD,VSLOB,GAMMA(1,I))     ! OUTBOARD LFM FLIP = 0
          END IF
          GAMMA(1,I) = FIELD(1)
          GAMMA(2,I) = FIELD(2)
          GAMMA(3,I) = FIELD(3)
         END IF 
        ELSE
          GAMMA(1,I) = BAD
          GAMMA(2,I) = BAD
          GAMMA(3,I) = BAD
        END IF
C
C END PRIMARY MAG SENSOR ALIGNMENT
C
       END DO
C
C SECONDARY MAG SENSOR ALIGNMENT
C
       DO I = 1,8 
C
        ISTAT = (I-1)/2 + 1
        IF ( GAMMA2(1,I).NE.BAD .AND.
     &       GAMMA2(2,I).NE.BAD .AND.
     &       GAMMA2(3,I).NE.BAD ) THEN
C
C OUTBOARD MAG IS SECONDARY
C
         IF ( INBOARD ) THEN
          IF ( OBFLIP(ISTAT) ) THEN
           CALL MPRD31(FIELD,VSLOB180,GAMMA2(1,I))  ! OUTBOARD LFM FLIP = 180
          ELSE
           CALL MPRD31(FIELD,VSLOB,GAMMA2(1,I))     ! OUTBOARD LFM FLIP = 0
          END IF
          GAMMA2(1,I) = FIELD(1)
          GAMMA2(2,I) = FIELD(2)
          GAMMA2(3,I) = FIELD(3)
C
C INBOARD MAG IS SECONDARY
C
         ELSE 
          IF ( IBFLIP(ISTAT) ) THEN
           CALL MPRD31(FIELD,VSLIB180,GAMMA2(1,I))  ! INBOARD LFM FLIP = 180
          ELSE 
           CALL MPRD31(FIELD,VSLIB,GAMMA2(1,I))     ! INBOARD LFM FLIP = 0
          END IF
          GAMMA2(1,I) = FIELD(1)
          GAMMA2(2,I) = FIELD(2)
          GAMMA2(3,I) = FIELD(3)
         END IF
        ELSE
         GAMMA2(1,I) = BAD
         GAMMA2(2,I) = BAD
         GAMMA2(3,I) = BAD
        END IF
C
C END SECONDARY MAG SENSOR ALIGNMENT
C
       END DO
C
      END IF
C
      IF ( BROTATE ) THEN
C
C ROTATE DATA WITH BOOM ALIGNMENT MATRIX AFTER GAMMAS (MODE SENSITIVE)
C
       IF ( INBOARD ) THEN
        CALL TRANS31(GAMMA,3,400,VBLIB,BAD)  ! INBOARD MAG PRIMARY
        CALL TRANS31(GAMMA2,3,8,VBLOB,BAD)   ! OUTBOARD MAG SECONDARY
       ELSE
        CALL TRANS31(GAMMA,3,400,VBLOB,BAD)  ! OUTBOARD MAG PRIMARY
        CALL TRANS31(GAMMA2,3,8,VBLIB,BAD)   ! INBOARD MAG SECONDARY
       END IF
C
      END IF
C
      RETURN
  800 FORMAT(1X,'*VIM5LFM*  PRIME LFM STATUS CHANGE IN RECORD ',I5)
      END
