      program lag3hkp03d
C...............................................Apr 2022
C Select WUmin from [-1d,0d]
C Select WLmin from [0d,+1d]
C Select WUmin from [-1d,+1d]
C
      CHARACTER*64 infile,outfile,infileq,txt
	CHARACTER*2 AYR
	DIMENSION xkp(0:7),wu(0:23),wl(0:23),we(0:23),iutkp(0:7)
C
      infile='wulessn0kp3d.txt'
	outfile='wulessn0kp3dt.txt'
	infileq='d:\web\graf\datw\qtcYR.txt'
C
       OPEN(10,file=infile,action='READ')
       OPEN(11,file=outfile)
Crem   30 FORMAT(1X,F8.3,1X,2I2,1X,I2,3(1X,F6.2),1X,F4.1)
   30 FORMAT(1X,F8.3,1X,I3,3(1X,I2),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+15)=0.
	wl(k+15)=0.
	we(k+15)=0.
	iutkp(k)=0
	ENDDO
	ndy=0
Crem    1	read(10,30,end=50) ryeari,imni,idyi,iuti,wui,wli,wei,xkpi
    1	read(10,30,end=50) ryeari,lagi,imni,idyi,iuti,xkpi
C?      if ((ryeari.eq.ryear).and.(imni.eq.imn).and.
C?	+(idyi.eq.idy)) then
C?	 GOTO 21
C?	                  else
C?	backspace(10)
C?	GOTO 31
C?	                 endif
C
	ryear=ryeari
	imn=imni
	idy=idyi
	iut=iuti
C
   21 iut1=iuti/3
     	xkp(iut1)=xkpi
C?      wu(iut1)=wui
C?	wl(iut1)=wli
C?	we(iut1)=wei
	iutkp(iut1)=iuti
Crem	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
C++
       lda1=lda+1
	do kk=1,lda1  ! d+1
C++ move data up to keep for d-1,d0,d+1 days
	 do k=0,7
	wu(k)=wu(k+8)
	wu(k+8)=wu(k+16)
	wl(k)=wl(k+8)
	wl(k+8)=wl(k+16)
	we(k)=we(k+8)
	we(k+8)=we(k+16)
	 enddo
	read(12,33,end=55) kyr,kmn,kdy,(wu(k+16),k=0,7),(wl(k+16),k=0,7),
	+(we(k+16),k=0,7)
	 enddo
   55	close(12)
	nitkp=itkp/3+8
C WUmax
      wumax=0.
	itwu=0
	n1wu=nitkp-8 !tkp-24h
C-	do k=0,15
C-	do k=0,nitkp
	do k=n1wu,nitkp
	if (wu(k).gt.wumax) then
	wumax=wu(k)
	itwu=(k-8)*3
	endif
	enddo
	jtwu=itwu-itkp
C WLmin
C-      n1twl=itwu/3+8
      wlmin=0.
	itwl=0
	n2wl=nitkp+8  !tkp+24h
C-	do k=8,23
C-	do k=nitkp,23
	do k=nitkp,n2wl
	if (wl(k).lt.wlmin) then
	wlmin=wl(k)
	itwl=(k-8)*3
	endif
	enddo
	jtwl=itwl-itkp
C WEmax
      n1twe=itwu/3+8
	n2twe=itwl/3+8
      wemax=0.
	itwe=0
C-	do k=0,23
	do k=n1twe,n2twe
	if (we(k).gt.wemax) then
	wemax=we(k)
	itwe=(k-8)*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
	DO k=0,7
	xkp(k)=0.
c-	wu(k)=0.
c-	wl(k)=0.
c-	we(k)=0.
	iutkp(k)=0
	ENDDO
      GOTO 1
   50	close(unit=10)
	close(unit=11)
	pause ' '
	STOP
	END