      SUBROUTINE SENSOR_ALIGN(GAMMA,GAMMA2,NPRI,NSEC,NSTAT2,BAD)

C THIS ROUTINE DETERMINES THE STATE OF THE LFM MECHANICAL FLIPPER
C AND APPLIES THE CORRECT SENSOR ALIGNMENT MATRICES TO ROTATE DATA.

C WRITTEN BY SANDY KRAMER, HUGHES STX, CODE 692, 11/07/96

C INPUT VARIABLES

C    BAD      FILL VALUE
C    GAMMA    PRIMARY MAG FIELD STRENGTH
C    GAMMA2   SECONDARY MAG FIELD STRENGTH
C    NPRI     NUMBER OF PRIMARY WORDS
C    NSEC     NUMBER OF SECONDARY WORDS
C    NSTAT2   NUMBER OF MAG STATUS 2 WORDS
C    VSLIB    INBOARD SENSOR ALIGNMENT MATRIX
C    VSLIB180 INBOARD SENSOR ALIGNMENT FLIP MATRIX
C    VSLOB    OUTBOARD SENSOR ALIGNMENT MATRIX
C    VSLOB180 OUTBOARD SENSOR ALIGNMENT FLIP MATRIX

C OUTPUT VARIABLES

C    GAMMA    PRIMARY MAG FIELD STRENGTH
C    GAMMA2   SECONDARY MAG FIELD STRENGTH

C LOCAL VARIABLES

C    FIELD    TEMPORARY VARIABLE

      REAL*4 GAMMA(3,NPRI),GAMMA2(3,NSEC),FIELD(3)

      INCLUDE 'UNPACK.INC'

C PRIMARY MAG SENSOR ALIGNMENT

      DO I = 1,NPRI

        IF ( MOD(NPRI,NSTAT2).NE.0 ) THEN
         WRITE(6,*) '*SENSOR_ALIGN*  MAG STAT2 TO PRI MAPPING ERROR!'
         STOP
        END IF

        IRATIO = NPRI/NSTAT2
        ISTAT = (I-1)/IRATIO + 1
        IF ( GAMMA(1,I).NE.BAD .AND.
     &       GAMMA(2,I).NE.BAD .AND.
     &       GAMMA(3,I).NE.BAD ) THEN

C INBOARD MAG IS PRIMARY

         IF ( PRIME(ISTAT) ) 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 OUTBOARD MAG IS PRIMARY

         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 END PRIMARY MAG SENSOR ALIGNMENT

      END DO

C SECONDARY MAG SENSOR ALIGNMENT

      DO I = 1,NSEC

        IF ( MOD(NSEC,NSTAT2).NE.0 ) THEN
         WRITE(6,*) '*SENSOR_ALIGN*  MAG STAT2 TO SEC MAPPING ERROR!'
         STOP
        END IF

        IRATIO = NSEC/NSTAT2
        ISTAT = (I-1)/IRATIO + 1
        IF ( GAMMA2(1,I).NE.BAD .AND.
     &       GAMMA2(2,I).NE.BAD .AND.
     &       GAMMA2(3,I).NE.BAD ) THEN

C OUTBOARD MAG IS SECONDARY

         IF ( PRIME(ISTAT) ) 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 INBOARD MAG IS SECONDARY

         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 END SECONDARY MAG SENSOR ALIGNMENT

      END DO

      RETURN
      END
