      program lag1hp0s47
C..Add Hpo, Apo 1h indices.....................................Sep 2022
C JPL 1h indices based on GTEC Shubin median  wg indices
C
C  Daily peak, t(peak), integral WUs, WLs, WEs for SSN=0,47 Wp storms
C 
C WUs for day SSN=0
C WLs for day SSN=0
C WEs for day SSN=0
C
      CHARACTER*64 infile,infhpo,outfile,infileu,infilel,infiler,txt
	+,infdst
	CHARACTER*2 AYR
	CHARACTER*4 AYEAR
	DIMENSION wu(0:71),wl(0:71),we(0:71),apo(0:71),hpo(0:71)
	+,idst(0:71)
C
C	infile='kpssn0.txt'
	infile='catwpstrm0.txt'                ! read(10
	infhpo='d:\Apkp\Hpo\HpoYEAR.txt'       !integral Apo,Hpo ! read(15
	infdst='d:\Apkp\dst\dstYEAR'	   ! read(16
	outfile='wules0hp47.txt'			   ! read(14
	infileu='d:\web\graf\dats\wupYRs.txt' ! read(11
	infilel='d:\web\graf\dats\wlnYRs.txt' ! read(12
	infiler='d:\web\graf\dats\werYRs.txt' ! read(13
C
       OPEN(10,file=infile,action='READ')
       OPEN(14,file=outfile,access='APPEND')
C
   31 format(1X,F8.3,1X,I3,3(1X,I2),1X,F4.1,1X,I3) ! cat 47 Wp storms SSN=0
   32 format(I4,2(1X,I2),1X,I2,32X,F7.3,1X,F4.0)	 ! Hpo,Apo
   33 format(3I2,24(1X,F4.2))                ! WU, WL, WE
   34	format(3X,2I2,1X,I2,10X,24(I4))         	 ! Dst
C
C
	ndy=0	!norm
C
C?      lda_out=0
C
C SSN=0 Wp storm events
        DO 41 iev=1,47  ! Events
C 
	DO k=0,71
	apo(k)=0.
	hpo(k)=0.
	wu(k)=0.
	wl(k)=0.
	we(k)=0.
	ENDDO
C Wp storms & SSN =0:
    	read(10,31,end=50) ryear,lda,imn,idy,iut,wp,nn
C-  101	continue
	iyear=int(ryear)
	iyr=iyear-iyear/100*100
C
       call blet4(iyear,AYEAR)
	infhpo(16:19)=AYEAR
	OPEN(15,file=infhpo,action='READ')
C Read Hpo Apo for given day d(SSN=0), d-1,d+1
        do lz=1,lda-1
          	DO j=0,23					
 	read(15,32,end=58) kyear,kmn,kdy,kut,hpo(j),apo(j)  ! d-1
	 ENDDO		
	  enddo
          	DO j=24,47					
 	read(15,32,end=58) kyear,kmn,kdy,kut,hpo(j),apo(j)	!d(Wpmax)
	 ENDDO		
          	DO j=48,71					
 	read(15,32,end=58) kyear,kmn,kdy,kut,hpo(j),apo(j)	!d+1
	 ENDDO		
   58	 close(15)
C
C Hpomax, t(Hpomax), skp
	ithpo=0
	it1=iut
	it2=iut+47  !48hrs ~ iut+-24h
      hpomax=max(hpo(it1),hpo(it2))
	shp=(hpo(it1)+hpo(it2))/2.  !SHpo for t(WPmax)+-24h
              DO j=it1+1,it2-1
	shp=shp+hpo(j)
	if (hpomax.lt.hpo(j)) then
	hpomax=hpo(j)
	ithp=j-24
	endif
	           enddo
C
C Read Dst
	infdst(16:19)=AYEAR
	OPEN(16,file=infdst,action='READ')
C
        do lz=1,lda-1
 	read(16,34,end=54) kdyr,kdmn,kddy,(idst(j),j=0,23)  ! d-1
	  enddo
 	read(16,34,end=54) kdyr,kdmn,kddy,(idst(j),j=24,47)	!d(Wpmax)
 	read(16,34,end=54) kdyr,kdmn,kddy,(idst(j),j=48,71)	!d+1
   54	 close(16)
C
C dstmin, t(dstmin), sdst
	itdst=0
	it1=iut
	it2=iut+47  !48hrs ~ iut+-24h
      idstmin=min(idst(it1),idst(it2))
	sdst=(idst(it1)+idst(it2))/2.  !SDst for t(WPmax)+-24h
              DO j=it1+1,it2-1
	sdst=sdst+idst(j)
	if (idstmin.gt.idst(j)) then
	idstmin=idst(j)
	itdst=j-24
	endif
	           enddo
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
	DO kk=1,lda-1
	read(11,33,end=55) kyr,kmn,kdy,(wu(k),k=0,23)
	read(12,33,end=56) kyr,kmn,kdy,(wl(k),k=0,23)
	read(13,33,end=57) kyr,kmn,kdy,(we(k),k=0,23)
	 ENDDO
	read(11,33,end=55) kyr,kmn,kdy,(wu(k),k=24,47)
	read(12,33,end=56) kyr,kmn,kdy,(wl(k),k=24,47)
	read(13,33,end=57) kyr,kmn,kdy,(we(k),k=24,47)
	read(11,33,end=55) kyr,kmn,kdy,(wu(k),k=48,71)
	read(12,33,end=56) kyr,kmn,kdy,(wl(k),k=48,71)
	read(13,33,end=57) kyr,kmn,kdy,(we(k),k=48,71)
   55	close(11)
   56	close(12)
   57	close(13)
c     
C WU rate
C WUmax, t(WUmax), sWU
	it1=iut
	it2=iut+47  !48hrs ~ iut+-24h
      wumax=wu(it1)
      itwu=it1-24
	if (wu(it2).gt.wumax) then
	wumax=wu(it2)
	itwu=it2-24
      endif
	swu=(wu(it1)+wu(it2))/2.  !SWU for t(WPmax)+-24h
              DO j=it1+1,it2-1
	swu=swu+wu(j)
	if (wumax.lt.wu(j)) then
	wumax=wu(j)
	itwu=j-24
	endif
	           enddo
C WL rate
C WLmin, t(WLmin), swl
	it1=iut
	it2=iut+47  !48hrs ~ iut+-24h
      wlmin=wl(it1)
      itwl=it1-24
	if (wl(it2).lt.wlmin) then
	wlmin=wl(it2)
	itwl=it2-24
      endif
	swl=(wl(it1)+wl(it2))/2.  !SWL for t(WPmax)+-24h
              DO j=it1+1,it2-1
	swl=swl+wl(j)
	if (wlmin.gt.wl(j)) then
	wlmin=wl(j)
	itwl=j-24
	endif
	           enddo
C WE rate
C WEmax, t(WEmax), swe
	it1=iut
	it2=iut+47  !48hrs ~ iut+-24h
      wemax=we(it1)
      itwe=it1-24
	if (we(it2).gt.wemax) then
	wemax=we(it2)
	itwe=it2-24
      endif
	swe=(we(it1)+we(it2))/2.  !SWE for t(WPmax)+-24h
              DO j=it1+1,it2-1
	swe=swe+we(j)
	if (wemax.lt.we(j)) then
	wemax=we(j)
	itwe=j-24
	endif
	           enddo
C
C Output
      ndy=ndy+1
      write(*,30) ryear,lda,imn,idy,iut,wp,ithp,hpomax,shp
	+,itdst,idstmin,sdst,itwu,wumax,swu,itwl,wlmin,swl
	+,itwe,wemax,swe,ndy
	write(14,30) ryear,lda,imn,idy,iut,wp,ithp,hpomax,shp
	+,itdst,idstmin,sdst,itwu,wumax,swu,itwl,wlmin,swl
     +,itwe,wemax,swe,ndy

C
C
   41 continue
   50	close(10)
C
   30 format(1X,F8.3,1X,I3,3(1X,I2),1X,F4.1,1X,I3,2(1X,F6.1)
     +,1X,I3,1X,I4,1X,F7.1,3(1X,I3,2(1X,F7.2)),1X,I4)
   	close(unit=14)
	pause ' '
	STOP
	END