      program readcosssn0
C..Add Moscow monitor cor(baseline=8848) daily mean, for the day of Kpmax...........May 2022
C
      CHARACTER*64 infile,outfile,infile1
	CHARACTER*8 yr1,yr2,yr3
		CHARACTER*4 AYEAR
		CHARACTER*2 AMN,ADY,AYR
	CHARACTER*10 yrmndy
	DIMENSION icos(0:23),kp(8)
C
      yr1='19581979'
      yr2='19802014'
	yr3='20152022'
      outfile='cosssn0.txt'
	infile='kpssn0.txt'
	infile1='d:\Data\Press\corYER1YER2.txt'
C
       OPEN(10,file=infile,action='READ')
  101	read(10,30) nn,iyr,imn,idy,kp
C      AYR=txt(7:8)
C	read(ayr,*) yr
C	inyr=int(yr)
	if (iyr.lt.61) then
	goto 101 ! prec years
	else
	backspace(10)
	endif
C
       OPEN(11,file=outfile,access='APPEND')
   30 format(1X,I4,1X,3I2,8(1X,I3))
   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,3082
    	read(10,30,end=50) nn,iyr,imn,idy,kp
c	call submmdd(lda,iyr,imn,idy)
      lda=ndoy(iyr,imn,idy)
	 call blet2(iyr,AYR)
	AYEAR(3:4)=AYR
	if (iyr.lt.30) then
	AYEAR(1:2)='20'
	iyear=2000+iyr
	else
	AYEAR(1:2)='19'
	iyear=1900+iyr
	endif
		z1=iyear/4.0
      jz=int(z1)*4
      IF(jz.EQ.iyear) THEN
	dnr=366.
		  ELSE
                dnr=365.
		       ENDIF
	del=1./dnr
        ryear=iyear+(lda-1)*del
CTEMP:
C	if (nn.lt.561) goto 41  !TEMPPPPPPPPPPPPPP
C
C 
      if(iyear.lt.1980) then
	infile1(18:25)=yr1
	else
	  if(iyear.lt.2015) then
	  infile1(18:25)=yr2
	      else
	  infile1(18:25)=yr3
	  endif
	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
C select daily peak
	icosav=0
        do iut=0,23
	icosav=icosav+icos(iut)
	 enddo
	cosav=icosav/24.
	rcosav=(cosav/8848.0-1.0)*100.0 !%	
C Output
    2  ndy=ndy+1
      write(*,32) ryear,lda,imn,idy,rcosav,ndy
	write(11,32) ryear,lda,imn,idy,rcosav,ndy
C
   41 continue
   32 format(1X,F8.3,1X,I3,2(1X,I2),1X,F7.3,1X,I4)
   50	close(unit=10)
	close(unit=11)
	pause ' '
	STOP
	END