      program swadd
C
C Add line Vsw for given YEAR, mn, dy1:dy2 and dayly mean
	dimension dat(0:23),ires(0:23),im(12)
      character*50 infile,outfile,txt
	character*4 AYEAR,BYEAR
	character*2 AYR,AMN,ADY
	DATA  IM/31,28,31,30,31,30,31,31,30,31,30,31/
	infile='d:\Apkp\OMNI\swYEARMN.txt'
	outfile= 'd:\Apkp\OMNI\swYEAR'
C
	write(*,*) ' Enter AYEAR:'
	read(*,*) AYEAR
	AYR=AYEAR(3:4)
	read(ayr,*) ryr
	iyr=int(ryr)
		ryr=float(iyr)
 	if(int(ryr/4.)*4.eq.iyr) THEN 
  	im(2)=29
	                          ELSE
	IM(2)=28
	ENDIF

		write(*,*) ' Enter AMN:'
	read(*,*) AMN
	read(amn,*) rmn
	imn=int(rmn)
105	write(*,*) 'AMN=',amn
C		write(*,*) ' Enter IDY1,idy2:'
C	read(*,*) IDY1,idy2
	idy1=1
	idy2=im(imn)
	write(*,*) 'dy1=',idy1,' dy2=',idy2
	lda1=ndoy(iyr,imn,idy1)
	lda2=ndoy(iyr,imn,idy2)
C
	infile(16:19)=AYEAR
	infile(20:21)=AMN
	outfile(16:19)=AYEAR
	OPEN(11,file=outfile,access='APPEND')
      OPEN(10,file=infile,action='READ')
  101	format(A50)
  102	format(A4,1X,I3,1X,I2,18X,F6.0)
  103	format(1X,3A2,24(1X,I4),2X,I4)

C
	DO i=1,12
	read(10,101) txt ! extra lines
	enddo
C Cycle day-to-day
	DO idy=idy1,idy2
	call blet2(idy,ADY)
	lda=ndoy(iyr,imn,idy)
C Cycle on UT:
c  	 icnt=0
	DO jut=0,23
    1	read(10,102,end=11) BYEAR,ldainp,iut,vsw
	if ((byear.eq.AYEAR).and.(ldainp.eq.lda)) then
		dat(iut)=vsw
C	icnt=icnt+1
C	if (icnt.eq.24) then
C	goto 11
C	else
C	goto 1
C	endif
C	                  else
C	goto 1
	endif
	 ENDDO
c   11	close(unit=10)
C-	if (icnt.eq.24) then
	sum=0.
	do iut=0,23
	sum=sum+dat(iut)
	ires(iut)=int(dat(iut))
	enddo
	aver=sum/24.
	iaver=nint(aver)
	write(*,103) AYR,AMN,ADY,(ires(k),k=0,23),iaver
	write(11,103) AYR,AMN,ADY,(ires(k),k=0,23),iaver
	ENDDO        ! next day
   11	close(unit=10)
      close(unit=11)
	imn=imn+1
	call blet2(imn,AMN)
	if (imn.lt.13) goto 105
C
	pause ' '
	stop
	end




