C*************************************************************   
CC 
      SUBROUTINE SUBTAU27MG(IYEAR,IMN,IDY,Rtau)
CC-------------------------------------------------------------------
CC......................................................Aug 2023
CC MgII-d.txt
CC......................................................Oct. 2017
CC Ytau27=(1-tau)/(1-tau**n)*(F0+tau*F_1+tau**2*F_2...+tau**29*F_26) =smoothed SSN1(tau) and F10.7(tau) index
CC
CC T.L. Gulyaeva........................................May. 2017 
C
CC Source: former IRI-Plas subroutine: 
CC        SUBROUTINE APF(IYYYY,IMN,ID,HOUR,IAP,F81,R81)
CC Modified to produce sunspot number R81, and solar radio flux F81 
CC from two successive kpYEAR annual files
CC Input: 
CC       kpYEAR  (preceding year: IYYYY-1)                              
CC       kpYEAR  (given year: IYYYY)                              
c--------------------------------------------------------------------
c 
c T.L. Gulyaeva ......................................  Sep. 2001. 
c Modified for producing solar radio flux F10.7, F81, averaged for
c 81 days preceeding given day
C 
C------------------------------------------------------------------------
      DIMENSION lm(12),rz(28)
C-     ,CC(81)
      CHARACTER*4 YEAR,YEAR_pre
      CHARACTER*80 infilekp
      COMMON /FIKP1/iyear_cur,imn_cur,idy_cur
C-	COMMON /BL1/CC,SMED,SMIN,SMAX
      DATA LM/31,28,31,30,31,30,31,31,30,31,30,31/
C
C START: 
      ipcnt=0 ! keep cnt -28,...,-1
C	tau=0.9623
      tau=exp(-1./26.)
         do k=1,28
       rz(k)=0.
         enddo

C   Input file kpYEAR starts from 1948:
C      if((iyyyy.lt.1948).or.(iyyyy.gt.iyear_cur)) goto 21 !*
C      IF ((iyyyy.eq.iyear_cur).and.(imn.gt.imn_cur)) goto 21 !*
C      IF ((iyyyy.eq.iyear_cur).and.(imn.eq.imn_cur).and.    !*
C     *   (idy.gt.idy_cur)) goto 21 !*TEST
C
        iy=iyyyy-1900

 770       z1=iy/4.0
        lm(2)=28
           if(iyyyy/4*4.eq.iyyyy) lm(2)=29
      if (iy.ge.100) then
      iyr=iy-100
      else
      iyr=iy
      endif
      call blet4(iyyyy,YEAR)
      iYYYY_pre=iYYYY-1
      call blet4(iYYYY_pre,YEAR_pre)
C
      idend=idy
C
C Day-of-year:
      ldaend=ndoy(iyyyy,imn,idend)       ! current day-of-year
C
C
C      infilekp='kpYEAR' 
      infilekp='MgII-d.txt'  
C
C==10    FORMAT(3I2,6X,8(I2),3X,8(I3),7X,I3,I3,1X,I1)
10    FORMAT(2X,I4,7X,I2,1X,I2,1X,F7.5)
C
C-------------------------------------------------------------------------------
C
C
      OPEN(13,FILE=infilekp,ERR=21)
C
C Read file 
C
  204 continue  
C
      READ(13,10,err=29,END=30)JYEAR,JMN,JDY,alym
      rz(28)=alym
C Move data 1 line up:
  140 do  k=1,27
      rz(k)=rz(k+1)
      enddo
C
      ipcnt=ipcnt+1

      if (ipcnt.lt.27) goto 204
	JYR=JYEAR-JYEAR/100*100
      IF ((JYEAR.eq.IYEAR).and.(JMN.eq.IMN).and.(JDY.eq.idend)) then 
      goto 207
      ELSE
      ipcnt=ipcnt-1
      goto 204
      ENDIF

C data for 27 preceding days are collected                       
  207       Rtau=0.0
C-	fsum=icv(1)/10.
C-	rsum=irz(1)/1.
      	fsum=0.
		rsum=0.
	STAU=1.
      icnv=0
C-      do k=1,26
      do k=27,1,-1
	rsum=rsum+stau*rz(k)
	stau=stau*0.9623
	enddo
	coef=(1.-tau)/(1.-stau)
	Rtau=coef*rsum
C
C
      goto 30
21      write(*,100)
100     format(1X,'Date is outside range of Kp-Ap indices file.')
      goto 30
  29  write(*,*) 'Error in input k-index file',infilekp 
C      pause ' '
      stop
  30  CLOSE(unit=13)
      RETURN
      END

C __________________________________________________________________________
C
