      program comparmed
C.......................................................Mar 2020
C
C Compare Shubin model fmF2 , hmF2 with observation
C monthly median data / model
C
      character*120 infile1,infile2,outfile,outmed,txt
		character*2 AYR,st,AMN
	character*4 AYEAR
	character*6 YYMMDD
      dimension CC(31,24),xmed(24),IM(12),ymod(24)
	DATA IM/31,28,31,30,31,30,31,31,30,31,30,31/
	common /BL1/CC,XMED
C
      infile1='testmofm2014.out'
		OPEN(13,file=infile1,action='READ')
	read(13,*) txt ! extra line
C
	infile2='d:\web\dat1\YR\moyrmn.txt' ! foR2 data
	st='mo' 
	AYEAR='2014' 
      AYR='14'
	read(ayr,*) ryr
	iyr=int(ryr)
	if(int(ryr/4.)*4.eq.iyr) THEN 
 	im(2)=29
	                          ELSE
	im(2)=28
	ENDIF

      outfile='comparmed.res'
	OPEN(10,file=outfile,access='APPEND')
	outmed='stYRmed.txt'
	outmed(1:2)=st
	outmed(3:4)=AYR
      	OPEN(11,file=outmed,access='APPEND')
	write(*,32) AYEAR,st,' foF2med'
	write(11,32) AYEAR,st,' foF2med'
C
   30 FORMAT(A6,2X,24(F3.1,2X)) ! foF2
   31 FORMAT(1X,2A2,1X,I2,2(2X,F5.1)) ! foF2
   32 FORMAT(1X,A4,1X,A2,A8) ! foF2med
   33 format(80X,F5.1) ! foF2
        infile2(13:14)=AYR
	infile2(18:19)=AYR
C
	DO 70 imn=1,12
	call blet2(imn,AMN)
      infile2(20:21)=AMN
		OPEN(12,file=infile2,action='READ')
      do i=1,4
	read(12,*) txt ! extra line
	enddo
	ndmn=im(imn)
	DO kk=1,ndmn
	read(12,30,end=50) YYMMDD,(CC(kk,k),k=1,24) 
	ENDDO
   50	close(unit=12)
C
       call submedmon(ndmn,24)
      xav=0.
        do jj=1,24
	xav=xav+xmed(jj)
	 enddo
	xav=xav/24.
C
C Read Model results for month:
       DO jj=1,24
	read(13,33,end=51) ymod(jj)
	enddo
C
C RMSE  and PE
       dev=0.
	zet=0.
	DO jj=1,24
	dev=dev+(xmed(jj)-ymod(jj))**2
	zet=zet+(xmed(jj)-xav)**2
	ENDDO
	RMSE=sqrt(dev/24.)
	PE=(1.-sqrt(dev/zet))*100. ! %
C
        do iut=0,23
	write(*,31) AYR,AMN,iut,xmed(iut+1),ymod(iut+1)
      write(11,31) AYR,AMN,iut,xmed(iut+1),ymod(iut+1)
	 enddo
        write(*,34) AYR,AMN,dev,zet,RMSE,PE
	write(10,34) AYR,AMN,dev,zet,RMSE,PE
   70 CONTINUE ! next month
   51   close(unit=13)
      close(unit=11)
	close(unit=10)
C
   34 format(1X,2A2,2(1X,F7.3),1X,F7.2,1X,F8.3) ! foF2
       pause ' '
	STOP
	END