C........................................................Dec. 2021
C WU, WL, WE global, north, south from magn. coord.data
C t0 at Dst storm onset
C
C........................................................Dec.2020
C Extract Wp, WU, WL and WE -index for -24h<t0< +48h for 477 HSS/Dst events 
C 
C Select period of 72hrs starting from t0-24h, ending t0+48h; t0 @ Vswmax_hour
C 
      PROGRAM decodkp5WUmf
C Input HSSUT.txt 1996-2019...........................Jan 2020
C add: select from -24h < to <+48h for HSS-events
C
C----------------------------------------------------------------
C
	DIMENSION inpn(0:71) ! 1h indices for 24h (or 72h file for 1h indices)
	+,ind24(0:23),inpn_pre(0:71),wpind(0:23),wnpn(0:71)
C
	CHARACTER*4 AYEAR
      CHARACTER*128 INFILE,INFHSS,txt
	+,outfilpn  !PCS
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
	CHARACTER*1 gns  ! global, north, south
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

	WRITE(*,*) ' Enter m, n, s or x for Wp : '
	read(*,*) gns 
C
C-	infile='d:/apkp/pc/PCNYEAR' !PCN AARI
CREM	infile='d:/web/graf/datw/wupYR.txt'
CREM	infile='d:/web/graf/datw/wlnYR.txt'
CPRE	infile='d:/web/graf/datw/werYR.txt'
C+	infile='d:/web/grif/datw/wptemYR.txt' ! 0 Wp from -15d mag coord
C+	infile='d:/web/grif/datw/wuiYRx.txt' ! 1
C+	infile='d:/web/grif/datw/wliYRx.txt' ! 2
	infile='d:/web/grif/datw/weiYRx.txt' ! 3
C
C	outfilpn='hsswu1d.txt' !total file for 1996-2019 =455 lines x 72 cols superposed epoch
C	outfilpn='hsswl1d.txt' !total file for 1996-2019 =455 lines x 72 cols superposed epoch
CPRE	outfilpn='hsswe1d.txt' !total file for 1996-2019 =455 lines x 72 cols superposed epoch
Ctot	outfilpn='hsswp3df.txt' !total file for 1996-2021 =380 lines x 72 cols superposed epoch ! 0
Cjan	outfilpn='hsswp3djan.txt' ! Jan subset
Ceqn	outfilpn='hsswp3deqn.txt' ! eqn subset
Cjul	outfilpn='hsswp3djul.txt' ! Jul subset
Ctot	outfilpn='hsswu3dx.txt' !total file for 1996-2019 =455 lines x 72 cols superposed epoch ! 1
Cjan	outfilpn='hsswu3dxjan.txt' !Jan
Ceqn	outfilpn='hsswu3dxeqn.txt' !eqn
Cjul	outfilpn='hsswu3dxjul.txt' !Jul
Ctot	outfilpn='hsswl3dx.txt' !total file for 1996-2019 =455 lines x 72 cols superposed epoch	! 2
Cjan	outfilpn='hsswl3dxjan.txt' !Jan
Ceqn	outfilpn='hsswl3dxeqn.txt' !eqn
Cjul	outfilpn='hsswl3dxjul.txt' !Jul
Ctot	outfilpn='hsswe3dx.txt' !total file for 1996-2019 =455 lines x 72 cols superposed epoch	! 3
Cjan	outfilpn='hsswe3dxjan.txt' !Jan
Ceqn	outfilpn='hsswe3dxeqn.txt' !eqn
	outfilpn='hsswe3dxjul.txt' !Jul
    1	CONTINUE
      	if (gns.eq.'x') goto 7
         infile(23:23)=gns
          outfilpn(8:8)=gns
C
    7	OPEN(11,FILE=OUTFILPN)
C
C HSS/Dst 477 storms from 1996 to 2019
C
   28 format(1X,F8.3,3(1X,I2),3(1X,F6.0))
C   29 format(1X,F8.3,1X,I3,2(1X,I2),1X,I3,1X,F6.0,1X,I2,2(1X,F6.0))
C   29 format(1X,F8.3,1X,I3,3(1X,I2),1X,I4,2(1X,I4),1X,I3,2(1X,I4))
C   29 format(1X,F8.3,1X,I3,3(1X,I2),1X,I4)
   29 format(1X,F8.3,1X,I3,3(1X,I2),1X,I4,2(1X,I4),1X,I3,2(1X,I4))
C-	infhss='cathss48m.txt' ! 1305 HSS events
Ctot      infhss='catdsthss3f.txt'  ! 380 HSS/Dst events
Cjan      infhss='cathssjan.txt'  ! JAN
Ceqn      infhss='cathsseqn.txt'  ! EQN
      infhss='cathssjul.txt'  ! JUL
	open(10,file=infhss,action='READ')
c		read(10,*) txt
C
C Cycle on events ====================================================
C
	DO 177 iev=1,380
c	read(10,29,end=27) ryear1,ldam,imn1,idy1,iutm,v1,iut1,v2,delv
      read(10,29,end=27) ryear1,lda1,imn1,idy1,iut1,idstpeak
	+,ivmin,idelsw,idt,ivmax,nout
	iutcnt=0
	iyear=int(ryear1)
	iyr=iyear-iyear/100*100
C
	lda1=ndoy(iyr,imn1,idy1) ! 1d
      lda0=lda1-1 ! -1d
	lda2=lda1+2
C
C   37 format(20X,24(I4))	 ! AE
   37 format(6X,24(1X,I4))	 !WU, WL, WE
  137 format(6X,24(1X,F4.1))	 ! Wp
C
	if (lda0.eq.0) then  !>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
C
C read last day of prec. year:
C
	iyear0=iyear-1
       call blet4(iyear0,AYEAR)
	if (gns.eq.'x') then
		infile(23:24)=AYEAR(3:4)  ! Wp
	else
	infile(21:22)=AYEAR(3:4)  ! WU, WL,WE
	endif
	open(15,file=infile,action='READ')
	read(15,*) txt ! title line
	ryear0=float(iyear0)
		if(int(ryear0/4.)*4.eq.iyear0) THEN 
	ndnyr=366
	                          ELSE
	ndnyr=365
	ENDIF

	DO 101 k=1,ndnyr
	      if (gns.eq.'x') then
 	read(15,137,end=102) (wpind(ii),ii=0,23)
	 do kk=0,23
	ind24(kk)=int(wpind(kk)*10.0)
	 enddo
	else
	read(15,37,end=102) (ind24(ii),ii=0,23)
	endif

  101	CONTINUE
  102	close(unit=15)
C last day of prec year from iut1 to 23 h:   
       DO ii=iut1,23
C-	if (ind24(ii).lt.9990) then
       inpn(iutcnt)=ind24(ii)
C-	else
C-	inpn(iutcnt)=inpn_pre(iutcnt)
C-	endif
      iutcnt=iutcnt+1
	ENDDO
	endif				 !>>>>>>>>>>>>>>>>>>>>>>>>
C
C Prec. day from current year:
       call blet4(iyear,AYEAR)
	if (gns.eq.'x') then
		infile(23:24)=AYEAR(3:4)  ! Wp
	else
	infile(21:22)=AYEAR(3:4)  ! WU, WL,WE
	endif
	open(15,file=infile,action='READ')
		read(15,*) txt ! title line
C
	if (lda0.gt.0) then !>>>>>>>>>>>>>>>>>>>>>>>>>>>
	DO 109 k=1,lda0
	      if (gns.eq.'x') then
 	read(15,137,end=103) (wpind(ii),ii=0,23)
	 do kk=0,23
	ind24(kk)=int(wpind(kk)*10.0)
	 enddo
	else
	read(15,37,end=104) (ind24(ii),ii=0,23)
	endif
  109	CONTINUE
       DO ii=iut1,23
C-	if (ind24(ii).lt.9990) then
       inpn(iutcnt)=ind24(ii)
C-		else
C- 	inpn(iutcnt)=inpn_pre(iutcnt)
C-       endif
      iutcnt=iutcnt+1
	ENDDO
	endif				 !>>>>>>>>>>>>>>>>>>>>>>>>
C 1st day from current year:
	      if (gns.eq.'x') then
 	read(15,137,end=103) (wpind(ii),ii=0,23)
	 do k=0,23
	ind24(k)=int(wpind(k)*10.0)
	 enddo
	else
	read(15,37,end=103) (ind24(ii),ii=0,23)
	endif
C	  1st day :
       DO ii=0,23
C-		if (ind24(ii).lt.9990) then
       inpn(iutcnt)=ind24(ii)
C-		else
C- 	inpn(iutcnt)=inpn_pre(iutcnt)
C- 	endif
      iutcnt=iutcnt+1
	ENDDO
C 2nd day from current year:
	      if (gns.eq.'x') then
 	read(15,137,end=103) (wpind(ii),ii=0,23)
	 do k=0,23
	ind24(k)=int(wpind(k)*10.0)
	 enddo
	else
	read(15,37,end=103) (ind24(ii),ii=0,23)
	endif
C	  2nd day :
       DO ii=0,23
C-		if (ind24(ii).lt.9990) then
       inpn(iutcnt)=ind24(ii)
C-		else
C- 	inpn(iutcnt)=inpn_pre(iutcnt)
C-       endif
      iutcnt=iutcnt+1
	ENDDO
C 3rd day from current year:
	      if (gns.eq.'x') then
 	read(15,137,end=103) (wpind(ii),ii=0,23)
	 do k=0,23
	ind24(k)=int(wpind(k)*10.0)
	 enddo
	else
	read(15,37,end=103) (ind24(ii),ii=0,23)
	endif
C	  3rd day :
       DO ii=0,23
	if (iutcnt.lt.72) then
C-		if (ind24(ii).lt.9990) then
       inpn(iutcnt)=ind24(ii)
C-		else
C- 	inpn(iutcnt)=inpn_pre(iutcnt)
C-           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)
	if (gns.eq.'x') then
		infile(23:24)=AYEAR(3:4)  ! Wp
	else
	infile(21:22)=AYEAR(3:4)  ! WU, WL,WE
	endif
	open(15,file=infile,action='READ')
		read(15,*) txt ! title line
C
	      if (gns.eq.'x') then
 	read(15,137,end=104) (wpind(ii),ii=0,23)
	 do k=0,23
	ind24(k)=int(wpind(k)*10.0)
	 enddo
	else
	read(15,37,end=104) (ind24(ii),ii=0,23)
	endif
C	  next day :
       DO ii=0,23
	if (iutcnt.lt.72) then
C-			if (ind24(ii).lt.9990) then
       inpn(iutcnt)=ind24(ii)
C-		else
C- 	inpn(iutcnt)=inpn_pre(iutcnt)
C- 	endif
      iutcnt=iutcnt+1
              	else
	exit
              	endif
	ENDDO
      endif	                !>>>>>>>>>>>>>>>>>>>>>
  104	close(unit=15)
C
   38 format(72(1X,I4))
  138 format(72(1X,F4.1))
C Output matrices:
       if (gns.eq.'x') then
	do k=0,71
	wnpn(k)=inpn(k)/10.0
	enddo
 	write(*,138) (wnpn(k),k=0,71)
	write(11,138) (wnpn(k),k=0,71)
	 else
 	write(*,38) (inpn(k),k=0,71)
	write(11,38) (inpn(k),k=0,71)
	 endif
	do jk=0,71
	inpn_pre(jk)=inpn(jk)
	enddo
  177	CONTINUE
   27 close(unit=10)
      close(unit=11)
	if (gns.eq.'x') goto 35		 ! Wp
C
	if (gns.eq.'m') then
	write(*,*) infile
	gns='n'
	goto 1
	endif
	if (gns.eq.'n') then
	write(*,*) infile
	gns='s'
	goto 1
	endif
C
C
   35      write(*,*) infile
      pause ' ' 
      STOP
       END

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