      SUBROUTINE PUTINQ(Q,TQ,NQ,NORUN,SAMTIM,LIM,HELJD)     
C     
C     SUBROUTINE TO ADD IN A SINE WAVE TO Q     
C     MODE=0 : SPECIFY THE SAMPLING TIME AND ANY TIME JUMPS 
C     MODE=-1: USE TIMES OF PREVIOUSLY EXISTING DATA  
C     MODE=-2: AS FOR MODE =-1 EXCEPT ZERO THE ALREADY EXISTING DATA    
C              I.E. DO NOT ADD IN BUT REPLACE   
C     MODE=-3: AS FOR MODE =-1 EXCEPT ADD NOISE IN WITH STDEV GIVEN BY
C              P
C     
      DIMENSION Q(1),SAMTIM(1),LIM(2,1)   
      DOUBLE PRECISION TQ(1),HELJD(1),T,PI,V,STDEV

      PI=3.141592653D0

      READ *,P,DPDT,A,AMEAN,DELT,NP,PHI,MODE 
      PRINT 2000,P,DPDT,A,AMEAN,DELT,NP,PHI,MODE
 2000 FORMAT(12X,'PERIOD',F10.2,'   DPDT',F10.6,3X,'AMPLT',F10.2,3X,'MEA
     2N',F10.2,4X,'INTEG INTERVAL',F5.1,'  NP ',I5    
     3/12X,'PHI ',F6.3/12X,'MODE ',I2)    
      IF(MODE.EQ.-1.OR.MODE.EQ.-2.OR.MODE.EQ.-3)THEN    
        IF(NQ.EQ.0)THEN 
          PRINT *,'NO ALREADY EXISTING DATA - MUST HAVE SOME'     
          STOP    
        END IF    
        NP=NQ     
      END IF
      IF(MODE.EQ.0)THEN 
        IF(NP.EQ.0.OR.DELT.EQ.0.0)THEN    
          PRINT *,'EITHER SAMPLING TIME OR NO OF PTS ZERO IN MODE 0'    
          STOP    
        END IF    
        NQ=NP     
        NORUN=1   
        SAMTIM(1)=DELT  
        LIM(1,1)=1
        LIM(2,1)=NP     
        DO I=1,NP
          Q(I)=0.0
        END DO
      END IF
      IC=-1 
      DO 3000 I=1,NP    
        IF(MODE.EQ.0.AND.I.GT.IC)THEN     
          READ *,IC,XTIME     
          IF(IC.GT.0)PRINT 2700,IC,XTIME  
 2700     FORMAT(12X,'DISCONTINUITY AT CHANNEL ',I5,'  WHICH IS ',     
     *           F11.2,' SECONDS AFTER THE PREVIOUS CHANNEL')
        END IF    
        IF(IC.EQ.0)IC=NP+1          
        IF(MODE.EQ.-1.OR.MODE.EQ.-2.OR.MODE.EQ.-3)THEN  
          T=TQ(I) 
        ELSE
          IF(I.EQ.1)T=DELT/2.0-DELT 
          T=T+DELT
          IF(IC.EQ.I)THEN     
            T=T-DELT+XTIME    
            LIM(2,NORUN)=I-1  
            NORUN=NORUN+1     
            LIM(1,NORUN)=I    
            LIM(2,NORUN)=NP   
          END IF  
          TQ(I)=T 
        END IF    
        IF(MODE.NE.-3)THEN
          ARG=2.0*PI*T/(P+DPDT*T/2.0D0)+PHI
          ARG=DMOD(1.0D0*ARG,2.0D0*PI)            
          Q(I)=Q(I)+AMEAN+A*COS(ARG)  
          IF(MODE.EQ.-2)Q(I)=AMEAN+A*COS(ARG)     
        ELSE
          STDEV=P
          V=0.0
          CALL ERRAN(V,STDEV,I)
          Q(I)=Q(I)+V
        END IF 
 3000 CONTINUE    
      RETURN
      END   



C===========================================================================
C
C		erran(v, se, ij)
C
C	introduces gaussian noise
C
C===========================================================================
C
	subroutine erran(v,se,ij)
	implicit real*8 (a-h, o-z)
C
	s = 0.0
	do i=1,12
	 s = s + ran3(ij)
	end do
 	v = v + se*(s - 6.0)
	return
	end
C
C===============================================================================
C
C				RAN3
C
C	Random number generator
C
C===============================================================================
C
      FUNCTION ran3(idum)
	implicit real*8 (a-h,o-z)
      INTEGER idum
      INTEGER MBIG,MSEED,MZ
      PARAMETER (MBIG=1000000000,MSEED=161803398,MZ=0,FAC=1./MBIG)
      INTEGER i,iff,ii,inext,inextp,k
      INTEGER mj,mk,ma(55)
      SAVE iff,inext,inextp,ma
      DATA iff /0/
C
      if(idum.lt.0.or.iff.eq.0)then
        iff=1
        mj=MSEED-iabs(idum)
        mj=mod(mj,MBIG)
        ma(55)=mj
        mk=1
        do 11 i=1,54
          ii=mod(21*i,55)
          ma(ii)=mk
          mk=mj-mk
          if(mk.lt.MZ)mk=mk+MBIG
          mj=ma(ii)
11      continue
        do 13 k=1,4
          do 12 i=1,55
            ma(i)=ma(i)-ma(1+mod(i+30,55))
            if(ma(i).lt.MZ)ma(i)=ma(i)+MBIG
12        continue
13      continue
        inext=0
        inextp=31
        idum=1
      endif
      inext=inext+1
      if(inext.eq.56)inext=1
      inextp=inextp+1
      if(inextp.eq.56)inextp=1
      mj=ma(inext)-ma(inextp)
      if(mj.lt.MZ)mj=mj+MBIG
      ma(inext)=mj
      ran3=mj*FAC
      return
      END
C  (C) Copr. 1986-92 Numerical Recipes Software m!1.
