      program exthpoeq12
C..Add peak Hpo (1h proxy pf kp)..for EQs at SSN0 1995-2022...............June 2022
C
      CHARACTER*64 outfile,infileq,infile
	+,txt
	CHARACTER*2 AMN,ADY,AUT,AMM
	DIMENSION hp(0:71),kap(0:71),kp(0:47)
C
c-      infileq='addeqssn0.txt' ! all SSN=0  1994-2020
Crem	infileq='addeqhpo1.txt'  ! peak Hp<3
crem	outfile='arreqhpo1.txt'  ! array Hp<3
	infileq='addeqhpo2.txt'  ! peak Hp>=3
	outfile='arreqhpo2.txt'  ! array Hp>=3
	infile='Hpo19952022.txt'
C
C
       OPEN(11,file=infileq,action='READ')
       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)
     +,3(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))
   35 format(1X,F8.3,1X,I3,4(1X,I2),2(1X,F6.2),1X,I4,2(1X,F4.1)
     +,2(1X,I4),1X,F5.2,2(1X,I3,1X,F7.3),1X,I4)
C
	DO k=0,71
	hp(k)=0.
	ENDDO
c	ndy1=0
c	ndy2=0
c1	ndy1=375
c2	ndy1=610
c3	ndy1=777
c4	ndy1=919
c5	ndy1=1052
c+1      ndy1=401
      ndy1=560
C
C	DO 41 iev=1,2000	   ! All SSN=0 1995-2022
	DO 41 iev=1,660		!Hp >=4 
crem	DO 41 iev=1,1150	!Hp <4 
C+  123	read(11,30,end=50) ryear,lda,imn,idy,iuteq,imm,glats,glons,kmds
c+ 	+,aeqmax,eqav,kk,knum,ndyeq
  123	read(11,35,end=50) ryear,lda,imn,idy,iuteq,imm,glats,glons,kmds
 	+,aeqmax,eqav,ndyeq,knum,hpeq,jt_pre,hp_pre,jt_nex,hp_nex,ndy
      iyear=int(ryear)
c--		IF (iyear.lt.1995) GOTO 123
C
       if (ndy.le.ndy1) goto 41  ! temp
C!Hpo data start from 1995
      DO j=0,71
	hp(j)=0.0
	kp(j)=0
	kap(j)=0
	enddo
C
	OPEN(10,file=infile,action='READ')
   1	read(10,31,end=55) kyear,txt
      IF (kyear.lt.iyear) then 
	goto 1
	ELSE
	backspace(10)
	ENDIF

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
	lda0=lda-1
	lda1=lda+1
	lda_nex=lda1
C+++
	    if (lda0.eq.0) then  !+++++++
C go back for 1 day
	do j=0,23
	backspace(10)
	enddo
C read data for the last day of prec. year
C
    	DO j=0,23					!3
	read(10,32,end=55) kyear,kmn,kdy,kut,hp(j+48),kap(j+48)
	 ENDDO						!3
	    endif				   !+++++++++
C+++
	if (lda1.gt.ndnr) lda_nex=lda
C
  43  	do 44 kk=1,lda_nex            !1
C Move data up:
      do jj=0,23
	hp(jj)=hp(jj+24)
	kap(jj)=kap(jj+24)
	hp(jj+24)=hp(jj+48)
	kap(jj+24)=kap(jj+48)
	 enddo					  !1
    	DO j=0,23					!3
	read(10,32,end=55) kyear,kmn,kdy,kut,hp(j+48),kap(j+48)
	 ENDDO						!3
   44	CONTINUE
C++
       if (lda1.gt.ndnr) THEN  
C	 +Read data for 1 Jan (the next year):
C Move data up:
      do jj=0,23
	hp(jj)=hp(jj+24)
	kap(jj)=kap(jj+24)
	hp(jj+24)=hp(jj+48)
	kap(jj+24)=kap(jj+48)
	 enddo					  !1
    	DO j=0,23					!3
	read(10,32,end=55) kyear,kmn,kdy,kut,hp(j+48),kap(j+48)
	 ENDDO						!3
	endif
C
C
   55	close(10)
C  123	read(11,35,end=50) ryear,lda,imn,idy,iuteq,imm,glats,glons,kmds
C 	+,aeqmax,eqav,ndyeq,knum,hpeq,jt_pre,hp_pre,jt_nex,hp_nex,ndy
      
C
      niteq=iuteq+24
c       hpeq=hp(niteq)
	kapeq=kap(niteq)
C hpmax_pre
c-      hp_pre=0.0
c-	it_pre=0
       kt1=niteq-24
	kt2=niteq+23
C
         GOTO 234
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)
c------------
  234	kk=0
	do k=kt1,kt2
	kp(kk)=nint(hp(k)*10.0)
	kk=kk+1
	enddo
C
C-      IF ((hpeq.lt.4.0).and.(hp_pre.lt.4.0).and.(hp_nex.lt.4.0))
       write(*,36) ndy,(kp(k),k=0,47)
	write(12,36) ndy,(kp(k),k=0,47)
C
   41 continue
   36 format(1X,I4,48(1X,I3))
   50	close(unit=11)
	close(unit=12)
	pause ' '
	STOP
	END