C........................................................Apr 2023
C Produce catalog Apo-peak, dt(Dstmin-Apomin),Dstmin + YEAR lda MN DD UT(Dstmin)
C
C Extract Dst  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 Extract dGEC  for -12h<t0< +36h for Apo events (t0 at Apo > 100 nT)
C 
C Select period of 48hrs starting from t0-12h, ending t0+35h; 
C 
      PROGRAM extpeakdst48
C Input HSSUT.txt 1995-2023...........................Jan 2020
C
C----------------------------------------------------------------
C
	DIMENSION inpn(0:71) ! 1h indices for 24h (or 72h file for 1h indices)
C	+,ind24(0:23)
C
      CHARACTER*128 INFILE,INFHSS
	+,outfile  !
	 CHARACTER*20 start
		 CHARACTER*2 AMN,ADY,AUT,DMN,DDY,DUT
	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='dst2d12.txt' !Dst total file for 1995-2023 Apo-storms =nev lines x 48 cols superposed epoch	! 3
  	infhss='Apostorm12.txt'  ! Apo storms Apo>=100 nT
	outfile='catapodst.txt'
C
C Start 
C
    1	CONTINUE
C
    7	OPEN(11,FILE=OUTFILE)
C
   28 format(1X,F8.3,3(1X,I2),3(1X,F6.0))
   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=infile,action='READ') ! Dst(t) -12h...t0...t0+35h
C
C
C Cycle on events ====================================================
C
       	nev=236 ! intense Apo storms	
C
        nn=0
	DO 177 iev=1,nev
      read(10,29,end=27) start,iyear,imn1,idy1,iut1,aep
	read(15,38,end=103) (inpn(k),k=0,47)
C
       call blet2(imn1,AMN)
	call blet2(idy1,ADY)
	call blet2(iut1,AUT)
	iyr=iyear-iyear/100*100
C
	lda1=ndoy(iyr,imn1,idy1) ! 1d
	ryear=float(iyear)
		if(int(ryear/4.)*4.eq.iyear) THEN 
	ndnyr=366
	                          ELSE
	ndnyr=365
	ENDIF
	del=1./ndnyr
	xyear=ryear+(lda1-1)*del
	kapot0=12         ! peak Apo
C
C peak Dstmin
       kdmin=600
	ktmin=0
       do k=0,35
	if (inpn(k).lt.kdmin) then
	ktmin=k
	kdmin=inpn(k)
	endif
	enddo
C
  123	continue
	kdtmin=ktmin-kapot0 ! dt= t(dstmin)-t(Apomax)
C
C lda0,imn0,idy0,iut0 for Dst min
	 lda0=lda1
	imn0=imn1
	idy0=idy1
	iut0=iut1
       IF (kdtmin.eq.0) goto 111
	     IF (kdtmin.lt.0) THEN
	iut0=iut1+kdtmin
	  if (iut0.ge.0) then
	  goto 111
	                 else
        iut0=iut0+24
	  lda0=lda1-1
	call submmdd(lda0,iyr,imn0,idy0)
              goto 111
	endif
	                      ELSE
	iut0=iut1+kdtmin
	  if (iut0.le.23) then
	  goto 111
	                 else
        iut0=iut0-24
	  lda0=lda1+1
	call submmdd(lda0,iyr,imn0,idy0)
              goto 111
	endif
	                      ENDIF
C      
C Output catalog
  111	call blet2(imn0,DMN)
	call blet2(idy0,DDY)
	call blet2(iut0,DUT)
	dmin=float(kdmin)
	nn=nn+1
      write(*,37) xyear,lda1,AMN,ADY,AUT,aep,lda0,DMN,DDY,DUT,dmin
	+,kdtmin,nn
      write(11,37) xyear,lda1,AMN,ADY,AUT,aep,lda0,DMN,DDY,DUT,dmin
	+,kdtmin,nn
  177	CONTINUE
   27 close(unit=10)
  103	close(unit=15)
      close(unit=11)
C
   37	format(1X,F8.3,2(1X,I3,3(1X,A2),1X,F5.0),2(1X,I4))
C
   35      write(*,*) infile
      pause ' ' 
      STOP
       END

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