	Program readdgecs1h
C...................................................May 2021
C Read DGEC, % (regarding Shub GTEC) 1h for SC23 and SC24 storms t0,...t0+48h 
C
       DIMENSION dgec3(3,0:23),idst3(3,0:23)
       INTEGER*4 idat(0:23),IM(12)
	 CHARACTER*131 infile,infidgec,infdst,outfile,txt
	CHARACTER*2 AYR,AMN,CYR,CMN,CDY
	+,BMN(3,0:23),BDY(3,0:23),ANN
	CHARACTER*4 AYEAR
	DATA  IM/31,28,31,30,31,30,31,31,30,31,30,31/
C
	infile='testwuxxnns.txt'
	infidgec='c:\msdev\Projects\GEC\devYRMNs.txt'
C
	outfile='dgecnn.txt'
	infdst='d:\apkp\dst\dstyear'
C
 101  format(1X,I4,3(1X,I2))
C
CNORM	DO 50 nn=9,24            ! storms  for SC23 SC24
	DO 50 nn=15,24            ! storms  for SC23 SC24
		call blet2(nn,ANN)
C
	infile(9:10)=ANN
      OPEN(10,file=infile,action='READ')
	read(10,101) kyear,kmn,kdy,kut
	close(unit=10)
C
      call blet4(kyear,AYEAR) 
	call blet2(kmn,AMN)
C	 
	AYR=AYEAR(3:4)
C
	infdst(16:19)=AYEAR
	OPEN(11,file=infdst,action='READ')
C
	read(AYR,*) ryr
	kyr=int(ryr)
		z1=kyr/4.0
      jz=int(z1)*4
      IF(jz.EQ.kyr) THEN
	ndnr=366
	IM(2)=29
        ELSE
	  	ndnr=365
	IM(2)=28
	       ENDIF
C
	do kk=1,3
	do k=0,23
	idst3(kk,k)=0
	dgec3(kk,k)=0.
	enddo
	enddo
C
      lda=ndoy(kyr,kmn,kdy)
   31 format(3X,2A2,1X,A2,10X,24(I4))
   30 format(A131)
C Read Dst
      jj=0
	DO idy=1,lda+2
	read(11,31,end=41) CYR,CMN,CDY,(idat(k),k=0,23)
	if (idy.eq.lda) then
	do k=0,23
	idst3(1,k)=idat(k)
	jj=jj+1
	enddo
	endif
	if (idy.eq.lda+1) then
	do k=0,23
	idst3(2,k)=idat(k)
	jj=jj+1
	enddo
	endif
	if (idy.eq.lda+2) then
	do k=0,23
	idst3(3,k)=idat(k)
	jj=jj+1
	enddo
	endif
	ENDDO
   41 close(unit=11)
C
C Read dgec
	infidgec(26:27)=AYR
	infidgec(28:29)=AMN
      OPEN(12,file=infidgec,action='READ')
	do i=1,4
	read(12,30) txt
	enddo
	jj=0
	imndy=im(kmn)
	IF (kdy+2.le.imndy)      THEN         ! 
	kdy2=kdy+2
	else
	kdy2=imndy
	endif
	DO idy=1,kdy2
	read(12,32,end=42) CYR,CMN,CDY,(idat(k),k=0,23)
	if (idy.eq.kdy) then
	do k=0,23
	dgec3(1,k)=idat(k)/10.
	BMN(1,k)=CMN
	BDY(1,k)=CDY
	jj=jj+1
	enddo
	endif
	if (idy.eq.kdy+1) then
	do k=0,23
	dgec3(2,k)=idat(k)/10.
	BMN(2,k)=CMN
	BDY(2,k)=CDY
	jj=jj+1
	enddo
	endif
	if (idy.eq.kdy+2) then
	do k=0,23
	dgec3(3,k)=idat(k)/10.
	BMN(3,k)=CMN
	BDY(3,k)=CDY
	jj=jj+1
	enddo
	exit
	endif
	ENDDO
   42 close(unit=12)
C
      if (jj.lt.48) then     !>>>>>>>>>>>>>>
C Add next month data
C
      kmn=kmn+1
	call blet2(kmn,AMN)
      infidgec(28:29)=AMN
C
      OPEN(12,file=infidgec,action='READ')
	do i=1,4
	read(12,30) txt
	enddo
C
	if (jj.eq.24) then 
	ndays=2
	else
	ndays=1
	endif
   32 format(3A2,24(1X,I4))
      DO idy=1,ndays
      read(12,32,end=43) CYR,CMN,CDY,(idat(k),k=0,23)
	if (idy.eq.ndays) then
	do k=0,23
	dgec3(3,k)=idat(k)/10.
	BMN(3,k)=CMN
	BDY(3,k)=CDY
	jj=jj+1
	enddo
	              else
	do k=0,23
	dgec3(2,k)=idat(k)/10.
	BMN(2,k)=CMN
	BDY(2,k)=CDY
	jj=jj+1
	enddo
	endif
	ENDDO
   43	close(unit=12)
      endif                 !>>>>>>>>>>>>>>>>>
C
C formulate OUTPUT results 
    8	outfile(5:6)=ANN
      OPEN(14,file=outfile)
C
C
C Select output values:
C
       kk=0
	DO iut=kut,23
      resdgec=dgec3(1,iut)
	iresdst=idst3(1,iut)
	write(*,102) ayear,BMN(1,iut),BDY(1,iut),iut,resdgec,iresdst
	write(14,102) ayear,BMN(1,iut),BDY(1,iut),iut,resdgec,iresdst
	kk=kk+1
      ENDDO
	DO iut=0,23
      resdgec=dgec3(2,iut)
	iresdst=idst3(2,iut)
	write(*,102) ayear,BMN(2,iut),BDY(2,iut),iut,resdgec,iresdst
	write(14,102) ayear,BMN(2,iut),BDY(2,iut),iut,resdgec,iresdst
	kk=kk+1
      ENDDO
	if (kk.eq.47) goto 49
	DO iut=0,kut-1
      resdgec=dgec3(3,iut)
	iresdst=idst3(3,iut)
	write(*,102) ayear,BMN(3,iut),BDY(3,iut),iut,resdgec,iresdst
	write(14,102) ayear,BMN(3,iut),BDY(3,iut),iut,resdgec,iresdst
      ENDDO
C
c	
   49   close(unit=14)
   50 CONTINUE                ! END tests files
      pause ' '
	stop
 102  format(1X,A4,2(1X,A2),1X,I2,1X,F6.1,1X,I4)
	end			