      program dipinterp
C............................................................Feb 2023
C
      DIMENSION DIPS(360),glons(360),glats(360),tecs(360),ivs(360)
	CHARACTER*64 infile,infile1,outfile1,outfile2
	CHARACTER*4 AYEAR
	CHARACTER*2 AMN,ADY,BMN,BDY
C
Cnorm      infile='c:\Msdev\Projects\Maps\Output\TermYEARMNDDn.dat'
      infile='c:\Msdev\Projects\Maps\Output\TermYEARMNDD.dat' !Feb03/2022
	infile1='dip180.txt'
   30 FORMAT(1X,F6.1,1X,F7.2)
C
	OPEN(13,file=infile1,action='READ')
	do kk=1,360
	read(13,30,end=51) glons(kk),DIPS(kk)
	enddo
   51	close(13)
C
	AYEAR='2022'  !TEMPPP
C=	AYEAR='2021'  !TEMPPP
	write(*,*) ' Enter AMN,ADY:'
	read(*,*) AMN,ADY
C
	outfile1='dipYEARMNDDss.txt'
      outfile2='dipYEARMNDDsr.txt'
	outfile1(4:7)=AYEAR
      outfile1(8:9)=AMN
	outfile1(10:11)=ADY
	outfile2=outfile1
	outfile2(13:13)='r'
      OPEN(11,file=outfile1)
	OPEN(12,file=outfile2)
C
	infile(35:38)=AYEAR
	infile(39:40)=AMN
	infile(41:42)=ADY
  31  format(3(1X,F6.1),1X,F7.1,1X,I4)
  33  format(2X,2I2,2X,2I2)
  34  format(2(1X,F6.1),1X,F7.1,1X,I4)
	OPEN(10,file=infile,action='READ')
C
         DO 45 iut=0,23
C
          DO 44 imm=0,45,15
C
        read(10,33) kMN,kDY,kut,kmm
C
	DO i=1,360
Cnorm	read(10,31,end=52) gloni,glats(i),glat00,tecs(i),ivs(i)
	read(10,34,end=52) gloni,glats(i),tecs(i),ivs(i) !03Feb2022
	enddo
C
C
       glon=-180.5
	delss=100.
	delsr=100.
C Select min deviation of glatSR-glatDIP
	DO 40 ii=1,360
	glon=glon+1.0
C
C      delii=abs(abs(DIPS(ii))-abs(glats(ii)))
      delii=abs(DIPS(ii)-glats(ii))
	if ((glats(ii+1).lt.glats(ii)).and.(delii.lt.delsr)) then
	 delsr=delii
	ksr=ii
	endif
	if ((glats(ii+1).gt.glats(ii)).and.(delii.lt.delss)) then
	 delss=delii
	kss=ii
	endif
   40	CONTINUE
C	
C Output TEC(ksr), Vs(ksr
C 
C      glatx=DIPS(2,k1)+(DIPS(2,k2)-DIPS(2,k1))/(DIPS(1,k2)-DIPS(1,k1))*
C	+(glon-DIPS(1,k1))
	write(*,32) AYEAR,AMN,ADY,kut,kmm,glons(kss),glats(kss)
	+,tecs(kss),ivs(kss)
	write(11,32) AYEAR,AMN,ADY,kut,kmm,glons(kss),glats(kss)
	+,tecs(kss),ivs(kss)
	write(*,32) AYEAR,AMN,ADY,kut,kmm,glons(ksr),glats(ksr)
	+,tecs(ksr),ivs(ksr)
	write(12,32) AYEAR,AMN,ADY,kut,kmm,glons(ksr),glats(ksr)
	+,tecs(ksr),ivs(ksr)
   44 CONTINUE
   45 CONTINUE
   32 FORMAT(1X,A4,1X,2A2,2(1X,I2),2(1X,F7.2),1X,F6.1,1X,I4)
  52  close(10)
       close(11)
	close(12)
      pause ' '
	STOP
	END
