	Program readwulems1h
C...................................................May 2021
C Read WU, WL,WE (regarding Shub TEC) 1h for 364 Dst storms t0,...t0+48h 
C
       DIMENSION wup(3,0:47),wln(3,0:47),wer(3,0:47)
     +,dstres(364,0:47),wures(364,0:47),wlres(364,0:47),weres(364,0:47)
       INTEGER*4 idat1(0:23),idat2(0:23),idat3(0:23)
	 CHARACTER*64 infile,infiwu,infiwl,infiwe,infdst
	+,outfile1,outfile2,outfile3,outfile
	CHARACTER*2 AYR,ANN,CYR,CMN,CDY,CMN1,CDY1,CMN2,CDY2,CMN3,CDY3
	CHARACTER*4 AYEAR,CYEAR
C
	infile='dststorm4.txt'
	OPEN(10,file=infile,action='READ')
C
	outfile1='wudst364.txt'
	outfile2='wldst364.txt'
	outfile3='wedst364.txt'
	outfile='dst364.txt
	infdst='d:\apkp\dst\dstyear'
	infiwu='d:\web\graf\dats\wurYRs.txt'
	infiwl='d:\web\graf\datw\wlnYRs.txt'
	infiwe='d:\web\graf\datw\werYRs.txt'
C
 101  format(6X,2I2,2(1X,I2),5X,2I2,2(1X,I2),1X,I4)
C
	DO 50 nn=1,364            ! Dst storms  for 2000-2017
C
	read(10,101) kyr1,kmn1,kdy1,kut1,kyr2,kmn2,kdy2,kut2,kdst
	close(unit=10)
C
	AYR=AYEAR(3:4)
	read(AYR,*) ryr
	kyr=int(ryr)
		z1=kyr/4.0
      jz=int(z1)*4
      IF(jz.EQ.kyr) THEN
	ndnr=366
        ELSE
	  	ndnr=365
	       ENDIF

      lda=ndoy(kyr,kmn,kdy)
C
	infiwu(21:22)=AYR
c--	infiwl(21:22)=AYR
c--	infiwe(21:22)=AYR
      OPEN(UNIT = 6,file=infiwu,access='SEQUENTIAL',action='READ')
C
    8	outfile(9:10)=ANN
      OPEN(14,file=outfile)
C
   34 format(BZ,A4,2(1X,A2),1X,I4)
   33 format(BZ,24(1X,I4))
  21  format(A4,2(1X,I2),97(1X,I4),2X,I4)		   !TG
C--	DO kk=1,lda+2
       	DO 81 kk=1,ndnr
C//	read(11,*,end=61) CYEAR,imn1,idy1,iaver(idat1(k),k=0,96)
	read(6,34,end=61) CYEAR,cmn,cdy,iaver1
      read(6,33,end=61) (idat1(k),k=0,23)
	read(6,33,end=61) (idat1(k),k=24,47)
	read(6,33,end=61) (idat1(k),k=48,71)
	read(6,33,end=61) (idat1(k),k=72,95)
C
         if (kk.eq.lda) then
	do k=0,95
	wup(1,k)=idat1(k)/100.
	enddo
	goto 81
             endif
         if (kk.eq.lda+1) then
	do k=0,95
	wup(2,k)=idat1(k)/100.
	enddo
	goto 81
             endif
         if (kk.eq.lda+2) then
		do k=0,95
 	wup(3,k)=idat1(k)/100.
	enddo
             endif
   81	CONTINUE
C--   61	close(unit=6)
   61 CONTINUE
C--	OPEN(UNIT = 6,file=infiwl,access='SEQUENTIAL',action='READ')
C--	DO kk=1,lda+2
       	DO 82 kk=1,ndnr
C	read(12,21,end=62) cyr,cmn1,cdy1,(wln(1,k),k=0,95),iwday
C>>	read(12,*,end=66) CYEAR,imn1,idy1,(idat2(k),k=0,96)
C--      read(6,*,end=62) txt
	read(6,34,end=62) CYEAR,cmn,cdy,iaver2
      read(6,33,end=62) (idat2(k),k=0,23)
	read(6,33,end=62) (idat2(k),k=24,47)
	read(6,33,end=62) (idat2(k),k=48,71)
	read(6,33,end=62) (idat2(k),k=72,95)
	         if (kk.eq.lda) then
 	do k=0,95
	wln(1,k)=idat2(k)/100.
	enddo
	goto 82
             endif
         if (kk.eq.lda+1) then
	do k=0,95
	wln(2,k)=idat2(k)/100.
	enddo
	goto 82
             endif
         if (kk.eq.lda+2) then
		do k=0,95
 	wln(3,k)=idat2(k)/100.
	enddo
             endif
   82	CONTINUE
C--   62	close(unit=6)
   62 CONTINUE
C--	OPEN(UNIT = 6,file=infiwe,access='SEQUENTIAL',action='READ')
C--   	DO kk=1,lda+2
       	DO 83 kk=1,ndnr
C	read(13,21,end=63) cyr,cmn1,cdy1,(wer(1,k),k=0,95),iwday
C>>	read(13,*,end=63) CYEAR,imn1,idy1,(idat3(k),k=0,96)
	read(6,34,end=63) CYEAR,cmn,cdy,iaver3
      read(6,33,end=63) (idat3(k),k=0,23)
	read(6,33,end=63) (idat3(k),k=24,47)
	read(6,33,end=63) (idat3(k),k=48,71)
	read(6,33,end=63) (idat3(k),k=72,95)
	         if (kk.eq.lda) then
 	do k=0,95
	wer(1,k)=idat3(k)/100.
	enddo
	cmn1=cmn
	cdy1=cdy
      goto 83
             endif
         if (kk.eq.lda+1) then
	do k=0,95
	wer(2,k)=idat3(k)/100.
	enddo
	cmn2=cmn
	cdy2=cdy
      goto 83
             endif
         if (kk.eq.lda+2) then
	do k=0,95
	wer(3,k)=idat3(k)/100.
	enddo
      cyr=cyear(3:4)
		cmn3=cmn
 	cdy3=cdy
      exit
             endif
   83	CONTINUE
   63	close(unit=6)
C
C Select output values:
      utout=kut+kmm/60.
	jn=kut*4-1
C
C      jj=0,192
       jj=0
	iflag=0
C?	j2=0
   39	iflag=iflag+1
      if (iflag.gt.1) then
	 jn=-1
	utout=0.0
	endif
   40	jn=jn+1
	if (iflag.eq.1) then			!1
      resu=wup(1,jn)
	resl=wln(1,jn)
	rese=wer(1,jn)
	write(*,102) ayear,cmn1,cdy1,utout,resu,resl,rese
	write(14,102) ayear,cmn1,cdy1,utout,resu,resl,rese
C//      write(*,102) ayear,cmn1,cdy1,utout,resu
C//	write(14,102) ayear,cmn1,cdy1,utout,resu
       jj=jj+1
	utout=utout+0.25
	   if (utout.lt.24.0) then		 !2
	   goto 40
	                 else		 !2
         goto 39
	                 endif		 !2			      
	else						 !1
	if (iflag.eq.2) then         !3
      resu=wup(2,jn)
	resl=wln(2,jn)
	rese=wer(2,jn)
	write(*,102) ayear,cmn2,cdy2,utout,resu,resl,rese
	write(14,102) ayear,cmn2,cdy2,utout,resu,resl,rese
       jj=jj+1
	utout=utout+0.25
	   if (utout.lt.24.0) then		 !4
	   goto 40
	                 else		 !4
         goto 39
	                 endif		 !4			      
	                                   else   !3
C	if (iflag.eq.3) then
	      if (jj.lt.192) then          !5
	      resu=wup(3,jn)
      	  resl=wln(3,jn)
            rese=wer(3,jn)
	write(*,102) ayear,cmn3,cdy3,utout,resu,resl,rese
	write(14,102) ayear,cmn3,cdy3,utout,resu,resl,rese
       jj=jj+1
	utout=utout+0.25
	 goto 40
	                    endif		 !5
	                                      endif !3
             endif                 !1
c	
      close(unit=14)
   50 CONTINUE                ! END tests files
      pause ' '
	stop
  20  format(i4,4(1x,i2.2),2x,f5.1,1x,i3,3x,4(3x,f7.2))
 100  format(1x,'Day Month Year      UT     SYM_H')
 102  format(1X,A4,2(1X,A2),1X,F5.2,3(1X,F6.2))
C// 102  format(1X,A4,2(1X,A2),1X,F5.2,1X,F6.3)
	end			