      SUBROUTINE CR5DET(GAMMA,GAMMA2,SCF,AMBIENT,TIME,BAD)
C
C OUTPUT DETAIL DATA FROM CR-5 EDR PROCESSING.
C
C ORIGINAL SOURCE - SBK 02/21/96
C
      CHARACTER DATAID*4
      INTEGER*2 TIME(6),DELTA(6),NEWTIME(6),DATATYPE(2),
     &          WORD30(2),WORD32(2)
      REAL*4 HDR(32),TIMEPD
      REAL*4 GAMMA(3,2400),GAMMA2(3,600),SCF(3,2400),AMBIENT(3,2400),
     &       PRIDAT(3,2400),SECDAT(3,600),SCFDAT(3,2400),AMBDAT(3,2400)
      REAL*4 SPV(6),RANGE,ANG(2),PL(3),
     &       HG(3,2400),MTB(3,3),MTB5(3,3),MHG(3,3)
      REAL*8 TD,TN,TP
C
      DATA DELTA/0,0,0,0,48,0/  ! 48 second count increment
C
      INCLUDE 'UNPACK.INC'
C
      EQUIVALENCE (SUMOUT(1),HDR(1)),    (DETOUT(1),HDR(1)),
     &            (HDR(1),DATAID),       (HDR(4),NEWTIME(1)),
     &            (HDR(12),TIMEPD),      (HDR(17),DATATYPE(1)), 
     &            (HDR(30),WORD30(1)),   (HDR(32),WORD32(1))
C
      RECWRITE = RECWRITE + 1 
C
      DATAID = 'LFM '
      TIMEPD = 48.0
      DATATYPE(1) = 1
      WORD32(1) = RECWRITE
      NEWTIME(1) = TIME(1)
      NEWTIME(2) = TIME(2)
      NEWTIME(3) = TIME(3)
      NEWTIME(4) = TIME(4)
      NEWTIME(5) = TIME(5)
      NEWTIME(6) = TIME(6)
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
C REASSIGN DETAIL VALUES TO LOCAL VARIABLES
C
      DO I = 1,2400
C
C SECONDARY MAG SAMPLE COUNTER (1-600)
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,2400,BAD)
        DO II = 1,2400
         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,600,BAD)
        DO II = 1,600
         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,2400,BAD)
        DO II = 1,2400
         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,2400,BAD)
        DO II = 1,2400
         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 BREAK 576 SECOND CR-5 RECORD INTO 48 SECOND SEGMENTS
C
      DO N = 1,12
C
       L = (N-1)*200
       K = (N-1)*50
C
       DO I = 1,200
C
C SECONDARY MAG SAMPLE COUNTER (1-50)
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+L)
         DETOUT(32+200+I) = PRIDAT(2,I+L)
         DETOUT(32+400+I) = PRIDAT(3,I+L)
C
C AVOID RENDUNDANT ASSIGNMENTS
C
         IF ( MOD(I-1,4).EQ.0 ) THEN
          DETOUT(32+600+ISEC) = SECDAT(1,ISEC+K)
          DETOUT(32+600+50+ISEC) = SECDAT(2,ISEC+K)
          DETOUT(32+600+100+ISEC) = SECDAT(3,ISEC+K)
         END IF
C
         IF ( SYS2(6) ) 
     &    WRITE(84,800) NEWTIME,I+L,(PRIDAT(J,I+L),J=1,3),
     &                  ISEC+K,(SECDAT(J,ISEC+K),J=1,3)
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+L)
         DETOUT(32+200+I) = AMBDAT(2,I+L)
         DETOUT(32+400+I) = AMBDAT(3,I+L)
C
         DETOUT(32+600+I) = SCFDAT(1,I+L)
         DETOUT(32+800+I) = SCFDAT(2,I+L)
         DETOUT(32+1000+I) = SCFDAT(3,I+L)
C
         IF ( SYS2(6) ) 
     &    WRITE(84,800) NEWTIME,I+L,(AMBDAT(J,I+L),J=1,3),
     &                  I+L,(SCFDAT(J,I+L),J=1,3)
C
        END IF
C
       END DO
C
C OUTPUT 48 SECOND RECORD W/HEADER BLOCK
C
       IF ( SYS2(4).AND..NOT.SYS2(5) ) THEN
        LEN = 750 + 32  
       ELSE IF ( SYS2(5).AND..NOT.SYS2(4) ) THEN
        LEN = 1200 + 32
       ELSE
        WRITE(68,*)
        WRITE(68,*) 
     &   '*CR5DET*  ENTRY INTO DETAIL OUTPUT ROUTINE WITH BAD FLAGS'
        LEN = 0
        STOP
       END IF
       WRITE(66) (DETOUT(I),I=1,LEN)
       CALL INC_TIME(NEWTIME,DELTA)
C
      END DO
C
      RETURN
  800 FORMAT(1X,I2,1X,I3.3,3(1X,I2.2),1X,I3.3,2(1X,I4,3(1X,F9.3)))
      END
