      program addcoskp3jm
C..Add Moscow monitor cor(baseline=8848) daily mean, for the day of Kpmax...........May 2022
C
      CHARACTER*64 infile,outfile,infile1,infile2
	CHARACTER*8 yr1,yr2
		CHARACTER*4 AYEAR
		CHARACTER*2 AMN,ADY
	CHARACTER*10 yrmndy
	DIMENSION icos(0:23),atm(0:23)
C
      yr1='19802014'
	yr2='20152022'
      outfile='addcoskp3jm.txt'
	infile='wulessw0kp3jm.txt'
	infile1='d:\Data\Press\corYER1YER2.txt'
	infile2='d:\Data\Press\AtmYER1YER2c.txt'
C
       OPEN(10,file=infile,action='READ')
       OPEN(11,file=outfile,access='APPEND')
   30 format(1X,F8.3,1X,I3,3(1X,I2),1X,F4.1,4(1X,I3,1X,F6.2),1X,I3
     +,1X,F6.1,1X,I3,1X,I4)
   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,563
    	read(10,30,end=50) ryear,lda,imn,idy,itkp,xkpmax,jtwu,wumax
     +,jtwl,wlmin,jtwe,wemax,jtwp,wpmax,jtsw,swmax,isumkp,nn
CTEMP:
       ndy=nn-1
	if (nn.lt.561) goto 41  !TEMPPPPPPPPPPPPPP
C
      iyear=int(ryear)
C 
      if(iyear.lt.2015) then
	infile1(18:25)=yr1
	infile2(18:25)=yr1
	else
	infile1(18:25)=yr2
	infile2(18:25)=yr2
	endif

 	OPEN(12,file=infile1,action='READ')
  1	read(12,*,end=55) yrmndy,dut,mcos
      if (mcos.eq.0) mcos=8848
C
	AYEAR=yrmndy(1:4)
	read(AYEAR,*) zyear
       kyear=int(zyear)
	if (kyear.ne.iyear) goto 1
		AMN=yrmndy(6:7)
 	ADY=yrmndy(9:10)
       read(AMN,*) rmn
	kmn=int(rmn)
	       read(ADY,*) rdy
	kdy=int(rdy)
C
	if ((iyear.eq.kyear).and.(imn.eq.kmn).and.(idy.eq.kdy)) then
	iut=int(dut)
	icos(iut)=mcos
	if (iut.lt.24) then
	goto 1
	else
	goto 55
	endif
	                  else
	goto 1
	                   endif
C
   55	close(12)

C  Atm pressure
   34	format(1X,F8.3,1X,I3,2(1X,I2),1X,I2,1X,F8.1)
       	OPEN(13,file=infile2,action='READ')
   3	read(13,34,end=57) xyear,ldai,jmni,jdyi,kuti,patm
	jyear=int(xyear)
	if (jyear.ne.iyear) goto 3
	if ((iyear.eq.jyear).and.(imn.eq.jmni).and.(idy.eq.jdyi)) then
	atm(kuti)=patm
	if (kuti.lt.24) then
	goto 3
	else
	              goto 57
	endif
	                                             else
	goto 3
	                                             endif
   57 close(13)
C
C select daily peak
	icosmax=0
	atmmax=0.
	icosav=0
	atmav=0.
        do iut=0,23
	icosav=icosav+icos(iut)
      atmav=atmav+atm(iut)
	if (icosmax.lt.icos(iut)) then 
	icosmax=icos(iut)
	itcos=iut
	endif
	if (atmmax.lt.atm(iut)) then 
	atmmax=atm(iut)
	itatm=iut
	endif
	 enddo
	cosav=icosav/24.
	atmav=atmav/24.
	rcos=(icosmax/8848.0-1.0)*100.0 !%	
	rcosav=(cosav/8848.0-1.0)*100.0 !%	
	rpam=(atmmax/9918.4-1.0)*100 !%
	ratmav=(atmav/9918.4-1.0)*100 !%

C Output
    2  ndy=ndy+1
      write(*,32) ryear,lda,imn,idy,itcos,rcos,rcosav,itatm,rpam,ratmav
	+,ndy
	write(11,32) ryear,lda,imn,idy,itcos,rcos,rcosav,itatm,rpam,ratmav
	+,ndy
C
   41 continue
   32 format(1X,F8.3,1X,I3,2(1X,I2),2(1X,I3,2(1X,F7.3)),1X,I4)
   50	close(unit=10)
	close(unit=11)
	pause ' '
	STOP
	END