      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 MODIFIED TO INCLUDE COMPOSITE MAG OPTION 01/30/2006 - SBK
C MODIFIED TO OUTPUT ONLY CALIBRATION DATA ON DETECTION 12/19/2006 - SBK
C CALIBRATION LOGIC CHANGED TO SKIP MAGCAL DATA BOUNDED BY CALIBRATION
C  BIT SET TO 1 AND ENDING WITH SERIES OF EFLIP BITS SET TO 1. - SBK 08/15/2007
C RESTORE ABILITY TO OVERRIDE MAGCAL BIT DETECTION. - SBK 02/27/2009
C
      LOGICAL*1 CALDAT, EFLPDAT
C
      INTEGER*2 TIME(6), CALCNT, EFLPCNT
C
      REAL*4 GAMMA(3,400),GAMMA2(3,8),SCF(3,400),AMBIENT(3,400)
C
      DATA SCF /1200*999.0/, CALDAT /.FALSE./, EFLPDAT /.FALSE./,
     &     CALCNT /0/, EFLPCNT /0/
C
      INCLUDE 'UNPACK.INC'
C
C Option to skip MAGCAL check when disabling data quality check. 
C 
      IF ( SYS2(11) ) GOTO 100
C
C CALIBRATION DETAIL DATA OUTPUT - FIRST CALIBRATION BIT SET MARKS BEGINNING
C OF CALIBRATION PERIOD.  PERIOD END MARKED BY SERIES OF EFLIP BITS.  LAST
C EFLIP BIT SET TO 1 ENDS CALIBRATION PERIOD. LOOK FOR RETURN OF EFLIP BIT
C WITH ZERO VALUE.
C
C
      DO I = 1,4
C
C DETECT FIRST MAGCAL INDICATOR BIT
C
       IF ( (IBCAL(I).OR.OBCAL(I)) .AND. .NOT.CALDAT ) THEN
        WRITE(6,*) '***VIM5AVE***BEGINNING OF MAGCAL PERIOD'
        WRITE(6,*) '***VIM5AVE***MAGCAL STATUS BIT DETECTED'
        CALDAT = .TRUE.
        EFLPDAT = .FALSE.
        CALCNT = CALCNT + 1
C
C DETECT SUBSEQUENT MAGCAL INDICATOR BITS
C
       ELSE IF ( (IBCAL(I).OR.OBCAL(I)) .AND. CALDAT ) THEN
        WRITE(6,*) '***VIM5AVE***MAGCAL STATUS BIT DETECTED'
        CALCNT = CALCNT + 1
C
C DETECT END OF MAGCAL START BIT SERIES
C
       ELSE IF ( .NOT.(IBCAL(I).OR.OBCAL(I)) .AND. CALDAT ) THEN
C
C MAGCAL START MARKED BY 4 CONSECUTIVE MAGCAL INDICATORS
C
        IF ( CALCNT.LT.4 ) THEN
         WRITE(6,*) '***VIM5AVE***MAGCAL STATUS ERROR DETECTED'
         CALCNT = 0
         CALDAT = .FALSE.
        END IF
C
       END IF
C
       IF ( CALCNT.GE.4 ) THEN
C
C ENCOUNTER END OF MAGCAL W/EFLIP BITS (4 EXPECTED)
C
        IF ( CALDAT .AND. EFLIP(I) ) THEN
         EFLPCNT = EFLPCNT + 1
         IF ( EFLPCNT.EQ.4 ) EFLPDAT = .TRUE.
        END IF
C
        IF ( EFLPDAT .AND. .NOT.EFLIP(I) ) THEN
         CALCNT = 0 
         CALDAT = .FALSE.
         EFLPDAT = .FALSE.
         WRITE(6,*) '***VIM5AVE***END OF MAGCAL PERIOD'
         RETURN
        END IF
C
       END IF
C
C END CHECK OF 4 VIM5 MAG STATUS VALUES
C
      END DO
C
      IF ( CALDAT ) THEN
        WRITE(6,*) '***VIM5AVE***SKIPPING MAGCAL DATA'
        RETURN
      END IF
C
C SKIP NON-RANGE 0 DATA
C
      DO IRNG = 1,4
       IF ( IBRNG(IRNG).GT.0 .OR. OBRNG(IRNG).GT.0 ) THEN
        WRITE(6,*) '***VIM5AVE***SKIPPING NON RANGE 0 DATA  ',
     &   IBRNG(IRNG),'  ',OBRNG(IRNG)
        RETURN
       END IF
      END DO
C
  100 CONTINUE
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 COMPOSITE MAG MODE - INTERPOLATED SECONDARY X AND Z REPLACE PRIMARY X AND Z
C
      IF ( SYS2(28) ) THEN
       CALL VIM5SPL(GAMMA,GAMMA2,SCF,TIME,BAD)
C
C SINGLE MAG MODE - PRIMARY
C
      ELSE 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
