      SUBROUTINE DELETE(Q,TQ,NQ,NORUN,SAMTIM,LIM,HELJD)     
C     
C     ROUTINE TO CHUCK OUT POINTS ACCORDING TO VALUES OF IC1 IC2  
C     IC1 , IC2 - 0  : ALL ZERO POINTS ARE DELETED    
C               < 0  : ALL VALUES OUTSIDE THE RANGE VAL1 TO VAL2 DELETED
C               > 0  : ALL POINTS IN THE SPECIFIED INTERVAL DELETED     
C               = 999: ALL POINTS OUTSIDE THE SPECIFIED INTERVAL AROUND 
C                      AN EPHEMERIS EVENT (USUALLY AN ECLIPSE) WILL BE  
C                      DELETED
C               = -1 : IF BOTH IC1 AND IC2 ARE -1, THE ABOVE <0 OPTION  
C                      IS NOT SELECTED.  INSTEAD, ANY POINTS WHICH ARE  
C                      ZERO OCCURRING AT THE BEGINNING OR ENDING OF A   
C                      RUN ARE DELETED.  THIS CLIPS THE ENDS OF RUNS SO 
C                      THAT THE USE OF THE BRIDGE FUNCTION WILL NOT     
C                      RESULT IN INTERPOLATION BETWEEN RUNS.
C     
      DIMENSION Q(NQ),SAMTIM(NORUN),LIM(2,NORUN)   
      DOUBLE PRECISION TQ(NQ)
      DOUBLE PRECISION TIME,E,EPOCH,PERIOD,SECOND,HELJD(NORUN)  
      NQOLD=NQ    
      READ *,IC1,IC2    
      IF(IC1.EQ.999.AND.IC2.EQ.999)GO TO 100    
      IF(IC1.EQ.-1.AND.IC2.EQ.-1)GO TO 200
      IF(IC1.EQ.0.AND.IC2.EQ.0)THEN 
        PRINT 1   
    1   FORMAT(12X,'ALL ZERO POINTS WILL BE DELETED') 
      END IF
      IF(IC1.GT.0.AND.IC2.GT.0)THEN 
        PRINT 2,IC1,IC2 
    2   FORMAT(12X,'ALL POINTS WILL BE DELETED WITHIN THE SPECIFIED'/   
     *         12X,'CHANNEL NUMBERS STARTING WITH ',I6,' TO ',I6/)
        GO TO 40  
      END IF
      IF(IC1.LT.0.AND.IC2.LT.0)THEN 
        PRINT 3   
    3   FORMAT(12X,'ALL POINTS WITH VALUES OUTSIDE THE SPECIFIED'/
     *         12X,'RANGE WILL BE DELETED')     
        GO TO 75  
      END IF
      JJJ=0 
      I=0   
    5 JJJ=JJJ+1   
      I=I+1 
      IF(JJJ.GT.NQ)GO TO 25   
      IF(Q(JJJ).NE.0.0)GO TO 5
      I1=I  
      IC1=JJJ     
    6 JJJ=JJJ+1   
      I=I+1 
      IF(JJJ.LE.NQ.AND.Q(JJJ).EQ.0.0)GO TO 6    
      IC2=JJJ-1   
      I2=I-1
      PRINT 15,I1,Q(IC1),TQ(IC1)    
      PRINT 52    
      PRINT 15,I2,Q(IC2),TQ(IC2)    
      PRINT *     
      DO 7 JJ=1,100000  
        J=IC1+JJ-1
        K=IC2+JJ  
        IF(K.GT.NQ)GO TO 8    
        Q(J)=Q(K) 
        TQ(J)=TQ(K)     
    7 CONTINUE    
      STOP 'DELETE ERROR'     
    8 IDIFF=IC2-IC1+1   
      NQ=NQ-IDIFF 
      DO 10 JK=1,NORUN  
        IF(LIM(1,JK).GE.IC1.AND.JK.GT.1)LIM(1,JK)=LIM(2,JK-1)+1   
        IF(LIM(2,JK).GE.IC1)THEN    
          IF(LIM(2,JK).GE.IC2)THEN  
            LIM(2,JK)=LIM(2,JK)-IDIFF     
          ELSE    
            LIM(2,JK)=IC1-1   
          END IF  
        END IF    
   10 CONTINUE    
      JJJ=IC1     
      GO TO 5     
C     
   15 FORMAT(12X,'CHANNEL ',I6,' IS DISCARDED , VALUE IS ',G15.4, 
     *       ' AT TIME ',G15.4)     
   25 PRINT   30,NQ     
   30 FORMAT(12X,10HNQ IS NOW ,I6)  
      PRINT   35,NORUN,(LIM(1,J),LIM(2,J),J=1,NORUN)  
   35 FORMAT(12X,'INDEX POINTERS FOR THE ',I3,' RUNS :'/    
     *       (14X,'FIRST POINT IS ',I6,'   LAST POINT IS ',I6))   
      RETURN
   40 PRINT 15,IC1,Q(IC1),TQ(IC1)   
      PRINT 52    
   52 FORMAT(12X,'THROUGH')   
      PRINT 15,IC2,Q(IC2),TQ(IC2)   
      PRINT *     
      DO   60 JJ=1,100000     
        J=IC1+(JJ-1)    
        K=IC2+JJ  
        IF(K.GT.NQ)GO TO   70 
        Q(J)=Q(K) 
        TQ(J)=TQ(K)     
   60 CONTINUE    
      STOP 'DELETE'     
   70 NQ=NQ-(IC2-IC1+1) 
      IDIFF=IC2-IC1+1   
      DO   65 JK=1,NORUN
        IF(LIM(1,JK).GE.IC1.AND.JK.GT.1)LIM(1,JK)=LIM(2,JK-1)+1   
        IF(LIM(2,JK).GE.IC1)THEN    
          IF(LIM(2,JK).GE.IC2)THEN  
            LIM(2,JK)=LIM(2,JK)-IDIFF     
          ELSE    
            LIM(2,JK)=IC1-1   
          END IF  
        END IF    
   65 CONTINUE    
      READ *,IC1,IC2    
      IF(IC1.NE.0.AND.IC2.NE.0)GO TO   40 
      PRINT   30,NQ     
      PRINT   35,NORUN,(LIM(1,J),LIM(2,J),J=1,NORUN)  
      RETURN
   75 READ *,VAL1,VAL2  
      JJJ=0 
      NQOLD=NQ    
      DO   80 J=1,NQOLD 
   76   JJJ=JJJ+1 
        IF(Q(J).LE.VAL2.AND.Q(J).GE.VAL1)GO TO   80   
        PRINT   15,JJJ,Q(J),TQ(J)   
        JJ=J+1    
        DO   78 K=JJ,NQ 
          Q(K-1)=Q(K)   
          TQ(K-1)=TQ(K) 
   78   CONTINUE  
        IF(NORUN.EQ.0)GO TO   79    
        DO   77 JKJK=1,NORUN  
          IF(LIM(1,JKJK).GT.J)LIM(1,JKJK)=LIM(1,JKJK)-1     
          IF(LIM(2,JKJK).GE.J)LIM(2,JKJK)=LIM(2,JKJK)-1     
   77   CONTINUE  
   79   NQ=NQ-1   
        IF(J.GT.NQ)GO TO   82 
        GO TO   76
   80 CONTINUE    
   82 PRINT   30,NQ     
      PRINT   35,NORUN,(LIM(1,J),LIM(2,J),J=1,NORUN)  
      RETURN

  100 READ *,EPOCH,PERIOD,SECOND    
      READ *,BEGIN,END  
      PRINT 110,EPOCH,PERIOD,SECOND,BEGIN,END   
  110 FORMAT(12X,'ALL POINTS OUTSIDE THE INTERVAL SURROUNDING THE'/     
     *       12X,'FOLLOWING EPHEMERIS WILL BE DELETED.'/    
     *       12X,'EPHEMERIS : EPOCH - ',F15.5/  
     *       12X,'            P     - ',F15.5/  
     *       12X,'            2ND   - ',1PE15.5/
     *       12X,'BEGINNING WITH PHASE ',0PF10.5,' UNTIL ',F10.5) 
      JJJ=NQ+1
      NQOLD=NQ    
  120 JJJ=JJJ-1 
      IF(JJJ.LT.1)GO TO 160
      TIME=TQ(JJJ)
      TIME=TIME/86400.0D0+HELJD(1)
      IF(BEGIN.LT.0.0)N=NINT((TIME-EPOCH)/PERIOD) 
      IF(BEGIN.GE.0.0)N=INT((TIME-EPOCH)/PERIOD) 
      E=EPOCH+PERIOD*N+SECOND*N*N 
      IF(TIME.GE.E+BEGIN*PERIOD.AND.TIME.LE.E+END*PERIOD)GO TO 120    
      IC2=JJJ

  125 JJJ=JJJ-1
      IF(JJJ.EQ.0)GO TO 130
      TIME=TQ(JJJ)
      TIME=TIME/86400.0D0+HELJD(1)
      IF(BEGIN.LT.0.0)N=NINT((TIME-EPOCH)/PERIOD) 
      IF(BEGIN.GE.0.0)N=INT((TIME-EPOCH)/PERIOD) 
      E=EPOCH+PERIOD*N+SECOND*N*N 
      IF(TIME.LT.E+BEGIN*PERIOD.OR.TIME.GT.E+END*PERIOD)GO TO 125    
  130 IC1=JJJ+1

      DO JJ=1,10000000
        J=IC1+JJ-1
        K=IC2+JJ
        IF(K.GT.NQ)GO TO 140
        Q(J)=Q(K)
        TQ(J)=TQ(K)
      END DO
      STOP 'DELETE ERROR'

  140 IDIFF=IC2-IC1+1
      NQ=NQ-IDIFF
      DO JK=1,NORUN
        IF(LIM(1,JK).GE.IC1.AND.JK.GT.1)LIM(1,JK)=LIM(2,JK-1)+1   
        IF(LIM(2,JK).GE.IC1)THEN    
          IF(LIM(2,JK).GE.IC2)THEN  
            LIM(2,JK)=LIM(2,JK)-IDIFF     
          ELSE    
            LIM(2,JK)=IC1-1   
          END IF  
          IF(LIM(2,JK).LT.LIM(1,JK))THEN
            PRINT 145,JK,LIM(1,JK),LIM(2,JK),NQ,IDIFF,IC1,IC2
  145       FORMAT(12X,'RUN ',I3,' DELETED   '/
     *             12X,'LIM(1,JK):        ',I10/
     *             12X,'LIM(2,JK):        ',I10/
     *             12X,'NQ:               ',I10/
     *             12X,'IDIFF:            ',I10/
     *             12X,'IC1:              ',I10/
     *             12X,'IC2:              ',I10)
          END IF
        END IF    
      END DO
      GO TO 120

  160 PRINT 30,NQ 
      PRINT   35,NORUN,(LIM(1,J),LIM(2,J),J=1,NORUN)  
      RETURN
  200 PRINT 205   
  205 FORMAT(12X,'ALL ZERO POINTS AT START AND ENDS OF RUNS TO'/  
     *       12X,'BE DELETED'/)     
      NQOLD=NQ    
      DO 300 I=NORUN,1,-1     
        JJJ=LIM(2,I)    
  210   IF(Q(JJJ).NE.0.0)GO TO 215  
        JJJ=JJJ-1 
        IF(JJJ.GE.LIM(1,I))GO TO 210
  215   IF(JJJ.EQ.LIM(2,I))GO TO 235
        IC2=LIM(2,I)    
        IC1=JJJ+1 
        PRINT 15,IC1,Q(IC1),TQ(IC1) 
        PRINT 52  
        PRINT 15,IC2,Q(IC2),TQ(IC2) 
        DO 220 JJ=1,10000000  
          J=IC1+JJ-1    
          K=IC2+JJ
          IF(K.GT.NQ)GO TO 225
          Q(J)=Q(K)     
          TQ(J)=TQ(K)   
  220   CONTINUE  
        STOP 'DELETE ERROR'   
  225   IDIFF=IC2-IC1+1 
        NQ=NQ-IDIFF     
        DO 230 JK=I,NORUN     
          IF(JK.GT.I)LIM(1,JK)=LIM(1,JK)-IDIFF  
          LIM(2,JK)=LIM(2,JK)-IDIFF 
  230   CONTINUE  
  235   JJJ=LIM(1,I)    
  240   IF(Q(JJJ).NE.0.0)GO TO 250  
        JJJ=JJJ+1 
        IF(JJJ.LE.LIM(2,I))GO TO 240
  250   IF(JJJ.EQ.LIM(1,I))GO TO 300
        IC1=LIM(1,I)    
        IC2=JJJ-1 
        PRINT 15,IC1,Q(IC1),TQ(IC1) 
        PRINT 52  
        PRINT 15,IC2,Q(IC2),TQ(IC2) 
        DO 260 JJ=1,10000000  
          J=IC1+JJ-1    
          K=IC2+JJ
          IF(K.GT.NQ)GO TO 275
          Q(J)=Q(K)     
          TQ(J)=TQ(K)   
  260   CONTINUE  
        STOP 'DELETE ERROR'   
  275   IDIFF=IC2-IC1+1 
        NQ=NQ-IDIFF     
        DO 280 JK=I,NORUN     
          IF(JK.GT.I)LIM(1,JK)=LIM(1,JK)-IDIFF  
          LIM(2,JK)=LIM(2,JK)-IDIFF 
  280   CONTINUE  
  300 CONTINUE    
  310 PRINT 30,NQ 
      PRINT 35,NORUN,(LIM(1,J),LIM(2,J),J=1,NORUN)    
      RETURN
      END   
