      program lagwp3hkp0jm
C..Add Wp.............................................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 wp(0:71)
C
      infile='wulessn0kp3jm.dat'
	outfile='wulessn0kp3jm.out'
	infileq='d:\web\graf\datm\wptecYRjm.txt'
C
       OPEN(10,file=infile,action='READ')
       OPEN(11,file=outfile)
   30 format(1X,F8.3,1X,I3,3(1X,I2),1X,F4.1,3(1X,I3,1X,F6.2),1X,I3
     +,1X,I4)
   33 format(3I2,24(1X,F4.1))
C
	DO k=0,71
	wp(k)=0.
	ENDDO
	ndy=0
C
	DO 41 iev=1,563
    	read(10,30,end=50) ryear,lda,imn,idy,itkp,xkpmax,jtwu,wumax
     +,jtwl,wlmin,jtwe,wemax,isumkp,nn
C
C
C
C 	 produce lag for day 
C++ READ 1h Wp
      iyear=int(ryear)
	iyr=iyear-iyear/100*100
C
	lda=ndoy(iyr,imn,idy)
	call blet2(iyr,AYR)
	infileq(23:24)=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,23
	wp(k)=wp(k+24)
	wp(k+24)=wp(k+48)
	 enddo
	read(12,33,end=55) kyr,kmn,kdy,(wp(k+48),k=0,23)
	 enddo
   55	close(12)
      nitkp=itkp+24
C Wpmax
      wpmax=0.
	itwp=0
	n1wp=nitkp-12 !tkp-12h
	n2wp=nitkp+12 !tkp+12h ! new
	do k=n1wp,n2wp  !new
	if (wp(k).gt.wpmax) then
	wpmax=wp(k)
	itwp=k-24
	endif
	enddo
	jtwp=itwp-itkp
C
C Output
      ndy=ndy+1
      write(*,32) ryear,lda,imn,idy,itkp,xkpmax,jtwu,wumax,jtwl,wlmin
	+,jtwe,wemax,jtwp,wpmax,isumkp,ndy
	write(11,32) ryear,lda,imn,idy,itkp,xkpmax,jtwu,wumax,jtwl,wlmin
	+,jtwe,wemax,jtwp,wpmax,isumkp,ndy
C
   41 continue
   32 format(1X,F8.3,1X,I3,3(1X,I2),1X,F4.1,4(1X,I3,1X,F6.2),1X,I3
     +,1X,I4)
   50	close(unit=10)
	close(unit=11)
	pause ' '
	STOP
	END