C........................................................Mar. 2022
C 481 Kpstorm1 & 86 KPstorm2 1994-2021 exclude Kp peak at t0-24 : t0 
C at least 2 days between the preceeding storm onset
C........................................................Apr.2021
C Extract Dst-index for -24h<t0< +48h 
C 
C Select period of 72hrs starting from t0-24h, ending t0+48h; t0 at Kppeak
C 
      PROGRAM decodkp5dstd1
C Input HSSUT.txt 1996-2010...........................Jan 2020
C add: select from -24h < to <+48h for HSS-events
C
C----------------------------------------------------------------
C
	DIMENSION inds(0:71) ! 3h indices for 24h (0r 72h file for 1h indices)
	+,ind24(0:23)
C
	CHARACTER*4 AYEAR
      CHARACTER*128 INFILE,INFHSS
	+,outfilae
      CHARACTER*1 kpx
	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/dst/dstYEAR' !Dst
C
	outfilae='kpdst3dx.txt' !total file for 1997-2019 =8 lines x 24 cols superposed epoch
  	infhss='kpstormx.txt'  ! Kp storms 1 or 2
C
C Start kpstorm1 or kpstorm2
       kpx='1'
C
   1	outfilae(8:8)=kpx
	infhss(8:8)=kpx 
      nout=0
	OPEN(11,FILE=OUTFILAE)
C
C DST 490 storms from 1996 to 2021
C
   28 format(1X,F8.3,3(1X,I2),3(1X,F6.0))
C   29 format(9X,2I2,2(1X,I2),5X,2I2,2(1X,I2),1X,I4,6X,2I2,2(1X,I2))
   29 format(1X,F8.3,1X,I3,3(1X,I2),1X,F3.1,1X,I3,1X,I3)
	open(10,file=infhss,action='READ')
C
C Cycle on events ====================================================
C
      lda_pre=16
	iyear_pre=1994
	nev=481
	if (kpx.eq.'2') nev=86
C
	DO 177 iev=1,nev   !
C
      read(10,29,end=27) ryear1,lda1,imn1,idy1,iut1,xkp,ikpsum,ninp
     	iutcnt=0
	do k=0,71
	inds(k)=0
	enddo
       ryear=ryear1
	iyear=int(ryear1)
	iyear1=iyear
	iyr1=iyear-iyear/100*100
C
      lda0=lda1-1 ! -1d
	lda2=lda1+2
C
C?      if ((iyear_pre.eq.iyear).and.(lda1-lda_pre.lt.2)) then
C?	iyear_pre=iyear
C?	lda_pre=lda1
C????	GOTO 177  !??????////// read data of current year  +++++++++++
C?	endif
	iyear_pre=iyear
	lda_pre=lda1
	ryear1=float(iyear1)
		if(int(ryear1/4.)*4.eq.iyear1) THEN 
	ndnyr=366
	                          ELSE
	ndnyr=365
	ENDIF
C	del=1./ndnyr
C	ryear=ryear1+(lda1-1)*del
C
   37 format(20X,24(I4))	 ! Dst
C
	if (lda0.eq.0) then  !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
C
C read last day of prec. year:
C
	iyear0=iyear-1
       call blet4(iyear0,AYEAR)
	infile(16:19)=AYEAR
	open(15,file=infile,action='READ')
	ryear0=float(iyear0)
		if(int(ryear0/4.)*4.eq.iyear0) THEN 
	ndnyr=366
	                          ELSE
	ndnyr=365
	ENDIF
C
	DO 101 k=1,ndnyr
	read(15,37,end=102) (ind24(ii),ii=0,23)
  101	CONTINUE
  102	close(unit=15)
C last day of prec year from iut1 to 23 h:   
       DO ii=iut1,23
       inds(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')
C
	if (lda0.gt.0) then !>>>>>>>>>>>>>>>>>>>>>>>>>>>
	DO 109 k=1,lda0
	read(15,37,end=103) (ind24(ii),ii=0,23)
  109	CONTINUE
       DO ii=iut1,23
       inds(iutcnt)=ind24(ii)
      iutcnt=iutcnt+1
	ENDDO
	endif				 !>>>>>>>>>>>>>>>>>>>>>>>>
C 1st day from current year:
	read(15,37,end=103) (ind24(ii),ii=0,23)
C	  1st day :
       DO ii=0,23
       inds(iutcnt)=ind24(ii)
      iutcnt=iutcnt+1
	ENDDO
C 2nd day from current year:
	read(15,37,end=103) (ind24(ii),ii=0,23)
C	  2nd day :
       DO ii=0,23
       inds(iutcnt)=ind24(ii)
      iutcnt=iutcnt+1
	ENDDO
C 3rd day from current year:
	read(15,37,end=103) (ind24(ii),ii=0,23)
C	  3rd day :
       DO ii=0,23
	if (iutcnt.lt.72) then
       inds(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')
C
	read(15,37,end=104) (ind24(ii),ii=0,23)
C	  next day :
       DO ii=0,23
	if (iutcnt.lt.72) then
       inds(iutcnt)=ind24(ii)
      iutcnt=iutcnt+1
              	else
	exit
              	endif
	ENDDO
  104	close(unit=15)
      endif	                !>>>>>>>>>>>>>>>>>>>>>
C
  138 format(72(1X,I4))
  139	FORMAT(1X,F8.3,1X,I3,3(1X,I2),2(1X,I4))
C Check Dst min at t0-24 : t0 Exclude these results
      mindst=0.
	do k=0,71
	if (inds(k).lt.mindst) then
	k1=k
	mindst=inds(k)
	endif
	enddo
	
CNO!~!!	if (k1.lt.23) goto 177  ! avoid output for Dst min at t0-24h : t0
C Output matrices:
      nout=nout+1
	write(*,39) nout
C--      write(*,139) ryear,lda1,imn1,idy1,iut1,idst,nout
C	write(12,139) ryear,lda1,imn1,idy1,iut1,idst,nout
C
 	write(*,138) (inds(k),k=0,71)
	write(11,138) (inds(k),k=0,71)
  177	CONTINUE
   27 close(unit=10)
      close(unit=11)
   39 format(1X,I4)
C
        if (kpx.eq.'1') then
	kpx='2'
	goto 1
	  endif
C
   35      write(*,*) infile
      pause ' ' 
      STOP
       END

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