	program delpchpos
c .......................................................Jan 2019
C Select peaks of delPch=Pchmax(3d)-Pchmin(-3d) for del > 100 i.u.
C movind 3day window, 
C Select all Del >0: output => 'delpchpos.res'
C for 2011-2018
C
c
	DIMENSION del(3000),ryears(3000),pchm(3000)
	+,pmin(3000)
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
	CHARACTER*64,infile,outfile,txt
	CHARACTER*2 AMD(3000,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
C
  791 FORMAT(A10,1X,I4)
      outfile=     'delpchpos1.res' ! All Del >0. (produce fittting curve and std
	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,3000
	read(10,36,end=102) RYEARS(ii),AMD(ii,1),AMD(ii,2),pmin(ii)
	+,pchm(ii),del(ii),knum
	if(RYEARS(ii).lt.2000.) exit
      icntot=icntot+1
	enddo      
  55  CLOSE(unit=10)
C start
c-	do i=1,3
c-	res(i)=0.
c-	enddo
C cycle on events	
	nres=0
Crem	res(1)=del(1)
Crem	res(2)=del(2)
Crem	peak(1)=pchm(1)
Crem		peak(2)=pchm(2)
	nout=0
	nout_pre=0
	DO 101 ii=1,icntot
c
     
C-	res(3)=del(ii)
C-		peak(3)=pchm(ii)
C-	 dpchm=AMAX1(res(1),res(2),res(3))
C-	IF (dpchm.lt.100.) THEN
      	IF (del(ii).le.0.) THEN
C
c-	goto 100
	goto 101
	                    ELSE
C prepare output	
c-	pcmax=AMAX1(peak(1),peak(2),peak(3))  
c-	nout=0
c-	if ((res(1).gt.100.).and.(peak(1).eq.pcmax)) nout=ii-2
c-	if ((res(2).gt.100.).and.(peak(2).eq.pcmax)) nout=ii-1
c-	if ((res(3).gt.100.).and.(peak(3).eq.pcmax)) nout=ii
c-      if ((nout.eq.0.).or.(nout.eq.nout_pre)) goto 100
C
C Output results:
      AMN=AMD(ii,1)
	ADY=AMD(ii,2)
	XYEAR=RYEARS(ii)
      nout=nout+1
C	ASTR=ryears(nout)
	ASTR=XYEAR
	pchmin=pmin(ii)
	pchmax=pchm(ii)
	delpch=del(ii)
	call subsolphase(ASTR,phi)
   37	format(1X,F8.3,2(1X,A2),3(1X,F7.2),1X,I4,1X,F6.3)
 99       nout_pre=nout
      nres=nres+1
	write(*,37) XYEAR,AMN,ADY,pchmin
	+,pchmax,delpch,nout,phi
      write(11,37) XYEAR,AMN,ADY,pchmin
	+,pchmax,delpch,nout,phi
C
	                      ENDIF
C Move data up 1 line:
c-100	do k=1,3
c-	res(k-1)=res(k)
c-	peak(k-1)=peak(k)
c-	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

