      program exthpoeqtot
C..Add peak Hpo (1h proxy pf kp)..for EQs at SSN0 1995-2022...............Oct 2022
C
      CHARACTER*64 outfile,infileq,infile
	+,txt
	CHARACTER*2 AMN,ADY,AUT,AMM
	CHARACTER*4 AYEAR,AYEAR_pre,AYEAR_nex
	DIMENSION hp(0:71),kap(0:71)
C
      infileq='addwcpiaeq.txt' ! all SSN=0  1994-2020
	outfile='addeqhpotot.txt'  ! peak Hpo
	infile='d:\Apkp\Hpo\HpoYEAR.txt'
C
C
       OPEN(11,file=infileq,action='READ')
	do i=1,18
	read(11,*) txt	   ! extra lines for 1994 (no Hpo data)
	enddo
C
       OPEN(12,file=outfile,access='APPEND')
   30 format(1X,F8.3,1X,I3,4(1X,I2),2(1X,F6.2),1X,I4,2(1X,F4.1)
     +,2(1X,I4))
   31 format(I4,A64)
   32 format(I4,2(1X,I2),1X,I2,32X,F7.3,1X,I4)
   33 format(3I2,24(1X,F4.1))
C
	DO k=0,71
	hp(k)=0.
	ENDDO
	ndy=0
C
	DO 41 iev=1,2000	   ! All SSN=0 1995-2022
  123	read(11,30,end=50) ryear,lda,imn,idy,iuteq,imm,glats,glons,kmds
 	+,aeqmax,eqav,ndyeq,knum
      iyear=int(ryear)
	call blet4(iyear,AYEAR)
C		IF (iyear.lt.1995) GOTO 123
C
C!Hpo data start from 1995
      DO j=0,71
	hp(j)=0.0
	kap(j)=0
	enddo
	iflag=0
C Prec year if lda=1:
       IF (lda.eq.1) THEN                  !+
	iflag=1
	iyear_pre=iyear-1
	call blet4(iyear_pre,AYEAR_pre)
	infile(16:19)=ayear_pre
	OPEN (10,file=infile,action='READ')
C
		z1=iyear_pre/4.0
      jz=int(z1)*4
      IF(jz.EQ.iyear_pre) THEN
	ndnr=366
		  ELSE
                ndnr=365
		       ENDIF
C
	 do kk=1,ndnr
	DO j=0,23
	read(10,32,end=54) kyear,kmn,kdy,kut,hp(j),kap(j)
	ENDDO
	 enddo
   54	close(10)
       ENDIF								 !+
C 
CC current year
C
	infile(16:19)=ayear
C
	OPEN(10,file=infile,action='READ')
C
C 	 produce lda for day 
C++ READ 1h hp
C
		z1=iyear/4.0
      jz=int(z1)*4
      IF(jz.EQ.iyear) THEN
	ndnr=366
		  ELSE
                ndnr=365
		       ENDIF

	iyr=iyear-iyear/100*100
C
	if (lda.eq.1) then			!1
C 
    	DO j=1,47					!
	read(10,32,end=51) kyear,kmn,kdy,kut,hp(j+24),kap(j+24)
	 ENDDO						!
   51	close(10)
	GOTO 1234
	endif						!1
C
C 3days of current year:
      if ((lda.gt.1).and.(lda.lt.ndnr)) THEN 	  !2
    	do 45 kk=1,lda-1
C 
    	DO j=0,23					!3
	read(10,32,end=55) kyear,kmn,kdy,kut,hp(j),kap(j)
	 ENDDO						!3
   45	CONTINUE
C lda
    	DO j=0,23					!3
	read(10,32,end=55) kyear,kmn,kdy,kut,hp(j+24),kap(j+24)
	 ENDDO						!3
C lda+1
    	DO j=0,23					!3
	read(10,32,end=55) kyear,kmn,kdy,kut,hp(j+48),kap(j+48)
	 ENDDO						!3
   55	close(10)
      GOTO 1234
	                               ENDIF       !2                         
C
C 2days of current yea+1day of next year
        IF (lda.eq.ndnr) THEN                 !3
    	do 44 kk=1,lda-1
C 
    	DO j=0,23					!
	read(10,32,end=56) kyear,kmn,kdy,kut,hp(j),kap(j)
	 ENDDO						!
   44	CONTINUE
C lda
    	DO j=0,23					!
	read(10,32,end=56) kyear,kmn,kdy,kut,hp(j+23),kap(j+23)
	 ENDDO						!
   56	close(10)
C
       iyear_nex=iyear+1
	call blet4(iyear_nex,AYEAR_nex)
	infile(16:19)=ayear_nex
C
	OPEN(10,file=infile,action='READ')
    	DO j=0,23					!
	read(10,32,end=57) kyear,kmn,kdy,kut,hp(j+48),kap(j+48)
	 ENDDO						!
   57	close(10)
             ENDIF                !3
C++
C
 1234     niteq=iuteq+24
       hpeq=hp(niteq)
	kapeq=kap(niteq)
C hpmax_pre
      hp_pre=0.0
	it_pre=0
       kt1=niteq-24
	kt2=niteq+23
C
	do k=niteq-1,kt1,-1
C
 	if (hp(k).gt.hp_pre) then
	hp_pre=hp(k)
	it_pre=k
	                endif  
	enddo
	jt_pre=it_pre-niteq
C
C hp_nex
      hp_nex=0.0
	it_nex=0
C
	do k=niteq+1,kt2  !new
C
 	if (hp(k).gt.hp_nex) then
	hp_nex=hp(k)
C	it_nex=k+24
	it_nex=k
	                endif  
	enddo
	jt_nex=it_nex-niteq
C Output
	call blet2(imn,AMN)
	call blet2(idy,ADY)
	call blet2(iuteq,AUT)
	call blet2(imm,AMM)
	kk=0
C
C      IF ((hpeq.lt.3.0).and.(hp_pre.lt.3.0).and.(hp_nex.lt.3.0))
       ndy=ndy+1
      write(*,35) ryear,lda,amn,ady,aut,amm,glats,glons,kmds
 	+,aeqmax,eqav,ndyeq,knum,hpeq,jt_pre,hp_pre,jt_nex,hp_nex,ndy
	write(12,35) ryear,lda,amn,ady,aut,amm,glats,glons,kmds
 	+,aeqmax,eqav,ndyeq,knum,hpeq,jt_pre,hp_pre,jt_nex,hp_nex,ndy
C
   41 continue
   35 format(1X,F8.3,1X,I3,4(1X,A2),2(1X,F6.2),1X,I4,2(1X,F4.1)
     +,2(1X,I4),1X,F5.2,2(1X,I3,1X,F7.3),1X,I4)
   36 format(48(1X,I3))
   50	close(unit=11)
	close(unit=12)
	pause ' '
	STOP
	END