C........................................................Oct 2023
C Coronal Hole Pch index at AE>1000 nT
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 depeakPchAE
C Input HSSUT.txt 1996-2019...........................Jan 2020
C add: select from -24h < to <+48h for HSS-events
C
C----------------------------------------------------------------
C
C	DIMENSION inpn(0:71) ! 1h indices for 24h (or 72h file for 1h indices)
C
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='cataePch3d.txt'
C	 infile='vswapoae3d.txt' !Vsw total file for 1995-2017 =nev lines x 72 cols superposed epoch	! 3
	infile='d:\Data\CH\pchdata.dat' !Pch 2011.03-2020.10
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') ! Pch daily index
C Cycle on events ====================================================
C
C
	nev=276
	nd=0
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	DO 177 iev=1,nev
      read(10,139,end=27) xyear,lda1,imn1,idy1,iut1,aep,lda2,iut2
	+,iapmax,nd
	if (nd.lt.193) goto 177
C	                   
       iyear=int(xyear)
	iyr=iyear-iyear/100*100
C
   37 format(A20,24(I4))	 ! AE
C
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
   38 FORMAT(1X,3I2,1X,F7.2)
C	READ(15, 38, END=104) (inpn(k),k=0,71)
  123		READ(15, 38, END=104) kyr,kmn,kdy,pch
C
        IF ((kyr.eq.iyr).and.(kmn.eq.imn1).and.(kdy.eq.idy1)) THEN
	write(*,138)  xyear,lda1,imn1,idy1,iut1,aep,lda2,iut2,iapmax,pch
	+,nd
	write(11,138)  xyear,lda1,imn1,idy1,iut1,aep,lda2,iut2,iapmax,pch
	+,nd
	                ELSE
      goto 123
	              ENDIF					
  138 format(1X,F8.3,1X,I3,3(1X,I2),1X,F5.0,2(1X,I3),1X,I4,1X,F7.2
     +,1X,I4)
  139 format(1X,F8.3,1X,I3,3(1X,I2),1X,F5.0,2(1X,I3),1X,I4,1X,I4)
  177	CONTINUE
  104	close(unit=15)
   27 close(unit=10)
      close(unit=11)
C
C
   35      write(*,*) infile
      pause ' ' 
      STOP
       END

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