      SUBROUTINE CR6DET(GAMMA,GAMMA2,SCF,AMBIENT,TIME,BAD)
C
C OUTPUT DETAIL DATA FROM CR-6 EDR PROCESSING.
C
C ORIGINAL SOURCE BY SANDY KRAMER, HUGHES STX, CODE 692  04/05/96
C MODIFIED OUTPUT FORMAT FOR UNIFORMITY ACROSS TELEMETRY MODES
C    SBK 02/23/2007
C
      CHARACTER DATAID*4,FLTID*4,COORD*2,MODE*4,TELEM*4
      INTEGER*2 TIME(6),DELTA(6),CR6TIME(6),DATATYPE(2),
     &          WORD30(2),WORD32(2)
      REAL*4 HDR(32),TIMEPD
      REAL*4 GAMMA(3,750),GAMMA2(3,375),SCF(3,750),AMBIENT(3,750),
     &       PRIDAT(3,750),SECDAT(3,375),SCFDAT(3,750),AMBDAT(3,750)
      REAL*4 SPV(6),RANGE,ANG(2),PL(3),
     &       HG(3,750),MTB(3,3),MTB5(3,3),MHG(3,3)
      REAL*8 TD,TN,TP
C
      DATA ICALL/0/, DELTA/0,0,0,0,1,920/, CR6TIME/6*0/, TELEM/'CR-6'/
C
      INCLUDE 'UNPACK.INC'
C
      EQUIVALENCE (SUMOUT(1),HDR(1)),    (DETOUT(1),HDR(1)),
     &            (HDR(1),DATAID),       (HDR(4),CR6TIME(1)),
     &            (HDR(12),TIMEPD),      (HDR(17),DATATYPE(1)), 
     &            (HDR(30),WORD30(1)),   (HDR(32),WORD32(1))
C
      IUNIT = 84
      RECWRITE = RECWRITE + 1
      ICALL = ICALL + 1
      IF ( SYS2(7) ) THEN
         COORD = 'HG'
      ELSE
         COORD = 'PL'
      END IF
C
C CHECK FOR CONFLICTING PROCESSING FLAGS
C
      IF ( SYS2(4).AND..NOT.SYS2(5) ) THEN
         MODE = 'SNGL'
         LEN = 3407
      ELSE IF ( SYS2(5).AND..NOT.SYS2(4) ) THEN
         MODE = 'DUAL'
         LEN = 4532
      ELSE
         WRITE(6,*)
         WRITE(6,*)
     &    '*CR6DET*  CONFLICTING DETAIL PROCESSING FLAGS'
       STOP
      END IF
C
      IF ( SYS2(6).AND.ICALL.EQ.1 ) THEN
C
C FORTRAN OUTPUT UNIT ASSIGNED TO FILE NAME IN VMS DCL ROUTINE CALLING
C THE VOYPROD EXECUTABLE  ex. $ ASSIGN FILE.DAT  FOR084
C
       OPEN(IUNIT,STATUS='NEW',FORM='FORMATTED',
     &      CARRIAGECONTROL='LIST',RECL=124)
      END IF
C
      IF ( SCID.EQ.0 ) THEN
       FLTID = "FLT2"
      ELSE IF ( SCID.EQ.1 ) THEN
       FLTID = "FLT1"
      ELSE
       FLTID = "FLT?"
      END IF
C
      IF ( SYS2(4) ) THEN
       WORD30(1) = 200
       WORD30(2) = 50
       WORD32(2) = 750
      ELSE IF ( SYS2(5) ) THEN
       WORD30(1) = 200
       WORD30(2) = 200
       WORD32(2) = 1200
      END IF
C
      DATAID = 'LFM '
      TIMEPD = 1440.0
      DATATYPE(1) = 1
      WORD32(1) = RECWRITE
      CR6TIME(1) = TIME(1)
      CR6TIME(2) = TIME(2)
      CR6TIME(3) = TIME(3)
      CR6TIME(4) = TIME(4)
      CR6TIME(5) = TIME(5)
      CR6TIME(6) = TIME(6)
C
      IF ( SYS2(4) ) THEN
       WORD30(1) = 750
       WORD30(2) = 375
       WORD32(2) = 3375
      ELSE IF ( SYS2(5) ) THEN
       WORD30(1) = 750
       WORD30(2) = 750
       WORD32(2) = 4500
      END IF
C
C REASSIGN DETAIL VALUES TO LOCAL VARIABLES
C
      DO I = 1,750
C
C SECONDARY MAG SAMPLE COUNTER (1-375)
C
       ISEC = (I-1)/2 + 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,2).EQ.0 ) THEN
        SECDAT(1,ISEC) = GAMMA2(1,ISEC)
        SECDAT(2,ISEC) = GAMMA2(2,ISEC)
        SECDAT(3,ISEC) = GAMMA2(3,ISEC)
       END IF
C
       AMBDAT(1,I) = AMBIENT(1,I)
       AMBDAT(2,I) = AMBIENT(2,I)
       AMBDAT(3,I) = AMBIENT(3,I)
C
       SCFDAT(1,I) = SCF(1,I)
       SCFDAT(2,I) = SCF(2,I)
       SCFDAT(3,I) = SCF(3,I)
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,750,BAD)
        DO II = 1,750
         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,375,BAD)
        DO II = 1,375
         SECDAT(1,II) = HG(1,II)
         SECDAT(2,II) = HG(2,II)
         SECDAT(3,II) = HG(3,II)
        END DO
C
       ELSE IF ( SYS2(5) ) THEN
C
C ROTATE AMBIENT AND SPACECRAFT FIELD DATA INTO HG COORDINATES
C
        CALL ROTATE(HG,AMBDAT,MHG,750,BAD)
        DO II = 1,750
         AMBDAT(1,II) = HG(1,II)
         AMBDAT(2,II) = HG(2,II)
         AMBDAT(3,II) = HG(3,II)
        END DO
C
        CALL ROTATE(HG,SCFDAT,MHG,750,BAD)
        DO II = 1,750
         SCFDAT(1,II) = HG(1,II)
         SCFDAT(2,II) = HG(2,II)
         SCFDAT(3,II) = HG(3,II)
        END DO
C
       END IF
C
      END IF
C
      DO I = 1,750
C
C SECONDARY MAG SAMPLE COUNTER (1-375)
C
       ISEC = (I-1)/2 + 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+750+I) = PRIDAT(2,I)
        DETOUT(32+1500+I) = PRIDAT(3,I)
C
C AVOID RENDUNDANT ASSIGNMENTS
C
        IF ( MOD(I-1,2).EQ.0 ) THEN
         DETOUT(32+2250+ISEC) = SECDAT(1,ISEC)
         DETOUT(32+2625+ISEC) = SECDAT(2,ISEC)
         DETOUT(32+3000+ISEC) = SECDAT(3,ISEC)
        END IF
C
        IF ( SYS2(6) ) THEN
          WRITE(IUNIT,800) FLTID,TELEM,COORD,MODE,CR6TIME,RANGE,ANG(2),
     &     ANG(1),(PRIDAT(J,I),J=1,3),(SECDAT(J,ISEC),J=1,3)
        END IF
C
       ELSE IF ( SYS2(5) ) THEN
C
C OUTPUT AMBIENT FIELD X,Y,Z FOLLOWED BY SPACECRAFT FIELD X,Y,Z
C
        DETOUT(32+I) = AMBDAT(1,I)
        DETOUT(32+750+I) = AMBDAT(2,I)
        DETOUT(32+1500+I) = AMBDAT(3,I)
C
        DETOUT(32+2250+I) = SCFDAT(1,I)
        DETOUT(32+3000+I) = SCFDAT(2,I)
        DETOUT(32+3750+I) = SCFDAT(3,I)
C
        IF ( SYS2(6) ) THEN
          WRITE(IUNIT,800) FLTID,TELEM,COORD,MODE,CR6TIME,RANGE,ANG(2),
     &     ANG(1),(AMBDAT(J,I),J=1,3),(SCFDAT(J,I),J=1,3)
        END IF
C
       END IF
C
       CALL INC_TIME(CR6TIME,DELTA)
C
      END DO
C
C WRITE BINARY DETAIL DATA
C
      WRITE(66) (DETOUT(I),I=1,LEN)
C
      RETURN
  800 FORMAT(2(A4,1X),A2,1X,A4,1X,I2,1X,I3.3,3(1X,I2.2),1X,I3.3,1X,F8.4,
     &       2(1X,F8.3),6(1X,F9.3))
      END
