      SUBROUTINE CR1DET(GAMMA,GAMMA2,SCF,AMBIENT,TIME,BAD)
C
C OUTPUT DETAIL DATA FROM CR1 EDR PROCESSING.
C
C ADAPTED FROM GS3DET ROUTINE BY S. B. KRAMER  10/04/95
C MODIFIED OUTPUT FORMAT FOR UNIFORMITY ACROSS TELEMETRY MODES 
C    SBK 11/28/2006      
C
      CHARACTER DATAID*4,FLTID*4,COORD*2,MODE*4,TELEM*4 
      INTEGER*2 CR1TIME(6),TIME(6),DELTA(6),
     &          DATATYPE(2),WORD30(2),WORD32(2)
      REAL*4 HDR(32),TIMEPD
      REAL*4 GAMMA(3,800),GAMMA2(3,800),SCF(3,800),AMBIENT(3,800),
     &       PRIDAT(3,800),SECDAT(3,800),SCFDAT(3,800),AMBDAT(3,800)
      REAL*4 SPV(6),RANGE,ANG(2),
     &       MTB(3,3),MTB5(3,3),MHG(3,3)
      REAL*8 TD,TN,TP
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
      DATA ICALL/0/, DELTA/5*0,60/, CR1TIME/6*0/, TELEM/'CR-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 = 4832
      ELSE IF ( SYS2(5).AND..NOT.SYS2(4) ) THEN
         MODE = 'DUAL'
         LEN = 4832
      ELSE
         WRITE(6,*)
         WRITE(6,*)
     & '*CR1DET*  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
      CR1TIME(1) = TIME(1)
      CR1TIME(2) = TIME(2)
      CR1TIME(3) = TIME(3)
      CR1TIME(4) = TIME(4)
      CR1TIME(5) = TIME(5)
      CR1TIME(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
C
C OUTPUT PRIMARY AND SECONDARY DETAIL FIELD
C
       WORD30(1) = 800
       WORD30(2) = 800
       WORD32(2) = 4800
C
      ELSE IF ( SYS2(5) ) THEN
C
C OUTPUT AMBIENT AND SPACECRAFT DETAIL FIELD
C
       WORD30(1) = 800
       WORD30(2) = 800
       WORD32(2) = 4800
C
      END IF
C
C REASSIGN DETAIL VALUES TO LOCAL VARIABLES
C
      DO I = 1,800
C
       IF ( SYS2(7) ) 
     &  CALL GETSEDR(CR1TIME,TD,TN,TP,SPV,RANGE,ANG,MTB,MTB5,MHG)
C
       IF ( SYS2(4) ) THEN
C
C GET PRIMARY VALUES
C
        IF ( SYS2(7) ) THEN
         CALL ROTATE1(PRIDAT(1,I),GAMMA(1,I),MHG,BAD)
        ELSE
         PRIDAT(1,I) = GAMMA(1,I)
         PRIDAT(2,I) = GAMMA(2,I)
         PRIDAT(3,I) = GAMMA(3,I)
        END IF
C
C OUTPUT PRIMARY FIELD X,Y,Z
C
        DETOUT(32+I) = PRIDAT(1,I)
        DETOUT(32+800+I) = PRIDAT(2,I)
        DETOUT(32+1600+I) = PRIDAT(3,I)
C
C GET SECONDARY VALUES
C
        IF ( SYS2(7) ) THEN
         CALL ROTATE1(SECDAT(1,I),GAMMA2(1,I),MHG,BAD)
        ELSE   
         SECDAT(1,I) = GAMMA2(1,I)
         SECDAT(2,I) = GAMMA2(2,I)
         SECDAT(3,I) = GAMMA2(3,I)
        END IF
C
C OUTPUT SECONDARY FIELD X,Y,Z
C
        DETOUT(32+2400+I) = SECDAT(1,I)
        DETOUT(32+2400+800+I) = SECDAT(2,I)
        DETOUT(32+2400+1600+I) = SECDAT(3,I)
C
C OUTPUT 800 PRIMARY SAMPLES AND 800 SECONDARY SAMPLES
C
        IF ( SYS2(6) ) THEN
         WRITE(IUNIT,800) FLTID,TELEM,COORD,MODE,CR1TIME,RANGE,ANG(2),
     &    ANG(1),(PRIDAT(J,I),J=1,3),(SECDAT(J,I),J=1,3)
        END IF
C
       ELSE IF ( SYS2(5) ) THEN
C
C GET AMBIENT FIELD VALUES
C
        IF ( SYS2(7) ) THEN
         CALL ROTATE1(AMBDAT(1,I),AMBIENT(1,I),MHG,BAD)
        ELSE 
         AMBDAT(1,I) = AMBIENT(1,I)
         AMBDAT(2,I) = AMBIENT(2,I)
         AMBDAT(3,I) = AMBIENT(3,I)
        END IF
C
C GET SPACECRAFT FIELD VALUES
C
        IF ( SYS2(7) ) THEN
         CALL ROTATE1(SCFDAT(1,I),SCF(1,I),MHG,BAD)
        ELSE
         SCFDAT(1,I) = SCF(1,I)
         SCFDAT(2,I) = SCF(2,I)
         SCFDAT(3,I) = SCF(3,I)
        END IF
C
C OUTPUT AMBIENT FIELD X,Y,Z 
C
        DETOUT(32+I) = AMBDAT(1,I)
        DETOUT(32+800+I) = AMBDAT(2,I)
        DETOUT(32+1600+I) = AMBDAT(3,I)
C
C OUTPUT SPACECRAFT FIELD X,Y,Z
C
        DETOUT(32+2400+I) = SCFDAT(1,I)
        DETOUT(32+3200+I) = SCFDAT(2,I)
        DETOUT(32+4000+I) = SCFDAT(3,I)
C
C OUTPUT 800 AMBIENT SAMPLES AND 800 SPACECRAFT FIELD SAMPLES
C
        IF ( SYS2(6) ) THEN
         WRITE(IUNIT,800) FLTID,TELEM,COORD,MODE,CR1TIME,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(CR1TIME,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
