      program lagw3hpo0umm
C...........................................................................Sep 2022
C Use Catalog EQs 
C
C..Add WUmax.WLmin.WUmax from UPC 15min mapos..for SSN=0, Kp>=3 days........Aug. 2022
C UPC 15mm indices based on -15d median 	from Dec 1996 to 2021
C WUmax +-24h
C WLmin +-24h 
C WEmax +-24h 
C
      CHARACTER*64 infhpo,infileu,infilel,infiler,outfile,infile,txt
	CHARACTER*2 AYR
	DIMENSION kwu(0:287),kwl(0:287),kwe(0:287),hpo(0:71)
C
C
      infile='cateqhpo0.txt'  !Catalog of 952 days SSN=0, Hpo<3  read(10,
	outfile='wulehpo0mm.txt'								  !write(11,
	infhpo='d:\Apkp\Hpo\HpoYEAR.txt'						  !read(15,
	infileu='d:\web\graf\datu\wupYRmm.txt'					  !read(12,
	infilel='d:\web\graf\datu\wlnYRmm.txt'					  !read(13,
	infiler='d:\web\graf\datu\werYRmm.txt'					  !read(14,
C
       OPEN(10,file=infile,action='READ')
       OPEN(11,file=outfile)
   30 format(1X,F8.3,1X,I3,3(1X,I2),1X,F4.1,1X,F6.1,1X,F5.0,1X,I4) !Hpo storms
   33 format(3I2,96(1X,I4))
C
	DO k=0,287
	kwu(k)=0
	kwl(k)=0
	kwe(k)=0
	ENDDO
	ndy=0
C
	DO 41 iev=1,1027
    	read(10,30,end=50) ryear,lda,imn,idy,itkp,hpomax,shp,apomax,nn
	 if (nn.lt.156) goto 41      !============== start UPC from Dec. 1996
C
      tkp=itkp+0.5  !time of mid1h Hpo
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)
	infileu(21:22)=AYR
	OPEN(12,file=infileu,action='READ')
	read(12,*) txt
	infilel(21:22)=AYR
	OPEN(13,file=infilel,action='READ')
	read(13,*) txt
	infiler(21:22)=AYR
	OPEN(14,file=infiler,action='READ')
	read(14,*) 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,95
	kwu(k)=kwu(k+96)
	kwl(k)=kwl(k+96)
	kwe(k)=kwe(k+96)
	kwu(k+96)=kwu(k+192)
	kwl(k+96)=kwl(k+192)
	kwe(k+96)=kwe(k+192)
	 enddo
	read(12,33,end=55) kyr,kmn,kdy,(kwu(k+192),k=0,95)
	read(13,33,end=56) kyr,kmn,kdy,(kwl(k+192),k=0,95)
	read(14,33,end=57) kyr,kmn,kdy,(kwe(k+192),k=0,95)
	 enddo
   55	close(12)
   56	close(13)
   57	close(14)
C??      nitkp=itkp+24
        tkp2=tkp+24.0  ! time Kpmax at t0 day2
C
C WUmax
      kwumax=0
	twu=0.
	twumax=0.
C	n1wp=nitkp-12 !tkp-12h
C	n2wp=nitkp+12 !tkp+12h ! new
C	tup=-24.0
C	do k=n1wp,n2wp  !new
       DO 101 k=0,287
	if (twu.gt.tkp2+12.) exit
	if (twu.lt.tkp2-12.0) goto 100
C
	if (kwu(k).gt.kwumax) then
	kwumax=kwu(k)
	twumax=twu-24.
C	itwp=k-24
	endif
  100	twu=twu+0.25
  101	CONTINUE
C	jtwp=itwp-itkp
      twup=twumax
	wumax=kwumax/100.
C
C WLmin
      kwlmin=0
	twl=0.
	twlmin=0.
C	n1wp=nitkp-12 !tkp-12h
C	n2wp=nitkp+12 !tkp+12h ! new
C	tup=-24.0
C	do k=n1wp,n2wp  !new
       DO 102 k=0,287
	if (twl.gt.tkp2+12.) exit
	if (twl.lt.tkp2-12.0) goto 99
C
	if (kwl(k).lt.kwlmin) then
	kwlmin=kwl(k)
	twlmin=twl-24.
C	itwp=k-24
	endif
   99	twl=twl+0.25
  102	CONTINUE
C	jtwp=itwp-itkp
      twlo=twlmin
	wlmin=kwlmin/100.
C
C WEmax
      kwemax=0
	twe=0.
	twemax=0.
c	n1wp=nitkp-12 !tkp-12h
c	n2wp=nitkp+12 !tkp+12h ! new
c	tup=-24.0
C	do k=n1wp,n2wp  !new
       DO 103 k=0,287
	if (twe.gt.tkp2+12.) exit
	if (twe.lt.tkp2-12.0) goto 98
C
	if (kwe(k).gt.kwemax) then
	kwemax=kwe(k)
	twemax=twe-24.
C	itwp=k-24
	endif
   98	twe=twe+0.25
  103	CONTINUE
C	jtwp=itwp-itkp
      twer=twemax
	wemax=kwemax/100.
C
C Output
      ndy=ndy+1
      write(*,32) ryear,lda,imn,idy,tkp,hpomax,shp,apomax,twup,wumax
	+,twlo,wlmin,twer,wemax,ndy
	write(11,32) ryear,lda,imn,idy,tkp,hpomax,shp,apomax,twup,wumax
	+,twlo,wlmin,twer,wemax,ndy
C
   41 continue
   32 format(1X,F8.3,1X,I3,2(1X,I2),1X,F4.1,1X,F6.1,1X,F5.0,1X,F7.1
     +,6(1X,F6.2),1X,I4)
   50	close(unit=10)
	close(unit=11)
	pause ' '
	STOP
	END