	program extgeoind
C....................................................July 2015
C Extract daily mean and daily peak for: AE, aa, ap, ap(tau),Dst indices
C
C Outfile: /APKP/giYEAR:
C AEav,AEmax,aaav,aamax,apav,apmax,aptav,aptmax,dstav,dstmin
c
	dimension idst(0:23),iaa(0:7),iap(0:7)
	+,apt(0:7),iae(0:23),res(10)
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
	CHARACTER*65 infaa,infdst,infapt,infae
	+,outfile
	CHARACTER*2 AYR
	CHARACTER*4 AYEAR
	CHARACTER*6 YYMMDD
C
	CALL DATE_AND_TIME(DATE=DD,TIME=TT,ZONE=ZZ)
      TT(5:)='      '
      WRITE(*,*) 'PC Date: Year,Month,Day = ',DD,'Time = ',TT
C

	infaa='d:/apkp/AA/aaYEAR' !aa, ap
	infdst='d:/apkp/dst/dstYEAR'	 ! Dst
	infapt='d:/apkp/APT/aptYEAR' ! ap(tau)
	infae='d:/apkp/AE/aeYEAR'	 ! AE
	outfile='giYEAR'  !

   36 format(1X,I4,A1)
   37 format(20X,24(I4))	 ! Dst
  137 format(13X,2(1X,I3)) ! ap,aa
  237 format(A6,8(1X,F5.1))  ! ap(tau)
C	iyear=1975   !TEMP

	iyear=1997   !TEMP
	ndyr=365	   ! for 1963; 1975; 1998 !!!!!!!!!!!!!!!
C start cycle on year: =====================================
CTEMP	do 400 KK=1,18	! 1998-2015
	iyear=2014		  ! TEMP for 2015
	iyear=iyear+1
C	iyear=2015 !TEMP
	call blet4(iyear,AYEAR)
	ryear=float(iyear)
	AYR=AYEAR(3:4)
	read(ayr,*) ryr
	iyr=int(ryr)
		if(int(ryr/4.)*4.eq.iyr) THEN 
	ndnyr=366
	                          ELSE
	ndnyr=366
	ENDIF
      outfile(3:6)=AYEAR
	OPEN(16,FILE=OUTFILE)

C Reading Dst file
C
	infdst(16:19)=AYEAR
	OPEN(10,FILE=infdst)

	infaa(14:17)=AYEAR
	OPEN(11,FILE=infaa)

	infapt(16:19)=AYEAR
	OPEN(12,FILE=infapt)

	infae(14:17)=AYEAR
	OPEN(13,FILE=infae)

	DO idd=1,ndnyr
	read(10,37,end=38) (idst(k),k=0,23)

C Reading aa & ap file
C
	do ii=0,7
	read(11,137,end=139) iap(ii),iaa(ii)
	enddo

C Reading apt file
	read(12,237,end=238) YYMMDD,(apt(k),k=0,7)
C Reading AE file
C
	read(13,37,end=339) (iae(ii),ii=0,23)
	avaa=0.
	avap=0.
	avapt=0.
	maxaa=0
	maxap=0
	pmaxapt=0.
	do k=0,7
	avaa=avaa+float(iaa(k))
	if (maxaa.lt.iaa(k)) maxaa=iaa(k)
	avap=avap+float(iap(k))
	if (maxap.lt.iap(k)) maxap=iap(k)
	avapt=avapt+apt(k)
	if (pmaxapt.lt.apt(k)) pmaxapt=apt(k)
	enddo

	avae=0.
	avdst=0.
	maxae=0
	mindst=500
	do k=0,23
	if (iae(k).lt.0) then
	aezz=132.8011-5.1295*idst(k)
	iae(k)=nint(aezz)
	if (iae(k).lt.0) iae(k)=0
	endif
	avae=avae+float(iae(k))
	if (maxae.lt.iae(k)) maxae=iae(k)
	avdst=avdst+float(idst(k))
	if (mindst.gt.idst(k)) mindst=idst(k)
	enddo
C Output results
	res(1)=avae/24.
	res(2)=float(maxae)
	res(3)=avaa/8.
	res(4)=float(maxaa)
	res(5)=avap/8.
	res(6)=float(maxap)
 	res(7)=avapt/8.
 	res(8)=pmaxapt
	res(9)=avdst/24.
	res(10)=float(mindst)
	write(*,33) YYMMDD,(res(k),k=1,10)
	write(16,33) YYMMDD,(res(k),k=1,10)

	ENDDO

   38	close(unit=10)
C
  139	close(unit=11)
C
  238	close(unit=12)
C
  339	close(unit=13)

	close(unit=16)

  400	CONTINUE ! next year
C
   33 format(1X,A6,10(1X,F7.1))
C
C
C	
	goto 900
    2 CONTINUE
           write(*,*) 'INPUT FILE IS NOT IN YOUR DIRECTORY '
  900 pause ' '
      STOP
      END

