      program solphasepch
C........................................................Jan 2019
C
C SolPhase for Pch-peaks and DelPch > 0. monthly 
C
	dimension RMIN(8),RMAX(8)
	character*65 infile, outfile1,txt
c	integer*2 iyr,imn
	character*2 BMN,BDY,AYR
	character*4 AYEAR
	DATA RMIN/1944.2,1954.3,1964.9,1976.5,1986.8,1996.9,2008.9,2019.6/
	DATA RMAX/1937.4,1947.5,1957.9,1968.9,1979.9,1989.6,2000.3,2014.5/
C-- 
	infile='delpchpos.res'	  ! daily data
	outfile1='solphasepcmax.txt'
C
   30	format(A65)
   36	format(1X,F8.3,2(1X,A2),3(1X,F7.2),1X,I4)
   71	format(A45)
   72	format(A4,A2,2(1X,F5.1))
C
      OPEN(11,FILE=INFILE,action='READ')
	read(11,30) txt !~ title line

	   	OPEN(12,FILE=OUTFILE1)
C
  121 FORMAT(1X,A4,A2,1X,F5.2,1X,F7.1,1X,I4)
  122	FORMAT(A6)
  123	FORMAT(12(1X,I4))
  124	FORMAT(A64)

C Cycle on years : 2011/03-2018/12
C
	iyyyy=2010
	do 150 KK=1,8 ! 2011-2018
      iyyyy=iyyyy+1
		zyear=float(iyyyy)
		if(int(zyear/4.)*4.eq.iyyyy) THEN 
	dnr=366.
	                          ELSE
	dnr=365.
	ENDIF
	del=1./dnr
	call blet4(iyyyy,AYEAR)
	AYR=AYEAR(3:4)
	read(ayr,*) ryr
	iyr=int(ryr)
C  Cycle on doy
C
	lda1=1
      lda2=int(dnr)
C
	do 55 lda=lda1,lda2	 ! <<<<<<<<<<<<<<<<<<<<<<<<
      ryear=zyear+(lda-1)*del
C	call blet2(imn,AMN)
C Monthly count of occurrence
   1	read(11,36,end=3) ASTR,bmn,bdy,pcmin,pcmax,delpc,npc
      if (ASTR.gt.ryear) then
	backspace(11)
	goto 55
	endif
C	ASTR=ryr+rmn/12.
C Select Sol Phase boundaries
	do j=1,8
	if ((ASTR.le.RMIN(j)).and.(ASTR.ge.RMAX(j))) then
	AMIN=RMIN(j)
	AMAX=RMAX(j)
	goto 149
	endif
	enddo
	do j=1,7
	if ((ASTR.ge.RMIN(j)).and.(ASTR.le.RMAX(j+1))) then
	amin=RMIN(j)
	amax=rmax(j+1)
	exit
	endif
	enddo
C end select
  149     FI=(ASTR-AMIN)/abs(AMAX-AMIN)
C
c	num=keq(imn)
c	if (num.eq.0) goto 151
      write(*,13) ASTR,BMN,BDY,fi,pcmax,delpc
	write(12,13) ASTR,BMN,BDY,fi,pcmax,delpc
  13  FORMAT(1X,F8.3,2(1X,A2),1X,F7.3,2(1X,F7.2))
           
  55	continue          !<<<<<<<<<<<<<<<<<<<<<<<<	   ! end cycle on day-to-day
 150	CONTINUE					   ! end cycle on year
    3	CLOSE(unit=11)
	CLOSE(unit=12)
	goto 180
C	goto 1
    2	write(*,*) 'input error'
	
180   continue
	pause ' '
      STOP
	end