      SUBROUTINE VIM5CAL(GAMMA,GAMMA2,TIME,BAD)
C
C OUTPUT CALIBRATION DETAIL DATA FROM VIM5 EDR PROCESSING.
C
C NEW CODE DEVELOPED FROM MODIFIED VIM5DET ROUTINE. 05/24/2006 SBK  
C REVISED 05/26/95 SBK - NEW ROTATION MATRIX FORMAT FROM SEDR ROUTINES
C MODIFIED 04/04/96 SBK - CHANGE ASCII OUTPUT FORMAT
C
      CHARACTER DATAID*4
      INTEGER*2 TIME(6),VIM5TIME(6),DELTA(6),
     &          DATATYPE(2),WORD30(2),WORD32(2)
      REAL*4 HDR(32),TIMEPD
      REAL*4 GAMMA(3,400),GAMMA2(3,8),PRIDAT(3,400),SECDAT(3,8)
      REAL*4 SPV(6),RANGE,ANG(2),PL(3),
     &       HG(3,400),MTB(3,3),MTB5(3,3),MHG(3,3)
      REAL*8 TD,TN,TP
C
      DATA ICALL/0/, DELTA/5*0,480/
C
      INCLUDE 'UNPACK.INC'
C
      EQUIVALENCE (SUMOUT(1),HDR(1)),    (DETOUT(1),HDR(1))
C
      EQUIVALENCE (HDR(1),DATAID),       (HDR(12),TIMEPD),
     &            (HDR(17),DATATYPE(1)), (HDR(30),WORD30(1)),
     &            (HDR(32),WORD32(1))
C
      RECWRITE = RECWRITE + 1 
C
      ICALL = ICALL + 1
      IF ( SYS2(6) .AND. ICALL.EQ.1 ) THEN
       OPEN(84,FILE='FOR084.DAT',STATUS='NEW',FORM='FORMATTED',
     &      CARRIAGECONTROL='LIST',RECL=106)
       OPEN(85,FILE='FOR085.DAT',STATUS='NEW',FORM='FORMATTED',
     &      CARRIAGECONTROL='LIST',RECL=106)
      END IF
C
      VIM5TIME(1) = TIME(1)
      VIM5TIME(2) = TIME(2)
      VIM5TIME(3) = TIME(3)
      VIM5TIME(4) = TIME(4)
      VIM5TIME(5) = TIME(5)
      VIM5TIME(6) = TIME(6)
C
      DATAID = 'LFM '
      TIMEPD = 192.0
      DATATYPE(1) = 1
      WORD32(1) = RECWRITE
      SYS2(4) = .TRUE.   
C
      IF ( SYS2(4) ) THEN
       WORD30(1) = 400
       WORD30(2) = 8
       WORD32(2) = 1224
      END IF
C
C REASSIGN DETAIL VALUES TO LOCAL VARIABLES
C
      DO I = 1,400
C
C SECONDARY MAG SAMPLE COUNTER (1-8)
C
       ISEC = (I-1)/50 + 1
C
       PRIDAT(1,I) = GAMMA(1,I)
       PRIDAT(2,I) = GAMMA(2,I)
       PRIDAT(3,I) = GAMMA(3,I)
C
C AVOID RENDUNDANT ASSIGNMENTS
C
       IF ( MOD(I-1,50).EQ.0 ) THEN
        SECDAT(1,ISEC) = GAMMA2(1,ISEC)
        SECDAT(2,ISEC) = GAMMA2(2,ISEC)
        SECDAT(3,ISEC) = GAMMA2(3,ISEC)
       END IF
C
      END DO
C
      IF ( SYS2(7) ) THEN
C
C GET SEDR DATA
C
       CALL GETSEDR(TIME,TD,TN,TP,SPV,RANGE,ANG,MTB,MTB5,MHG)
C
       IF ( SYS2(4) ) THEN
C
C ROTATE PRIMARY AND SECONDARY DATA INTO HG COORDINATES
C
        CALL ROTATE(HG,PRIDAT,MHG,400,BAD)
        DO II = 1,400
         PRIDAT(1,II) = HG(1,II)
         PRIDAT(2,II) = HG(2,II)
         PRIDAT(3,II) = HG(3,II)
        END DO
C
        CALL ROTATE(HG,SECDAT,MHG,8,BAD)
        DO II = 1,8
         SECDAT(1,II) = HG(1,II)
         SECDAT(2,II) = HG(2,II)
         SECDAT(3,II) = HG(3,II)
        END DO
C
       END IF
C
      END IF
C
C OUTPUT 400 PRIMARY SAMPLES AND 8 SECONDARY SAMPLES
C
      DO I = 1,400
C
C SECONDARY MAG SAMPLE COUNTER (1-8)
C
       ISEC = (I-1)/50 + 1
C
       IF ( SYS2(4) ) THEN
C
C OUTPUT PRIMARY FIELD X,Y,Z FOLLOWED BY SECONDARY FIELD X,Y,Z
C
        DETOUT(32+I) = PRIDAT(1,I)
        DETOUT(32+400+I) = PRIDAT(2,I)
        DETOUT(32+800+I) = PRIDAT(3,I)
C
C AVOID RENDUNDANT ASSIGNMENTS
C
        IF ( MOD(I-1,50).EQ.0 ) THEN
         DETOUT(32+1200+ISEC) = SECDAT(1,ISEC)
         DETOUT(32+1200+8+ISEC) = SECDAT(2,ISEC)
         DETOUT(32+1200+16+ISEC) = SECDAT(3,ISEC)
        END IF
C
        IF ( SYS2(6) ) THEN
         WRITE(84,800) TIME,I,(PRIDAT(J,I),J=1,3),
     &                 I,(GAMMA(J,I),J=1,3)
         IF ( MOD(I-1,50).EQ.0 ) 
     &    WRITE(85,800) TIME,ISEC,(SECDAT(J,ISEC),J=1,3),
     &                  ISEC,(GAMMA2(J,ISEC),J=1,3)
        END IF
C
       END IF
C
       CALL INC_TIME(VIM5TIME,DELTA)
C
      END DO
C
      IF ( SYS2(4) ) THEN
       LEN = 1256
      ELSE
       WRITE(68,*)
       WRITE(68,*) 
     &  '*VIM5DET*  ENTRY INTO DETAIL OUTPUT ROUTINE WITH BAD FLAGS'
       LEN = 0
       STOP
      END IF
      WRITE(66) (DETOUT(I),I=1,LEN)
C
      RETURN
  800 FORMAT(1X,I2,1X,I3.3,3(1X,I2.2),1X,I3.3,2(1X,I4,3(1X,F9.3)))
      END
