C**********************************************************************
C                                                                     *
C    INTERFACE ROUTINE FOR VOYAGER POST FILTERING                     *
C                                                                     *
C    EXTRACT LFM RECORDS AND TRAILER RECORD FROM VOYAGER              *
C    SUMMARY TAPES. BUFFER HEADER DATA AND B1, B2, B3 DATA            *
C    FOR USE BY VOYAGER POST FILTERING PROGRAM.                       *
C                                                                     *
C    R. S. KENNON CODE 694                                            *
C                                                                     *
C    INPUT: VOYAGER SUMMARY TAPE                                      *
C                                                                     *
C    PDL:                                                             *
C      READ SUMMARY TAPE                                              *
C      IF RECORD TYPE NOT LFM OR TRAILER LABEL GET NEXT RECORD        *
C      IF RECORD TYPE IS LFM                                          *
C       STORE HEADER RECORD IN HEADER BUFFER                          *
C       STORE B1,B2,B3 IN BBUF(3,20000)                               *
C       STORE RMS IN RMS BUFFER                                       *
C       STORE N IN N BUFFER                                           *
C         STORE 10000 POINTS IN BBUF                                  *
C        IF STARTUP                                                   *
C         FILL BBUF WITH 20000 POINTS                                 *
C        ENDIF                                                        *
C       CALL POST FILTER ROUTINES                                     *
C       CALL FILOUT TO OUTPUT FILTERED POINTS                         *
C      ENDIF                                                          *
C      IF RECORD TYPE IS SECOND HDR1                                  *
C       CREATE DUMMY HEADER RECORD                                    *
C       CALL POST FILTER ROUTINES                                     *
C       CALL FILOUT TO OUTPUT FILTERED POINTS                         *
C       CALL POST FILTER ROUTINES                                     *
C       CALL FILOUT TO OUTPUT TRAILER RECORD                          *
C      ENDIF                                                          *
C                                                                     *
C                                                                     *
C      OPEN I/O UNITS FROM THIS ROUTINE  SBK 06/16/94                 *
C                                                                     *
C      I/O UNITS                                                      *
C         4  FILTER STATISTICS FILE                                   *
C         8  FILTER COEFFICIENT NAME FILE                             *
C         9  FILTER COEFFICIENT FILE                                  *
C        10  FILTER PARAMETERS FILE                                   *
C                                                                     *
C**********************************************************************
      LOGICAL*1 REC(10000)
      INTEGER*4 RECORD(568),LENGTH,HDATA(25600),
     &          SDATA(32000),N(25),NBUF(20000)
      INTEGER*2 HEAD(51200)
      REAL*4 B1(25),B2(25),B3(25),BBUF(3,20000),RMS(75),
     &       RMSBUF(3,20000),FILBUF(3,20000)
      CHARACTER LFM*4,RTYPE*4,HDR1*4,DSN*50,COFINM*8,COEFNM*8
      LOGICAL*1 STARTUP,TRAIL,SPL
      EQUIVALENCE (RTYPE,RECORD(1)),    (B1(1),RECORD(199)),
     &            (B2(1),RECORD(224)),  (B3(1),RECORD(249)),
     &            (RMS(1),RECORD(274)), (N(1),RECORD(349)),
     &            (COFINM,RECORD(18)),
     &            (HEAD(1),HDATA(1)),   (REC(1), RECORD(1))
      DATA LFM/'LFM '/, HDR1/'HDR1'/,STARTUP/.TRUE./, TRAIL/.FALSE./,
     &     SPL/.FALSE./,NOR/400/
C
C OPEN FILTER STATISTICS FILE
C
      WRITE(6,*)
      WRITE(6,*) 'ENTER FILTER STATISTICS FILE NAME'
      READ(5,'(A)') DSN
      WRITE(6,*) DSN
      OPEN(4,FILE=DSN,FORM='FORMATTED',STATUS='NEW',
     &     RECORDTYPE='VARIABLE',RECL=8191,IOSTAT=IOS)
      WRITE(6,*) IOS,' UNIT 4 OPEN STATUS'
      IF ( IOS.NE.0 ) STOP
C
C OPEN FILTER COEFFICIENT NAME FILE
C
      WRITE(6,*)
      WRITE(6,*) 'ENTER FILTER COEFFICIENT NAME FILE'
      READ(5,'(A)') DSN 
      WRITE(6,*) DSN
      OPEN(8,FILE=DSN,STATUS='OLD',FORM='FORMATTED',IOSTAT=IOS,
     &     READONLY)
      WRITE(6,*) IOS,' UNIT 8 OPEN STATUS'
      IF ( IOS.NE.0 ) STOP
C
C GET MOST CURRENT COEFFICIENT FILE NAME
C 
      IYEAR=0
      IDAY=0
 100  FORMAT(I2,1X,I3,A8)
      DO I=1,10000
       READ(8,100,END=24) IYEAR,IDATE,COEFNM
C      IRDDAT=(IYEAR*1000)+IDATE
C      IF(IRDDAT.GE.INDATE) GO TO 24
      END DO
  24  CONTINUE
C
C OPEN FILTER COEFFICIENTS FILE
C
      WRITE(6,*)
      WRITE(6,*) 'ENTER FILTER COEFFICIENT FILE NAME'
      WRITE(6,*) DSN
      OPEN(UNIT=9,FILE='DKB100:[VOYAGER.DATA.FILTER]'//COEFNM//'.DAT',
     & STATUS='OLD',IOSTAT=IOS,READONLY)
      WRITE(6,*) IOS,' UNIT 9 OPEN STATUS'
      IF ( IOS.NE.0 ) STOP
C 
C OPEN FILTER PARAMETERS FILE
C
      WRITE(6,*)
      WRITE(6,*) 'ENTER FILTER PARAMETERS FILE NAME'
      READ(5,'(A)') DSN 
      WRITE(6,*) DSN
      OPEN(10,FILE=DSN,STATUS='OLD',FORM='FORMATTED',IOSTAT=IOS,
     &     READONLY)
      WRITE(6,*) IOS,' UNIT 10 OPEN STATUS'
      IF ( IOS.NE.0 ) STOP
C
C OPEN INPUT SUMMARY DATA FILE
C
      WRITE(6,*)
      WRITE(6,*) 'ENTER INPUT SUMMARY DSN'
      READ(5,'(A)') DSN
      WRITE(6,*) DSN
      OPEN(11,FILE=DSN,FORM='FORMATTED',STATUS='OLD',
     &     RECORDTYPE='VARIABLE',RECL=8191,IOSTAT=IOS,READONLY)
      WRITE(6,*) IOS,' UNIT 11 OPEN STATUS'
      IF ( IOS.NE.0 ) STOP
C
C OPEN OUTPUT 1.92 SECOND FILTERED SUMMARY FILE
C
      WRITE(6,*)
      WRITE(6,*) 'ENTER OUTPUT SUMMARY DSN'
      READ(5,'(A)') DSN
      WRITE(6,*) DSN
      OPEN(12,FILE=DSN,FORM='UNFORMATTED',STATUS='NEW',
     &     RECORDTYPE='VARIABLE',RECL=8191,IOSTAT=IOS)
      WRITE(6,*) IOS,' UNIT 12 OPEN STATUS'
      IF ( IOS.NE.0 ) STOP
C
      WRITE(6,*)
      WRITE(6,*) 'BEGIN FILTERING SUMMARY DATA...'
      WRITE(6,*)
   3  CONTINUE
      L=0
      J=0
      JS = 0
      DO 25 M=1,2
       DO 18 NR=1,400
   20   CONTINUE
C
C******* READ SUMMARY TAPE ********************************************
C
    5   CONTINUE
        READ(11,'(Q,<LEN>A1)',END=10,ERR=20) LEN,(REC(I),I=1,LEN)
        COFINM = COEFNM
        GO TO 6
   10   CONTINUE
        WRITE(4,*)' HIT END OF FILE. DID NOT DETECT TRAILER LABEL !'
        RTYPE = HDR1
C
C******* CHECK FOR TRAILER LABEL **************************************
C
    6   CONTINUE
C
        IF (RTYPE.EQ.HDR1.AND.TRAIL) THEN
         DO 8 I=1,32
          J=J+1
          HDATA(J)=0
    8    CONTINUE
C
         IF (STARTUP) THEN
          IYR=HEAD(7)
          IDY=HEAD(8)
          IHR=HEAD(9)
          IMN=HEAD(10)
          ISC=HEAD(11)
          IMS=HEAD(12)
          IF (L.LT.10000) SPL=.TRUE.
         END IF
         CALL FILMAIN(BBUF,HDATA,FILBUF,COFINM,IOS)
C
         IF (.NOT.SPL) THEN
          CALL FILOUT(FILBUF,RECORD,HDATA,SDATA,RMSBUF,NBUF,NOR,M)
          CALL FILMAIN(BBUF,HDATA,FILBUF,COFINM,IOS)
         END IF
C
         NOR= L/25
         IF (NOR.GE.400) NOR=NOR-400
         MM=M+1
         IF (MM.EQ.3) MM=1
         CALL FILOUT(FILBUF,RECORD,HDATA,SDATA,RMSBUF,NBUF,NOR,MM)
         WRITE(12) RECORD
C******** PRINT OUT START TIME AND END TIME OF SUMMARY TAPE ***********
         JJ=2*J-64
         NYR=HEAD(JJ-57)
         NDY=HEAD(JJ-56)
         NHR=HEAD(JJ-55)
         NMN=HEAD(JJ-54)
         NSC=HEAD(JJ-53)
         NMS=HEAD(JJ-52)
         WRITE(4,101)
         WRITE(4,102) IYR,IDY,IHR,IMN,ISC,IMS
         WRITE(4,103)
         WRITE(4,102) NYR,NDY,NHR,NMN,NSC,NMS
         STOP
        ELSE
         TRAIL=.TRUE.
         IF (RTYPE.EQ.'HDR1') WRITE(12) RECORD
        END IF
C
        IF (RTYPE.NE.LFM) GOTO 5
C
C****** STORE HEADER *************************************************
C
        DO 12 I=1,32
         J=J+1
         HDATA(J)=RECORD(I)
   12   CONTINUE
C
C****** BUFFER X,Y,Z AND RMS'S **************************************
C
        DO 22 K=1,25
         L=L+1
C
C REMOVE VALUES GREATER THAN 0.5 nT  SBK 06/23/94
C
         IF ( B1(K).GT.0.5 .OR. 
     &        B2(K).GT.0.5 .OR.
     &        B3(K).GT.0.5 ) THEN
          B1(K) = 999.0
          B2(K) = 999.0
          B3(K) = 999.0
         END IF
C
         BBUF(1,L)=B1(K)
         BBUF(2,L)=B2(K)
         BBUF(3,L)=B3(K)
C        FILBUF(1,L)=B1(K)  ! TEST ASSIGNMENT
C        FILBUF(2,L)=B2(K)  ! TEST ASSIGNMENT
C        FILBUF(3,L)=B3(K)  ! TEST ASSIGNMENT
         RMSBUF(1,L)=RMS(K)
         RMSBUF(2,L)=RMS(K+25)
         RMSBUF(3,L)=RMS(K+50)
         NBUF(L)=N(K)
   22   CONTINUE
C
C****** STORE SEDR BLOCK **********************************************
C
        DO 32 IS=1,40
         JS = JS + 1
         SDATA(JS) = RECORD(528+IS)
   32   CONTINUE
C
   18  CONTINUE
C
       IF (STARTUP) THEN
        STARTUP=.FALSE.
        IYR=HEAD(7)
        IDY=HEAD(8)
        IHR=HEAD(9)
        IMN=HEAD(10)
        ISC=HEAD(11)
        IMS=HEAD(12)
       ELSE
C
C***** CALL STEVE'S ROUTINES ******************************************
C******** FIRST CALL AFTER 800 SUMMARY RECORDS (20,000 1.92 SEC AVGS) *
C******** NEXT CALLS AFTER 400 SUMMARY RECORDS (10,000 1.92 SEC AVGS) *
C
        CALL FILMAIN(BBUF,HDATA,FILBUF,COFINM,IOS)
        IF (IOS.NE.0) STOP
C
C****** OUTPUT RECORD *************************************************
C
        CALL FILOUT(FILBUF,RECORD,HDATA,SDATA,RMSBUF,NBUF,NOR,M)
       END IF
C
   25 CONTINUE
C
      GO TO 3
  101 FORMAT(1X,'START TIME OF SUMMARY TAPE')
  102 FORMAT(1X,I2,1X,I3,':',I2,':',I2,':',I2,'.',I3)
  103 FORMAT(1X,'LAST TIME ON SUMMARY TAPE')
      END
