      program fcqdays
C.......................................................July 2015
C Calculate fc-map -mean and std for Geom-Qdays and Wp-qdays.  
	dimension fav(12,0:23),std(12,0:23),fc(71,0:72)
c	+,fav2(12,0:23),std2(12,0:23)
	character*64 qdays,infile,outfile
	character*33 txt
	character*4 AYEAR,CYEAR(17,12)
	character*2	AYR,AMN,ADY,AUT,CMN(17,12)
	+,qmag(17,12),qion(17,12),DD1,DD2
C
	qdays='qdays.txt'
	OPEN(10,file=qdays,action='read')

	outfile='faveqdmag.txt'	  ! DD1
C	outfile='fstdqdion.txt'	  ! DD2

	read(10,31) txt ! extra line
	write(*,31) txt

	DO i=1,17
	do k=1,12
	read(10,32,end=111) CYEAR(i,k),CMN(i,k),qmag(i,k),qion(i,k)
	enddo
	enddo
  111	close(unit=10)

   31	format(A33)
   32	format(A4,A2,2(1X,A2))

   33	format(73(1X,F4.1))
c   35	format(A4,3(1X,I2),2(I4))
c   36	format(A4,3(1X,A2),2(1X,I3))
c   37	format(A13,5X,I3)
	infile='d:\web\dfs\YR\MN\fcMNDDUT.jYR'
C
c	iyear=1998
C start cycle on year: =====================================
	DO 400 KK=1,17	! 1999-2015
c	iyear=iyear+1
	DO 300 imn=1,12
	iflag=1
	AYEAR=CYEAR(kk,imn)
	AMN=CMN(kk,imn)
	DD1=qmag(kk,imn)
	DD2=qion(kk,imn)
C
	AYR=AYEAR(3:4)
C	read(ayr,*) ryr
C	iyr=int(ryr)
C 
C
	infile(12:13)=AYR
	infile(15:16)=AMN
	infile(20:21)=AMN
	infile(28:29)=AYR
C
    1	   if (iflag.eq.1) then
      ADY=DD1
	outfile(7:9)='mag'
	                else
	ADY=DD2
	outfile(7:9)='ion'
	   endif
	infile(22:23)=ADY
		OPEN(15,FILE=outfile,access='APPEND')  ! f-ave, f-std
C Cycle on UT
	DO iut=0,23
	call blet2(iut,AUT)
	infile(24:25)=AUT
	OPEN(11,FILE=infile,action='READ')
	fav(imn,iut)=0.
	glat=-90.0
	    do ilat=1,60  !read  for glat ~[-60,+60]
	glat=glat+2.5
	read(11,33,end=133) (fc(ilat,k),k=0,72)
	if ((ilat.ge.12).and.(ilat.le.60)) then
C fof2 mean:
	do j=0,72
      fav(imn,iut)=fav(imn,iut)+fc(ilat,j)
	enddo
	endif
		enddo
	fav(imn,iut)=fav(imn,iut)/3577.
C  std
	dev=0.
	icnt=0
      do ilat=12,60
	do j=0,72
	dev=dev+(fav(imn,iut)-fc(ilat,j))**2.
	icnt=icnt+1
	enddo
	enddo
	std(imn,iut)=sqrt(dev/3577.)
  133	close(unit=11)  !
C Output results:
      write(*,34) AYEAR,AMN,ADY,AUT,fav(imn,iut),std(imn,iut)
	write(15,34) AYEAR,AMN,ADY,AUT,fav(imn,iut),std(imn,iut)
	ENDDO ! end cycle on UT
	close(unit=15)
C
	iflag=iflag+1
	if (iflag.lt.3) goto 1  ! ion q-day
	 
  300	CONTINUE ! end cycle on MN
   34	format(1X,A4,3(1X,A2),1X,F6.2,1X,F7.4)

  400	continue ! end cycle on year
	pause ' '
	stop
	END
C
