      SUBROUTINE MAKEGAMMAS(GAMMA,NPREC,GAMMA2,NSREC,
     &                      NSTAT1,NSTAT2,BAD,TIME)

C THIS SUBROUTINE SUBTRACTS THE ZERO OFFSET FROM EACH COUNT AND MULTIPIES THE
C RESULTING DIFFERENCE BY THE SENSITIVITY TO YIELD FIELD STRENGTH IN GAMMAS.

C WRITTEN BY SANDY KRAMER, CODE 692, 12/30/96

C INPUT VARIABLES

C    BAD      FILL VALUE
C    IAX      MAG AXIS
C    IBOFF    INBOARD MAG OFFSET (GLOBAL)
C    IBRNG    INBOARD RANGE (GLOBAL)
C    IBSENS   INBOARD MAG SENSITIVITY (GLOBAL)
C    PRIME    PRIMARY MAG STATUS (FALSE = OUTBOARD PRIMARY)
C    NPREC    NUMBER OF PRIMARY WORDS
C    NSREC    NUMBER OF SECONDARY WORDS
C    NSTAT1   NUMBER OF MAG STATUS 1 WORDS
C    NSTAT2   NUMBER OF MAG STATUS 2 WORDS
C    OBOFF    OUTBOARD MAG OFFSET (GLOBAL)
C    OBRNG    OUTBOARD RANGE (GLOBAL)
C    OBSENS   OUTBOARD MAG SENSITIVITY (GLOBAL)
C    PREC     PRIMARY FULL WORD (GLOBAL)
C    SREC     SECONDARY FULL WORD (GLOBAL)
C    TIME     INTEGER TIME ARRAY

C OUTPUT VARIABLES

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

C LOCAL VARIABLES

C    ICYC     MAG STATUS WORD 1 COUNTER
C    ICYC2    MAG STATUS WORD 2 COUNTER
C    INBOARD  PRIMARY MAG STATUS
C    IWRD     PRIMARY FULL WORD COUNTER
C    IWRD2    SECONDARY FULL WORD COUNTER
C    PRIOFF   PRIMARY MAG OFFSET
C    PRISEN   PRIMARY MAG SENSITIVITY
C    RSTAT1   NUMBER OF PRIMARY WORDS PER STAT1 WORD
C    SECOFF   SECONDARY MAG OFFSET
C    SECSEN   SECONDARY MAG SENSITIVITY
C    SRATIO   NUMBER OF PRIMARY WORDS PER SECONDARY WORD

      INTEGER*2 TIME(6)
      INTEGER*4 PRIOFF(3),SECOFF(3),RSTAT1,SRATIO
      LOGICAL*1 INBOARD
      REAL*4 GAMMA(3,NPREC),GAMMA2(3,NSREC),PRISEN(3),SECSEN(3)

      INCLUDE 'UNPACK.INC'

      SRATIO = NPREC/NSREC
      RSTAT1 = NPREC/NSTAT1
      IF ( NSTAT2.GT.0 ) THEN
       RSTAT2 = NPREC/NSTAT2
      ELSE
       RSTAT2 = -1 ! accommodate CR-6 mode lacking MAG status 2 word
      END IF
      DO IWRD = 1,NPREC

       ICYC = INT((IWRD-1)/RSTAT1) + 1 
       ICYC2 = INT((IWRD-1)/RSTAT2) + 1 
       IWRD2 = INT((IWRD-1)/SRATIO) + 1
       IF ( SYS2(24) ) THEN
        INBOARD = .TRUE. ! user forced
       ELSE IF ( NSTAT2.EQ.0 ) THEN
        INBOARD = .FALSE. ! CR-6
       ELSE
        INBOARD = PRIME(ICYC2) ! normal mode
       END IF

       DO IAX = 1,3

C ASSIGN OFFSETS AND SENSITIVITIES TO LOCAL VARIABLES

        IF ( INBOARD ) THEN ! INBOARD MAG PRIMARY
         PRIOFF(IAX) = IBOFF(IAX,IBRNG(ICYC))
         SECOFF(IAX) = OBOFF(IAX,OBRNG(ICYC))
         PRISEN(IAX) = IBSENS(IAX,IBRNG(ICYC))
         SECSEN(IAX) = OBSENS(IAX,OBRNG(ICYC))
        ELSE ! OUTBOARD MAG PRIMARY
         PRIOFF(IAX) = OBOFF(IAX,OBRNG(ICYC))
         SECOFF(IAX) = IBOFF(IAX,IBRNG(ICYC))
         PRISEN(IAX) = OBSENS(IAX,OBRNG(ICYC))
         SECSEN(IAX) = IBSENS(IAX,IBRNG(ICYC))
        END IF

C CONVERT PRIMARY MAG COUNTS TO GAMMAS
        
        IF ( PREC(IAX,IWRD).NE.0 ) THEN
         GAMMA(IAX,IWRD) = (PREC(IAX,IWRD)-PRIOFF(IAX))*PRISEN(IAX)
        ELSE
         GAMMA(IAX,IWRD) = BAD
        END IF
       
C CONVERT SECONDARY MAG COUNTS TO GAMMAS

        IF ( MOD(IWRD-1,SRATIO).EQ.0 ) THEN
         IF ( SREC(IAX,IWRD2).NE.0 ) THEN
          GAMMA2(IAX,IWRD2) = (SREC(IAX,IWRD2)-SECOFF(IAX))*SECSEN(IAX)
         ELSE
          GAMMA2(IAX,IWRD2) = BAD
         END IF
        END IF

       END DO 

C WRITE GAMMAS TO FILE

       IF ( SYS2(32) ) THEN
        IF ( IWRD.EQ.1 ) WRITE(81,'(1X,''EDR TIME: '',6(1X,I3))') TIME
        WRITE(81,'(4(1X,I5),3(1X,F7.3),4(1X,I5),3(1X,F7.3))') 
     &  IWRD,PREC(1,IWRD),PREC(2,IWRD),PREC(3,IWRD),
     &  GAMMA(1,IWRD),GAMMA(2,IWRD),GAMMA(3,IWRD),
     &  IWRD2,SREC(1,IWRD2),SREC(2,IWRD2),SREC(3,IWRD2),
     &  GAMMA2(1,IWRD2),GAMMA2(2,IWRD2),GAMMA2(3,IWRD2)
       END IF

      END DO

      RETURN 
      END

