      program lagapstau6
C...........................................................Oct 2023
C -12:to:+11h (Hpo), Apo(tau6) to=0 at Starlink launch
C
       DIMENSION hpo24(24),apo24(24)
	CHARACTER*128 INFHSS,INFILE,OUTFILE
C	+,outhpo,outapo
	CHARACTER*2 AMN,ADY,AUT,AMM
	CHARACTER*4 AYEAR
C
       infhss='Starlink20192023.txt'
	infile='d:\Apkp\Hpo\Apotau6.txt'
C
	outfile='catapostau6.txt'
	OPEN(11,file=infhss,action='READ')
C
	OPEN(10,file=outfile,access='APPEND')
C
   28 format(I3,1X,I4,4(1X,I2))
   29 format(1X,I4,3(1X,I2),3X,F4.0,2X,F6.2)
C
C
	DO 177 iev=1,126
      read(11,28,end=127) nn,iyear,imn1,idy1,iut1,imm
	if (nn.lt.75) goto 177 
	call blet4(iyear,AYEAR)
	if (iyear.eq.2020) then
	ndyr=366
	else
	ndyr=365
	endif
	del=1./ndyr
C
      iyr1=iyear-iyear/100*100
	 lda1=ndoy(iyr1,imn1,idy1)
	xyear=iyear+del*(lda1-1)
      call blet2(imn1,AMN)
	call blet2(idy1,ADY)
	call blet2(iut1,AUT)
	call blet2(imm,AMM)
C
	lda0=lda1
	iut0=iut1-12
	if (iut0.lt.0) then
	lda0=lda1-1
	iut0=iut0+24
	endif
C
C	infile(16:19)=AYEAR
      OPEN(12,file=infile,action='READ')
	nut2=1
    1	read(12,29,end=128) iyear2,imn2,idy2,iut2,apo,aptau
      iyr2=iyear2-iyear2/100*100
	 lda2=ndoy(iyr2,imn2,idy2)
	IF (iyear2.lt.iyear) GOTO 1  
      IF (lda2.lt.lda0) GOTO 1  
C
       	IF (nut2.gt.24) then               !1
		GOTO 2 !
	                    ELSE               !1
C	
       hpo24(nut2)=apo
	 apo24(nut2)=aptau
	nut2=nut2+1
	GOTO 1
	ENDIF                                    !1
C
    2	CONTINUE
C
  23	apomax=0. 
      DO kk=1,24
	if (apomax.lt.apo24(kk)) then
      apomax=apo24(kk)
C	hpomax=hpo24(kk)
      kdtmax=kk-13
C	if (kdtmax.gt.0) then
C	continue
C	 kdtmax=kk
C	endif
	endif
C	ires(kk)=nint(hpo24(kk)*100)
	ENDDO
C Prepare lag (tapomax-t1)
C
C?        lagt=iut0-iut1
C?       if (lda1.eq.lda0) then
C?	 GOTO 23
C?	endif
C
C?	if (lda2.gt.lda1) then
C?	 lagt=iut2+24-iut1
C?	else
C?	lagt=iut2-24-iut1
C?	                 endif
C
C?	natau=natau+1
       if (kdtmax.lt.0) then
	ldatau=lda0
	iuttau=iut1+kdtmax+24
	else
	ldatau=lda1
	iuttau=iut1+kdtmax
	endif
      write(*,32) xyear,lda1,amn,ady,aut,amm,ldatau,iuttau,kdtmax
	+,apomax,iev
	write(10,32) xyear,lda1,amn,ady,aut,amm,ldatau,iuttau,kdtmax
	+,apomax,iev
  128 close(12)
C	
  177	CONTINUE
  127 close(11)
      close(10)
  32	FORMAT(1X,F8.3,1X,I3,4(1X,A2),1X,I3,1X,I2,1X,I3,F7.2,1X,I3)
  33	FORMAT(24(1X,I4))
       pause ' '
	STOP
	END