      program addeqkpssn0
C..Add EQ daily Mmax, Dkm, Mav, glat,glon set, for the day of SSN0 (all Kp).......May 2022
C
      CHARACTER*64 infile,outfile,infileq
		CHARACTER*4 AYEAR
	DIMENSION eqs(115),glats(115),glons(115),kmds(115),kms(115)
	+,kp(0:7)
C
      outfile='addeqkpssn0.txt'
	infile='kpssn0.txt'
	infileq='d:\Apkp\Equak\Obninsk\eqYEAR.txt'
C
       OPEN(10,file=infile,action='READ')
	DO k=1,1061
    	read(10,30,end=50) nn,iyr,imn,idy,(kp(k),k=0,7)
	ENDDO
C
       OPEN(11,file=outfile)
   30 format(1X,I4,1X,3I2,8(2X,I2))
   33 format(1X,F8.3,1X,I3,2(1X,I2),1X,F5.3,1X,I2,1X,F5.3,1X,I4)
   31 format(I4,A64)
C
	ndy=0
C
	DO 41 iev=1,1974
    	read(10,30,end=50) nn,iyr,imn,idy,(kp(k),k=0,7)
C-	ndy=iev
c
       lda=ndoy(iyr,imn,idy)
	      		z1=iyear/4.0
      jz=int(z1)*4
      IF(jz.EQ.iyear) THEN
	dnr=366.
		  ELSE
                dnr=365.
		       ENDIF
	del=1./dnr
C
      if (iyr.lt.30) then
	iyear=2000+iyr
	               else
	iyear=1900+iyr
	endif
	ryear=iyear+(lda-1)*del
	do k=1,115
	eqs(k)=0.0
	glats(k)=0.0
	glons(k)=0.0
	kmds(k)=0
	kms(k)=0
	enddo
	eqav=0.0
	aeqmax=0.0
	knum=0
	kk=0
C//	if (iyear.eq.2021) goto 2 ! no EQ file for 2021
C
C 
       call blet4(iyear,AYEAR)
	infileq(25:28)=AYEAR
      ldak_pre=0

 	OPEN(12,file=infileq,action='READ')
	kk=0
      	iflag=0
  1	read(12,*,end=55) kyear,kmn,kdy,dut,glat,glon,kmd,eqmag,knum
      ldak=ndoy(iyr,kmn,kdy)
	if (kmn.gt.imn) goto 55
	if (ldak.eq.lda) then
	iflag=1
	kk=kk+1
	glats(kk)=glat
	glons(kk)=glon
	kmds(kk)=kmd
	eqs(kk)=eqmag
	kms(kk)=knum
	eqav=eqav+eqmag
	endif
C
	 goto 1
C
   55	close(12)
   	 if (iflag.eq.0) goto 41
      eqav=eqav/kk
C
C select daily peak
	aeqmax=0.
        do jj=1,kk
	if (aeqmax.lt.eqs(jj)) then 
	aeqmax=eqs(jj)
	knum=kms(jj)
	jm=jj
	endif
	 enddo

C Output
    2  ndy=ndy+1
      write(*,32) ryear,lda,imn,idy,glats(jm),glons(jm),kmds(jm),aeqmax
	+,eqav,kk,knum,ndy
	write(11,32) ryear,lda,imn,idy,glats(jm),glons(jm),kmds(jm),aeqmax
	+,eqav,kk,knum,ndy
C-  141	 	ldak_pre=ldak
C
   41 continue
   32 format(1X,F8.3,1X,I3,2(1X,I2),2(1X,F6.2),1X,I4,2(1X,F4.1)
     +,3(1X,I4))
   50	close(unit=11)
	pause ' '
	STOP
	END