******************************************************************
*
*	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)
C
      INTEGER*2 TIME(6)
      LOGICAL*1 SROTATE,BROTATE,INBOARD,BEFORE
      REAL*4 GAMMA(3,640),GAMMA2(3,80)
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,20,4)
C
C RECONSTRUCT PRIMARY FULL WORDS FROM DIFFERENCE WORDS
C
      CALL DIFFERENCE(PDIFF,640,PREF,40,PREC)
C
C RECONSTRUCT SECONDARY FULL WORDS FROM DIFFERENCE WORDS
C
      CALL DIFFERENCE(SDIFF,80,SREF,20,SREC)
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) .OR.
     &     PRIME(5).NE.PRIME(6) .OR.
     &     PRIME(6).NE.PRIME(7) .OR.
     &     PRIME(7).NE.PRIME(8) .OR.
     &     PRIME(8).NE.PRIME(9) .OR.
     &     PRIME(9).NE.PRIME(10) .OR.
     &     PRIME(10).NE.PRIME(11) .OR.
     &     PRIME(11).NE.PRIME(12) .OR.
     &     PRIME(12).NE.PRIME(13) .OR.
     &     PRIME(13).NE.PRIME(14) .OR.
     &     PRIME(14).NE.PRIME(15) .OR.
     &     PRIME(15).NE.PRIME(16) .OR.
     &     PRIME(16).NE.PRIME(17) .OR.
     &     PRIME(17).NE.PRIME(18) .OR.
     &     PRIME(18).NE.PRIME(19) .OR.
     &     PRIME(19).NE.PRIME(20) ) 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) .AND. PRIME(6) .AND.
     &          PRIME(7) .AND. PRIME(8) .AND.
     &          PRIME(9) .AND. PRIME(10) .AND.
     &          PRIME(11) .AND. PRIME(12) .AND.
     &          PRIME(13) .AND. PRIME(14) .AND.
     &          PRIME(15) .AND. PRIME(16) .AND.
     &          PRIME(17) .AND. PRIME(18) .AND.
     &          PRIME(19) .AND. PRIME(20) ) 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 CR4CNT(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,640,VSLOB,BAD)
        CALL TRANS31(GAMMA2,3,80,VSLIB,BAD)
       ELSE 
        CALL TRANS31(GAMMA,3,640,VSLIB,BAD)
        CALL TRANS31(GAMMA2,3,80,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,640,VBLOB,BAD)
        CALL TRANS31(GAMMA2,3,80,VBLIB,BAD)
       ELSE
        CALL TRANS31(GAMMA,3,640,VBLIB,BAD)
        CALL TRANS31(GAMMA2,3,80,VBLOB,BAD)
       END IF
C
      END IF
C
      RETURN
  800 FORMAT(1X,'*CR4LFM*  PRIME LFM STATUS CHANGE IN RECORD ',I5)
      END
