      PROGRAM SUMMARY_EXTRACT
C
C EXTRACT 48 OR 1.92 SECOND DATA FROM SUMMARY RECORDS
C
C IMPORTANT NOTES REGARDING DATA STORAGE:
C     ORIGINAL DATA FORMAT DICTATES THAT FIELD COMPONENTS
C     STORED IN ORDER XXX...,YYY...,ZZZ...
C     FIELD RMS DATA WRITTEN AS X,Y,Z,...,X,Y,Z
C     BE CAREFUL HOW YOU REFERENCE DATA!
C
C WRITTEN BY SANDY KRAMER, GST, CODE 690
C
      CHARACTER RECTYPE*4,TELFMT*4,FLTID*4,TIMEFMT*4,DSN*50,MODE*4,
     &          PATH*50
      INTEGER*2 DELTA(6),TIME(6),MODCNT(3),DATAID(2)
      INTEGER*4 IB192(25),IB96(5),IB48,SIB48
      LOGICAL*1 MAGSYS(32),REC(10000)
      REAL*4 B192(25,3),DEL192(25),LAM192(25),FMOD192(25),
     &       FMAG192(25),RMS192(25,3),
     &       B96(3,5),DEL96(5),LAM96(5),FMOD96(5),
     &       FMAG96(5),RMS96(5,3),
     &       B48(3),DEL48,LAM48,FMOD48,
     &       FMAG48,RMS48(3),DEL,LAM,F1,F2,BX,BY,BZ,
     &       SRMS48(3),SX48,SY48,SZ48,SFMOD48,
     &       MTB(3,3),MTB5(3,3),MHG(3,3),POS(3),ANG(2),VEL(3),
     &       HDR1(100),HDR(32),DATA(341),SCFLD(155),POSN(13),ATTD(27),
     &       SUMOUT(568)
      REAL*8 DD,TD,TN,TP,T1,T2,REALTIME
C
      EQUIVALENCE (HDR(1),RECTYPE),       (HDR(2),TELFMT),
     &            (HDR(3),FLTID),         (HDR(4),TIME(1)),
     &            (HDR(7),DD),            (HDR(9),TD),
     &            (HDR(11),TIMEFMT),      (HDR(12),TIMEPD),
     &            (HDR(13),MODCNT(1)),    (HDR(17),DATAID(1)),
     &            (HDR1(77), MAGSYS(1)),
     &            (DATA(1),FMAG48),       (DATA(2),FMOD48),
     &            (DATA(3),DEL48),        (DATA(4),LAM48),     
     &            (DATA(5),B48(1)),       (DATA(8),RMS48(1)),
     &            (DATA(11),IB48),        (DATA(12),FMAG96(1)),
     &            (DATA(17),FMOD96(1)),   (DATA(22),DEL96(1)),  
     &            (DATA(27),LAM96(1)),    (DATA(32),B96(1,1)),  
     &            (DATA(47),RMS96(1,1)),  (DATA(62),IB96(1)),
     &            (DATA(67),FMAG192(1)),  (DATA(92),FMOD192(1)),
     &            (DATA(117),DEL192(1)),  (DATA(142),LAM192(1)),
     &            (DATA(167),B192(1,1)),  (DATA(242),RMS192(1,1)),
     &            (DATA(317),IB192(1)),
     &            (SCFLD(148),SRMS48(1)), (SCFLD(151),SIB48),
     &            (SCFLD(152),SX48),      (SCFLD(153),SY48),
     &            (SCFLD(154),SZ48),      (SCFLD(155),SFMOD48),
     &            (POSN(1),TN),           (POSN(3),TP),
     &            (POSN(5),POS(1)),       (POSN(11),RANGE),
     &            (POSN(12),ANG(1)),      (POSN(8),VEL(1)),
     &            (ATTD(1),MTB(1,1)),     (ATTD(10),MTB5(1,1)),
     &            (ATTD(19),MHG(1,1)),
     &            (SUMOUT(1),HDR(1)),     (SUMOUT(33),DATA(1)),
     &            (SUMOUT(342),SCFLD(1)), (SUMOUT(529),POSN(1)),
     &            (SUMOUT(542),ATTD(1)),  (SUMOUT(1),HDR1(1)),
     &            (SUMOUT(1),REC(1))
C
C SET DELTA TO 1.92 SECONDS
C
      DELTA(1) = 0
      DELTA(2) = 0
      DELTA(3) = 0
      DELTA(4) = 0
      DELTA(5) = 1
      DELTA(6) = 920
C
      WRITE(6,*)
      WRITE(6,*) 'ENTER SUMMARY PATH'
      READ(5,'(A)') PATH
      WRITE(6,*) 'ENTER SUMMARY DSN'
      READ(5,'(A)') DSN
    1 CONTINUE
      WRITE(6,*) 'ENTER DESIRED RESOLUTION (1 = 48s, 2 = 1.92s)'
      READ(5,*) IRES
      IF ( IRES.NE.1 .AND. IRES.NE.2 ) THEN
       WRITE (6,*) 'INVALID ENTRY - TRY AGAIN OR ctl-y TO EXIT'
       GOTO 1
      END IF
c INPUT FILE
      OPEN(70,FILE=PATH//DSN,STATUS='OLD',FORM='FORMATTED',
     &     RECORDTYPE='VARIABLE',RECL=8191,READONLY)
      WRITE(6,'(1X,A)') DSN
C OUTPUT FILE
      IF ( IRES.EQ.1 ) THEN
       OPEN(66,FILE=PATH//'S48'//DSN,STATUS='NEW',FORM='FORMATTED',
     &      RECORDTYPE='VARIABLE',RECL=8191,CARRIAGECONTROL='LIST')
      ELSE IF ( IRES.EQ.2 ) THEN 
       OPEN(66,FILE=PATH//'S192'//DSN,STATUS='NEW',FORM='FORMATTED',
     &      RECORDTYPE='VARIABLE',RECL=8191,CARRIAGECONTROL='LIST')
      END IF
C
      WRITE(6,*)
      WRITE(6,*) 'ENTER START TIME'
      WRITE(6,*) 'YY DDD HH MM SS'
      READ(5,'(I2,1X,I3,3(1X,I2))',END=2) (TIME(I),I=1,5)
    2 T1 = REALTIME(TIME)
      WRITE(6,*)
      WRITE(6,*) 'ENTER STOP TIME'
      WRITE(6,*) 'YY DDD HH MM SS'
      READ(5,'(I2,1X,I3,3(1X,I2))',END=3) (TIME(I),I=1,5)
    3 T2 = REALTIME(TIME)
C
      FILL = 999.0
      NCNT = 0
   10 CONTINUE
       READ(70,'(Q,<LEN>A1)',END=100,ERR=10) LEN,(REC(I),I=1,LEN)
       NCNT = NCNT + 1
C
       IF (REALTIME(TIME).LT.T1) GOTO 10
       IF (REALTIME(TIME).GE.T2) GOTO 100
C OUTPUT HEADER RECORD TO SCREEN
       IF (DATAID(1).EQ.8) THEN
        IF (MAGSYS(21).AND.MAGSYS(22)) THEN
         MODE = 'ERR '
        ELSE IF (MAGSYS(21)) THEN
         MODE = 'PRI '
        ELSE IF (MAGSYS(22)) THEN
         MODE = 'SEC '
        ELSE 
         MODE = 'DUAL'
        END IF
        WRITE(6,801) NCNT,RECTYPE,TIME,MODE
        GOTO 10
       END IF
C
       IF (DATAID(1).NE.1) GOTO 10
       IF ( ABS(B48(1)).GT.999.0 .OR. ABS(B48(2)).GT.999.0 .OR. 
     &      ABS(B48(3)).GT.999.0 ) THEN
        FMAG48 = 999.0
        FMOD48 = 999.0
        B48(1) = 999.0
        B48(2) = 999.0
        B48(3) = 999.0
        RMS48(1) = 999.0
        RMS48(2) = 999.0
        RMS48(3) = 999.0
       END IF 
       READ(FLTID,'(3X,I1)') ID
C OUTPUT 48 SECOND DATA
       IF ( IRES.EQ.1 ) THEN
        WRITE(66,804) FLTID,TIME(1),REALDAYS(TIME),FMAG48,FMOD48,
     &                DEL48,LAM48,(B48(J),J=1,3),(RMS48(K),K=1,3),
     &                (POS(L),L=1,3),RANGE
       END IF
C
C EXTRACT 1.92 SEC AVGS
C
       DO I = 1,25
        F1 = DATA(66+I)
        F2 = DATA(91+I)
        DEL = DATA(116+I)
        LAM = DATA(141+I)
        BX = DATA(166+I)
        BY = DATA(191+I)
        BZ = DATA(216+I)
        IF ( ABS(BX).GT.999.0 .OR. ABS(BY).GT.999.0 .OR.
     &      ABS(BZ).GT.999.0 ) THEN
         F1 = 999.9
         F2 = 999.9
         BX = 999.9
         BY = 999.9
         BZ = 999.9
         DEL = 999.9
         LAM = 999.9
         RMS192(1,I) = 999.9
         RMS192(2,I) = 999.9
         RMS192(3,I) = 999.9
        END IF
        IF ( I.GT.1 ) THEN
         CALL INC_TIME(TIME,DELTA)
        END IF
C OUTPUT 1.92 SECOND DATA
        IF ( IRES.EQ.2 ) THEN
         WRITE(66,804) FLTID,TIME(1),REALDAYS(TIME),F1,F2,
     &                 DEL,LAM,BX,BY,BZ,(RMS192(K,I),K=1,3),
     &                 (POS(L),L=1,3),RANGE
        END IF
       END DO
      GOTO 10
  100 CONTINUE
      WRITE(6,*) "DONE"
      WRITE(6,'(1X,''NUMBER OF RECORDS READ: '',I5)') NCNT
      CLOSE(70)
C
      STOP
  801 FORMAT(1X,I5,1X,A4,6(1X,I3),1X,A4)
  804 FORMAT(A4,1X,I2,15(1X,F10.5))
      END
      SUBROUTINE INC_TIME(TIME,DELTA)
C
C INCREMENT CALENDAR TIME (YY,DDD,HH,MM,SS,FFF) BY DELTA(6)
C
      INTEGER*2 TIME(6),DELTA(6)
C
      IYR = TIME(1)
      LEAP = 365
      IF (MOD(IYR,4).EQ.0) LEAP = 366
C
      TIME(6) = TIME(6) + DELTA(6)
      IF (TIME(6).GT.999) THEN
       TIME(5) = TIME(5) + 1
       TIME(6) = TIME(6) - 1000
      END IF
C
      TIME(5) = TIME(5) + DELTA(5)
      IF (TIME(5).GT.59) THEN
       TIME(4) = TIME (4) + 1
       TIME(5) = TIME (5) - 60
      END IF
C
      TIME(4) = TIME(4) + DELTA(4)
      IF (TIME(4).GT.59) THEN
       TIME(3) = TIME(3) + 1
       TIME(4) = TIME(4) - 60
      END IF
C
      TIME(3) = TIME(3) + DELTA(3)
      IF (TIME(3).GT.23) THEN
       TIME(2) = TIME(2) + 1
       TIME(3) = TIME(3) - 24
      END IF
C
      TIME(2) = TIME(2) + DELTA(2)
      IF (TIME(2).GT.LEAP) THEN
       TIME(1) = TIME(1) + 1
       TIME(2) = TIME(2) - LEAP
       IF (TIME(1).GT.99) TIME(1) = TIME(1) - 100
      END IF
C
      TIME(1) = TIME(1) + DELTA(1)
C
      RETURN
      END
      REAL*8 FUNCTION REALTIME(TIME)
C
C CONVERT INTEGER CALENDAR TIME INTO DECIMAL YEAR REAL TIME.
C
      INTEGER*2 TIME(6)
      REAL*8 DAYS
C
      DAYS = 365.0D0
      IF (MOD(TIME(1),4).EQ.0) DAYS = 366.0D0
      REALTIME = DBLE(TIME(1)) + 
     &           DBLE(TIME(2)-1)/DAYS +
     &           DBLE(TIME(3))/24.0D0/DAYS +
     &           DBLE(TIME(4))/60.0D0/24.0D0/DAYS +
     &           DBLE(TIME(5))/60.0D0/60.0D0/24.0D0/DAYS +
     &           DBLE(TIME(6))/1000.0D0/60.0D0/60.0D0/24.0D0/DAYS
C
C ASSUME 2 DIGIT YEAR.  ANY YEAR BEFORE VOYAGER LAUNCH (77) IS INTO NEXT
C CENTURY.
C
      IF (TIME(1).LT.77) REALTIME = REALTIME + 100.0D0
C
      RETURN                                                            
      END
      REAL*8 FUNCTION REALDAYS(TIME)
C
C CONVERT INTEGER CALENDAR TIME INTO DECIMAL YEAR REAL TIME.
C
      INTEGER*2 TIME(6)
      REAL*8 DAYS
C
      REALDAYS = DBLE(TIME(2)-1) +
     &           DBLE(TIME(3))/24.0D0 +
     &           DBLE(TIME(4))/60.0D0/24.0D0 +
     &           DBLE(TIME(5))/60.0D0/60.0D0/24.0D0 +
     &           DBLE(TIME(6))/1000.0D0/60.0D0/60.0D0/24.0D0
C
      RETURN                                                            
      END
