      program eqsgindex
C....................................................May 2016
C geomagn. indices for storm EQs M6.0+ 1999-2015
C
	dimension iap(0:12),iaa(0:12),iae(0:12),idst(0:12),apt(0:12)
	+,xhieq(0:12),aver(0:12),cnt(0:12)
      CHARACTER*65 infilae,infildst,infilaa,infilap,infilapt,infilxhi
	+,outaver
	 CHARACTER*4 AYEAR
	CHARACTER*2 BMN,BDY

c+      infilae='eqaei12s.txt'  ! storm AE for EQ M6.0+ 1999- during 12 hrs 
c+	infildst='eqdst12s.txt'  ! storm Dst for EQ M6.0+ 1999- during 12 hrs 
c+	infilaa='eqaai12s.txt'  ! storm aa for EQ M6.0+ 1999- during 12 hrs 
c+	infilap='eqapi12s.txt'  ! storm ap for EQ M6.0+ 1999- during 12 hrs 
c+	infilapt='eqapt12s.txt'  ! storm apt for EQ M6.0+ 1999- during 12 hrs 
c+	infilxhi='eqxhi12s.txt'
      infilae='eqaei12q.txt'  ! quiet AE for EQ M6.0+ 1999- during 12 hrs 
	infildst='eqdst12q.txt'  ! quiet Dst for EQ M6.0+ 1999- during 12 hrs 
	infilaa='eqaai12q.txt'  ! quiet aa for EQ M6.0+ 1999- during 12 hrs 
	infilap='eqapi12q.txt'  ! quiet ap for EQ M6.0+ 1999- during 12 hrs 
	infilapt='eqapt12q.txt'  ! quiet apt for EQ M6.0+ 1999- during 12 hrs 
	infilxhi='eqxhi12q.txt'
	outaver='eqsgindex.res'
		OPEN(11,FILE=INFILAE,action='READ')
	OPEN(12,FILE=INFILDST,action='READ')
	OPEN(13,FILE=INFILAA,action='READ')
	OPEN(14,FILE=INFILAP,action='READ')
	OPEN(15,FILE=INFILAPT,action='READ')
	OPEN(16,FILE=INFILXHI,action='READ')
	OPEN(10,FILE=OUTaver,access='APPEND')
C
   33 format(1X,A4,2A2,1X,F5.2,13(1X,I5))
   34 format(1X,A4,2A2,1X,F5.2,13(1X,F5.1))
   35 format(13(1X,F7.2))
C
      do k=0,12
      aver(k)=0.0
	cnt(k)=0.0
	enddo
	cnttot=0.
C
C+	DO 100 ii=1,302
	DO 100 ii=1,1331
	read(11,33,end=27,err=2) AYEAR,BMN,BDY,UT1,(iae(k),k=0,12)
	read(12,33,end=27,err=2) AYEAR,BMN,BDY,UT1,(idst(k),k=0,12)
	read(13,33,end=27,err=2) AYEAR,BMN,BDY,UT1,(iaa(k),k=0,12)
	read(14,33,end=27,err=2) AYEAR,BMN,BDY,UT1,(iap(k),k=0,12)
	read(15,34,end=27,err=2) AYEAR,BMN,BDY,UT1,(apt(k),k=0,12)
	read(16,34,end=27,err=2) AYEAR,BMN,BDY,UT1,(xhieq(k),k=0,12)
	 cnttot=cnttot+1.
	do k=0,12
c+	if (xhieq(k).lt.90.0) then      ! only daytime:
	if (xhieq(k).gt.90.0) then      ! only nighttime:
	aver(k)=aver(k)+iae(k)-idst(k)+iaa(k)+iap(k)+apt(k)
	cnt(k)=cnt(k)+1. ! depending on XHI for night or day
	endif
	enddo
  100	CONTINUE
  27  close(unit=11)
      close(unit=12)
	close(unit=13)
	close(unit=14)
	close(unit=15)
	close(unit=16)
	do k=0,12
	aver(k)=aver(k)/cnt(k)
      enddo
		write(*,35) (aver(k),k=0,12)
	write(10,35) (aver(k),k=0,12)
		write(*,35) (cnt(k),k=0,12)
	write(10,35) (cnt(k),k=0,12)
		goto 900
    2 CONTINUE
           write(*,*) 'INPUT FILE IS NOT IN YOUR DIRECTORY '
  900 pause ' '
      STOP
      END
