C ========================================================
C 
      SUBROUTINE SUBRECOGPC(fmh,outgap)
C...............................................Oct. 2013
C Use subinterps.for
C.............................................Jan. 2012
C Based on subremov.for subroutine
C
C Include here reconstruction of missed data from relevant map
C
C----------------------------------------------------------------
C
C program for producing 2-mns  xxx.br OUTPUT web files foF2, M3000F2 and hmF2
C
C*********TEST
C 
	DIMENSION XDAT(0:23),IRES(0:23)
     +,if2(31,0:23),ih2(31,0:23),im3(31,0:23)
	+,xf2(0:23),xm3(0:23),xhm(0:23)
     +,x(24),y(24),ynm(24)                             ! Sep 2019
      CHARACTER*128  OUTGAP,ZZZ(80),XXX,INFILE1,INFILE2,YYY(80)
	+,XEND,infilet                                    ! Sep 2019          
	CHARACTER*2 ST,AMN,DY1,DY2,dd1,dd2,DY22,YY,DMN,PM,PY,XMN,XDY,XYR
	+,PD,SDY,SS(0:23),AUT,AYR
     +,CMN,CDY,CC
     	CHARACTER*4 YEAR,TITS(4),TITPAR,TTT
	+,CYEAR,CTIME
	CHARACTER*30 STNAME
	CHARACTER*13 STAT
	CHARACTER(10) DD
      CHARACTER(5) ZZ,YYYYY,YDAT(0:23)
	CHARACTER*3	VVV
	CHARACTER*1 fmh,f1,C
	INTEGER*2 ID1,ID2,idy,nday,iut,ipy,ipm,idy1
C
	COMMON /BL1/ST,YEAR,AYR,AMN,DY1,DY2,DD1,DD2,ID1,ID2,JF,JH,JT,DY22
	+,STNAME
	COMMON /BL2/ STAT,GLAT,GLON,DD,TTT,ZZ,R12
	COMMON /BL8/ IF2,IM3,IH2
	DATA TITS/'foF2','M3F2','hmF2','TEC '/
C
 	 cyear=DD(1:4)
 	 cmn=DD(5:6)
	cdy=DD(7:8)			 
	ctime=TTT
	ctime(3:4)='UT'


C First create .br file for prec. month PM (prec. year PY) + current month AMN (current year YY)
	read(ayr,*) ryr
	iyr=int(ryr)
	read(amn,*) rmn
	imn=int(rmn)
	read(DY1,*) rdy
	idy1=int(rdy)
	read(DY2,*) rdy2
	idy2=int(rdy2)

	idy=idy1
	call blet2(idy,PD)
	 ipm=imn-1
	ipy=iyr
C for current JAN
	if (imn.eq.1) then
	ipy=ipy-1
	ipm=12
	endif
C
	call blet2(ipy,PY)
	call blet2(ipm,PM)
	if (imn.eq.1) then
	DMN='00'
	else
	DMN=PM
	endif
C+++++++++++++
C
	infile1='c:/web/graf/dat1/yy/STYYPMf.txt'

	infile1(21:22)=ST
	if (fmh.eq.'f') then
	infile1(16:16)='1'
	infile1(27:)='.txt'
	TITPAR=TITS(1)
	endif
	if (fmh.eq.'m') then
	infile1(16:16)='5'
	infile1(27:)='m.txt'
	TITPAR=TITS(2)
	endif
	if (fmh.eq.'h') then
	infile1(16:16)='6'
	infile1(27:)='h.txt'
	TITPAR=TITS(3)
	endif
	if (fmh.eq.'t') then
	infile1(16:16)='7'
	infile1(27:)='t.txt'
	TITPAR=TITS(4)
	endif
	infile2=infile1     ! for current month
	infile1(18:19)=PY
	infile1(23:24)=PY
	infile1(25:26)=PM

	infile2(18:19)=AYR
	infile2(23:24)=AYR
	infile2(25:26)=AMN
C
C Avoid input of prec. month for
C
	j1=0  ! 
	OPEN(22,FILE=OUTGAP,action='write') ! foF2
crem 	if (fmh.eq.'t') goto 199
C
C input of prec. month :
C
  200		OPEN(20,FILE=infile1)
      jcnt=0
      do 141 j=1,41
  198	READ (20,17,END=195,ERR=211) XXX
	if (j.lt.5) goto 141  ! avoid 4 extra lines
	XXX(1:2)=PY
	XXX(3:4)=PM
c	if (imn.eq.1) then
c	XXX(3:4)=DMN
c	ENDIF
      ZZZ(j-4)=XXX
c-	write(*,17) XXX
  	jcnt=jcnt+1
 141	continue
 195	close(unit=20)

   17	format(A128)
C  117	FORMAT(A13,1X,A4)
  117	FORMAT(A13,1X,A2,1X,A4,2X,'glat ='F5.1,1X,'glon ='F5.1)
 118	FORMAT('Date of issue: ',A4,'.',A2,'.',A2,1X,'Time:',A4)
  201	format('YYMMDD|UT 0    1    2    3    4    5    6    7    8'
    	&'    9   10   11   12   13   14   15   16   17   18   19   20'
	&'   21   22   23'/'-------------------------------------------'
     &'-------------------------------------------------------------'
     &'------------------------')

C Correct OUTGAP:
	do j=1,jcnt  !  prec. month
c-	write(*,17) ZZZ(j)  ! temp
	write(22,17) ZZZ(j)
	enddo
	j1=jcnt ! last prec. month record
C
C Current month:
 199	continue
c
      OPEN(21,FILE=infile2) !	 reading INFILE2
	jcnt=0
	 do 142 j=1,41
  	READ (21,17,END=196,ERR=212) XXX

	if (j.lt.5) then 
	goto 142  ! avoid 4 extra lines
	endif
	XYR=XXX(1:2)
	XMN=XXX(3:4)
	XDY=XXX(5:6)
	if ((xyr.eq.AYR).and.(xmn.eq.AMN).and.(xdy.eq.PD)) goto 196 ! till prec.day
	j1=j1+1
	YYY(j-4)=XXX
	  	jcnt=jcnt+1
 142	continue
 196	close(unit=21)
	jend=jcnt
	XEND=XXX
C++
 
      OPEN(23,FILE=infile2)  ! write here results: for DY1...DY2 
	write(*,117) stat,st,TITPAR,glat,glon 
c	write(*,118) CYEAR,CMN,CDY,CTIME
c-	write(*,201)
	write(23,117) stat,st,TITPAR,glat,glon 
	write(23,118) CYEAR,CMN,CDY,CTIME
	write(23,201)
C++
		do j=1,jend
	XXX=YYY(j)
	XMN=XXX(3:4)
	XDY=XXX(5:6)
	if ((xmn.eq.amn).and.(xdy.eq.dy1)) then 
	goto 207
	else
c-	write(*,17) YYY(j)
	write(22,17) YYY(j)
 	write(23,17) YYY(j)
	endif
	enddo
 207	continue
C
C Add new data from DY1 to DY2
C
  316 FORMAT(3I2,1X,24(F4.1,1X))       ! Sep 2019
  317 FORMAT(3I2,2X,24(A5))
  300	CONTINUE
	infile1='c:/web/graf/20YR/STYYf.br'
	infile1(15:16)=AYR
	infile1(18:19)=ST
	infile1(20:21)=AYR
	infile1(22:22)=fmh
	OPEN(11,FILE=infile1)
C
C Add reading TEC with foF2 and hmF2
	if ((fmh.eq.'t').or.(fmh.eq.'m')) goto 304  ! Sep 2019
c	 outgap='c:\web\graf\temp\styrt.br'
      infilet=outgap
C-	infilet(22:22)='t'
	infilet(17:17)='t'
	OPEN(24,FILE=infilet,action='READ') 
C
  304	nday=idy1-1
  303	nday=nday+1
	if (nday.gt.idy2) goto 302 ! end reading new data
C new data:
	call blet2(nday,SDY)
	do k=0,23
	ydat(k)='000  '
	xhm(k)=0.
	enddo
	if (fmh.eq.'t') goto 305  !
C
  306	READ (11,317,END=1306,ERR=211) JY,JM,JD,(ydat(k),k=0,23)
	if ((jy.eq.iyr).and.(jm.eq.imn).and.(jd.eq.nday)) then 
	goto 1306
	else
	goto 306
	endif
 1306 CONTINUE
       if ((fmh.eq.'m').or.(fmh.eq.'t')) goto 305  !
C reading TEC:
 1307     READ (24,316,END=305,ERR=211) KY,KM,KD,(x(k),k=1,24)  !tec
 	if ((ky.eq.iyr).and.(km.eq.imn).and.(kd.eq.nday)) then 
		goto 305
			else
				goto 1307
	endif
C
C Reconstruction of missed data
C
  305	continue
	iflag=0
	do 301 iut=0,23		  ! Cycle on UT	=====================
	YYYYY=ydat(iut)
	VVV=YYYYY(1:3)
	YY=YYYYY(4:5)
	if (YY(1:1).eq.'/') then  
	VVV='000'
C	YY='  '                                          ! Sep 2019
	endif
	SS(iut)=YY
	if (vvv(1:1).eq.' ') vvv(1:1)='0'
	if (vvv(2:2).eq.' ') vvv(2:2)='0'
	if (vvv(3:3).eq.' ') vvv(3:3)='0'
	read(vvv,*) rrr
	xdat(iut)=rrr
	ires(iut)=nint(rrr)
	if (fmh.eq.'m') goto 301 ! avoid M3000 reconstruction
C 
C Reconstruction from map:
          	if (rrr.eq.0.) then               !===
	if((fmh.eq.'h').and.(if2(nday,iut).gt.0).and.(im3(nday,iut).gt.0))
	+ goto 327
		call blet2(iut,AUT)
C
  	f1=fmh
 	call subinter1(f1,ayr,amn,sdy,aut,resm)
	xdat(iut)=resm
	ires(iut)=nint(resm)
	yy(1:1)='/'
	if (fmh.ne.'t') SS(iut)=YY				 
	       endif							  !===
C
  327	if (fmh.eq.'h') then						!+++
        if (xdat(iut).gt.170.) GOTO 301

C Check if hmF2 can be produced from foF2 & M3000 F2
	   if ((if2(nday,iut).gt.0).and.(im3(nday,iut).gt.0)) then !********
 	iflag=1
Crem	xdat(iut)=0.
	yy(1:1)=' '												   !********
	endif													   !+++
	endif
  301	continue  ! End cycle on UT
	if ((fmh.eq.'h').and.(iflag.gt.0)) then  !++++++++++++++
C Produce hmF2 from M3000P
	do iut=0,23
	xf2(iut)=float(if2(nday,iut))/10.
	xm3(iut)=float(im3(nday,iut))/100.
	xhm(iut)=xdat(iut)
	enddo
		xlat=glat
 	xlon=glon
	call subhmf2(xlat,xlon,nday,xf2,xm3,xhm)
		glat=xlat
	glon=xlon
	do 1301 k=0,23
	if (xdat(k).eq.0.) xdat(k)=xhm(k)
	ires(k)=nint(xdat(k))
 1301	continue
	endif								   !++++++++++++++++++
C
  310	format(3A2,2X,24(I3,A2))
  311	format(3A2,1X,24(I4,1X))
C Record results for the day:
c  335 	do kk=0,23
c	ires(kk)=nint(xdat(kk))
c	if (fmh.eq.'f') if2(nday,kk)=ires(kk)
c	if (fmh.eq.'m') im3(nday,kk)=ires(kk)
c	if (fmh.eq.'h') then ih2(nday,kk)=ires(kk)
c	enddo
	if (fmh.eq.'t') then
C	write(*,311) ayr,amn,sdy,(ires(k),k=0,23)
	write(22,311) ayr,amn,sdy,(ires(k),k=0,23)
	write(23,311) ayr,amn,sdy,(ires(k),k=0,23)
C	else									 ! Sep 2019
          GOTO 303 ! Sep 2019
	endif                                       ! Sep 2019
	if (fmh.eq.'m') then
C	write(*,310) ayr,amn,sdy,(ires(k),ss(k),k=0,23)
	write(22,310) ayr,amn,sdy,(ires(k),ss(k),k=0,23)
	write(23,310) ayr,amn,sdy,(ires(k),ss(k),k=0,23)
C	else									 ! Sep 2019
          GOTO 335 ! Sep 2019
	endif                                       ! Sep 2019
C		GOTO 303	  ! next day			  ! Sep 2019
C++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C polyfit foF2 and hmF2
C
	      do k=1,24
      IF (fmh.eq.'f') THEN
	fof2=xdat(k-1)/10.
 	ynm(k)=1.24*fof2**2  ! NmF2
	y(k)=ynm(k)  !First NmF2
	                  else
	y(k)=xdat(k-1) ! hmF2
	endif
	enddo
       call subpoly3c(x,y,p1f,p2f,p3f,rmse)
C Output profile:
	DO jj=0,23
	xx=x(jj+1)  !TEC
	yprox=xx*(p1f*xx+p2f)+p3f  ! Polyval
	if ((fmh.eq.'f').and.(yprox.lt.4.)) yprox=ynm(jj+1)
	if ((fmh.eq.'h').and.((yprox.lt.175.).or.(yprox.gt.500.))) 
     +	yprox=xdat(jj+1)
	CC=ss(jj)
	C=CC(1:1)
	IF ((C.eq.'/').or.(C.eq.'_')) THEN
	yres=yprox
      if (fmh.eq.'f') yres=sqrt(yprox/1.24)*10.
	CC(1:1)='_'
	ss(jj)=CC
	            else
	yres=xdat(jj)
	endif
        ires(jj)=nint(yres)
	ENDDO

	write(*,310) ayr,amn,sdy,(ires(k),ss(k),k=0,23)
	write(22,310) ayr,amn,sdy,(ires(k),ss(k),k=0,23)
	write(23,310) ayr,amn,sdy,(ires(k),ss(k),k=0,23)
C-	endif                                       ! Sep 2019
C Record results for the day:
  335 	do kk=0,23
	ires(kk)=nint(xdat(kk))
	if (fmh.eq.'f') if2(nday,kk)=ires(kk)
	if (fmh.eq.'m') im3(nday,kk)=ires(kk)
	if (fmh.eq.'h') ih2(nday,kk)=ires(kk)
	enddo
	GOTO 303	  ! next day
c
  302	 close(unit=11)
      goto 210
C
 211  write(*,*) 'No input datafile:',infile1
      pause ' '
	goto 210
 212	write(*,*) 'No input datafile:',infile2
      pause ' '
 210	CONTINUE
	close(unit=22)
	close(unit=23)
      if (fmh.ne.'t') close(unit=24)
C	
	write(*,331) stat
  331	format(1X,A13)
crem		pause ' '
      RETURN
       END
c------------------------------------------------------------------------
      subroutine subinter1(fm,syr,smn,sdy,sut,res)
C.........................................................Mar. 2014
C
C.........................................................Oct. 2013
C Use fcF2 & hcF2 results of MAPIRIP
C
C.................................................................Nov. 2012
C JPL maps
C.................................................................Dec. 2011
C++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C Use fc,hc,tc interpolated from directories dfc, dhc, dtc maps
C
	DIMENSION fhdat(0:72,71)	  ! maps fcF2,hcF2,TEC
	CHARACTER*13 STAT,txt
	CHARACTER*1 fm
	CHARACTER*2 syr,smn,sdy,sut,cyr,cmn,cdy
	CHARACTER(10) DDDD
	CHARACTER*4 TIMC 
	CHARACTER(5) ZZZ
      CHARACTER*40 infilef
            COMMON /BL2/ STAT,GLAT,GLON,DDDD,TIMC,ZZZ,R12

C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C Use fc,hc predictions from directories dfc, dhs
cold	  call subsimfh(isp,jdy,nut,fres,hres)	! model simulation
c---	fres=0.
c---	hres=0.
C NEW ========================================================
C++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
		CYR=DDDD(3:4)
	read(cyr,*) ryr
	iyr=int(ryr)
 	 CMN=DDDD(5:6)
	read(cmn,*) rmn
	imn=int(rmn)
	CDY=DDDD(7:8)
	read(cdy,*) rdy2
	 ndy2=int(rdy2)  ! current day
	read(sdy,*) rdy1
	 ndy1=int(rdy1)	  ! input day
	read(smn,*) rmn1
	imn=int(rmn)
	imn1=int(rmn1)
	lda1=ndoy(iyr,imn1,ndy1)
	lda2=ndoy(iyr,imn,ndy2)

C Use fc,hc predictions from directories dfc or dhc
c-	infilef='c:\web\dfs\YR\MN\fcMNDYUT.jYR'
	infilef='c:\web\graf\dfc\YR\MN\ut\fcMNDYUT.jYR'
c//      infilef='c:\web\graf\dfc\YR\MN\ut\fcMNDYUT.jYR' !++
	infilef(17:18)=SYR							 !++
	infilef(20:21)=SMN							 !++
	infilef(36:37)=SYR							 !++
	infilef(28:29)=SMN							 !++
      infilef(14:14)=fm		   ! f or h or t	 !++
	infilef(26:26)=fm	   ! f or h	or t		 !++
	infilef(27:27)='c'							 !++
	infilef(30:31)=sdy							 !++
	infilef(32:33)=sut
C check availability of 'c' file
	OPEN(10,FILE=INFILEF)		! 'c'
	read(10,*,err=301,end=302) txt
	if (txt(1:1).eq.'') then
	goto 302
	else
	close (unit=10)
	goto 310
	endif
  302		infilef(27:27)='a' 						 !++
 	close(unit=10)
	goto 303
  301	write(*,*) 'Error reading file ',infilef
	pause ' '
	goto 302
  303		OPEN(10,FILE=INFILEF)		! 'a'
 	read(10,*,err=304,end=305) txt
	if (txt(1:1).eq.'') then 
	goto 305
	else
	close (unit=10)
	goto 310
	endif
  305		infilef(27:27)='b' 						 !++
 	 close(unit=10)
	goto 310
  304	write(*,*) 'Error reading file ',infilef
 	pause ' '
	goto 305

 204	format(73(1X,F4.0))
 310	OPEN(10,FILE=INFILEF)		!
 	read(10,*,err=307,end=307) txt
	if (txt(1:1).eq.'') then 
	goto 307
	else
	backspace(10)
	goto 311
	endif
  307	write(*,*) 'Error reading file ',infilef
 	pause ' '
	STOP

 311		do ilat=1,71
	read(10,204) (fhdat(k,ilat),k=0,72)	
	enddo
	close(unit=10)
C
C      interpolate for glat,glon
C Linear Interpolation for TEC(amlat,amlon)  
	aglat=-90.
	do 161 ilat=1,71 ! select pair of lats-lines (k) & (k+1)
c	aglat=-87.5,-85,...,87.5
	aglat=-90.+2.5*ilat
	aglat2=aglat+2.5
C
	if ((glat.gt.aglat).and.(glat.le.aglat2)) then  !   <<<<1
C Select pair of longs embracing glong(MLT)
	 clon=-185.
	along=glon
	if (along.ge.180.) along=along-360.
	do j=0,71 ! select pair of longs (j) & (j+1)
	clon=clon+5.
	clon2=clon+5.
	if ((along.ge.clon).and.(along.lt.clon2)) then  ! +++2
	res11=fhdat(j,ilat)
	res12=fhdat(j+1,ilat)
	res21=fhdat(j,ilat+1)
	res22=fhdat(j+1,ilat+1)
	res1=res11+(res12-res11)/5.*(along-clon)
	res2=res21+(res22-res21)/5.*(along-clon)
	res=res1+(res2-res1)/2.5*(glat-aglat)
	endif											 ! +++2
	enddo	! end select pair of longs (j) & (j+1)
	exit
	endif											 !   <<<<1
  161 continue		   ! interpolation end
	RETURN
	END
c---------------------------------------------------------------------------
C ===========================================================
	SUBROUTINE SUBSEC2(out1,out3,out4)
C ...................................................................Jan. 2012
C ............Send results to web/graf/YEAR/styrf.br, styrm.br, styrh.br
C
C Send output results to c:\web\idce\styr.br files..................23.12.2009
C
c   EXTRACT HOURLY "FOF2 & M3000" fromSEC FILES /daily
c  INPUT c:\2006\SEC\STMND2.TXT OUTPUT STMNf2.br ~ m3.br ~ tc.br
      DIMENSION IFF(31,0:23),IMM(31,0:23)
	+,IHH(31,0:23)
	+,xf2(0:23),xm3(0:23),xhm(0:23)
	INTEGER*2 NFULF,NFULM,MAXD2,ID1,ID2
	+,JD
      CHARACTER*128 infile,out1,out3,out4,txt	  ! outfiles graf/YEAR/styrf. m., h.br
 	CHARACTER*10 CCM,title,aa
	CHARACTER*30 STNAME 
	CHARACTER*13 STAT
	CHARACTER(10) TT,DD
	CHARACTER*4 YEAR,TTT
	CHARACTER(5) ZZ
 	CHARACTER*2 ST,AMN,DY1,DY2,DDD
     +,DD1,DD2,YY
	+,FULF(80),FULM(80),DY22,CYR,CMN,CDY 
 	COMMON /BL1/ST,YEAR,YY,AMN,DY1,DY2,DD1,DD2,ID1,ID2,JF,JH,JT,DY22
	+,STNAME
	 COMMON /BL2/ STAT,GLAT,GLON,DD,TTT,ZZ,R12
      COMMON /BL4/ FULF,FULM,NFULF,NFULM,MAXD2

C Start:
C    
	CALL DATE_AND_TIME(DATE=DD,TIME=TT,ZONE=ZZ) 
	TTT(1:4)=TT(1:4)
      CCM=YEAR
	CCM(5:5)=' '
	CCM(6:7)=AMN
	CCM(8:8)=' '
C
   1	continue
C New: 
      infile='c:\web\graf\20YR\MN\YRMNDYDourbes_iono.txt' 
	infile(21:)=STNAME
      infile(13:16)=YEAR
	infile(18:19)=AMN
	

  333	format(A128)
		open(11,file=out1)   ! 
		open(13,file=out3)   ! 
		open(14,file=out4)   ! 
	icn1=0
	icn3=0
	icn4=0
   55 READ(11, 333, END=44) txt 
 	CYR=txt(1:2)
	CMN=txt(3:4)
	CDY=txt(5:6)
	if ((cyr.eq.yy).and.(cmn.eq.amn).and.(cdy.eq.dy1)) then  
	goto 44
	                  else
         icn1=icn1+1
      GOTO 55
      CONTINUE
                  	endif
   44 mf_strings = icn1
      REWIND (11)
      icount=0
      do while (icount.lt.mf_strings)
         READ(11, 333, END=444) txt
         icount=icount+1
      enddo
  444 print*,'YES!'
      ENDFILE 11
      close(UNIT=11)
CCC
  155 READ(13, 333, END=144) txt 
 	CYR=txt(1:2)
	CMN=txt(3:4)
	CDY=txt(5:6)
	if ((cyr.eq.yy).and.(cmn.eq.amn).and.(cdy.eq.dy1)) then  
	goto 144
	                  else
         icn3=icn3+1
      GOTO 155
      CONTINUE
                  	endif
  144 mm_strings = icn3
      REWIND (13)
      icount=0
      do while (icount.lt.mm_strings)
         READ(13, 333, END=1444) txt
         icount=icount+1
      enddo
 1444 print*,'YES!'
      ENDFILE 13
      close(UNIT=13)
CCC
  255 READ(14, 333, END=244) txt 
 	CYR=txt(1:2)
	CMN=txt(3:4)
	CDY=txt(5:6)
	if ((cyr.eq.yy).and.(cmn.eq.amn).and.(cdy.eq.dy1)) then  
	goto 244
	                  else
         icn4=icn4+1
      GOTO 255
      CONTINUE
                  	endif
  244 mh_strings = icn4
      REWIND (14)
      icount=0
      do while (icount.lt.mh_strings)
         READ(14, 333, END=2444) txt
         icount=icount+1
      enddo
 2444 print*,'YES!'
      ENDFILE 14
      close(UNIT=14)
CCC

C ===================================
		open(11,file=out1,access='APPEND')   ! 
		open(13,file=out3,access='APPEND')   ! 
		open(14,file=out4,access='APPEND')   ! 
	kfulf=0
	kfulh=0
	kfulm=0
	DO 77 JD=id1,id2				   ! day-to-day cycle
	do k=0,23
	iff(jd,k)=0
	ihh(jd,k)=0
	imm(jd,k)=0
	enddo

C define day for comparison:
	call blet2(jd,DDD)
c-	CDAY(JD)=DDD 
     	OPEN(12,FILE=INFILE)
C
	CCM(9:10)=DDD
	ist=0
   10	read (12,150,err=3,end=70) title
  150	format(A10)
	if (title(6:6).eq.' ') title(6:6)='0'
	if (title(9:9).eq.' ') title(9:9)='0'
 	if (title.eq.ccm) then
	ist=ist+1
	 backspace(12)
	goto 11
	                      else
	goto 10
	                       endif
   11	continue
c
C -----------------------------------------------
      LT=0   ! Start hour-to-hour cycle
  220    IFL=0
      ICNF2=0 				! count for data within [LT1,LT1+1]
      SHRF2=0.
      ICNH2=0 				! count for data within [LT1,LT1+1]
      SHRH2=0.
	ICNM3=0
	SHRM3=0.

  280	read (12,160,err=3,end=400) AA,IDY,LTH,LTM,F2M,HM2,AM3
  160	FORMAT(A8,I2,2X,2(I2),4X,F4.1,1X,F3.0,1X,F5.2)
c==  290	if (idy.gt.jd) goto 400

  290	if (idy.ne.jd) then
	if (idy.lt.jd) then
      goto 280
	else
	goto 400
	endif
	endif
C
	if (f2m.lt.0.) f2m=0.
	if (hm2.lt.0.) hm2=0.
	if (am3.lt.0.) am3=0.
C
      RLT1=LTH+(LTM+5.)/60.        ! UT of record
	LT1=int(rlt1)
C    		 ! Missed record for LT ---------
  380	if ((LT1.gt.LT).and.(icnf2.eq.0)) then		 ! Missed record for LT ---------
	iff(jd,lt)=0
	ihh(jd,lt)=0
	imm(jd,lt)=0
	xf2(lt)=0.
	xm3(lt)=0.
	xhm(lt)=0.
	lt=lt+1
	if (lt.ge.23) then 
	goto 76
	              else
	backspace(12)
	goto 280
	endif
	              ENDIF ! ------------
C LT1=LT  :
  390 IF (LT1.lt.(LT+1)) THEN 	  !
	if (f2m.gt.0.) then          !!
	      ICNF2=ICNF2+1 
      SHRF2=SHRF2+f2m
	endif      
	if (hm2.gt.0.) then          !!
	      ICNH2=ICNH2+1 
      SHRH2=SHRH2+hm2
	endif      
	if (AM3.gt.0.) then          !!
	      ICNM3=ICNM3+1 
      SHRM3=SHRM3+AM3
	endif
	goto 280
	     		   ENDIF             !
 400	if (icnf2.gt.0) then		! foF2
	cnt1=float(icnf2)
  	shrf2=shrf2*10./cnt1  ! foF2
	backspace(12)
		               else
	shrf2=0.
	endif
	if (icnh2.gt.0) then		! hmF2
	 cnt2=float(icnh2)
  	shrh2=shrh2/cnt1  ! hmF2
		               else
	shrh2=0.
	endif
	if (icnm3.gt.0) then		! M3000F2
 	cnt=float(icnm3)
  	shrm3=shrm3*100./cnt+.5
	                else
	shrm3=0.
	endif
 480	IFF(JD,lt)=nint(SHRF2)
      IHH(JD,lt)=nint(shrh2)
	IMM(JD,lt)=nint(shrm3)
C----------------------------------------
 481	xf2(lt)=shrf2/10.
	xm3(lt)=shrm3/100.
	xhm(lt)=shrh2

	lt=lt+1
  630 IF (lt.lt.24) then 
      GOTO 220
	              else
	goto 76
	              endif

C
C No data for particular day:
   70	if (ist.eq.0) then
	write(*,*) "No data for day:",jd
	endif
		do k=0,23
 	iff(jd,k)=0
	ihh(jd,k)=0
	imm(jd,k)=0
	enddo

   76	close (unit=12)
C check full sets of foF2, M3F2 at DAY2:
Ctemp	if (jd.eq.maxd2) then
	cntf=0
	cnth=0
	cntm=0
	do n=0,23
	if (IFF(JD,n).gt.0) cntf=cntf+1
	if (IHH(JD,n).gt.0) cnth=cnth+1
	if (IMM(JD,n).gt.0) cntm=cntm+1
	enddo
	if (cntf.eq.24) then
	kfulf=kfulf+1
	endif
	if (cnth.eq.24) then
	kfulh=kfulh+1
	endif
	if (cntm.eq.24) then
	kfulm=kfulm+1
	endif
Ctemp	            endif
C Add hmF2 calculation
	if ((cnth.lt.24).and.(cntm.gt.0)) then
	xlat=glat
	xlon=glon
c-	xdip=dip
	call subhmf2(xlat,xlon,jd,xf2,xm3,xhm)
      glat=xlat
      glon=xlon
c-	dip=xdip


	do k=0,23
	ihh(jd,k)=nint(xhm(k))
	enddo
	endif
C	 Write results in web\graf\YEAR\   outfiles:
c-	write(*,177) YY,amn,ddd,(iff(jd,k),k=0,23)
c-	write(*,177) YY,amn,ddd,(imm(jd,k),k=0,23)
c-	write(*,177) YY,amn,ddd,(ihh(jd,k),k=0,23)
C
	write(11,177) YY,amn,ddd,(iff(jd,k),k=0,23)
	write(13,177) YY,amn,ddd,(imm(jd,k),k=0,23)
	write(14,177) YY,amn,ddd,(ihh(jd,k),k=0,23)
   77 continue                 ! go to the next day
C End of daily cycle ===============
  177	format(3A2,2X,24(I3,2X))
C
	goto 2
3	 write(*,*) 'INPUT FILE IS NOT IN YOUR DIRECTORY '
   2      CLOSE (unit=12)
         CLOSE (unit=11)
	   CLOSE (unit=13)
	   CLOSE (unit=14)
      RETURN
	END
c----------------------------------------------------------------------
      subroutine subpoly3c(x,y,p1,p2,p3,rmse)
	dimension x(24),y(24) 
	nut=24  ! diurnal UT set
C  
	s0=24.
	s1=0.
	s2=0.
	s3=0.
	s4=0.
	syx2=0.
	syx=0.
	sy=0.
      DO N=1,nut
	x1=x(N)
	x2=x1*x1
	x3=x1*x2
	x4=x2*x2
	y1=y(N)
	yx=y1*x1
	yx2=y1*x2
	s1=s1+x1
	s2=s2+x2
	s3=s3+x3
	s4=s4+x4
	syx2=syx2+yx2
	syx=syx+yx
	sy=sy+y1
      ENDDO 
C
      d1=(s0*s3-s1*s2)
      c1=(s0*s4-s2*s2)/d1
      c2=(s0*syx2-sy*s2)/d1
	d2=(s0*s2-s1*s1)
      c3=(s0*s3-s1*s2)/d2
      c4=(s0*syx-sy*s1)/d2

C
C p1=a; p2=b; p3=c
C
C polyfit2 coefficients
	p1=(c2-c4)/(c1-c3)
	p2=c2-p1*c1
	p3=(sy-p1*s2-p2*s1)/s0
C RMSE
      sum=0.
	DO N=1,nut
	del=y(N)-(x(N)*(p1*x(N)+p2)+p3)
	sum=sum+del**2
	ENDDO
	rmse=sqrt(sum/nut)
C
	RETURN
	END
C

