C........................................................Nov 2023
C E(Bz) Em(By) peak
C........................................................Apr 2023
C Produce catalog VBz-peak, t(Vbzmax), Vbzmax
C Add for t0(dgec=0): lda0,mn0,dd0,ut0
C
C Extract VBz  for -12h<t0< +36h for 236 Apo > 100 nT events 1995-2023
C
C........................................................Mar. 2023
C 
C t0 at Apo >=100nT storm peak
C
C 
C Select period of 48hrs starting from t0-12h, ending t0+35h; 
C 
      PROGRAM extpeakezey90
C Input HSSUT.txt 1995-2023...........................Jan 2020
C
C----------------------------------------------------------------
C
	DIMENSION iney(0:47),inez(0:47) ! 1h indices for 48h file 
C
      CHARACTER*128 INFILE1,INFILE2,INFHSS
	+,outfile  !
		 CHARACTER*2 AMN,ADY,AUT
	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
	infile1='ebyapomn90.txt' !Em(By) total file for 1995-2023: 90 Apo-storms =nev lines x 48 cols superposed epoch	! 3
	infile2='ebzapomn90.txt' !E(Bz) total file for 1995-2023: 90 Apo-storms =nev lines x 48 cols superposed epoch	! 3
  	infhss='catapo6mn90.txt'  ! Apo 90 storms Apo>=90 nT + GECav-pre-storm 24hrs
	outfile='catapoebzby90.txt'
C
C Start 
C
    1	CONTINUE
C
    7	OPEN(11,FILE=OUTFILE)
C
   28 format(1X,F8.3,1X,I3,3(1X,I2),1X,F5.0,1X,I3)
   29 format(A20,1X,I4,3(1X,I2),1X,F5.0)
   38 format(48(1X,I4))
C
	open(10,file=infhss,action='READ')
	open(15,file=infile1,action='READ') ! EBz(t) -12h...t0...t0+35h
	open(16,file=infile2,action='READ') ! EBy(t) -12h...t0...t0+35h
C
C
C Cycle on events ====================================================
C
       	nev=90 ! intense Apo(tau6)>=90 nT monthly peak storms	
C
	DO 177 iev=1,nev
      read(10,28,end=27) xyear,lda1,imn1,idy1,iut1,aep,ninp
C	read(15,38,end=103) (inpn(k),k=0,47)
	read(15,*,end=103) iney				   ! Eby to-12...t0...to+35 hrs
	read(16,*,end=104) inez				   ! Eby to-12...t0...to+35 hrs
C
       call blet2(imn1,AMN)
	call blet2(idy1,ADY)
	call blet2(iut1,AUT)
	iyear=int(xyear)
	iyr=iyear-iyear/100*100
C
	kapot0=12         ! peak Apo
C
C peak VBz
       kdmax=0
	ktmax=0
       do k=0,24
	if (iney(k).gt.kdmax) then
	ktmax=k
	kdmax=iney(k)
	endif
	enddo
C
       ndmax=0
	ntmax=0
       do k=0,24
	if (inez(k).gt.ndmax) then
	ntmax=k
	ndmax=inez(k)
	endif
	enddo
C
	kdtmax=ktmax-kapot0
	 ldaby=lda1
	 	iutby=iut1+kdtmax
	if (iutby.lt.0) then 
	ldaby=lda1-1
	iutby=iutby+24
	endif
C
	ndtmax=ntmax-kapot0
	 ldabz=lda1
	 	iutbz=iut1+kdtmax
	if (iutbz.lt.0) then 
	ldabz=lda1-1
	iutbz=iutbz+24
	endif
C Output catalog
C   
 333  dmaxby=kdmax/10.
      dmaxbz=ndmax/10.
      write(*,37) xyear,lda1,AMN,ADY,AUT,aep,ldaby,iutby,kdtmax
	+,dmaxby,ldabz,iutbz,ndtmax,dmaxbz,iev
      write(11,37) xyear,lda1,AMN,ADY,AUT,aep,ldaby,iutby,kdtmax
	+,dmaxby,ldabz,iutbz,ndtmax,dmaxbz,iev
  177	CONTINUE
   27 close(unit=10)
  103	close(unit=15)
  104	close(unit=16)
      close(unit=11)
C
   37	format(1X,F8.3,1X,I3,3(1X,A2),1X,F5.0,2(3(1X,I3)
     +,1X,F8.2),1X,I3)
C
   35      write(*,*) infile1,infile2
      pause ' ' 
      STOP
       END

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