      program extreuv3n
C........................................................Sep. 2016
C Extract EUV (1) 26 - 34 nm flux; (2) 0.1 - 50 nm flux 
C Add SSN1 and SSN2 excluding missed EUV (=0.0) days
C Add MgII excluding missed EUV (=0.0) days
C Filling gaps in EUV data by regressions with MgII index
C   euv1=109.9796*mg2 -15.6095
C   euv2=229.4961*mg2 -32.7736
C
	CHARACTER*90 OUTFILE,infile,infilemg
	CHARACTER*4 AYEAR,CYEAR
	CHARACTER*2 AYR,AMN,ADY,cmn,cdy
	CHARACTER*1 OPT
	 dimension im(12)
	DATA  IM/31,28,31,30,31,30,31,31,30,31,30,31/
C
  15	FORMAT(A4)
  16	FORMAT(A1)
C-  201	      WRITE(*,*)' ENTER year: '
C-		READ(*,15) AYEAR
C
C-	opt='1'			!TEMP
	opt='2'			!TEMP
C
	outfile='euvtot1mgn.txt'
	outfile(7:7)=opt
CREM	outfile='euvtot2mgn.txt'

	OPEN(10,file=outfile,access='APPEND')
	infilemg='MgII.txt'
	OPEN(11,file=infilemg,action='READ')
C  Cycle on years			   +++++++++++++++++++++++++++++++++
	DO 201 iyear=1996,2016
	call blet4(iyear,AYEAR)
      write(*,*) ayear
C
	AYR=AYEAR(3:4)
	read(ayr,*) ryr
	iyr=int(ryr)
	if(int(ryr/4.)*4.eq.iyr) THEN 
	im(2)=29
	ndnr=366
	                          ELSE
	IM(2)=28
	ndnr=365
	ENDIF
C
	infile='d:\apkp\euv\euvYEAR-1.txt'	! or -2.txt
C	infile='d:\apkp\euv\euvYEAR-2.txt'	! or -2.txt
      infile(16:19)=AYEAR
	      infile(21:21)=opt           ! Option SSN1 or SSN2

	OPEN(13,file=infile,action='READ')
C
C day-to-day cycle
      DO 222 lda=1,ndnr  !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	call submmdd(lda,iyr,imn,idy)
	call blet2(imn,AMN)
	call blet2(idy,ADY)

	read(13,17,end=223) CYEAR,CMN,CDY,lda1,res
	read(11,18,end=224) CYEAR,JMN,JDY,xmg2
	goto 227
  224	xmg2=-1.0
C-  225	read(12,19,end=226) CYEAR,KMN,KDY,issn2
C-      goto 227
C-  226	issn2=-1
  227 continue
	if (res.eq.0.0) then
	if (opt.eq.'1') then
	resi=109.9796*xmg2-15.6095
	                else
      resi=229.4961*xmg2-32.7736
	endif
C?	GOTO 222
	                else
      resi=res*1.0E-10
	endif
	write(*,20) CYEAR,CMN,CDY,resi,xmg2
	write(10,20) CYEAR,CMN,CDY,resi,xmg2
  222	continue	!<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  223 close(unit=13)
201	continue           ! next year
   17	format(1X,A4,2A2,1X,I4,1X,1PE10.4)
   18	format(2X,A4,6X,2(1X,I2),F8.5)
   19	format(A4,2(1X,I2),10X,I4)
C  20	format(1X,A4,2A2,1X,1PE10.4,1X,F9.5)
  20	format(1X,A4,2A2,2(1X,F9.5))
      pause ' '
	stop
	END
