      program extdst
C..............................................................Sep. 2018
C Extract Dst index from ISGI format to Kyoto WDC format
C Produce dstYEAR file  
      dimension idst(0:23)
	character*64 inpdst,outfile,rest
	character*4 AYEAR
	character*2	AYR,AMN,ADY
C
	inpdst='d:/apkp/dst/Dst_2018-01-01.dat'  !dst  for 01/2018 - 08/2018
	outfile='d:/apkp/dst/dstYEAR'
CREM	iyear=1962   !START+++++++++++++++++++++
C+	iyear=2009
C+	iyear=1952
		iyear=2017      !  for 01/2018 - 08/2018
   30 FORMAT(A64)
C start cycle on year: =====================================
C	do 400 KK=1,11	! 1953-1963
	iyear=iyear+1
	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=365
	ENDIF
C Reading kpap file for aa-index
   33	format(A31,8(I3))
   37	format(A13,5X,I3)
   34	format(A13,2(1X,I3))
   35	format(A4,3(1X,I2),2(I4))
   36	format(A4,3(1X,A2),2(1X,I3))
C
	inpdst(17:20)=AYEAR
	outfile(16:19)=AYEAR
	OPEN(16,FILE=outfile,access='APPEND')


C Reading AE-ISGI file
C
	OPEN(11,FILE=inpdst,action='READ')
	do i=1,25
	read(11,30) rest ! extra lines
	enddo
   31 FORMAT(I4,3(1X,I2),17X,I4)
   32 FORMAT('DST',2A2,'*',A2,'RRX020',3X,'0',24(I4),I4)
C Monthly cycle
      DO 99 kmn=1,12  ! for 2018 !!!!!!!!!!!!!!!!!
      aver=0.
	do idd=1,ndnyr
	do ii=0,23
	read(11,31,end=139) iyear,imn,idy,iut,iaeindex
	aver=aver+iaeindex
	call blet2(imn,AMN)
	call blet2(idy,ADY)
	idst(iut)=iaeindex
	enddo
	aver=aver/24.
	iaeday=nint(aver)
c	write(*,35)	ayear,imn,idy,iut,iap(idd,ii),iaaindex
c	write(16,35)	ayear,imn,idy,iut,iap(idd,ii),iaaindex
	write(*,32)	ayr,amn,ady,(idst(k),k=0,23),iaeday
	write(16,32)	ayr,amn,ady,(idst(k),k=0,23),iaeday
	enddo
   99	CONTINUE
  139	close(unit=11)
	close(unit=16)
  400	continue
	pause ' '
	stop
	END
C
