      SUBROUTINE QAVE(TQ,Q,NOQ,NORUN,SAMTIM,LIM,HELJD)     
C     THIS SUBROUTINE AVERAGES CONSECUTIVE CHANNELS IN ARRAY
C     Q. IT ALSO ALLOWS SPECIFIC PHASE INTERVALS TO BE
C     EXCLUDED FROM THE AVERAGING.
C     ***** INPUT DATA *****  
C     1.) NAV, THE REQUIRED SAMPLING TIME (FREE FORMAT)     
C     2.) NINTVL, NUMBER OF PHASE INTERVALS
C     3.) IF NINTVL>0, EPOCH AND PERIOD AND SECOND
C     4.) PHASE INTERVALS..
C     3.) IF NINTVL<0, THEN NO EPHEMERIS AND SIMPLY
C     4.) INDEX INTERVALS TO AVOID IN THE AVERAGING
C     
      DIMENSION Q(NOQ),SAMTIM(NORUN),LIM(2,NORUN)   
      DIMENSION PH1(200),PH2(200),INT1(200),INT2(200)
      DOUBLE PRECISION TQ(NOQ), TIME, TV, EPOCH, PERIOD, SECOND
      DOUBLE PRECISION SUMT, HELJD(*)

      DO 500 KJ=1,NORUN 
        READ *,NAV,NINTVL
        PRINT 100,NAV
  100   FORMAT(12X,'AVERAGING POINTS ',I5,' AT A TIME') 
        IF(NINTVL.LT.0)GO TO 300
C
C       THIS SECTION IF THE INTERVALS ARE EPHEMERISED PHASE INTERVALS
C
        IF(NINTVL.GT.0)THEN
          READ *,EPOCH,PERIOD,SECOND   
          PRINT 110,EPOCH,PERIOD,SECOND
  110     FORMAT(12X,'ALL POINTS INSIDE PHASE INTERVALS WITH RESPECT'/
     *           12X,'TO THE FOLLOWING EPHEMERIS WILL BE EXCLUDED.'/    
     *           12X,'EPHEMERIS : EPOCH - ',F15.5/  
     *           12X,'            P     - ',F15.5/  
     *           12X,'            2ND   - ',1PE15.5)
          PRINT 115,NINTVL
  115     FORMAT(12X,'NUMBER OF EXCLUSION PHASE INTERVALS:',I5)     
          DO J=1,NINTVL
            READ *,PH1(J),PH2(J)
            PRINT 120,PH1(J),PH2(J)
  120       FORMAT(14X,'FROM PHASE ',0PF10.5,' UNTIL ',F10.5) 
          END DO
        END IF

        IP1=LIM(1,KJ)   
        IP2=LIM(2,KJ)   
        IF(KJ.EQ.1)INDEX=0    
        IF(KJ.GT.1)INDEX=LIM(2,KJ-1)
        N=IP1-1
        M=0
        SUM=0.0
        SUMT=0.0D0
  125   N=N+1
        IF(N.GT.IP2)GO TO 200
        TIME=TQ(N)
        TIME=TIME/86400.0D0+HELJD(1)
        DO K=1,NINTVL
          BEGIN=PH1(K)
          END=PH2(K)
          IF(BEGIN.LT.0.0)NCYC=NINT((TIME-EPOCH)/PERIOD) 
          IF(BEGIN.GE.0.0)NCYC=INT((TIME-EPOCH)/PERIOD) 
          E=EPOCH+PERIOD*NCYC+SECOND*NCYC*NCYC 
          IF(TIME.GE.E+BEGIN*PERIOD.AND.TIME.LE.E+END*PERIOD)THEN
            IF(M.GT.0)THEN
              SUM=SUM/M
              SUMT=SUMT/M
              INDEX=INDEX+1
              Q(INDEX)=SUM
              TQ(INDEX)=SUMT
              M=0
              SUMT=0.0
              SUM=0.0
            END IF
            INDEX=INDEX+1
            Q(INDEX)=Q(N)
            TQ(INDEX)=TQ(N)
            GO TO 125
          END IF
        END DO
        M=M+1
        SUM=SUM+Q(N)
        SUMT=SUMT+TQ(N)
        IF(M.LT.NAV)GO TO 125

        IF(M.GT.0)THEN
          SUM=SUM/M
          SUMT=SUMT/M
          INDEX=INDEX+1
          Q(INDEX)=SUM
          TQ(INDEX)=SUMT
          M=0
          SUMT=0.0
          SUM=0.0
        END IF
        GO TO 125
          
  200   CONTINUE  
        SAMTIM(KJ)=NAV * SAMTIM(KJ)     
        IF(KJ.GT.1)LIM(1,KJ)=LIM(2,KJ-1)+1
        IF(INDEX.NE.0)LIM(2,KJ)=INDEX     
        GO TO 500

C
C       THIS SECTION IF THE INTERVALS ARE SIMPLE INDICES
C
  300   IF(NINTVL.LT.0)THEN
          NINTVL=-NINTVL
          PRINT 315,NINTVL
  315     FORMAT(12X,'NUMBER OF EXCLUSION INTERVALS:',I5)     
          DO J=1,NINTVL
            READ *,INT1(J),INT2(J)
            PRINT 320,INT1(J),INT2(J)
  320       FORMAT(14X,'FROM INDEX ',I6,' UNTIL ',I6) 
          END DO
        END IF

        IP1=LIM(1,KJ)   
        IP2=LIM(2,KJ)   
        IF(KJ.EQ.1)INDEX=0    
        IF(KJ.GT.1)INDEX=LIM(2,KJ-1)
        N=IP1-1
        M=0
        SUM=0.0
        SUMT=0.0D0
  325   N=N+1
        IF(N.GT.IP2)GO TO 400
        TIME=TQ(N)
        TIME=TIME/86400.0D0+HELJD(1)
        DO K=1,NINTVL
          IF(N.GE.INT1(K).AND.N.LE.INT2(K))THEN
            IF(M.GT.0)THEN
              SUM=SUM/M
              SUMT=SUMT/M
              INDEX=INDEX+1
              Q(INDEX)=SUM
              TQ(INDEX)=SUMT
              M=0
              SUMT=0.0
              SUM=0.0
            END IF
            INDEX=INDEX+1
            Q(INDEX)=Q(N)
            TQ(INDEX)=TQ(N)
            GO TO 325
          END IF
        END DO
        M=M+1
        SUM=SUM+Q(N)
        SUMT=SUMT+TQ(N)
        IF(M.LT.NAV)GO TO 325

        IF(M.GT.0)THEN
          SUM=SUM/M
          SUMT=SUMT/M
          INDEX=INDEX+1
          Q(INDEX)=SUM
          TQ(INDEX)=SUMT
          M=0
          SUMT=0.0
          SUM=0.0
        END IF
        GO TO 325
          
  400   CONTINUE  
        SAMTIM(KJ)=NAV * SAMTIM(KJ)     
        IF(KJ.GT.1)LIM(1,KJ)=LIM(2,KJ-1)+1
        IF(INDEX.NE.0)LIM(2,KJ)=INDEX     

  500 CONTINUE    
      NOQ=LIM(2,NORUN)  
      PRINT 550,NORUN,(LIM(1,J),LIM(2,J),SAMTIM(J),J=1,NORUN)     
  550 FORMAT(12X,'INDEX POINTERS FOR THE ',I3,' RUNS :'/    
     *       (14X,'FIRST POINT IS ',I5,'   LAST POINT IS ',I5,    
     *      '    SAMPLING TIME IS ',F6.1))
      PRINT 575,NOQ     
  575 FORMAT(12X,'NQ IS NOW ',I7)   
      RETURN
      END   

