      SUBROUTINE VIM5AVE(GAMMA,GAMMA2,BAD,TIME)
C
C MASTER CALLING ROUTINE FOR THE PRODUCTION OF VIM-5 AVERAGES
C MODIFIED TO PROCESS BOTH SINGLE MAGS CONCURRENTLY 07/26/95 - SBK
C
      INTEGER*2 TIME(6)
C
      REAL*4 GAMMA(3,400),GAMMA2(3,8),SCF(3,400),AMBIENT(3,400)
C
      DATA SCF /1200*999.0/
C
      INCLUDE 'UNPACK.INC'
C
C GET SPACECRAFT FIELD AND AMBIENT FIELD VALUES
C
      CALL VIM5FLD(GAMMA,GAMMA2,SCF,AMBIENT,TIME,BAD)
C
C DETAIL OUTPUT
C
      IF ( SYS2(4).OR.SYS2(5) ) THEN
       CALL VIM5DET(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 VIM5PRI(GAMMA,SCF,TIME,BAD)
C
C SINGLE MAG MODE - SECONDARY
C
      ELSE IF ( (.NOT.SYS2(21)) .AND. SYS2(22) ) THEN
       CALL VIM5SEC(GAMMA2,SCF,TIME,BAD)
C
C SINGLE MAG MODE - PRIMARY AND SECONDARY
C
      ELSE IF ( SYS2(21) .AND. SYS2(22) ) THEN
       CALL VIM5PNS(GAMMA,GAMMA2,TIME,BAD)
C
C DUAL MAG MODE - AMBIENT AND SPACECRAFT FIELDS
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 VIM5PRI(SCF,SCF,TIME,BAD)
       ELSE
        CALL VIM5PRI(AMBIENT,SCF,TIME,BAD)
       END IF
C
      END IF
C
      RETURN
      END
