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 decodkp5swd
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)
	+,ind24(0:23)
C
	CHARACTER*4 AYEAR
      CHARACTER*128 INFILE,INFHSS
	+,outfilsw,outcat,txt !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='d:/apkp/OMNI/swYEAR.txt'	 ! Vsw
C
	outfilsw='hsssw3d.txt' !total file for 1996-2021 =436 lines x 72 cols superposed epoch
	OPEN(11,FILE=OUTFILSW)
	OUTCAT='catdsthss3.txt'  ! Catalog HSS at DSTpeak
	OPEN(12,FILE=OUTCAT)
C
C Dst 380 storms from 1996 to 2021
C
C   28 format(24X,2I2,2(1X,I2),1X,I4)		   ! Dst peak
C   28 format(9X,2I2,2(1X,I2),16X,I4)		   ! t0 @ Dst start
   28 format(1X,F8.3,1X,I3,3(1X,I2),1X,I4)
C	infhss='dststorms3.txt' ! 436 Dst events
  	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
C	read(10,28,end=27) iyr,imn1,idy1,iut1,idstpeak
      read(10,28,end=27) ryear,lda1,imn1,idy1,iut1,idstpeak
	iyear=int(ryear)
	iyr=iyear-iyear/100*100
	iutcnt=0
	do k=0,71
	insw(k)=0
	enddo
C	if (iyr.gt.90) then
C	iyear=1900+iyr
C	else
C	iyear=2000+iyr
C	endif
      xyear=float(iyear)
C
	if(int(xyear/4.)*4.eq.iyear) THEN 
 	dnr=366.
	                          ELSE
	dnr=365.
	ENDIF
	delyr=1./dnr
C
	lda1=ndoy(iyr,imn1,idy1) ! 1d
      lda0=lda1-1 ! -1d
	lda2=lda1+2
C
C
	if (lda0.eq.0) then  !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
C
C read last day of prec. year:
C
  137 format(I4,1X,I3,1X,I2,28X,I4) ! Vsw
	iyear0=iyear-1
       call blet4(iyear0,AYEAR)
	infile(16:19)=AYEAR
	open(15,file=infile,action='READ')
	do i=1,9
	read(15,*) txt  ! extra line
	enddo
C
	ryear0=float(iyear0)
		if(int(ryear0/4.)*4.eq.iyear0) THEN 
	ndnyr=366
	                          ELSE
	ndnyr=365
	ENDIF

	DO 101 k=1,ndnyr
	  do i=0,23
	read(15,137,end=102) jyear,jlda,jut,jvsw
	if (jvsw.lt.9990) then
	ind24(i)=jvsw
	else
	ind24(i)=0
	endif
	  enddo
  101	CONTINUE
  102	close(unit=15)
C last day of prec year from iut1 to 23 h:   
       DO ii=iut1,23
       insw(iutcnt)=ind24(ii)
      iutcnt=iutcnt+1
	ENDDO
	endif				 !>>>>>>>>>>>>>>>>>>>>>>>>
C
C Prec. day from current year:
       call blet4(iyear,AYEAR)
	infile(16:19)=AYEAR
	open(15,file=infile,action='READ')
	do i=1,9
	read(15,*) txt  ! extra line
	enddo
C
	if (lda0.gt.0) then !>>>>>>>>>>>>>>>>>>>>>>>>>>>
	DO 109 k=1,lda0
	  do i=0,23
	read(15,137,end=103) jyear,jlda,jut,jvsw
	if (jvsw.lt.9990) then
	ind24(i)=jvsw
	else
	ind24(i)=0
	endif
	  enddo
  109	CONTINUE
       DO ii=iut1,23
       insw(iutcnt)=ind24(ii)
      iutcnt=iutcnt+1
	ENDDO
	endif				 !>>>>>>>>>>>>>>>>>>>>>>>>
C 1st day from current year:
	  do i=0,23
	read(15,137,end=103) jyear,jlda,jut,jvsw
	if (jvsw.lt.9990) then
	ind24(i)=jvsw
	else
	ind24(i)=0
	endif
	  enddo
C	  1st day :
       DO ii=0,23
       insw(iutcnt)=ind24(ii)
      iutcnt=iutcnt+1
	ENDDO
C 2nd day from current year:
	  do i=0,23
	read(15,137,end=103) jyear,jlda,jut,jvsw
	if (jvsw.lt.9990) then
	ind24(i)=jvsw
	else
	ind24(i)=0
	endif
	  enddo
C	  2nd day :
       DO ii=0,23
       insw(iutcnt)=ind24(ii)
      iutcnt=iutcnt+1
	ENDDO
C 3rd day from current year:
	  do i=0,23
	read(15,137,end=103) jyear,jlda,jut,jvsw
	if (jvsw.lt.9990) then
	ind24(i)=jvsw
	else
	ind24(i)=0
	endif
	  enddo
C	  3rd day :
       DO ii=0,23
	if (iutcnt.lt.72) then
       insw(iutcnt)=ind24(ii)
      iutcnt=iutcnt+1
              	else
	exit
              	endif
	ENDDO
  103	close(unit=15)
C
C Next year if necessary:
       if (iutcnt.lt.72) then   !>>>>>>>>>>>>>>>>>>>>>>>>>>>
	iyear1=iyear+1
	call blet4(iyear1,AYEAR)
	infile(16:19)=AYEAR
	open(15,file=infile,action='READ')
	do i=1,9
	read(15,*) txt  ! extra line
	enddo
C
	  do i=0,23
	read(15,137,end=103) jyear,jlda,jut,jvsw
	if (jvsw.lt.9990) then
	ind24(i)=jvsw
	else
	ind24(i)=0
	endif
	  enddo
C	  next day :
       DO ii=0,23
	if (iutcnt.lt.72) then
       insw(iutcnt)=ind24(ii)
      iutcnt=iutcnt+1
              	else
	exit
              	endif
	ENDDO
  104	close(unit=15)
      endif	                !>>>>>>>>>>>>>>>>>>>>>
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
	ryear=xyear+delyr*(lda1-1)
C
	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
C Output matrices:
 	write(*,138) (insw(k),k=0,71)
	write(11,138) (insw(k),k=0,71)
	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)
      close(unit=11)
	close(unit=12)
C
C
   35      write(*,*) infile
      pause ' ' 
      STOP
       END

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