      program solphasehss
C........................................................Mar 2020
C
C SolPhase for 742 B+HSS events kp<3
C
	dimension RMIN(8),RMAX(8)
	character*65 infile, outfile
	character*2 AYR
	character*4 AYEAR
	DATA RMIN/1944.2,1954.3,1964.9,1976.5,1986.8,1996.9,2008.9,2020.6/
	DATA RMAX/1937.4,1947.5,1957.9,1968.9,1979.9,1989.6,2000.3,2014.5/
C-- 
	infile='cathss48bkp.txt' ! 742 B+HSS events kp<3
	outfile='solphasehss.txt'
C
   29 format(1X,F8.3,1X,I3,2(1X,I2))
   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')
C
	   	OPEN(12,FILE=OUTFILE)
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)
      nn=0
C
	DO 55 k=1,742
      read(11,29,end=3) ryear,lda,imnbt,idybt
	nn=nn+1
C Cycle on years : 1996-2019
C
c-	iyyyy=1995
c-	do 150 KK=1,8 ! 2011-2018
c-      iyyyy=iyyyy+1
c-		zyear=float(iyyyy)
      iyyyy=int(ryear)
		if(int(ryear/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
c	lda1=1
c      lda2=int(dnr)
C
c	do 55 lda=lda1,lda2	 ! <<<<<<<<<<<<<<<<<<<<<<<<
c      ryear=zyear+(lda-1)*del
C	call blet2(imn,AMN)
C Monthly count of occurrence
   1	continue
C      read(11,36,end=3) ASTR,bmn,bdy,pcmin,pcmax,delpc,npc
c      if (ASTR.gt.ryear) then
c	backspace(11)
c	goto 55
c	endif
C	ASTR=ryr+rmn/12.
C Select Sol Phase boundaries
      ASTR=ryear
	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
C      write(*,13) ASTR,BMN,BDY,fi,pcmax,delpc
C	write(12,13) ASTR,BMN,BDY,fi,pcmax,delpc
	write(*,13) ryear,lda,imnbt,idybt,fi,nn
	write(12,13) ryear,lda,imnbt,idybt,fi,nn
  13  FORMAT(1X,F8.3,1X,I3,2(1X,I2),1X,F7.3,1X,I4)
           
  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