	program delpchpeak
c .......................................................Jan 2019
C Select peaks of delPch=Pchmax(3d)-Pchmin(-3d) for del > 100 i.u.
C movind 3day window, 
C if del_max > 100. during 3 days, output at pchmax during 3 days
C for 2011-2018
C
c
	DIMENSION res(0:3),peak(0:3),del(1763),ryears(1763),pchm(1763)
	+,pmin(1763)
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
	CHARACTER*64,infile,outfile,txt
	CHARACTER*2 AMD(1763,2)
C
	CALL DATE_AND_TIME(DATE=DD,TIME=TT,ZONE=ZZ)
      TT(5:)='      '
      WRITE(*,*) 'PC Date: Year,Month,Day = ',DD,'Time = ',TT
C
  791 FORMAT(A10,1X,I4)
		  outfile='delpchpeak.res'
	OPEN(11,FILE=OUTFILE)
C
   36	format(1X,F8.3,2(1X,A2),3(1X,F7.2),1X,I4)
   30	format(A64)

	infile='delpch3d.txt'
		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,1763
	read(10,36,end=102) RYEARS(ii),AMD(ii,1),AMD(ii,2),pmin(ii)
	+,pchm(ii),del(ii),knum
      icntot=icntot+1
	enddo      
  55  CLOSE(unit=10)
C start
	do i=1,3
	res(i)=0.
	enddo
C cycle on events	
	nres=0
	res(1)=del(1)
	res(2)=del(2)
	peak(1)=pchm(1)
		peak(2)=pchm(2)
	nout=0
	nout_pre=0
	DO 101 ii=3,icntot
c     
	res(3)=del(ii)
		peak(3)=pchm(ii)
	 dpchm=AMAX1(res(1),res(2),res(3))
	IF (dpchm.lt.100.) THEN
C
	goto 100
	                    ELSE
C prepare output	
	pcmax=AMAX1(peak(1),peak(2),peak(3))  
	nout=0
	if ((res(1).gt.100.).and.(peak(1).eq.pcmax)) nout=ii-2
	if ((res(2).gt.100.).and.(peak(2).eq.pcmax)) nout=ii-1
	if ((res(3).gt.100.).and.(peak(3).eq.pcmax)) nout=ii
      if ((nout.eq.0.).or.(nout.eq.nout_pre)) goto 100
C
C Output results:
 99       nout_pre=nout
      nres=nres+1
	write(*,36) RYEARS(nout),AMD(nout,1),AMD(nout,2),pmin(nout)
	+,pchm(nout),del(nout),nres
      write(11,36) RYEARS(nout),AMD(nout,1),AMD(nout,2),pmin(nout)
	+,pchm(nout),del(nout),nres
C
	                      ENDIF
C Move data up 1 line:
100	do k=1,3
	res(k-1)=res(k)
	peak(k-1)=peak(k)
	enddo

101	CONTINUE	   
102	CLOSE(unit=10)
	CLOSE(unit=11)
	goto 900
    2 CONTINUE
           write(*,*) 'INPUT FILE IS NOT IN YOUR DIRECTORY '
  900 pause ' '
      STOP
      END

