C........................................................Sep. 2023
C 
C t0 at AE>=1000nT storm peak
C
C Extract Ae & Vsw-peak 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 depeakVswAE
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)
C
	CHARACTER*4 AYEAR
      CHARACTER*128 INFILE,INFHSS
	+,outfile  !
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
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/AE/aeYEAR.txt'
C
	outfile='cataevsw3d.txt'
	 infile='vswapoae3d.txt' !Vsw total file for 1995-2017 =nev lines x 72 cols superposed epoch	! 3
C  	infhss='AEstorm1000.txt'  ! AE storms AE>=1000 nT
       infhss='cataeapo3d.txt'
C
C Start 
C
    1	CONTINUE
C
    7	OPEN(11,FILE=OUTFILE)
C
C Kp_1_2 nev storms from 1994 to 2021
C
   28 format(1X,F8.3,1X,I3,3(1X,I2),1X,F5.0)
C   29 format(1X,I4,3(1X,I2),1X,F5.0)
C-  712  FORMAT (A10,22X,F8.2)
C_  197 format(3X,2A2,1X,A2,10X,24(I4))
  197 format(3X,2A2,1X,A2,10X,24(1X,I4))
C
	open(10,file=infhss,action='READ')
C
	open(15,file=infile,action='READ') ! Apo storm profiles
C Cycle on events ====================================================
C
	nev=276
	nd=0
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	DO 177 iev=1,nev
      read(10,28,end=27) xyear,lda1,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
C
   37 format(A20,24(I4))	 ! AE
C
       call blet4(iyear,AYEAR)
C_		infile(14:17)=AYEAR  ! 
C
	ryear=float(iyear)
		IF(int(ryear/4.)*4.eq.iyear) THEN 
	ndnyr=366
	                          ELSE
	ndnyr=365
	ENDIF
c        del=1./ndnyr
c	xyear=ryear+(lda1-1)*del
C
	READ(15, 38, END=104) (inpn(k),k=0,71)
C
C Select Apomax from iut1-6h to iut1+6h
	iapmax=0
	iuts1=18
	iuts2=36
	 DO kk=iuts1,iuts2
	if (iapmax.lt.inpn(kk)) THEN
 	iapmax=inpn(kk)
	iutmax=kk
	ENDIF
	 ENDDO
C
   38 format(72(1X,I4))
  138 format(1X,F8.3,1X,I3,3(1X,I2),1X,F5.0,2(1X,I3,1X,I4),1X,I4)
  139 format(1X,F8.3,1X,I3,3(1X,I2),1X,F5.0,2(1X,I3),1X,F7.0,1X,I4)
C Output matrices:
	nd=nd+1
	 lda2=lda1
	iut2=iutmax
	if (iutmax.lt.24) then
	lda2=lda1-1
	iut2=iut2-24
	      else
	iut2=iut2-24
	endif
	apmax=iapmax*1.
	write(*,139) xyear,lda1,imn1,idy1,iut1,aep,lda2,iut2,apmax
 	+,nd
 	write(11,139) xyear,lda1,imn1,idy1,iut1,aep,lda2,iut2,apmax
	+,nd
  177	CONTINUE
  104	close(unit=15)
   27 close(unit=10)
      close(unit=11)
C
C
   35      write(*,*) infile
      pause ' ' 
      STOP
       END

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