      program lag3hkp0jm
C..Add SumKp.............................................May 2022
C JPL 3h indices based on monthly-median
C WUmax from -24h:tkp:+24h
C WLmin from -24h:tkp:+24h
C WEmax from -24h:tkp:+24h
C
      CHARACTER*64 infile,outfile,infileq,txt
	CHARACTER*2 AYR
	DIMENSION xkp(0:8),wu(0:23),wl(0:23),we(0:23),iutkp(0:8)
C
      infile='wulessn0kp3jm.txt'
	outfile='wulessn0kp3jm.res'
	infileq='d:\web\graf\datm\wjmYR_3.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,1X,I3)
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
    1	read(10,30,end=50) ryeari,imni,idyi,iuti,wui,wli,wei,xkpi,isumkp
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
c+      wu(iut1)=wui
c+	wl(iut1)=wli
c+	we(iut1)=wei
	iutkp(iut1)=iuti
	 ryear=ryeari
	imn=imni
	idy=idyi
	iut=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
C
      lda1=lda+1
	do kk=1,lda1
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
C1	n1wu=nitkp-8 !tkp-24h
	n1wu=nitkp-4 !tkp-12h
C1	n2wu=nitkp+8 !tkp+24h ! new
	n2wu=nitkp+4 !tkp+12h ! new
Cpre	do k=n1wu,nitkp
	do k=n1wu,n2wu  !new
	if (wu(k).gt.wumax) then
	wumax=wu(k)
	itwu=(k-8)*3
	endif
	enddo
	jtwu=itwu-itkp
C WLmin
Cpre      n1twl1=itwu/3+8
C1	n1twl1=nitkp-8	  !new
         n1twl1=itwu/3+8
C-       n1twl1=n1wu
      wlmin=0.
	itwl=0
C1	n2wl=nitkp+8  !tkp+24h
        n2wl=n1twl1+8
	if (n2wl.gt.23) n2wl=23 !++++++++
	do k=n1twl1,n2wl
	if (wl(k).lt.wlmin) then
	wlmin=wl(k)
	itwl=(k-8)*3
	endif
	enddo
	jtwl=itwl-itkp
C WEmax
Cpre      n1twe=itwu/3+8
C1      n1twe=nitkp-8	  !new
C-	n1twe=n1wu
       	n1twe=itwu/3+8
Cpre	n2twe=itwl/3+8
C1      n2twe=nitkp+8  !new
C-      n2twe=n2wu
       n2twe=itwl/3+8
		if (n2twe.gt.23) n2twe=23 !++++++++
      wemax=0.
	itwe=0
	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,isumkp,ndy
	write(11,32) ryear,lda,imn,idy,itkp,xkpmax,jtwu,wumax,jtwl,wlmin
	+,jtwe,wemax,isumkp,ndy
C
   32 format(1X,F8.3,1X,I3,3(1X,I2),1X,F4.1,3(1X,I3,1X,F6.2),1X,I3
     +,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.
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