      SUBROUTINE CR3DET(GAMMA,GAMMA2,SCF,AMBIENT,TIME,BAD)
C
C OUTPUT DETAIL DATA FROM CR-3 EDR PROCESSING.
C
C ORIGINAL SOURCE - SBK 04/04/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),CR3TIME(6),DELTA(6),
     &          DATATYPE(2),WORD30(2),WORD32(2)
      REAL*4 HDR(32),TIMEPD
      REAL*4 GAMMA(3,400),GAMMA2(3,100),SCF(3,400),AMBIENT(3,400),
     &       PRIDAT(3,400),SECDAT(3,100),SCFDAT(3,400),AMBDAT(3,400)
      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,240/, CR3TIME/6*0/, TELEM/'CR-3'/
C
      INCLUDE 'UNPACK.INC'
C
      EQUIVALENCE (SUMOUT(1),HDR(1)),    (DETOUT(1),HDR(1)),
     &            (HDR(1),DATAID),       
     &            (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 = 1532
      ELSE IF ( SYS2(5).AND..NOT.SYS2(4) ) THEN
         MODE = 'DUAL'
         LEN = 2432
      ELSE
         WRITE(6,*)
         WRITE(6,*)
     &    '*CR3DET*  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
      CR3TIME(1) = TIME(1)
      CR3TIME(2) = TIME(2)
      CR3TIME(3) = TIME(3)
      CR3TIME(4) = TIME(4)
      CR3TIME(5) = TIME(5)
      CR3TIME(6) = TIME(6)
C
      IF ( SCID.EQ.0 ) THEN
       FLTID = "FLT2"
      ELSE IF ( SCID.EQ.1 ) THEN
       FLTID = "FLT1"
      ELSE
       FLTID = "FLT?"
      END IF
      DATAID = 'LFM '
      TIMEPD = 48.0
      DATATYPE(1) = 1
      WORD32(1) = RECWRITE
C
      IF ( SYS2(4) ) THEN
       WORD30(1) = 400
       WORD30(2) = 100
       WORD32(2) = 1500
      ELSE IF ( SYS2(5) ) THEN
       WORD30(1) = 400
       WORD30(2) = 400
       WORD32(2) = 2400
      END IF
C
C REASSIGN DETAIL VALUES TO LOCAL VARIABLES
C
      DO I = 1,400
C
C SECONDARY MAG SAMPLE COUNTER (1-100)
C
       ISEC = (I-1)/4 + 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,4).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,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,100,BAD)
        DO II = 1,100
         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,400,BAD)
        DO II = 1,400
         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,400,BAD)
        DO II = 1,400
         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
C WRITE 48 SECOND CR-3 RECORD 
C
      DO I = 1,400
C
C SECONDARY MAG SAMPLE COUNTER (1-100)
C
       ISEC = (I-1)/4 + 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,4).EQ.0 ) THEN
         DETOUT(32+1200+ISEC) = SECDAT(1,ISEC)
         DETOUT(32+1300+ISEC) = SECDAT(2,ISEC)
         DETOUT(32+1400+ISEC) = SECDAT(3,ISEC)
        END IF
C
        IF ( SYS2(6) ) THEN
         WRITE(IUNIT,800) FLTID,TELEM,COORD,MODE,CR3TIME,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+400+I) = AMBDAT(2,I)
        DETOUT(32+800+I) = AMBDAT(3,I)
C
        DETOUT(32+1200+I) = SCFDAT(1,I)
        DETOUT(32+1600+I) = SCFDAT(2,I)
        DETOUT(32+2000+I) = SCFDAT(3,I)
C
        IF ( SYS2(6) ) THEN 
         WRITE(IUNIT,800) FLTID,TELEM,COORD,MODE,CR3TIME,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(CR3TIME,DELTA)
C
      END DO
      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
