      SUBROUTINE CR5AVE(GAMMA,GAMMA2,BAD,TIME)
C
C MASTER CALLING ROUTINE FOR THE PRODUCTION OF CR-5 AVERAGES
C
C ORIGINAL SOURCE BY SANDY KRAMER, HUGHES STX, CODE 692 - 10/21/94
C MODIFIED TO INCLUDE DELTA MODULATED WORDS -  SBK 02/21/96
C
      INTEGER*2 TIME(6)
C
      REAL*4 GAMMA(3,2400),GAMMA2(3,600),SCF(3,2400),AMBIENT(3,2400)
C
      DATA SCF /7200*999.0/
C
      INCLUDE 'UNPACK.INC'
C
C GET SPACECRAFT FIELD AND AMBIENT FIELD VALUES
C
      CALL CR5FLD(GAMMA,GAMMA2,SCF,AMBIENT,TIME,BAD)
C
C DETAIL OUTPUT
C
      IF ( SYS2(4).OR.SYS2(5) ) THEN
       CALL CR5DET(GAMMA,GAMMA2,SCF,AMBIENT,TIME,BAD)
       RETURN
      END IF
C
C SINGLE MAG MODE - PRIMARY
C
      IF ( SYS2(21) .AND. (.NOT.SYS2(22)) ) THEN
       CALL CR5PRI(GAMMA,SCF,TIME,BAD)
C
C SINGLE MAG MODE - SECONDARY
C
      ELSE IF ( SYS2(22) .AND. (.NOT.SYS2(21)) ) THEN
       CALL CR5SEC(GAMMA2,SCF,TIME,BAD)
C
C SINGLE MAG MODE - PRIMARY AND SECONDARY
C
      ELSE IF ( SYS2(21) .AND. SYS2(22) ) THEN
       CALL CR5PNS(GAMMA,GAMMA2,TIME,BAD)
C
C DUAL MAG MODE
C
      ELSE IF ( (.NOT.SYS2(21)) .AND. (.NOT.SYS2(22)) ) THEN
C
C IF SYS2(27) IS TRUE, USE S/C FIELD AS AMBIENT
C
       IF (SYS2(27)) THEN
        CALL CR5PRI(SCF,SCF,TIME,BAD)
       ELSE
        CALL CR5PRI(AMBIENT,SCF,TIME,BAD)
       END IF
C
      END IF
C
      RETURN
      END
