******************************************************************
*
*       TITLE:  DATA BUFFERRING ROUTINE
*
*       FILE NAME:  QUE.FOR
*
*       PURPOSE:   TO BUFFER DATA PRIOR TO FILTER PROCESSING
*
*	AUTHOR            DATE            CHANGE
*	--------          ----            -------
*       S. B. KRAMER     12/17/92         ORIGINAL CODE
*                        04/02/93         ADD SECONDARY MODE BUFFERRING
*
******************************************************************
      SUBROUTINE QUE(GAMMA,LEN,GAMMA2,LEN2,
     &               LAST,SIGMUL,PASS,BAD)
C
C BUFFER GAMMA VALUES FROM THREE MAGNETOMETER AXES OF PRIMARY AND 
C SECONDARY MODES.  WHEN SUFFICIENT NUMBER OF POINTS IS ACCUMULATED, 
C PASS ARRAYS TO FILTERING ROUTINE AND THEN AVERAGING ROUTINE.
C PRIMARY AND SECONDARY MODE DATA ARE PROCESSED IN PARALLEL.  IT IS
C ASSUMED THAT THE NUMBER OF SECONDARY MODE POINTS PER RECORD IS ALWAYS
C LESS THAN OR EQUAL TO THE NUMBER OF PRIMARY POINTS.
C
C ORIGINAL CODE WRITTEN BY SANDY KRAMER, HUGHES STX, CODE 692
C MODIFIED FOR PARALLEL PROCESSING OF PRI AND SEC MODES    SBK 09-23-93
C ADD CR-5 AVERAGING CALL   SBK 02-14-95
C FIX RECORD COUNTER (NREC) AND DEFINE SCMODE AS INTEGER   SBK 02-16-95
C FIX FILTER BUFFER INDEXING ON UNPACK   SBK 02-20-95
C
      PARAMETER (IARR=50000,MAX=10000,IFULL=48000,
     &           IBACK=500,IFRONT=500)
C
C IARR    MAX BUFFER ARRAY LENGTH  <= IFULL+IFRONT+IBACK
C MAX     MAX NUMBER OF RECORDS TO BE BUFFERED FOR FILTER
C IFULL   MAX NUMBER OF DATA POINTS TO BE PASSED INTO FILTER
C IFRONT  NUMBER OF FRONT END FILTER PRIME POINTS
C IBACK   NUMBER OF BACK END FILTER PRIME POINTS
C 
      INTEGER*2 BUFFTIME(6),DATATYPE(2),EDRTIME(6),
     &          BEGTIME(6),ENDTIME(6) 
C
      INTEGER*4 NTOTG(6),NTOTB(6),PASS,BLOCKLEN(2,MAX),SCMODE
C
      LOGICAL*1 CLEAN/.TRUE./,LAST
C
      REAL*4 HEADBUFF(32,MAX),HDR(32),AVE(6),
     &       GAMMA(3,LEN),GAMMA2(3,LEN2),
     &       BUFFER(IARR,3),BUFFER2(IARR,3)
C
      SAVE IBUFF,BUFFER,IBUFF2,BUFFER2,HEADBUFF,BLOCKLEN,NREC
C
      DATA IBUFF/0/,IBUFF2/0/,NREC/0/
C
      INCLUDE 'UNPACK.INC'
C
      EQUIVALENCE (SUMOUT(1),      HDR(1)),
     &            (HDR(4),    BUFFTIME(1)),
     &            (HDR(17),   DATATYPE(1))
C
C GO TO FILTER IF FINAL EDR
C
      IF (LAST) GOTO 100
C
C INITIALIZE START PRIME AREA FOR PRIMARY AND SECONDARY MODE BUFFERS 
C
      IF (IBUFF.EQ.0) THEN
       DO J = 1,3
        DO I = 1,IFRONT
         BUFFER(I,J) = BAD
         BUFFER2(I,J) = BAD
        END DO
       END DO
      END IF
C
C LOAD ONE SCIENCE BLOCK OF PRIMARY MODE DATA INTO BUFFER ARRAY
C
      DO I=1,LEN
       IBUFF = IBUFF + 1
       IF (IBUFF.GT.IARR) THEN
        WRITE(6,*) '***QUE***PRIMARY BUFFER ARRAY OVERFLOW'
        STOP
       END IF
       DO J = 1,3
        BUFFER(IBUFF+IFRONT,J) = GAMMA(J,I)
C
C PRIME FRONT END OF PRIMARY MODE BUFFER ARRAY WITH FIRST <IFRONT> POINTS 
C IN REVERSE ORDER
C
        IF (IBUFF.LE.IFRONT) BUFFER(IFRONT+1-IBUFF,J) = GAMMA(J,I)
       END DO
      END DO
C
C LOAD ONE SCIENCE BLOCK OF SECONDARY MODE DATA INTO BUFFER ARRAY
C
      DO I=1,LEN2
       IBUFF2 = IBUFF2 + 1
       IF (IBUFF2.GT.IARR) THEN 
        WRITE(6,*) '***QUE***SECONDARY BUFFER ARRAY OVERFLOW'
        STOP
       END IF
       DO J=1,3
        BUFFER2(IBUFF2+IFRONT,J) = GAMMA2(J,I)
C
C PRIME FRONT END OF SECONDARY MODE BUFFER ARRAY WITH FIRST <IFRONT> POINTS 
C IN REVERSE ORDER
C
        IF (IBUFF2.LE.IFRONT) BUFFER2(IFRONT+1-IBUFF2,J) = GAMMA2(J,I)
       END DO
      END DO
C
C INCREMENT RECORD COUNTER
C
      NREC = NREC + 1
      IF ( NREC.GT.MAX ) THEN
       WRITE(6,*) '***QUE***NUMBER OF RECORDS OVERFLOWS MAX ARRAY SIZE'
       STOP
      END IF
C
C STORE CURRENT HEADER IN BUFFER
C
      DO IHD = 1,32
       HEADBUFF(IHD,NREC) = HDR(IHD)
      END DO
C
C GET BEGINNING TIME
C
      IF (NREC.EQ.1) THEN
       DO IT = 1,6
        BEGTIME(IT) = BUFFTIME(IT)
       END DO
      END IF      
C
C STORE PRIMARY AND SECONDARY DATA COUNTS IN BUFFER
C
      BLOCKLEN(1,NREC) = LEN
      BLOCKLEN(2,NREC) = LEN2
C
      IF (IBUFF.GE.IFULL) THEN
C
  100  CONTINUE
C
C GET END TIME
C
       DO IT = 1,6
        ENDTIME(IT) = BUFFTIME(IT)
       END DO
C
C PRIME FILTER BACK END WITH LAST <IBACK> POINTS IN REVERSE ORDER
C
       DO J = 1,3
C
        DO I = 1,IBACK
C
C PRIMARY MODE FILTER
C
         IF (I.LE.IBUFF) THEN
          BUFFER(IFRONT+IBUFF+I,J) = BUFFER(IFRONT+IBUFF-I+1,J)
         ELSE
          BUFFER(IFRONT+IBUFF+I,J) = BAD
         END IF
C
C SECONDARY MODE FILTER
C
         IF (I.LE.IBUFF2) THEN
          BUFFER2(IFRONT+IBUFF2+I,J) = BUFFER2(IFRONT+IBUFF2-I+1,J)
         ELSE
          BUFFER2(IFRONT+IBUFF2+I,J) = BAD
         END IF
C
        END DO
C
       END DO
       NBUFF = IBUFF + IFRONT + IBACK
       NBUFF2 = IBUFF2 + IFRONT + IBACK
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C PASS BUFFERRED DATA TO FILTERING ROUTINE
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
       IF ( CLEAN ) THEN   ! SET CLEAN TO FALSE FOR TESTING WITHOUT FILTERING 
C
C FILTER PRIMARY MODE GAMMAS
C
        CALL CLEAN1(BUFFER(1,1),NBUFF,NTOTB(1),NTOTG(1),
     &              SIGMUL,BAD,AVE(1),PASS)
C
        CALL CLEAN1(BUFFER(1,2),NBUFF,NTOTB(2),NTOTG(2),
     &              SIGMUL,BAD,AVE(2),PASS)
C
        CALL CLEAN1(BUFFER(1,3),NBUFF,NTOTB(3),NTOTG(3),
     &              SIGMUL,BAD,AVE(3),PASS)
C

C
        WRITE(68,*)
        WRITE(68,*) 'POPULATION FILTER PROCESSING'
        WRITE(68,'(1X,''START TIME:  '',I2,1X,I3.3,3(1X,I2.2),
     &             1X,I3.3)') BEGTIME
        WRITE(68,'(1X,''END TIME:    '',I2,1X,I3.3,3(1X,I2.2),
     &             1X,I3.3)') ENDTIME
        WRITE(68,'(3X,''# NON-FILL PTS'',6X,''# BAD PTS'',11X,
     &        ''AVERAGE VALUE'',5X,''TOTAL'')')
        WRITE(68,'(3X,''X'',5X,''Y'',5X,''Z'',5X,''X'',5X,''Y'',
     &        5X,''Z'',7X,''X'',7X,''Y'',7X,''Z'',4X,''PTS'')')
        WRITE(68,'(3(1X,I5),3(1X,I5),3(1X,F7.3),1X,I5)') 
     &       (NTOTG(I),I=1,3),(NTOTB(I),I=1,3),(AVE(I),I=1,3),
     &       IBUFF
C
C FILTER SECONDARY MODE GAMMAS
C
        CALL CLEAN1(BUFFER2(1,1),NBUFF2,NTOTB(4),NTOTG(4),
     &              SIGMUL,BAD,AVE(4),PASS)
C
        CALL CLEAN1(BUFFER2(1,2),NBUFF2,NTOTB(5),NTOTG(5),
     &              SIGMUL,BAD,AVE(5),PASS)

C
        CALL CLEAN1(BUFFER2(1,3),NBUFF2,NTOTB(6),NTOTG(6),
     &              SIGMUL,BAD,AVE(6),PASS)
C
        WRITE(68,'(3(1X,I5),3(1X,I5),3(1X,F7.3),1X,I5)') 
     &       (NTOTG(I),I=4,6),(NTOTB(I),I=4,6),(AVE(I),I=4,6),
     &       IBUFF2
C
C END FILTERING ROUTINE
C
       END IF
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C EXTRACT PRIMARY AND SECONDARY MODE SCIENCE BLOCKS 
C FROM CONCATENATED BUFFER ARRAYS
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
       IOFF = 0
       IOFF2 = 0
       DO I = 1,NREC
C
        ILEN = BLOCKLEN(1,I)
        ILEN2 = BLOCKLEN(2,I)
C
        DO J = 1,3
C
C PRIMARY MODE REBLOCK
C
         DO K = 1,ILEN
          L = IOFF + K
          GAMMA(J,K) = BUFFER(L+IFRONT,J)
         END DO
C
C SECONDARY MODE REBLOCK
C
         DO K = 1,ILEN2
          L = IOFF2 + K
          GAMMA2(J,K) = BUFFER2(L+IFRONT,J)
         END DO
C
C END PRIMARY AND SECONDARY MAG GAMMAS REBLOCK
C
        END DO
        IOFF = IOFF + ILEN
        IOFF2 = IOFF2 + ILEN2
C
C REBLOCK HEADER BLOCKS
C
        DO IHD = 1,32
         HDR(IHD) = HEADBUFF(IHD,I)
        END DO
C
C EXTRACT SPACECRAFT MODE
C
        SCMODE = DATATYPE(2)
C
C EXTRACT EDR TIME 
C
        DO II = 1,6
         EDRTIME(II) = BUFFTIME(II)
        END DO
C
C CALL SUMMARY AVERAGING ROUTINES
C
        IF (SCMODE.EQ.13) CALL VIM5AVE(GAMMA,GAMMA2,BAD,EDRTIME)
        IF (SCMODE.EQ.6) CALL CR6AVE(GAMMA,GAMMA2,BAD,EDRTIME)
        IF (SCMODE.EQ.5) CALL CR5AVE(GAMMA,GAMMA2,BAD,EDRTIME)
        IF (SCMODE.EQ.4) CALL CR4AVE(GAMMA,GAMMA2,BAD,EDRTIME)
        IF (SCMODE.EQ.3) CALL CR3AVE(GAMMA,GAMMA2,BAD,EDRTIME)
        IF (SCMODE.EQ.2) CALL CR2AVE(GAMMA,GAMMA2,BAD,EDRTIME)
        IF (SCMODE.EQ.1) CALL CR1AVE(GAMMA,GAMMA2,BAD,EDRTIME)
        IF (SCMODE.EQ.0) CALL GS3AVE(GAMMA,GAMMA2,BAD,EDRTIME)
C
       END DO
C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C
C REINITIALIZE POINT AND RECORD COUNTERS
C
       IBUFF = 0
       IBUFF2 = 0
       NREC = 0
C
      END IF
C
      RETURN
      END
