C .......................................................Jan 2024
C GEC(GPLR) 2.5x5 grids
C                                                        Dec 2011
C
C Add GEC calculation for ut=24h
C Add daily rate-of-change, daily mean GEC -> OUTFILE2
C
C T.L.Gulyaeva...........................................July 2011
C 
      PROGRAM MAPGEC6jc
C----------------------------------------------------------------
C Select all latitudes: res1=87.5N,85N,,,,5N,00,5S,...,87.5S 
C                        
C each line in input file for TEC at:[-180, -165,...0,15E,30E,...,165E]
C separate files for UT=0,1,2,3,...23hUT
C Produce GYRMNDY.res files 	 (UT GEC)
C output array: res(72)
C program for EXTRACTING FROM "uhrgdoy0.iYR" ionex_TEC data 
C Output: GYRMNDY.res data for day into annual file gecyr.br
C all results for given year, includint YRMNDY  GEC(t), t=0,1,...,23 UT + daily mean, GEC(t)=GEC/1.E+32, GECU
C outfile='gecyr.br' 
C if input file jplg (2h UT step), linear interpolation is applied for 1h UT results
C
C*********TEST
	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
	CHARACTER*80 INFILE,OUTFILE,OUTFILE2,infcoef,outyear
	CHARACTER*4 AYEAR
	CHARACTER*131 txt
	character*2 ayr,amn,ady,aut
	integer iyr,imn,iut
	dimension itxx(73),im(12)
	+,gecoef(71),iamp(0:7)
	dimension res(72),rgec(0:24),igec(0:24),iroc(0:24)
C_	     COMMON  /CONST/UMR
	DATA  IM/31,28,31,30,31,30,31,31,30,31,30,31/
C	! 87.5,85,...,2.5,0
       infcoef='geccoef2h.txt'
	OPEN(10,file=infcoef,action='READ')
	DO kk=1,71
	read(10,*,end=19) fi,coefi
	GECOEF(kk)=coefi*1.0E+06
	ENDDO
   19	close(10)
C
C	UMR=ATAN(1.0)*4./180.
	IRUN=0
c	comp='          '

	CALL DATE_AND_TIME(DATE=DD,TIME=TT,ZONE=ZZ)
      TT(5:)='      '
      WRITE(*,*) 'PC Date: Year,Month,Day = ',DD,'Time = ',TT
c
118	format(A4)
C	pause  ' '
C+	WRITE(*,'(\A\)') ' ENTER YEAR OF INPUT FILE='
C+      READ(*,'(A)') AYEAR
	WRITE(*,'(\A\)') ' ENTER MN OF INPUT FILE='
      READ(*,*) mninp
C+	read(ayear,*) year
C       DO 105 iyear=1994,2022
Cnorm       DO 105 iyear=2008,2022
        iyear=2026   !TEMPPPPPPPPPPPPPPPPPP
	call blet4(iyear,ayear)
	ayr=ayear(3:4)
	read(ayr,*) ryr 
      iyr=int(ryr)
	year=float(iyear)
		iiyr=int(year)
 	if(int(year/4.)*4.eq.iiyr) THEN 
	im(2)=29
	                          ELSE
	IM(2)=28
	ENDIF
c
c outfiles =================================
       outfile2='gechYR.txt' ! 3h Amplitude of GEC
	outfile='ghYRMN.txt'
C_	gh='+'        ! 1h day
      outfile(3:4)=ayr
	outfile2(5:6)=ayr	 
	OPEN(13,FILE=OUTFILE2,ACCESS='APPEND')
	outyear='gecYEAR.txt'  !TEMPPPPPPPPP
CC CALCULATION OF DAY OF YEAR	(LDA)
C
        infile='c:\web\graf\dtc\YR\MN\ut\tcMNDDUT.jYR' ! UT=0, 1,...,23 
	infile(17:18)=AYR
	infile(36:37)=AYR
	outyear(4:7)=AYEAR
      OPEN(14,FILE=OUTYEAR,ACCESS='APPEND')
	txt(1:9)='YYMMDD|UT'
	do k=0,24
	iroc(k)=k
	enddo
  276 format(A9,23(I2,3X),I2,2X,'Day')	
      write(*,276) txt,(iroc(k),k=0,23)
	write(14,276) txt,(iroc(k),k=0,23)

C Cycle on month:
C
C       DO 100 imn=1,12
        imn1=1
	imn2=12
c	if (iyear.eq.2023) then 
c	imn10=mninp  !TEMPPPPPPPPPPPPP
c	imn10=mninp  !TEMPPPPPPPPPPPPP
c	endif
c       DO 100 imn=imn1,imn2
       imn=mninp
	call blet2(imn,AMN)
c	xdy=1.      ! start day-of-month
C
      infile(20:21)=AMN
	infile(28:29)=AMN
	outfile(5:6)=amn
      		OPEN(12,FILE=OUTFILE,ACCESS='APPEND')
C
	idy1=1
	idy2=IM(imn)
c	if ((iyear.eq.2023).and.(imn.eq.9)) then 
c	idy1=1
c	idy2=4 !TEMPPPPPPPPPPPPPP
c	endif
C Cycle day-to-day +++++++++++++++++++++++++++++
          DO 101 idy=idy1,idy2
	call blet2(idy,ADY)
	infile(30:31)=ADY
C
C
C  700	iidy=int(xdy)
	do n=0,24
	rgec(n)=0.
	enddo
C
	lda=ndoy(iyr,imn,idy)
	daynr=float(lda)
C
C?   11	continue     ! day-to-day cycle
C--------------------------------
C Start input for given UT:++++++++++++++++++++
C UT cycle
       DO 775 iut=0,23
	 call blet2(iut,AUT)
	 infile(32:33)=AUT 
   27 format(6X,F3.1)
	OPEN(11,FILE=INFILE,ACTION='READ')

c ***************************************************************
D start cycle on hr-to-hr input:
   80	GEC=0.
c      	do k=1,73
c  	itxx0(k)=0.		! prec. lati TEC
c	enddo

C
  	ilat=0
C start cycle on glatitudes:
	glati=-90.0
C
	DO 777 lat=1,71	   ! Start cycle on glati

	res(lat)=0.        ! GEC for each glat
C_		ilat=ilat+1
	glati=glati+2.5
	 acoef=gecoef(lat)
C++++++++++++++++++
C_   87	read (111,90,end=9,err=2) glat,txt
C_   90	format(3X,F5.1,A60)
C       if (lat.eq.72) goto 171
   1	READ (11,180,END=9,ERR=2) (itxx(i),i=1,73)
  180	format(73(1X,I4))
C Select values for Long=-179.5,-178.5,...0.5,,..,179.5...!!!!!!!!!!!!:
C
171	nlon=0
C
 	sum=0.
	glong=-185.0
C
	do 776 nlon=1,72     ! for long-cycle
	glong=glong+5.0 
C++++++++
C
C	rtav(nlon)=(itxx0(nlon)+itxx0(nlon+1)+itxx(nlon)+itxx(nlon+1))/4.
c//      isum=isum+itxx(nlon)
c  172    sum=sum+rtav(nlon)
  172    sum=sum+itxx(nlon)
C++			 
C
 776	continue ! long cycle
	res(lat)=sum*1.0E+15*acoef
 182	format(3A2,24(1X,I4))							
 121 	GEC=GEC+res(lat)
c	do i=1,73
c	itxx0(i)=itxx(i)  ! prec. lati TEC
C-	itxx(i)=0.
c	enddo
C
 777  continue  ! lati cycle
   9   close(11)
C --------------- 
C OUTPUT OF RESULTS
 176  FORMAT(1X,I2,2X,E12.6)
c	do kk=1,72
c1	write(*,176) iut,GEC
c1	write(12,176) iut,GEC
c	enddo
	rgec(iut)=GEC/1.0E+32	 
  775 continue   ! End calculation for given UT   Continue to the next UT hr :
C
C_  32		irun=irun+1 
C
C Output of daily results
C
  207	continue
C
Cif (rgec(24).eq.0) rgec(24)=rgec(22)+(rgec(23)-rgec(22))*2. ! Lin. extrap.
	sum=0.
	do i=0,23
	sum=sum+rgec(i)
	igec(i)=nint(rgec(i)*100.)
	enddo
	sum=sum/24.
c-	iroc(24)=nint(sum*100)
	iaver=nint(sum*100.) 
	igec(24)=iaver
C
C      Add output of 4 title lines
	IF (idy.eq.1) THEN
	txt(1:3)='GEC'
	write(12,275) txt
	write(*,275) txt
  275 format(A3)
	WRITE(*,*) 'PC Date: Year,Month,Day = ',DD,' Time = ',TT
	WRITE(12,*) 'PC Date: Year,Month,Day = ',DD,' Time = ',TT
      write(*,276) txt,(iroc(k),k=0,23)
	write(12,276) txt,(iroc(k),k=0,23)
	txt(1:50)='--------------------------------------------------'
	txt(51:100)='--------------------------------------------------'
	txt(101:131)='-------------------------------'
  274 format(A131)
	write(12,274) txt
	write(*,274) txt
	ENDIF
C
C	write(*,177) ayr,amn,ady,(igec(i),i=0,24)  ! GEC
	write(12,177) ayr,amn,ady,(igec(i),i=0,24)
	write(14,177) ayr,amn,ady,(igec(i),i=0,24)
C+++++++++++++++++++++++++++++++++++++++++++++++
Ctemp Produce 3-h UT amplitude of GEC
C
	do i=0,7
	i1=i*3
	xmin=9999.
	xmax=0.
C
	do k=1,3
	if (rgec(i1+k-1).lt.xmin) xmin=rgec(i1+k-1)	  ! 3h Amplitude
	if (rgec(i1+k-1).gt.xmax) xmax=rgec(i1+k-1)
	enddo
	iamp(i)=nint((xmax-xmin)*100.)
	enddo
	write(*,277) ayr,amn,ady,(iamp(i),i=0,7),iaver	 ! 3h Amplitude
	write(13,277) ayr,amn,ady,(iamp(i),i=0,7),iaver
 277  format(3A2,1X,8(I4,1X),2X,I4)
 177  format(3A2,25(1X,I4))
ctemp 177  format(3A2,1X,25(I4,1X))
C	pause ' '
  101  CONTINUE
 	close (unit=12)
  100  CONTINUE
C       
	close (unit=13)
	close (unit=14)
  105  CONTINUE
C 
		goto 35	   ! end of calculations
C
c	write (*,888) alat,alon,aday
c  888	format(1X,' alat=',A3,' alon=',A3,' day=',A3)

    2 CONTINUE
      if (irun.eq.0) then
       write(*,*) 'INPUT FILE IS NOT IN YOUR DIRECTORY '
 	pause ' '
	goto 35
      endif

C_   33	if ((iidy.le.idy2).and.(idy1.lt.idy2)) goto 32
   35      write(*,*) infile,outfile
	write(*,*) irun
      pause ' ' 
      STOP
       END

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