      program lag1hkp0jm
C..Add SumKp.............................................Aug 2022
C JPL 1h indices based on -15d median geomag. wi indices
C WUmax from -24h:tkp:+24h
C WLmin from -24h:tkp:+24h
C WEmax from -24h:tkp:+24h
C
      CHARACTER*64 infile,outfile,infileu,infilel,infiler,txt
	CHARACTER*2 AYR
	DIMENSION wu(0:71),wl(0:71),we(0:71)
C
      infile='wulessn0kp3jm.res'
	outfile='wules0kp3jm.txt'
	infileu='d:\web\graf\datm\wupYRjm.txt'
	infilel='d:\web\graf\datm\wlnYRjm.txt'
	infiler='d:\web\graf\datm\werYRjm.txt'
C
       OPEN(10,file=infile,action='READ')
       OPEN(14,file=outfile)
C ryear    lda mn dd iut3 Kp dtwu  wu   dtwl  wl   dtwe  we    dtwp  wp  dtsw Vsw   Skp   nn      
C 1996.033  13  1 13  3  5.0  -3   2.22   6  -3.83   6   5.80   9   4.70  42  605.0 280    1
   30 FORMAT(1X,F8.3,1X,I3,3(1X,I2),1X,F4.1,4(1X,I3,1X,F6.2),1X,I3
     +,1X,F6.1,1X,I3,1X,I4)
   33 format(3I2,24(1X,F4.2))
C
C
	ndy=0
C
      lda_out=0
C
C SSN=0 Kp>=3 events
        DO 41 iev=1,664  ! Events
C 
	DO k=0,71
	wu(k)=0.
	wl(k)=0.
	we(k)=0.
	ENDDO
C
C    	read(10,30,end=50) ryear,lda,imn,idy,itkp,xkpmax,jtwu3,wu3
    	read(10,*,end=50) ryear,lda,imn,idy,itkp,xkpmax,jtwu3,wu3
     +,jtwl3,wl3,jtwe3,we3,isumkp,nn
C
C
   21 tkp=itkp+1.5
	iyear=int(ryear)
	iyr=iyear-iyear/100*100
C
C
C++ READ 1h WU,WL,WE
C
	call blet2(iyr,AYR)
	infileu(21:22)=AYR
	infilel(21:22)=AYR
	infiler(21:22)=AYR
	OPEN(11,file=infileu,action='READ')
	read(11,*) txt
      OPEN(12,file=infilel,action='READ')
	read(12,*) txt
      OPEN(13,file=infiler,action='READ')
	read(13,*) 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
	wu(k)=wu(k+24)
	wu(k+24)=wu(k+48)
	wl(k)=wl(k+24)
	wl(k+24)=wl(k+48)
	we(k)=we(k+24)
	we(k+24)=we(k+48)
	 enddo
	read(11,33,end=55) kyr,kmn,kdy,(wu(k+48),k=0,23)
	read(12,33,end=56) kyr,kmn,kdy,(wl(k+48),k=0,23)
	read(13,33,end=57) kyr,kmn,kdy,(we(k+48),k=0,23)
	 ENDDO
   55	close(11)
   56	close(12)
   57	close(13)
      nitkp=itkp+24
C WUmax
      wumax=0.
	itwu=0
	n1=nitkp-24 !tkp-24h
	n2=nitkp+24 !tkp+24h
	do k=n1,n2  !new
	if (wu(k).gt.wumax) then
	wumax=wu(k)
	itwu=k-24
	endif
	enddo
	jtwu=itwu-itkp
	twu=itwu-tkp
C WLmin
      wlmin=0.
	itwl=0
	do k=n1,n2
	if (wl(k).lt.wlmin) then
	wlmin=wl(k)
	itwl=k-24
	endif
	enddo
	jtwl=itwl-itkp
	twl=itwl-tkp
C WEmax
      wemax=0.
	itwe=0
	do k=n1,n2
	if (we(k).gt.wemax) then
	wemax=we(k)
	itwe=k-24
	endif
	enddo
	jtwe=itwe-itkp
	twe=itwe-tkp
C
C Output
        if (iev.eq.1) GOTO 123
      IF((abs(wumax-wumax_pre).lt.0.01).and.(abs(wlmin-wlmin_pre)  !++++++++++
	+.lt.0.01)) THEN
	if ((wumax.eq.wu_out).and.(wlmin.eq.wl_out)) GOTO 123
	if (xkpmax.ge.xkpmax_pre) then	  !==========
	 if (lda.eq.lda_out) goto 123
      ndy=ndy+1
      write(*,32) ryear,lda,imn,idy,tkp,xkpmax,twu,wumax,twl,wlmin
	+,twe,wemax,isumkp,ndy
	write(14,32) ryear,lda,imn,idy,tkp,xkpmax,twu,wumax,twl,wlmin
	+,twe,wemax,isumkp,ndy
	 lda_out=lda
	wu_out=wumax
	wl_out=wlmin
	 goto 123
C	endif
                                                      else       !===========
C		if ((xkpmax.lt.xkpmax_pre).and.(lda_pre.gt.lda_out)) then     
       if (lda_pre.eq.lda_out) goto 123
	if ((wumax_pre.eq.wu_out).and.(wlmin_pre.eq.wl_out)) GOTO 123
      ndy=ndy+1
	write(*,32) ryear_pre,lda_pre,imn_pre,idy_pre,tkp_pre,xkpmax_pre
	+,twu_pre,wumax_pre,twl_pre,wlmin_pre,twe_pre,wemax_pre,isumkp_pre
     +,ndy
	write(14,32) ryear_pre,lda_pre,imn_pre,idy_pre,tkp_pre,xkpmax_pre
	+,twu_pre,wumax_pre,twl_pre,wlmin_pre,twe_pre,wemax_pre,isumkp_pre
     +,ndy
	 lda_out=lda_pre
	wu_out=wumax_pre
	wl_out=wlmin_pre
	goto 123													!==============
	                          endif
	                                          ELSE					   !++++++++++
	 if ((wumax.eq.wu_out).and.(wlmin.eq.wl_out)) GOTO 123
      ndy=ndy+1
      write(*,32) ryear,lda,imn,idy,tkp,xkpmax,twu,wumax,twl,wlmin
	+,twe,wemax,isumkp,ndy
	write(14,32) ryear,lda,imn,idy,tkp,xkpmax,twu,wumax,twl,wlmin
	+,twe,wemax,isumkp,ndy
	  lda_out=lda
	wu_out=wumax
	wl_out=wlmin
                                       ENDIF								!++++++++++
C
  123    ryear_pre=ryear
	lda_pre=lda
	imn_pre=imn
	idy_pre=idy
	tkp_pre=tkp
	xkpmax_pre=xkpmax
	twu_pre=twu
	wumax_pre=wumax
	twl_pre=twl
	wlmin_pre=wlmin
	twe_pre=twe
	wemax_pre=wemax
	isumkp_pre=isumkp
C
   41 continue
C
   32 format(1X,F8.3,1X,I3,2(1X,I2),2(1X,F4.1),3(1X,F5.1,1X,F6.2),1X,I3
     +,1X,I4)
   50	close(unit=14)
	pause ' '
	STOP
	END