C........................................................Dec. 2021
C 380 HSS/Dst storms
C........................................................Jan.2020
C Extract PCN and PCS -index for -24h<t0< +48h for 941 HSS events 
C 
C Select period of 72hrs starting from t0-24h, ending t0+48h; t0 @ Vswmax_hour
C 
      PROGRAM decodkp5PCS
C Input HSSUT.txt 1996-2010...........................Jan 2020
C add: select from -24h < to <+48h for HSS-events
C
C----------------------------------------------------------------
C
	DIMENSION inpn(0:71) ! 3h indices for 24h (0r 72h file for 1h indices)
	+,ind24(0:23),inpn_pre(0:71)
C	+,kae(0:23)
C     +,kap(0:23),kaa(0:23)
C
C	CHARACTER*2 AYR,AMN,ADY,AUT,AMM
	CHARACTER*4 AYEAR
C	+,DYR(941),DMN(941),DDY(941),DUT(941)
      CHARACTER*128 INFILE,INFHSS
C	+,outfilpn  !PCN
	+,outfilps  !PCS
	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
CREM	infile='d:/apkp/pc/PCNYEAR' !PCN AARI
	infile='d:/apkp/pc/PCSYEAR' !PCS
C
CREM	outfilpn='hsspcn3d.txt' !PCN total file for 1996-2021 =380 lines x 24 cols superposed epoch
	outfilps='hsspcs3d.txt' !PCS total file for 1996-2021 =380 lines x 24 cols superposed epoch
C	OPEN(11,FILE=OUTFILPN)  !PCN
	OPEN(11,FILE=OUTFILPS)  !PCS
C
C HSS 941 storms from 1994 to 2019
C
   28 format(1X,F8.3,3(1X,I2),3(1X,F6.0))
   29 format(1X,F8.3,1X,I3,3(1X,I2),1X,I4,2(1X,I4),1X,I3,2(1X,I4))

C	infhss='outhss100.txt' ! 941 HSS events
	infhss='catdsthss3f.txt' ! 941 HSS events
	open(10,file=infhss,action='READ')
C
C Cycle on events ====================================================
C
	DO 177 iev=1,941
      read(10,29,end=27) ryear1,lda1,imn1,idy1,iut1,idstpeak
	+,ivmin,idelsw,idt,ivmax,nout
C	read(10,28,end=27) ryear1,imn1,idy1,iut1,v1,v2,delv
	iutcnt=0
c-	do k=0,71
c-	inpn(k)=0
c-	enddo
	iyear=int(ryear1)
	iyr=iyear-iyear/100*100
C
C++	lda1=ndoy(iyr,imn1,idy1) ! 1d
      lda0=lda1-1 ! -1d
	lda2=lda1+2
C
C   37 format(20X,24(I4))	 ! AE
   37 format(7X,24(1X,I4))	 !PCN, PCS
C
	if (lda0.eq.0) then  !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
C
C read last day of prec. year:
C
	iyear0=iyear-1
       call blet4(iyear0,AYEAR)
	infile(15:18)=AYEAR
	open(15,file=infile,action='READ')
	ryear0=float(iyear0)
		if(int(ryear0/4.)*4.eq.iyear0) THEN 
	ndnyr=366
	                          ELSE
	ndnyr=365
	ENDIF

	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
	if (ind24(ii).lt.9990) then
       inpn(iutcnt)=ind24(ii)
	else
	inpn(iutcnt)=inpn_pre(iutcnt)
	endif
      iutcnt=iutcnt+1
	ENDDO
	endif				 !>>>>>>>>>>>>>>>>>>>>>>>>
C
C Prec. day from current year:
       call blet4(iyear,AYEAR)
	infile(15:18)=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
	if (ind24(ii).lt.9990) then
       inpn(iutcnt)=ind24(ii)
		else
 	inpn(iutcnt)=inpn_pre(iutcnt)
       endif
      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
		if (ind24(ii).lt.9990) then
       inpn(iutcnt)=ind24(ii)
		else
 	inpn(iutcnt)=inpn_pre(iutcnt)
 	endif
      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
		if (ind24(ii).lt.9990) then
       inpn(iutcnt)=ind24(ii)
		else
 	inpn(iutcnt)=inpn_pre(iutcnt)
       endif
      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
		if (ind24(ii).lt.9990) then
       inpn(iutcnt)=ind24(ii)
		else
 	inpn(iutcnt)=inpn_pre(iutcnt)
           endif
      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(15:18)=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
			if (ind24(ii).lt.9990) then
       inpn(iutcnt)=ind24(ii)
		else
 	inpn(iutcnt)=inpn_pre(iutcnt)
 	endif
      iutcnt=iutcnt+1
              	else
	exit
              	endif
	ENDDO
  104	close(unit=15)
      endif	                !>>>>>>>>>>>>>>>>>>>>>
C
  138 format(72(1X,I4))
C Output matrices:
 	write(*,138) (inpn(k),k=0,71)
	write(11,138) (inpn(k),k=0,71)
	do jk=0,71
	inpn_pre(jk)=inpn(jk)
	enddo
  177	CONTINUE
   27 close(unit=10)
      close(unit=11)
C
C
   35      write(*,*) infile
      pause ' ' 
      STOP
       END

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