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

C THIS ROUTINE APPLIES BOOM ALIGNMENT MATRICES TO ROTATE 3 AXIS FIELD DATA.

C WRITTEN BY SANDY KRAMER, HUGHES STX, CODE 692, 12/30/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    PRIME    PRIMARY MAG FLAG ARRAY (GLOBAL)
C    VBLIB    INBOARD SENSOR ALIGNMENT MATRIX
C    VBLOB    OUTBOARD SENSOR ALIGNMENT MATRIX

C OUTPUT VARIABLES

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

C LOCAL VARIABLES

C    FIELD    TEMPORARY VARIABLE
C    INBOARD  PRIMARY MAG FLAG

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

      INCLUDE 'UNPACK.INC'

C PRIMARY MAG SENSOR ALIGNMENT

      DO I = 1,NPRI

       IF ( NSTAT2.EQ.0 ) THEN
        INBOARD = .FALSE.
        ISTAT = 0
        IBFLIP(0) = .FALSE.
        OBFLIP(0) = .FALSE.
       ELSE 
        IRATIO = NPRI/NSTAT2
        ISTAT = (I-1)/IRATIO + 1
        INBOARD = PRIME(ISTAT)
       END IF

       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 ( INBOARD ) THEN
         CALL MPRD31(FIELD,VBLIB,GAMMA(1,I))  
         GAMMA(1,I) = FIELD(1)
         GAMMA(2,I) = FIELD(2)
         GAMMA(3,I) = FIELD(3) 

C OUTBOARD MAG IS PRIMARY

        ELSE 
         CALL MPRD31(FIELD,VBLOB,GAMMA(1,I)) 
         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 ( NSTAT2.EQ.0 ) THEN
        INBOARD = .FALSE.
        ISTAT = 0
        IBFLIP(0) = .FALSE.
        OBFLIP(0) = .FALSE.
       ELSE
        IRATIO = NSEC/NSTAT2
        ISTAT = (I-1)/IRATIO + 1
        INBOARD = PRIME(ISTAT)
       END IF

       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 ( INBOARD ) THEN
         CALL MPRD31(FIELD,VBLOB,GAMMA2(1,I))
         GAMMA2(1,I) = FIELD(1)
         GAMMA2(2,I) = FIELD(2)
         GAMMA2(3,I) = FIELD(3)

C INBOARD MAG IS SECONDARY

        ELSE 
         CALL MPRD31(FIELD,VBLIB,GAMMA2(1,I))
         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
