******************************************************************
*
*	TITLE:  CONVERT RECONSTRUCTED DIGITAL VALUES INTO GAMMAS
*
*	FILE NAME:  CR2LFM.FOR
*
*	PURPOSE:  TO CONVERT DIGITAL CR-2 MAG SCIENCE DATA
*
*	HISTORY:
*
*	AUTHOR            DATE            CHANGE
*	--------          ----            -------
*       S. B. KRAMER     03/22/96         ORIGINAL CODE
*                                         (MODE CR-2)
*
*
*	COMMON AREAS:
*
*		SEE UNPACK.INC FOR COMMON AREA DEFINITIONS
*
*	NOTES:
*
*
*	PDL:
*
*	   CALL COMMAND AND STATUS WORD EXTRACTION ROUTINE
*	   CALL COUNTS TO GAMMAS CONVERSION ROUTINE
*	   CALL SENSOR ALIGNMENT ROUTINE
*	   CALL BOOM ALIGNMENT ROUTINE
*
*	RETURN
*
******************************************************************
      SUBROUTINE CR2LFM(GAMMA,GAMMA2,BAD,TIME)
C
      INTEGER*2 TIME(6)
      LOGICAL*1 SROTATE,BROTATE,INBOARD,BEFORE
      REAL*4 GAMMA(3,400),GAMMA2(3,200)
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,5,1)
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) .OR.
     &     PRIME(4).NE.PRIME(5) ) THEN
C
       WRITE(6,800) RECNUM
C
C CHANGE IN PRIMARY LFM STATUS DETECTED.  USE USER DEFINED DEFAULT.
C
       INBOARD = PRIME(0)
C
      ELSE IF ( PRIME(1) .AND. PRIME(2) .AND.
     &          PRIME(3) .AND. PRIME(4) .AND.
     &          PRIME(5) ) 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 CR2CNT(GAMMA,GAMMA2,BAD,TIME,INBOARD)
C
      IF ( SROTATE ) THEN
C
C ORTHOGONALIZE DATA WITH SENSOR MATRIX AFTER GAMMAS (MODE SENSITIVE)
C
       IF ( .NOT.INBOARD ) THEN
        CALL TRANS31(GAMMA,3,400,VSLOB,BAD)
        CALL TRANS31(GAMMA2,3,200,VSLIB,BAD)
       ELSE 
        CALL TRANS31(GAMMA,3,400,VSLIB,BAD)
        CALL TRANS31(GAMMA2,3,200,VSLOB,BAD)
       END IF
C
      END IF
C
      IF ( BROTATE ) THEN
C
C ROTATE DATA WITH BOOM ALIGNMENT MATRIX AFTER GAMMAS (MODE SENSITIVE)
C
       IF ( .NOT.INBOARD ) THEN
        CALL TRANS31(GAMMA,3,400,VBLOB,BAD)
        CALL TRANS31(GAMMA2,3,200,VBLIB,BAD)
       ELSE
        CALL TRANS31(GAMMA,3,400,VBLIB,BAD)
        CALL TRANS31(GAMMA2,3,200,VBLOB,BAD)
       END IF
C
      END IF
C
      RETURN
  800 FORMAT(1X,'*CR2LFM*  PRIME LFM STATUS CHANGE IN RECORD ',I5)
      END
