      program lag3hkp0
C...............................................Apr 2022
C
      CHARACTER*64 infile,outfile,infileq,txt
	CHARACTER*2 AYR
	DIMENSION xkp(0:7),wu(0:7),wl(0:7),we(0:7),iutkp(0:7)
C
      infile='wulessn0kp3n.txt'
	outfile='wulessn0kp3d.txt'
	infileq='d:\web\graf\datw\qtcYR.txt'
C
       OPEN(10,file=infile,action='READ')
       OPEN(11,file=outfile)
   30 FORMAT(1X,F8.3,1X,2I2,1X,I2,3(1X,F6.2),1X,F4.1)
C.      ryear_pre=1996.030
C.	imn_pre=1
C.	idy_pre=12
C.	iut_pre=21
      ryear=1996.030
	imn=1
	idy=12
	iut=21
	DO k=0,7
	xkp(k)=0.
	wu(k)=0.
	wl(k)=0.
	we(k)=0.
	iutkp(k)=0
	ENDDO
	ndy=0
    1	read(10,30,end=50) ryeari,imni,idyi,iuti,wui,wli,wei,xkpi
C.      if ((ryeari.eq.ryear_pre).and.(imni.eq.imn_pre).and.
C.	+(idyi.eq.idy_pre)) then
      if ((ryeari.eq.ryear).and.(imni.eq.imn).and.
	+(idyi.eq.idy)) then
C?	 ryear=ryeari
C?	imn=imni
C?	idy=idyi
C?	iut=iuti
	 GOTO 21
	                  else
	backspace(10)
	GOTO 31
	                 endif
C
C
   21 iut1=iuti/3
     	xkp(iut1)=xkpi
      wu(iut1)=wui
	wl(iut1)=wli
	we(iut1)=wei
	iutkp(iut1)=iuti
	goto 1
C?	if (iut1.le.7) GOTO 1
C
   33 format(3I2,8(1X,F4.2),8(1X,F5.2),8(1X,F4.2))
C 	 produce lag for day 
C Kpmax tkp
   31   xkpmax=0.
	itkp=0
   	do k=0,7
	if (xkp(k).gt.xkpmax) then
	xkpmax=xkp(k)
	itkp=iutkp(k)
	endif
	enddo
C++ READ qtc 3h WU,WL,WE
      iyear=int(ryear)
	iyr=iyear-iyear/100*100
C
	lda=ndoy(iyr,imn,idy)
	call blet2(iyr,AYR)
	infileq(21:22)=AYR
	OPEN(12,file=infileq,action='READ')
	read(12,*) txt
	do kk=1,lda
	read(12,33,end=55) kyr,kmn,kdy,(wu(k),k=0,7),(wl(k),k=0,7),
	+(we(k),k=0,7)
	 enddo
   55	close(12)
C WUmax
      wumax=0.
	itwu=0
	do k=0,7
	if (wu(k).gt.wumax) then
	wumax=wu(k)
	itwu=k*3
	endif
	enddo
	jtwu=itwu-itkp
C WLmin
      wlmin=0.
	itwl=0
	do k=0,7
	if (wl(k).lt.wlmin) then
	wlmin=wl(k)
	itwl=k*3
	endif
	enddo
	jtwl=itwl-itkp
C WEmax
      wemax=0.
	itwe=0
	do k=0,7
	if (we(k).gt.wemax) then
	wemax=we(k)
	itwe=k*3
	endif
	enddo
	jtwe=itwe-itkp
C
C Output
      ndy=ndy+1
      write(*,32) ryear,lda,imn,idy,itkp,xkpmax,jtwu,wumax,jtwl,wlmin
	+,jtwe,wemax,ndy
	write(11,32) ryear,lda,imn,idy,itkp,xkpmax,jtwu,wumax,jtwl,wlmin
	+,jtwe,wemax,ndy
C
   32 format(1X,F8.3,1X,I3,3(1X,I2),1X,F4.1,3(1X,I3,1X,F6.2),1X,I4)
   41 continue
C?   	ryear_pre=ryear
C?	imn_pre=imn
C?      idy_pre=idy
C?	iut_pre=iut
C
	ryear=ryeari
	imn=imni
	idy=idyi
	iut=iuti
	DO k=0,7
	xkp(k)=0.
	wu(k)=0.
	wl(k)=0.
	we(k)=0.
	iutkp(k)=0
	ENDDO
      GOTO 1
   50	close(unit=10)
	close(unit=11)
	pause ' '
	STOP
	END