C........................................................Dec.2021
C Extract Vsw-peak, delVsw for -24h<t0< +48h for 380-start events 
C 
C Select period of 72hrs starting from t0-24h, ending t0+48h; t0 @ Dstmin_hour
C 
      PROGRAM decodkp5swf
C Input HSSUT.txt 1996-2021...........................Jan 2020
C add: select from -24h < to <+48h for HSS-events
C
C----------------------------------------------------------------
C
	DIMENSION insw(0:71) ! 3h indices for 24h (0r 72h file for 1h indices)
C
      CHARACTER*128 INFILE,INFHSS
	+,outcat !Vsw
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
C++
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
      infile='hsssw3d.txt' !total file for 1996-2021 =380 storms
       OPEN(11,file=infile,action='READ')
	OUTCAT='catdsthss3f.txt'  ! Catalog HSS at DSTpeak
	OPEN(12,FILE=OUTCAT)
C
C Dst 380 storms from 1996 to 2021
C
   28 format(1X,F8.3,1X,I3,3(1X,I2),1X,I4,2(1X,I4),1X,I3,2(1X,I4))
  	infhss='catdsthss3.txt'  ! 380 HSS/Dst storms
C
	open(10,file=infhss,action='READ')
C
C Cycle on events ====================================================
C
      nout=0
	DO 177 iev=1,380
      read(10,28,end=27) ryear,lda1,imn1,idy1,iut1,idstpeak,ivmin
	+,idelsw,idt,ivmax,ninp

C
       read(11,138,end=25) (insw(k),k=0,71)
C
        if (ivmin.gt.0) GOTO 123  ! ================OUTPUT Catalogue 380
C
  137 format(I4,1X,I3,1X,I2,28X,I4) ! Vsw
C
  138 format(72(1X,I4))
C
C Extract Vswmin,Vswmax,delsw,dt(Vswmax)
C
	ivmax=0
C++	do ii=0,23
      do ii=0,47             ! Check ivmax -24UT : +24 UT
	if (insw(ii).gt.ivmax) then
	ivmax=insw(ii)
	iutmax=ii
	endif
	enddo
C
	ivmin=2000
	do ii=0,iutmax
	if (insw(ii).lt.ivmin) then
	ivmin=insw(ii)
	iutmin=ii
	endif
	enddo
C
       idelsw=ivmax-ivmin
	idt=iutmax-24
C
123	IF (idelsw.gt.50) THEN
	nout=nout+1
	write(*,29) ryear,lda1,imn1,idy1,iut1,idstpeak,ivmin,idelsw
	+,idt,ivmax,nout
	write(12,29) ryear,lda1,imn1,idy1,iut1,idstpeak,ivmin,idelsw
	+,idt,ivmax,nout
	ENDIF
  177	CONTINUE
   29 format(1X,F8.3,1X,I3,3(1X,I2),1X,I4,2(1X,I4),1X,I3,2(1X,I4))
   27 close(unit=10)
   25   close(unit=11)
	close(unit=12)
C
C
   35      write(*,*) infile
      pause ' ' 
      STOP
       END

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