C........................................................Feb.2018
C Gulyaeva Tamara <gulyaeva@izmiran.ru>
C
C Extract maps foF2, hmF2, TEC, from daily JPL-IONEX file
      PROGRAM EXTMAPSJ
C----------------------------------------------------------------
C Select :Input 71 geographic latitudes: res1=87.5,85,,.0..-87.5N 
C at: 0,1,...,72 geographic longitudes [-180,-175,...,180] = IONEX step
C Produce maps fc (foF2),  hc (hmF2), tc (TEC) for 0, 1,..., 23hr UT.syr  
C
C*********TEST
	 integer ifcf2(71,0:72)
	CHARACTER*2 AYR,AMN,DY1,DY2
	CHARACTER*3 DOY
	character*1 gh				 ! indicator = f(for fc), h(hc), or t(tc()
	DIMENSION GLATS(71),GLONS(0:72)
	 INTEGER*2 iyr,imn,idy,nndy,iut,ndyend
	+,iut0
C**********TEST
      CHARACTER*128 INFILE,outfile

	CHARACTER(10) DD
      CHARACTER(10) TT
      CHARACTER(5) ZZ
	CHARACTER*4 AYEAR,RYEAR
	CHARACTER*60 txt
	character*2 rmn,rdy,ady,ady0,RUT
	character*16 start,txt1
	integer*4 ixx(16)
	dimension itxx(0:72)

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/      WRITE(*,'(\A\)') ' ENTER YEAR OF INPUT FILE='
c/      READ(*,'(A)') AYEAR
	AYEAR='2018' !temp
		ayr=ayear(3:4)
c//      WRITE(*,'(\A\)') ' ENTER f, or h, or t for INPUT FILE='
c//      READ(*,'(A)') gh
	gh='h'
      WRITE(*,'(\A\)') ' ENTER MN OF INPUT FILE='
      READ(*,'(A)') AMN

		WRITE(*,*) 'Enter ndy1, ndy2 for input: '
 	read(*,987) nndy,ndyend
  987	format(I2)
	call blet2(nndy,DY1)
	call blet2(ndyend,DY2)
	ady0=DY1
C
C
	glat=-90.
	DO i=1,71
	glat=glat+2.5
	glats(i)=glat
	ENDDO
C
	glon=-195.
	do i=0,72
	glon=glon+5.0
	glons(i)=glon
	enddo
C++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

C
	IRUN=0 ! NORMAL COMMAND
	iut0=0 ! NORMAL COMMAND
C
crem	start='START OF TEC MAP'
crem	start='START OF foF2 MAP'
	start='START OF hmF2 MAP'
	infile='d:\web\dfs\yr\yrmndyf.txt'  ! ionex type daily file
crem	infile='d:\utec\YEAR\jplgDOY0.YRi'  ! ionex type daily file
crem	infile='d:\utec\YEAR\jhrgDOY0.YRi'  ! ionex type daily file
	outfile='d:\web\dfs\yr\mn\fcmndyut.jyr'  ! one map file
	infile(9:9)=gh
crem	infile(9:12)=ayear
	infile(12:13)=ayr
	infile(15:16)=ayr
crem	infile(23:24)=ayr
	infile(17:18)=amn
	infile(21:21)=gh
	outfile(9:9)=gh
	outfile(12:13)=ayr
	outfile(15:16)=amn
	outfile(18:18)=gh
	outfile(20:21)=amn
	outfile(28:29)=ayr

  118	format(A4)
cc	pause  ' '
C
C =========================================
c
	read(ayear,*) xyear
	IYYYY=int(xyear)
	read(ayr,*) xyr
	iyr=int(xyr)
c
	read(amn,*) xmn
	imn=int(xmn)
CC
	write(*,199) dy1
  199	format(' day=',A2)

	read(dy1,*) xdy
	idy1=int(xdy)
	read(dy2,*) xdy2
	idy2=int(xdy2)

c outfiles =================================
	idy=idy1
  80	call blet2(idy,ADY)
	outfile(22:23)=ady
	 lda=ndoy(iyr,imn,idy)
	call blet3(lda,DOY)
c//	infile(19:20)=ady
crem      infile(18:20)=DOY
	   infile(19:20)=ady
C++
	OPEN(111,FILE=INFILE,ACTION='READ')

   	do i=1,1200					             !!
	READ (111,187,END=33,ERR=2) txt,txt1
  187	format(A60,A16)
	if (txt1.eq.start) goto 88
	enddo									 !!
  88	continue

C
   11	continue     ! day-to-day cycle >>>>>>>>>>>>>>>
C
   27 format(A60)

C
c ***************************************************************
D start cycle on hr-to-hr input:

C Start of reading VTEC, or foF2, or hmF2 for UT ========================
  700 	read (111,89,end=9,err=85) ryear,rmn,rdy,rut
CADD++++++++++++++++++++++++++++++++++++++++++++++++
	if (rut(1:1).eq.' ') rut(1:1)='0'
	read(rut,*) xut
	iut=int(xut)

	outfile(24:25)=rut

   89 format(2X,A4,4X,A2,4X,A2,4X,A2)

   85	continue
      write(*,89) ryear,rmn,rdy,rut
	

C
C Start input for given UT:++++++++++++++++++++
C
C =========================================
C
	jt=0
C
C 
  	ilat=0
D start cycle on glatitudes:
	glati=90.0
	nnlat=72
C
	DO 777 lat=1,71	   ! Start cycle on glati ++++++++++++++++++++++++++++++++
		ilat=ilat+1   ! ilat=1 for 87.5
	glati=glati-2.5
	ALATI=glati        ! for iriplas
	 
c   77	DO kl=1,72                 ! cycle on latitudes
   87	read (111,90,end=9,err=2) glat,txt
   90	format(2X,F6.1,A60)
  91   	j1=0
C
C
	do j=1,4        
	j1=(j-1)*16
   1	READ (111,180,END=9,ERR=2) (ixx(i),i=1,16)
  180	format(16(1X,I4))
	do k=1,16
	if (ixx(k).le.0) ixx(k)=20
	itxx(j1+k-1)=ixx(k)
	enddo
	enddo			  ! start selection of 4 longitudes
	READ (111,179,END=9,ERR=2) (ixx(i),i=1,9)
  179	format(9(1X,I4))
	do k=1,9
	if (ixx(k).le.0) ixx(k)=20
	itxx(63+k)=ixx(k)
	enddo
CC
	nnlat=nnlat-1  ! Keep results for GLAT=87.5(-87.5) at nnlat=71(1)
	do kk=0,72
	ifcf2(nnlat,kk)=itxx(kk)
	enddo
C Select values for Long=-180,-165,...0,15,30,..,165,180!!!!!!!!!!!!:
C
	ii=-1
	 jj=71
C====================================================
c//	write(*,181) iut,ALATI
  777	continue   ! End cycle of reading all GLATs

  181 format(2X,I2,2X,F5.1)
C
C ==================================================
C
C --------------- 
C Prepare output of results for nnlong=0,1,...,24

		OPEN(12,FILE=OUTFILE)
	DO nnlat=1,71
		write(12,182) (ifcf2(nnlat,k),k=0,72)
	ENDDO
 182	format(73(1X,I4))							
	close(unit=12)
	if (iut.lt.24) then
	READ (111,187,END=33,ERR=2) txt,txt1
crem	READ (111,187,END=33,ERR=2) txt,txt1
	if (txt1.eq.start) then
	goto 11
	else
	goto 33
	endif
	endif
	 
DD  775 continue   ! End calculation for given UT
C
    9	continue
C Continue to the next UT hr :
  32		irun=irun+1 
C

C 
   33	close(unit=111)

  101 idy=idy+1
	irun=0
c	pause ' '
	if ((idy.le.idy2).and.(idy1.lt.idy2)) then
	goto 80
	else
		goto 35	   ! end of calculations
	endif
C
    2 CONTINUE
      if (irun.eq.0) then
       write(*,*) 'INPUT FILE IS NOT IN YOUR DIRECTORY '
 	pause ' '
	goto 35
      endif

   35      write(*,*) infile,outfile
	write(*,*) irun
C temp
      pause ' ' 
      STOP
       END

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