C........................................................Apr 2021
C
C...................................................
C Extract WU,WL,WE for -24h<t0< +48h for Dst<-250nT events (ev=8,SC24), (ev=9, SC23)
C 
C Select period of 72hrs starting from t0-24h, ending t0+48h; t0 @ SSC
C 
      PROGRAM decodwc1ds
C Input storm22c.txt 1986-1996, storm23c.txt 1997-2008, storm24c.txt = testing storms....Apr 2021
C add: select from -24h < to <+48h for dst_storms-events to=t(SSC)
C
C----------------------------------------------------------------
C
	DIMENSION inwu(0:71),inwl(0:71),inwe(0:71) ! 1h indices for 24h (0r 72h file for 1h indices)
	+,kwu(0:23),kwl(0:23),kwe(0:23),jwu(0:23),jwl(0:23),jwe(0:23)
C
	CHARACTER*2 AYR
	CHARACTER*4 AYEAR
C	+,DYR(1132),DMN(1132),DDY(1132),DUT(1132)
      CHARACTER*128 INFILE1,INFILE2,INFILE3,INFSTORM,txt
	+,outfilwu,outfilwl,outfilwe !WU,WL,WE
	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
c++	infile1='d:/web/graf/datw/wupYR.txt'  !JPL
c++	infile2='d:/web/graf/datw/wlnYR.txt'  !JPL
c++	infile3='d:/web/graf/datw/werYR.txt'  !JPL
	infile1='d:/web/graf/dats/wupYRs.txt'  !Shub
	infile2='d:/web/graf/dats/wlnYRs.txt'  !Shub
	infile3='d:/web/graf/dats/werYRs.txt'  !Shub
C
c+	outfilwu='wuc23d.txt' !total file for 1997-2009 =9 lines x 72 cols superposed epoch
c+		outfilwl='wlc23d.txt' !total file for 1997-2009 =9 lines x 72 cols superposed epoch
c+	outfilwe='wec23d.txt' !total file for 1997-2009 =9 lines x 72 cols superposed epoch
C+	outfilwu='wuc24d.txt' !total file for 1997-2009 =9 lines x 72 cols superposed epoch
C+		outfilwl='wlc24d.txt' !total file for 1997-2009 =9 lines x 72 cols superposed epoch
C+	outfilwe='wec24d.txt' !total file for 1997-2009 =9 lines x 72 cols superposed epoch
c+	outfilwu='wus24d.txt' !total file for 1997-2009 =9 lines x 72 cols superposed epoch	 Shub
c+		outfilwl='wls24d.txt' !total file for 1997-2009 =9 lines x 72 cols superposed epoch	Shub
c+	outfilwe='wes24d.txt' !total file for 1997-2009 =9 lines x 72 cols superposed epoch	  Shub
	outfilwu='wus23d.txt' !total file for 1997-2009 =9 lines x 72 cols superposed epoch	 Shub
		outfilwl='wls23d.txt' !total file for 1997-2009 =9 lines x 72 cols superposed epoch	Shub
	outfilwe='wes23d.txt' !total file for 1997-2009 =9 lines x 72 cols superposed epoch	  Shub

	OPEN(11,FILE=OUTFILWU)
	OPEN(12,FILE=OUTFILWL)
	OPEN(13,FILE=OUTFILWE)
C
C kp/Dst 8(9) storms from 1996 to 2019
C
   28 format(1X,F8.3,3(1X,I2),3(1X,F6.0))
   29 format(6X,I4,2I2,1X,I2,2X,I4,2I2,1X,I2,1X,F6.1,1X,I4,2I2,1X,I2)

	INFSTORM='storms23c.txt'
C+	INFSTORM='storms24c.txt'

	open(10,file=INFSTORM,action='READ')
C	read(10,*) txt

C
C Cycle on events ====================================================
C
	DO 177 iev=1,9        ! SC23
c-	DO 177 iev=1,8        ! SC24
      read(10,29,end=27) iyear1,imn1,idy1,iut1,iyear2,imn2,idy2,iut2,dst
	+,iyear3,imn3,idy3,iut3
	iutcnt=0
	do k=0,71
	inwu(k)=0
	inwl(k)=0
	inwe(k)=0
	enddo
	iyear=iyear1
	iyr=iyear-iyear/100*100
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
	iyear0=iyear-1
       call blet4(iyear0,AYEAR)
	AYR=AYEAR(3:4)
	infile1(21:22)=AYR
	open(15,file=infile1,action='READ')
	read(15,*) txt   ! extra line
	infile2(21:22)=AYR
 	open(16,file=infile2,action='READ')
	read(16,*) txt   ! extra line
      infile3(21:22)=AYR
	open(17,file=infile3,action='READ')
	read(17,*) txt   ! extra line

	ryear0=float(iyear0)
		if(int(ryear0/4.)*4.eq.iyear0) THEN 
	ndnyr=366
	                          ELSE
	ndnyr=365
	ENDIF

	DO 101 k=1,ndnyr

	read(15,137,end=102) jyr,jmn,jdy,(jwu(k),k=0,23)
	 read(16,137,end=112) jyr,jmn,jdy,(jwl(k),k=0,23)
	 read(17,137,end=122) jyr,jmn,jdy,(jwe(k),k=0,23)
	  do i=0,23
	kwu(i)=jwu(i)
	kwl(i)=jwl(i)
	kwe(i)=jwe(i)
	  enddo
  101	CONTINUE
  102	close(unit=15)
  112	close(unit=16)
  122	close(unit=17)
C last day of prec year from iut1 to 23 h:   
       DO ii=iut1,23
       inwu(iutcnt)=kwu(ii)
 	inwl(iutcnt)=kwl(ii)
 	inwe(iutcnt)=kwe(ii)
      iutcnt=iutcnt+1
	ENDDO
	endif				 !>>>>>>>>>>>>>>>>>>>>>>>>
C
  137 format(3I2,24(1X,I4)) ! wu,wl,we 
C Prec. day from current year:
       call blet4(iyear,AYEAR)
	AYR=AYEAR(3:4)
	infile1(21:22)=AYR
	open(15,file=infile1,action='READ')
	read(15,*) txt   ! extra line
	infile2(21:22)=AYR
	open(16,file=infile2,action='READ')
	read(16,*) txt   ! extra line
	infile3(21:22)=AYR
	open(17,file=infile3,action='READ')
	read(17,*) txt   ! extra line
C
	if (lda0.gt.0) then !>>>>>>>>>>>>>>>>>>>>>>>>>>>
	DO 109 k=1,lda0
	read(15,137,end=103) jyr,jmn,jdy,(jwu(k),k=0,23)
	 read(16,137,end=113) jyr,jmn,jdy,(jwl(k),k=0,23)
	 read(17,137,end=123) jyr,jmn,jdy,(jwe(k),k=0,23)
	  do i=0,23
	kwu(i)=jwu(i)
	kwl(i)=jwl(i)
	kwe(i)=jwe(i)
	  enddo
  109	CONTINUE
       DO ii=iut1,23
       inwu(iutcnt)=kwu(ii)
	inwl(iutcnt)=kwl(ii)
	inwe(iutcnt)=kwe(ii)
      iutcnt=iutcnt+1
	ENDDO
	endif				 !>>>>>>>>>>>>>>>>>>>>>>>>
C 1st day from current year:
	read(15,137,end=103) jyr,jmn,jdy,(jwu(k),k=0,23)
	 read(16,137,end=113) jyr,jmn,jdy,(jwl(k),k=0,23)
	 read(17,137,end=123) jyr,jmn,jdy,(jwe(k),k=0,23)
	  do i=0,23
	kwu(i)=jwu(i)
	kwl(i)=jwl(i)
	kwe(i)=jwe(i)
	  enddo
C	  1st day :
       DO ii=0,23
       inwu(iutcnt)=kwu(ii)
	inwl(iutcnt)=kwl(ii)
	inwe(iutcnt)=kwe(ii)
      iutcnt=iutcnt+1
	ENDDO
C 2nd day from current year:
	read(15,137,end=103) jyr,jmn,jdy,(jwu(k),k=0,23)
	 read(16,137,end=113) jyr,jmn,jdy,(jwl(k),k=0,23)
	 read(17,137,end=123) jyr,jmn,jdy,(jwe(k),k=0,23)
	  do i=0,23
	kwu(i)=jwu(i)
	kwl(i)=jwl(i)
	kwe(i)=jwe(i)
	  enddo
C	  2nd day :
       DO ii=0,23
       inwu(iutcnt)=kwu(ii)
	inwl(iutcnt)=kwl(ii)
	inwe(iutcnt)=kwe(ii)
      iutcnt=iutcnt+1
	ENDDO
C 3rd day from current year:
	read(15,137,end=103) jyr,jmn,jdy,(jwu(k),k=0,23)
	 read(16,137,end=113) jyr,jmn,jdy,(jwl(k),k=0,23)
	 read(17,137,end=123) jyr,jmn,jdy,(jwe(k),k=0,23)
	  do i=0,23
	kwu(i)=jwu(i)
	kwl(i)=jwl(i)
	kwe(i)=jwe(i)
	  enddo
C	  3rd day :
       DO ii=0,23
	if (iutcnt.lt.72) then
       inwu(iutcnt)=kwu(ii)
	inwl(iutcnt)=kwl(ii)
	inwe(iutcnt)=kwe(ii)
      iutcnt=iutcnt+1
              	else
	exit
              	endif
	ENDDO
  103	close(unit=15)
  113	close(unit=16)
  123	close(unit=17)
C
C Next year if necessary:
       if (iutcnt.lt.72) then   !>>>>>>>>>>>>>>>>>>>>>>>>>>>
	iyear1=iyear+1
	call blet4(iyear1,AYEAR)
	AYR=AYEAR(3:4)
	infile1(21:22)=AYR
	open(15,file=infile1,action='READ')
	read(15,*) txt   ! extra line
	infile2(21:22)=AYR
	open(16,file=infile2,action='READ')
	read(16,*) txt   ! extra line
	infile3(21:22)=AYR
	open(17,file=infile3,action='READ')
	read(17,*) txt   ! extra line
C
	read(15,137,end=104) jyr,jmn,jdy,(jwu(k),k=0,23)
	 read(16,137,end=114) jyr,jmn,jdy,(jwl(k),k=0,23)
	 read(17,137,end=124) jyr,jmn,jdy,(jwe(k),k=0,23)
	  do i=0,23
	kwu(1)=jwu(i)
	kwl(1)=jwl(i)
	kwe(1)=jwe(i)
	  enddo
C	  next day :
       DO ii=0,23
	if (iutcnt.lt.72) then
       inwu(iutcnt)=kwu(ii)
	inwl(iutcnt)=kwl(ii)
	inwe(iutcnt)=kwe(ii)
      iutcnt=iutcnt+1
              	else
	exit
              	endif
	ENDDO
  104	close(unit=15)
  114	close(unit=16)
  124	close(unit=17)
      endif	                !>>>>>>>>>>>>>>>>>>>>>
C
  138 format(72(1X,I3))
  139 format(72(1X,I4))
C Output matrices:
 	write(*,138) (inwu(k),k=0,71)
	write(11,138) (inwu(k),k=0,71)
 	write(*,139) (inwl(k),k=0,71)
	write(12,139) (inwl(k),k=0,71)
 	write(*,138) (inwe(k),k=0,71)
	write(13,138) (inwe(k),k=0,71)
  177	CONTINUE
   27 close(unit=10)
      close(unit=11)
	close(unit=12)
	close(unit=13)
C
C
   35      write(*,*) infile1,infile2,infile3
      pause ' ' 
      STOP
       END

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