      program lagw2kp0umm
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 Outout: (1) Moderate Set1: 3<=Kp<5; (2) Intense Set2: Kp>=5
C
      CHARACTER*64 infileu,infilel,infiler,outfile1,outfile2,infile,txt
	CHARACTER*2 AYR
	DIMENSION kwu(0:287),kwl(0:287),kwe(0:287)
C
      infile='wulessn0kp3jm.dat' ! Catalog of days SSN=0, Kp>=3
	outfile1='wulessn0kp1umm.out'
	outfile2='wulessn0kp2umm.out'
	infileu='d:\web\graf\datu\wupYRmm.txt'
	infilel='d:\web\graf\datu\wlnYRmm.txt'
	infiler='d:\web\graf\datu\werYRmm.txt'
C
       OPEN(10,file=infile,action='READ')
       OPEN(11,file=outfile1)
	OPEN(15,file=outfile2)
   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,96(1X,I4))
C
	DO k=0,287
	kwu(k)=0
	kwl(k)=0
	kwe(k)=0
	ENDDO
	ndy1=0
	ndy2=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
	 if (nn.lt.73) goto 41      !============== start UPC from Dec. 1996
C
      tkp=itkp+1.5  !time of mid3h Kp
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
Cpre       DO 101 k=0,287
       DO 101 k=96,191
Cpre	if (twu.gt.tkp2+12.) exit
Cpre	if (twu.lt.tkp2-12.0) goto 100
C
	if (kwu(k).gt.kwumax) then
	kwumax=kwu(k)
Cpre	twumax=twu-24.
      	twumax=twu
C	itwp=k-24
	endif
  100	twu=twu+0.25
  101	CONTINUE
C	jtwp=itwp-itkp
      twup=twumax-tkp
	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
Cpre       DO 102 k=0,287
       DO 102 k=96,191
Cpre	if (twl.gt.tkp2+12.) exit
Cpre	if (twl.lt.tkp2-12.0) goto 99
      if (twl.lt.twumax) goto 99
C
	if (kwl(k).lt.kwlmin) then
	kwlmin=kwl(k)
Cpre	twlmin=twl-24.
        	twlmin=twl
C	itwp=k-24
	endif
   99	twl=twl+0.25
  102	CONTINUE
C	jtwp=itwp-itkp
      twlo=twlmin-tkp
	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
Cpre       DO 103 k=0,287
        DO 103 k=96,191
Cpre	if (twe.gt.tkp2+12.) exit
Cpre	if (twe.lt.tkp2-12.0) goto 98
      if (twe.lt.twumax) goto 98
C
	if (kwe(k).gt.kwemax) then
	kwemax=kwe(k)
Cpre	twemax=twe-24.
	twemax=twe
C	itwp=k-24
	endif
   98	twe=twe+0.25
  103	CONTINUE
C	jtwp=itwp-itkp
      twer=twemax-tkp
	wemax=kwemax/100.
C
C Output
            IF (xkpmax.lt.5.0) THEN
      ndy1=ndy1+1
      write(*,32) ryear,lda,imn,idy,tkp,xkpmax,twup,wumax,twlo,wlmin
	+,twer,wemax,isumkp,ndy1
	write(11,32) ryear,lda,imn,idy,tkp,xkpmax,twup,wumax,twlo,wlmin
	+,twer,wemax,isumkp,ndy1
	                        ELSE
      ndy2=ndy2+1
      write(*,32) ryear,lda,imn,idy,tkp,xkpmax,twup,wumax,twlo,wlmin
	+,twer,wemax,isumkp,ndy2
	write(15,32) ryear,lda,imn,idy,tkp,xkpmax,twup,wumax,twlo,wlmin
	+,twer,wemax,isumkp,ndy2
                               ENDIF
C
   41 continue
   32 format(1X,F8.3,1X,I3,2(1X,I2),1X,F5.2,1X,F4.1,6(1X,F6.2)
     +,1X,I3,1X,I4)
   50	close(unit=10)
	close(unit=11)
	close(unit=15)
	pause ' '
	STOP
	END