********************************************************************************

      PROGRAM SUMMREAD
C
C THIS PROGRAM READS BINARY VOYAGER SUMMARY DATA AND EXTRACTS
C BASIC FIELD MAGNITUDE AND COMPONENT VALUES.
C
C WRITTEN BY SANDY KRAMER, HSTX, CODE 692, NASA GSFC - 01/94
C
C 06/30/97: revised as SUMREAD1 to output to selected output file (unit=80) /JFC
C
C Input parameters:
C
C    Line 1:  Input filename for VAX binary data file
C    Line 2:  Output filename for ASCII 
C    Line 3:  "1", "2", or "3" for 48-sec, 9.6-sec, or 1.92-sec data, respect.
C    Line 4:  Start date/time in format "yy ddd hh mm ss"
C    Line 5:  End date/time in format "yy ddd hh mm ss"
C
      CHARACTER RECTYPE*4,TELFMT*4,FLTID*4,TIMEFMT*4,DSN*50,MODE*4
      CHARACTER*60 OUTFIL
      INTEGER*2 TIME(6),MODCNT(3),DATAID(2),DELTA(6),STIME(6),ETIME(6)
      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(3,25),
     &       B96(5,3),DEL96(5),LAM96(5),FMOD96(5),
     &       FMAG96(5),RMS96(3,5),
     &       B48(3),DEL48,LAM48,FMOD48,
     &       FMAG48,RMS48(3),
     &       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
      WRITE(6,*)
      WRITE(6,*) 'ENTER SUMMARY INPUT FILENAME'
      READ(5,'(A)') DSN
      OPEN(70,FILE=DSN,STATUS='OLD',FORM='FORMATTED',
     &     RECORDTYPE='VARIABLE',RECL=8191,READONLY)
      WRITE(6,*) 'ENTER ASCII OUTPUT FILENAME'
      READ(5,'(A)') OUTFIL
      OPEN(80,FILE=OUTFIL,STATUS='NEW')
C
      WRITE(6,*)
      WRITE(6,*) '   SELECT DATA RESOLUTION'
      WRITE(6,*) '      1 - 48 SEC'
      WRITE(6,*) '      2 - 9.6 SEC'
      WRITE(6,*) '      3 - 1.92 SEC'
      READ(5,*) IAV
      IF ( IAV.LT.1 .OR. IAV.GT.3 ) THEN
       STOP
      ELSE IF ( IAV.EQ.2 ) THEN
       DELTA(1) = 0
       DELTA(2) = 0
       DELTA(3) = 0
       DELTA(4) = 0
       DELTA(5) = 9
       DELTA(6) = 600
      ELSE IF ( IAV.EQ.3 ) THEN
       DELTA(1) = 0
       DELTA(2) = 0
       DELTA(3) = 0
       DELTA(4) = 0
       DELTA(5) = 1
       DELTA(6) = 920
      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) (STIME(I),I=1,5)
    2 T1 = REALTIME(STIME)
C
      WRITE(6,*)
      WRITE(6,*) '   ENTER STOP TIME'
      WRITE(6,*) 'YY DDD HH MM SS'
      READ(5,'(I2,1X,I3,3(1X,I2))',END=3) (ETIME(I),I=1,5)
    3 T2 = REALTIME(ETIME)
      NCNT = 0
   10 CONTINUE
       READ(70,'(Q,<LEN>A1)',END=100,ERR=10) LEN,(REC(I),I=1,LEN)
       NCNT = NCNT + 1
C
       IF (DATAID(1).EQ.8) THEN
C
C WRITE HEADER DATA
C
        IF (MAGSYS(21).AND.MAGSYS(22)) THEN
         MODE = 'PRI '
        ELSE IF (MAGSYS(21)) THEN
         MODE = 'PRI '
        ELSE IF (MAGSYS(22)) THEN
         MODE = 'SEC '
        ELSE 
         MODE = 'DUAL'
        END IF
        WRITE(80,801) NCNT,RECTYPE,TIME,MODE
        GOTO 10
       END IF
C
       IF (DATAID(1).NE.1) GOTO 10
C
C ADD TIME CHECK HERE
C
       IF ( REALTIME(TIME).LT.T1 ) GOTO 10
       IF ( REALTIME(TIME).GE.T2 ) GOTO 100
       READ(FLTID,'(3X,I1)') ID
       IF ( IAV.EQ.1 ) THEN
C
C WRITE 48 SEC AVGS
C
        WRITE(80,805) ID,TIME,FMAG48,FMOD48,
     &               DEL48,LAM48,B48,
     &               RMS48,IB48,RANGE
C    &               SX48,SY48,SZ48,IB48  ! S/C FIELD
       ELSE IF ( IAV.EQ.2 ) THEN
C
C WRITE 9.6 SEC AVGS
C
        DO I96 = 1,5
         WRITE(80,805) ID,TIME,FMAG96(I96),FMOD96(I96),
     &                DEL96(I96),LAM96(I96),(B96(I96,I),I=1,3),
     &                (RMS96(I,I96),I=1,3),IB96(I96)
         CALL INC_TIME(TIME,DELTA)
        END DO
       ELSE 
C
C WRITE 1.92 SEC AVGS
C
        DO I192 = 1,25
         WRITE(80,805) ID,TIME,FMAG192(I192),FMOD192(I192),
     &                DEL192(I192),LAM192(I192),(B192(I192,I),I=1,3),
     &                (RMS192(I,I192),I=1,3),IB192(I192)
         CALL INC_TIME(TIME,DELTA)
        END DO
       END IF
      GOTO 10
  100 CONTINUE
      WRITE(6,*)
      WRITE(6,'(1X,''NUMBER OF RECORDS READ: '',I5)') NCNT
      CLOSE(70)
      CLOSE(80)
C
      STOP
  801 FORMAT(1X,I5,1X,A4,6(1X,I3),1X,A4)
  805 FORMAT(1X,I1,1X,I2,1X,I3.3,3(1X,I2),1X,I3.3,2(1X,F9.3),2(1X,F5.1),
     &       6(1X,F9.3),1X,I2,1X,F9.3)
      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
