      program polyfht8c
C.......................................................Aug 2019
C Apply Polyfit2 least=square coefficients for 0,1,...,23 UT of two parameters for 8 GMSS stations
C
      dimension IM(12)
	+,pf1(8),pf2(8),pf3(8),ph1(8),ph2(8),ph3(8)
	 INTEGER idatf(0:23),idath(0:23),idatt(0:23)
	character*128 infilet,infpolyf,infpolyh
	+,outfilef,outfileh,txt       ! July 2019
      CHARACTER*2 AYR,AMN,ADY
	+,CC,CF(0:23),CH(0:23),CYR,CMN,CDY			   ! July 2019
      CHARACTER*4 STAT,STS(8),STCOEF(8)			! Aug 2019
      DATA IM/31,28,31,30,31,30,31,31,30,31,30,31/
      DATA STS/
     + 'adis','cztg','kiru','mobn','nico','sutm','syog','zamb'/     ! Aug 2019
C
C
      WRITE(*,*) ' ENTER JYR and JMN FOR INPUT FILE='  ! July 2019
      READ(*,*) JYR,JMN                                ! July 2019
C    
	call blet2(jyr,AYR)
	call blet2(jmn,AMN)
	ryr=float(jyr)
	if(int(ryr/4.)*4.eq.jyr) THEN 
		im(2)=29
	                          ELSE
		IM(2)=28
	ENDIF
	ndmn=im(jmn)
C
C Input Poly coefficients
C
      infpolyf='polYRMNfm.txt'
	infpolyh='polYRMNhm.txt'
	infpolyf(4:5)=AYR
	infpolyh(4:5)=AYR
	infpolyf(6:7)=AMN
	infpolyh(6:7)=AMN
	OPEN(10,file=infpolyf,action='READ')
	OPEN(11,file=infpolyh,action='READ')
	DO k=1,8
	read(10,*,end=41) STCOEF(k),pf1(k),pf2(k),pf3(k) 
	read(11,*,end=42) STCOEF(k),ph1(k),ph2(k),ph3(k) 
	enddo
   41	close(unit=10)
   42	close(unit=11)
C
C Output data
C
      outfilef='d:\web\dat1\YR\statYRMN.txt'                        ! Aug 2019
	outfileh='d:\web\dat6\YR\statYRMNh.txt'                       ! Aug 2019
	outfilef(13:14)=AYR
	outfilef(20:21)=AYR
	outfilef(22:23)=AMN
	outfileh(13:14)=AYR
	outfileh(20:21)=AYR
	outfileh(22:23)=AMN
C Input data
	infilet='d:\web\graf\dat0\YR\statYRMNt.txt'                     ! Aug 2019
	infilet(18:19)=AYR
	infilet(25:26)=AYR
	infilet(27:28)=AMN

   1  DO 1234 ist=1,8
C
       stat=sts(ist)                                  ! Aug 2019
	if (stat.ne.STCOEF(ist)) THEN
	write (*,*) ' Check stations ST names:',stat,stcoef(ist)
	pause ' '
	goto 1234
	endif
	outfilef(16:19)=stat							! Aug 2019
	outfileh(16:19)=stat							! Aug 2019
	infilet(21:24)=stat								! Aug 2019
C
	OPEN(12,file=infilet,action='READ')
	OPEN(13,file=outfilef)
	OPEN(14,file=outfileh)
C
       DO k=1,4
	read(12,29,end=45) txt ! title line TEC
C foF2 => TECU
	if (k.eq.1) then
	n=index(txt,'TECU')
	txt(n:n+3)='fpF2'
	endif
	write(13,29) txt  ! write foF2
C hmF2 => TECU
	if (k.eq.1) then
	txt(n:n+6)='hpF2   '
	endif
	write(14,29) txt  ! write hmF2
	 ENDDO
C
   29 FORMAT(A128)
   30 FORMAT(3A2,2X,24(I3,A2))
   31 FORMAT(3A2,1X,24(I4,1X))
C
C Day-to-day cycle
C
	DO 10 jdy=1,ndmn
	call blet2(jdy,ADY)
	read(12,31,end=45) CYR,CMN,CDY,(idatt(k),k=0,23)
C
      DO iut=0,23
	tec=idatt(iut)/10.0
C check foF2
	CC='_ '
	 rnm=tec*(pf1(ist)*tec+pf2(ist))+pf3(ist)
	if (rnm.lt.4.0) rnm=4.0
	fof2=sqrt(rnm/1.24)
	CF(iut)=CC
	idatf(iut)=nint(fof2*10.)
C check hmF2
	 hmF2=tec*(ph1(ist)*tec+ph2(ist))+ph3(ist)
	if (hmF2.gt.999.) hmF2=999.0
	CH(iut)=CC
	idath(iut)=nint(hmf2)
	ENDDO
	write(*,30) CYR,CMN,CDY,(idatf(k),CF(k),k=0,23)
	write(13,30) CYR,CMN,CDY,(idatf(k),CF(k),k=0,23)
	write(*,30) CYR,CMN,CDY,(idath(k),CH(k),k=0,23)
	write(14,30) CYR,CMN,CDY,(idath(k),CH(k),k=0,23)
   10	CONTINUE			 ! next day
   45 close(unit=12)
   	close(unit=13)
 	close(unit=14)
C
 1234 CONTINUE     ! next st
C
C
	pause ' '
	END
C=========================================
