      program addmg3kp0jm
C..Add deviation MgII(daily-monthly) for the day of Kpmax.............................................May 2022
C
      CHARACTER*64 infile,outfile,infilem,txt
C
      infile='addmgkp3jm.txt'
	infilem='cntmgmon.res'
	outfile='adevmgkp0jm.txt'
C
       OPEN(10,file=infile,action='READ')
       OPEN(11,file=outfile,access='APPEND')
   30 format(1X,F8.3,1X,I3,2(1X,I2),2X,F9.7,1X,I4)
   33 format(1X,F8.3,1X,I2,2X,F8.5)
   31 format(I4,A64)
C
	ndy=0
C
	DO 41 iev=1,563
    	read(10,30,end=50) ryear,lda,imn,idy,emg,ndy
CTEMP:
C       ndy=nn-1
c      if (nn.le.125) goto 41 !Temp
C
      iyear=int(ryear)
C 
 	OPEN(12,file=infilem,action='READ')
	read(12,*) txt ! title line
  1	read(12,33,end=55) xyear,kmn,avmg
C
       kyear=int(xyear)
	if ((iyear.eq.kyear).and.(imn.eq.kmn)) then
	goto 55
	                  else
	goto 1
	                   endif
C
   55	close(12)
C Output
    2  continue
C      ndy=ndy+1
      ratmg=emg/avmg
      write(*,32) ryear,lda,imn,idy,emg,ratmg,ndy
	write(11,32) ryear,lda,imn,idy,emg,ratmg,ndy
C
   41 continue
   32 format(1X,F8.3,1X,I3,2(1X,I2),2X,F9.7,1X,F7.4,1X,I4)
   50	close(unit=10)
	close(unit=11)
	pause ' '
	STOP
	END