      subroutine subtecupc(AYEAR,AMN,ADY,iutcur,immcur)
C..................................................Oct 2024
C read UADG hour-by-hour for current day UT=00, 01,...23 UT
C .................................................Aug 2022
C Extract TEC UT maps from daily UPC UADGF GIM-TEC file
C
	CHARACTER*2 AYR,AMN,ADY,RMN,RDY,ADY0,AUT,AMM,UTEND,MMEND
	CHARACTER*3 DDD
	INTEGER itec(71,0:72)
	CHARACTER*128 infile
Cx	+,outfile
	CHARACTER*16 start,txt1,startrms
	CHARACTER*1 qs,fht,om 
C	  qs='u' uqrg, qs ='c' uadg// om='o' TEC-obs'; om=m' CCIR median // fht='f' foF2,=h hmF2, =t TEC
	CHARACTER*60 txt
	CHARACTER*4 RYEAR,AYEAR
      CHARACTER*11 readglat
 	integer*4 ixx(16)
	dimension itxx(0:72)
      COMMON /MTEC/itec
      COMMON /DAN/RZ,daynr,dnr,imap,irun,qs,iutend,immend 
C irun=1 TECobs; =2 TEC-CCIR; =3 foF2-CCIR; =4 hmF2-ccir
	if (irun.eq.1) then
	om='o'        ! TECobs
	fht='t'
	               else
	om='m'		  ! TEC-CCIR
      	if (irun.eq.2) fht='t'
      	if (irun.eq.3) fht='f'
      	if (irun.eq.4) fht='h'
	endif
C
      AYR=AYEAR(3:4)
	start='START OF TEC MAP'
	startrms='START OF RMS MAP'
	read(ady,*) zdy
	idy=int(zdy)
	read(ayr,*) zyr
	iyr=int(zyr)
	read(amn,*) zmn
	imn=int(zmn)
	lda=ndoy(iyr,imn,idy)
	call blet3(lda,DDD)
1001	ndyend=idy       ! for one day TEC maps
      IRN=0 ! NORMAL COMMAND
      iut0=iutcur
	ady0=ady
C
        IF (irun.eq.1) THEN
      infile='uqrgDDD0.YRi'	!!!!!!!! uqrg-file-hourly-15min or uaqdg
      if (qs.eq.'u') then
	 infile(2:3)='qr'       !uqrg
	               else
	 infile(2:3)='ad'	  ! uadh
	endif
	infile(5:7)=DDD
	infile(10:11)=ayr		  ! 
                        ELSE
      infile='YRMN00ti_15m.txt'	  ! CCIR
	infile(7:7)=fht
      infile(1:2)=ayr
      infile(3:4)=amn
                       ENDIF
	iruns=96
C 
	 OPEN(20,FILE=INFILE,ACTION='READ')
	ihr=0 
C+++++++++++++++++++++++++++++++++++++++++++++++++
C start cycle on hr-to-hr input:
   80	do i=1,1600					             !!
	READ (20,187,END=33,ERR=2) txt,txt1
  187	format(A60,A16)
      if ((i.eq.12).and.(irun.eq.1)) then
	write(*,187) txt,txt1
	utend=txt(23:24)
	mmend=txt(29:30)
	if(utend(1:1).eq.' ') utend(1:1)='0'
	if(mmend(1:1).eq.' ') mmend(1:1)='0'
	read(utend,*) rutend
	iutend=int(rutend)
	read(utend,*) rutend
	iutend=int(rutend)
	read(mmend,*) rmmend
	immend=int(rmmend)
	endif 
      if (txt1.eq.startrms) goto 33 ! END reading TEC
	if (txt1.eq.start) goto 88
	enddo									 !!

C Start of reading VTEC ========================
  183 READ (20,187,END=33,ERR=2) txt,txt1
      if (txt1.eq.start) then 
      goto 88
	             else
	goto 183
	             endif
   88	read (20,89,end=9,err=85) ryear,rmn,rdy,iut,imm
CADD++++++++++++++++++++++++++++++++++++++++++++++++
  185	if ((iut.eq.iutcur).and.(imm.eq.immcur)) then
      goto 85  ! 
                                               else
      goto 183
	                    endif
   89 format(2X,A4,4X,A2,4X,A2,2(4X,I2))
   85	continue
      rut=float(iut)
	call blet2(iut,AUT)	
	call blet2(imm,AMM)	

C
 	if (rdy(1:1).eq.' ') rdy(1:1)='0'
	read (rdy,*) day
	idy=int(day)
C start cycle on glatitudes:
	glati=90.0
C
      	DO 777 lat=1,71	   ! Start cycle on glati ++++++++++++++++++++++++++++++++
	glati=glati-2.5
C
   87	read (20,*,end=9,err=2) readglat
   90	format(3X,F5.1,A60)
  91   	j1=0
C
C Add check of wrong TEC.le.0<=itec_pre +++++++++++++
C
	itec_pre=20	  ! assumed TEC_pre 
	do j=1,4        
	j1=(j-1)*16
   1	READ (20,180,END=9,ERR=2) (ixx(i),i=1,16)
  180	format(16(1X,I4))
	do k=1,16
	if ((ixx(k).le.0).or.(ixx(k).eq.9999)) then
	ixx(k)=itec_pre ! replace wrong TEC
	endif
	itxx(j1+k-1)=ixx(k)
	itec_pre=ixx(k)		! latest TEC_pre
	enddo
	enddo			  ! start selection of 4 longitudes
	READ (20,179,END=9,ERR=2) (ixx(i),i=1,9)
  179	format(9(1X,I4))
	do k=1,9
	if ((ixx(k).le.0).or.(ixx(k).eq.9999)) then
	ixx(k)=itec_pre ! replace wrong TEC
	endif
	itxx(63+k)=ixx(k)
	itec_pre=ixx(k)		! latest TEC_pre
	enddo
C
C Select values for Long=-180,-165,...0,15,30,..,165,180!!!!!!!!!!!!:
C
	ii=-1
	 jj=71
C====================================================
	do il=-180,180,5     ! for long-cycle
	ii=ii+1					   ! ii=0 for glong=-180,...,ii=72 for glong=180
	ires=ii
	if (itxx(ii).lt.10) itxx(ii)=10 ! Replace TEC<1 by TEC=1 TECU (=0.1TECU)
	glong=float(il)
CC++		   
	itec(lat,ires)=itxx(ii)	! remember tec map
	enddo
  777	continue   ! End cycle of reading all GLATs
	GOTO 33       !end of current TEC map(UTcur,MMcur) =========>
C
 182	format(73(1X,I4))							
    9	continue
  
C Continue to the next 15min-UT hr :
  32		irn=irn+1 
 	ady0=rdy
	read(20,187,END=33,ERR=2) txt,txt1 ! final line for UT_15min
  133 	read(20,187,END=33,ERR=2) txt,txt1 ! final line for UT_15min
   33	close(unit=20)
	goto 35	   ! end of calculations
C
    2 CONTINUE
      if (irn.eq.0) then
       write(*,*) 'INPUT FILE IS NOT IN YOUR DIRECTORY '
 	pause ' '
	goto 35
      endif
   35      write(*,*) infile
   	write(*,*) irn
	 call subratfh(AYEAR,AMN,ADY,iutcur,immcur)
C      pause ' ' 
      RETURN
       END
C -----------------------------------------------------------
