      program iawpn3kp0umm
C..................................................................Aug. 2022
C..Add Ip.In.daily mean intensity of W+, W- from UPC 15min mapos..for SSN=0 day, Kp>=3 days
C UPC 15mm indices based on -15d median 	from Dec 1996 to 2021
C
C
      CHARACTER*64 infilep,infilen,outfile,infile,txt
	CHARACTER*2 AYR
	DIMENSION kwu(0:287),kwl(0:287)
C
      infile='wulessn0kp3jm.dat' ! Catalog of days SSN=0, Kp>=3
	outfile='iaws0kp3umm.out'
	infilep='d:\web\graf\datu\apwYRmm.txt'
	infilen='d:\web\graf\datu\anwYRmm.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,96(1X,I4))
C
	DO k=0,287
	kwu(k)=0
	kwl(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
	 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)
	infilep(21:22)=AYR
	OPEN(12,file=infilep,action='READ')
	read(12,*) txt
	infilen(21:22)=AYR
	OPEN(13,file=infilen,action='READ')
	read(13,*) txt
C
      lda1=lda+1
	if (iyear.eq.1996) lda1=idy+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)
	kwu(k+96)=kwu(k+192)
	kwl(k+96)=kwl(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)
	 enddo
   55	close(12)
   56	close(13)
C??      nitkp=itkp+24
        tkp2=tkp+24.0  ! time Kpmax at t0 day2
C
C WUmax
      kwumax=0
	kwuav=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
c       DO 101 k=0,287
       DO 101 k=96,191
c	if (twu.gt.tkp2+12.) exit
c	if (twu.lt.tkp2-12.0) goto 100
C
      kwuav=kwuav+kwu(k)
	if (kwu(k).gt.kwumax) then
	kwumax=kwu(k)
c	twumax=twu-24.
	twumax=twu
C	itwp=k-24
	endif
  100	twu=twu+0.25
  101	CONTINUE
        wuav=kwuav/96. ! SLD mean Ip
C	jtwp=itwp-itkp
      twup=twumax
C
C WLmax
      kwlmax=0
	kwlav=0
	twl=0.
	twlmax=0.
C	n1wp=nitkp-12 !tkp-12h
C	n2wp=nitkp+12 !tkp+12h ! new
C	tup=-24.0
C	do k=n1wp,n2wp  !new
C       DO 102 k=0,287
       DO 102 k=96,191
c	if (twl.gt.tkp2+12.) exit
c	if (twl.lt.tkp2-12.0) goto 99
      kwlav=kwlav+kwl(k)
C
	if (kwl(k).gt.kwlmax) then
	kwlmax=kwl(k)
	twlmax=twl
C	itwp=k-24
	endif
   99	twl=twl+0.25
  102	CONTINUE
        wlav=kwlav/96. ! SLD mean In
C	jtwp=itwp-itkp
      twlo=twlmax
C	wlmin=kwlmin/100.
C
C
C Output
      ndy=ndy+1
      write(*,32) ryear,lda,imn,idy,tkp,xkpmax,twup,kwumax,wuav
	+,twlo,kwlmax,wlav,isumkp,ndy
	write(11,32) ryear,lda,imn,idy,tkp,xkpmax,twup,kwumax,wuav
	+,twlo,kwlmax,wlav,isumkp,ndy
C
   41 continue
   32 format(1X,F8.3,1X,I3,2(1X,I2),1X,F5.2,1X,F4.1
     +,2(1X,F6.2,1X,I4,1X,F5.1),1X,I3,1X,I4)
   50	close(unit=10)
	close(unit=11)
	pause ' '
	STOP
	END