C........................................................Mar. 2023
C 
C t0 at AE>=1000nT storm peak
C
C Extract Dst  for -24h<t0< +48h for AE events 
C 
C Select period of 72hrs starting from t0-24h, ending t0+48h; t0 @ AEmax_hour
C 
      PROGRAM decoddstAEst
C Input HSSUT.txt 1996-2019...........................Jan 2020
C add: select from -24h < to <+48h for HSS-events
C
C----------------------------------------------------------------
C
	DIMENSION inpn(0:71) ! 1h indices for 24h (or 72h file for 1h indices)
	+,ind24(0:23),inpn_pre(0:71)
C
	CHARACTER*4 AYEAR
      CHARACTER*128 INFILE,INFHSS,txt
	+,outfile  !
	 CHARACTER*20 start
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
      CHARACTER*2 DMN,DDY,DYR
C
      ICALLS=0
	CALL DATE_AND_TIME(DATE=DD,TIME=TT,ZONE=ZZ)
      TT(5:)='      '
      WRITE(*,*) 'PC Date: Year,Month,Day = ',DD,'Time = ',TT
C
C-      infile='d:/Apkp/AE/WWWaeYEAR.dat'
C_      infile='d:/Apkp/AE/aeYEAR'
C_      infile='d:/Apkp/Hpo/HpoYEAR.txt'
C_      infile='d:/web/dat0/YR/ghYRdgec.txt'
      infile='d:/Apkp/dst/dstYEAR'
C
	outfile='dstae3d.txt' !GEC total file for 1995-2017 AE-storms =nev lines x 72 cols superposed epoch	! 3
C//  	infhss='kpstormx.txt'  ! Kp storms 1 or 2
  	infhss='AEstorm1000.txt'  ! AE storms AE>=1000 nT
C
C Start 
C
    1	CONTINUE
C
Cnorm    7	OPEN(11,FILE=OUTFILE)
    7	OPEN(11,FILE=OUTFILE,access='APPEND') !Temp
C
C Kp_1_2 nev storms from 1994 to 2021
C
   28 format(1X,F8.3,3(1X,I2),3(1X,F6.0))
   29 format(A20,1X,I4,3(1X,I2),1X,F5.0)
  197 format(3X,2A2,1X,A2,10X,24(1X,I4))
C
	open(10,file=infhss,action='READ')
	DO i=1,33
C_	DO i=1,425  !temp
		read(10,*) txt  ! data for 1994
	ENDDO
C
C Cycle on events ====================================================
C
Cnorm	nev=434
       	nev=434	 !TEmp
C
	DO 177 iev=1,nev
      read(10,29,end=27) start,iyear,imn1,idy1,iut1,aep
C
	iutcnt=0
	iyr=iyear-iyear/100*100
C
	lda1=ndoy(iyr,imn1,idy1) ! 1d
      lda0=lda1-1 ! -1d
	lda2=lda1+2
	ryear=float(iyear)
		if(int(ryear/4.)*4.eq.iyear) THEN 
	ndnyr=366
	                          ELSE
	ndnyr=365
	ENDIF
	if (lda2.gt.ndnyr) lda2=lda2-ndnyr
C
C   37 format(A20,24(I4))	 ! AE
C   37	format(3A2,24(1X,I4)) !GEC
   37 format(3X,2A2,1X,A2,10X,24(I4))	 ! Dst
C
	if (lda0.eq.0) then  !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
C
C read last day of prec. year:
C
	iyear0=iyear-1
       call blet4(iyear0,AYEAR)
		infile(16:19)=AYEAR  ! 

C
	open(15,file=infile,action='READ')
	ryear0=float(iyear0)
		if(int(ryear0/4.)*4.eq.iyear0) THEN 
	ndnyr=366
	                          ELSE
	ndnyr=365
	ENDIF

	DO 101 k=1,ndnyr
	read(15,37,end=102) DYR,DMN,DDY,(ind24(ii),ii=0,23)
  101	CONTINUE
  102	close(unit=15)
C last day of prec year from iut1 to 23 h:   
       DO ii=iut1,23
       inpn(iutcnt)=ind24(ii)
      iutcnt=iutcnt+1
	ENDDO
	endif				 !>>>>>>>>>>>>>>>>>>>>>>>>
C
C Prec. day from current year:
       call blet4(iyear,AYEAR)
		infile(16:19)=AYEAR  ! 
	open(15,file=infile,action='READ')
C
	if (lda0.gt.0) then !>>>>>>>>>>>>>>>>>>>>>>>>>>>
	DO 109 k=1,lda0
	read(15,37,end=102) DYR,DMN,DDY,(ind24(ii),ii=0,23)
  109	CONTINUE
       DO ii=iut1,23
       inpn(iutcnt)=ind24(ii)
      iutcnt=iutcnt+1
	ENDDO
	endif				 !>>>>>>>>>>>>>>>>>>>>>>>>
C 1st day from current year:
	read(15,37,end=102) DYR,DMN,DDY,(ind24(ii),ii=0,23)
C	  1st day :
       DO ii=0,23
       inpn(iutcnt)=ind24(ii)
      iutcnt=iutcnt+1
	ENDDO
C
	IF (lda2.gt.lda1) THEN              !-------------
C 2nd day from current year:
	read(15,37,end=102) DYR,DMN,DDY,(ind24(ii),ii=0,23)
C	  2nd day :
       DO ii=0,23
       inpn(iutcnt)=ind24(ii)
      iutcnt=iutcnt+1
	ENDDO
	                   ELSE
	GOTO 777
	                   ENDIF           !-------------------
	IF (lda2.gt.lda1) THEN              !-------------
C 3rd day from current year:
	read(15,37,end=102) DYR,DMN,DDY,(ind24(ii),ii=0,23)
C	  3rd day :
       DO ii=0,23
	if (iutcnt.lt.72) then
       inpn(iutcnt)=ind24(ii)
      iutcnt=iutcnt+1
              	else
	exit
              	endif
	ENDDO
  103	close(unit=15)
	                   ELSE
	GOTO 777
	                   ENDIF           !-------------------
C
C Next year if necessary:
 777       if (iutcnt.lt.72) then   !>>>>>>>>>>>>>>>>>>>>>>>>>>>
	iyear1=iyear+1
	call blet4(iyear1,AYEAR)
		infile(16:19)=AYEAR  ! 
	open(15,file=infile,action='READ')
C
	read(15,37,end=102) DYR,DMN,DDY,(ind24(ii),ii=0,23)
C	  next day :
       DO ii=0,23
	if (iutcnt.lt.72) then
       inpn(iutcnt)=ind24(ii)
      iutcnt=iutcnt+1
              	else
	exit
              	endif
	ENDDO
      endif	                !>>>>>>>>>>>>>>>>>>>>>
	     if (iutcnt.lt.72) then   !>>>>>>>>>>>>>>>>>>>>>>>>>>>
	read(15,37,end=102) DYR,DMN,DDY,(ind24(ii),ii=0,23)
C	  next day :
       DO ii=0,23
	if (iutcnt.lt.72) then
       inpn(iutcnt)=ind24(ii)
      iutcnt=iutcnt+1
              	else
	exit
              	endif
	ENDDO
      endif	                !>>>>>>>>>>>>>>>>>>>>>
  104	close(unit=15)
C
   38 format(72(1X,I4))
C Output matrices:
 	write(*,38) (inpn(k),k=0,71)
	write(11,38) (inpn(k),k=0,71)
	do jk=0,71
	inpn_pre(jk)=inpn(jk)
	enddo
  177	CONTINUE
   27 close(unit=10)
      close(unit=11)
C
C
   35      write(*,*) infile
      pause ' ' 
      STOP
       END

C***********************************************************
