	program delpchthr2
c .......................................................Nov 2019
C Exclude consequtive days
C
C Select peaks of delPch=Pchmax(3d)-Pchmin(-3d) for del > Threshold Th(year)
C movind 3day window, 
C Select all Del >0: output => 'delpchpos.res'
C for 2011-2018
C
c
	DIMENSION del(1435),ryears(1435),pchm(1435)
	+,pmin(1435),phis(1435)
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
	CHARACTER*64,infile,outfile,txt
	CHARACTER*2 AMD(1435,2),AMN,ADY
C
	CALL DATE_AND_TIME(DATE=DD,TIME=TT,ZONE=ZZ)
      TT(5:)='      '
      WRITE(*,*) 'PC Date: Year,Month,Day = ',DD,'Time = ',TT
	        PI=ATAN(1.0)*4.
C
  791 FORMAT(A10,1X,I4)
Crem      outfile='delpchthr1.res' ! All Del >0. except day-by-day
      outfile='delpchthr.res' ! All Del >0. except day-by-day
	OPEN(11,FILE=OUTFILE)
C
Crem   36	format(1X,F8.3,2(1X,A2),3(1X,F7.2),1X,I4,1X,F5.3)
   36	format(1X,F8.3,2(1X,A2),3(1X,F7.2),1X,F6.3,1X,F5.1,1X,I4)
   30	format(A64)

Crem	infile='delpchpos1.res'
	infile='delpchthr2.res'
		OPEN(10,FILE=infile,ACTION='READ')
	read(10,30) txt !~ title line
	write(11,30) txt
C
 	icntot=0 ! count of dPch
	do ii=1,1435
	read(10,36,end=102) RYEARS(ii),AMD(ii,1),AMD(ii,2),pmin(ii)
Crem	+,pchm(ii),del(ii),knum,phis(ii)
	+,pchm(ii),del(ii),phis(ii),thr,num
      icntot=icntot+1
	 write(*,36) RYEARS(ii),AMD(ii,1),AMD(ii,2),pmin(ii)
	+,pchm(ii),del(ii),phis(ii),thr,icntot
	 write(11,36) RYEARS(ii),AMD(ii,1),AMD(ii,2),pmin(ii)
	+,pchm(ii),del(ii),phis(ii),thr,icntot
	enddo      
  55  CLOSE(unit=10)
      goto 102
C start
C cycle on events	
	nres=0
	nout=0
	DO 101 ii=1,icntot
c
       xyear=RYEARS(ii)
     	 dpch=del(ii)
C	     f(x) = a*(sin(xyear-pi)) + b*((xyear-10)^2) + c
Coefficients (with 95% confidence bounds):
       a =      -9.379
       b =    0.003629
       c =  -1.447e+04

	thr=a*(sin(xyear-pi)) + b*((xyear-10.)**2.) + c
      	IF (del(ii).lt.thr) THEN
C
	goto 101
	                    ELSE
C prepare output	
C
C Output results:
      AMN=AMD(ii,1)
	ADY=AMD(ii,2)
      nout=nout+1
	pchmin=pmin(ii)
	pchmax=pchm(ii)
	delpch=del(ii)
	phi=phis(ii)
   37	format(1X,F8.3,2(1X,A2),3(1X,F7.2),1X,F6.3,1X,F5.1,1X,I4)
 99       nout_pre=nout
      nres=nres+1
	write(*,37) XYEAR,AMN,ADY,pchmin
	+,pchmax,delpch,phi,thr,nres
      write(11,37) XYEAR,AMN,ADY,pchmin
	+,pchmax,delpch,phi,thr,nres
C
	                      ENDIF
101	CONTINUE	   
102	CLOSE(unit=11)
	goto 900
    2 CONTINUE
           write(*,*) 'INPUT FILE IS NOT IN YOUR DIRECTORY '
  900 pause ' '
      STOP
      END

