C******************** START FILE ECNTER.FOR ; GROUP ECINTR ******************
C================================================================
C  ECNTER
C
C  INTERPOLATE ECE ELECTRON TEMPERATURE TOKAMAK DATA DMC JAN 1984
C
C  RETURN THE ELECTRON TEMPERATURE
C
      REAL FUNCTION ECNTER(TIM0,RAD0,T,NT,F,NF,TE,NT1,
     >                        IHARM,RB,TB,NRB,ISTAT)
C
      REAL T(NT),F(NF),TE(NT1,NF),RB(NRB),TB(NRB)
C
C  INPUT DATA
C  ----------
C   TIM0  TIME AT WHICH ELECTRON TEMPERATURE IS DESIRED (SECONDS)
C   RAD0  MAJOR RADIUS AT WHICH ELECTRON TEMPERATURE IS DESIRED (CM)
C
C   T(NT)  ECE ELECTRON TEMPERATURE DATA TIME VECTOR (SECONDS)
C   F(NF)  ECE TE DATA DETECTOR FREQUENCIES (GHZ) RELATED TO POSITION
C          (MAJOR RADIUS RAD0) VIA MAGNETIC FIELD STRENGTH
C   TE(NT1,NF)
C          ECE ELECTRON TEMPERATURE DATA (TIME CONTIGUOUS ORGANIZATION)
C          NOTE NT1 MAY BE .GT. NT
C
C   IHARM  ECE HARMONIC DETECTED (1 2 OR 3) ***
C
C   RB(NRB)
C          MAJOR RADIUS * TOROIDAL FIELD STRENGTH (TESLA * CM)
C          TIME DEPENDENT BUT INDEPENDENT OF POSITION (IGNORING
C          PLASMA PARA/DIAMAGNETISM)
C   TB(NRB)
C          TIME VECTOR ASSOCIATED WITH RB DATA
C
C  OUTPUT DATA
C  -----------
C   ECNTER ELECTRON TEMPERATURE AT REQUESTED RADIUS, TIME
C   ISTAT  =0 NORMAL EXIT CODE
C          =  SUM
C           +1 IF EXTRAPOLATION IN ECE TIME
C           +2 IF EXTRAPOLATION IN RB TIME
C           +4 IF EXTRAPOLATION IN ECE POSITION (<--> FREQUENCY)
C
C  FLAT EXTRAPOLATION IS USED THROUGHOUT
C
C--------------------------------------------------------------------
C
C  PROCEDURE:
C   (1)  CALCULATE ECE TIME INTERPOLATION FACTOR
C   (2)  CALCULATE RB TIME INTERPOLATION FACTOR
C        AND RB AT THE REQUESTED TIME TIM0
C   (3)  CALCULATE RADII CORRESPONDING TO ECE DETECTOR FREQUENCIES
C        FOR CALCULATED VALUE OF RB
C   (4)  CALCULATE POSITION INTERPOLATION FACTOR
C   (5)  INTERPOLATE DATA AND EXIT
C
C--------------------------------------------------------------------
C  STATEMENT FUNCTION:  POSITION (MAJOR RADIUS) AS A FUNCTION OF
C  RB=RADIUS*TOROIDAL FIELD (TESLA*CM), F=ECE FREQUENCY (GHZ),
C  AND IHARM=DETECTED HARMONIC
C
C  RESULT IS RADIUS IN CM
C
      RADIUS(ZF,ZRB,JHARM)=28.0*ZRB*JHARM/ZF
C
C--------------------------------------------------------------------
C
C  BEGIN
C   CLEAR STATUS INDICATOR
C
      ISTAT=0
C
C  ECE TIME INTERPOLATION FACTOR
C
      IT0=MIN0(NT,MAX0(1,IT0))
      CALL ECINTR(TIM0,T,NT,IT0,IT0P,XIT0,XIT0C,IEX)
      IF(IEX.GT.0) ISTAT=ISTAT+1
C
C  RB TIME INTERPOLATION FACTOR
C
      ITRB=MIN0(NRB,MAX0(1,ITRB))
      CALL ECINTR(TIM0,TB,NRB,ITRB,ITRBP,XIRB,XIRBC,IEX)
      IF(IEX.GT.0) ISTAT=ISTAT+2
C
C  CURRENT RADIUS*TOROIDAL FIELD
C
      RB0=RB(ITRB)*XIRBC+RB(ITRBP)*XIRB
C
C  ECE POSITION INTERPOLATION FACTOR
C
C  ASSUME RADIUS FUNCTION IS MONOTONIC VS. INDEX BUT DONT KNOW IF
C  IT IS INCREASING OR DECREASING
C
      R1=RADIUS(F(1),RB0,IHARM)
      R2=RADIUS(F(NF),RB0,IHARM)
      IF(R2.GT.R1) THEN
        IDIR=1
      ELSE
        IDIR=-1
      ENDIF
C
C  CHECK FOR EXTRAPOLATION
C
      ZTEST1=IDIR*(R1-RAD0)
      ZTEST2=IDIR*(RAD0-R2)
      IF(ZTEST1.GE.0.0) THEN
C  RAD0 AT OR BELOW LOWEST INDEX
        IR0=1
        XIR0=0.0
        IF(ZTEST1.GT.0.0) ISTAT=ISTAT+4
      ELSE IF(ZTEST2.GE.0.0) THEN
C  RAD0 AT OR ABOVE HIGHEST INDEX
        IR0=NF
        XIR0=0.0
        IF(ZTEST2.GT.0.0) ISTAT=ISTAT+4
      ELSE
C  INBETWEEN, CARRY OUT SEARCH
C   INITIAL GUESS
        IR=IFIX(1.0+(FLOAT(NF)-1.0)*(RAD0-R1)/(R2-R1))
        R=RADIUS(F(IR),RB0,IHARM)
        ZTEST=(R-RAD0)*IDIR
        IF(ZTEST.LT.0) THEN
C  SCAN UP IN INDEX
          DO WHILE(ZTEST.LT.0)
            IR=IR+1
            RP=R
            R=RADIUS(F(IR),RB0,IHARM)
            ZTEST=(R-RAD0)*IDIR
          ENDDO
          IR0=IR-1
          XIR0=(RAD0-RP)/(R-RP)
        ELSE IF(ZTEST.GT.0) THEN
C  SCAN DOWN IN INDEX
          DO WHILE(ZTEST.GT.0)
            IR=IR-1
            RP=R
            R=RADIUS(F(IR),RB0,IHARM)
            ZTEST=(R-RAD0)*IDIR
          ENDDO
          IR0=IR
          IR=IR+1
          XIR0=(RAD0-R)/(RP-R)
        ELSE
C  BULL'S EYE
          IR0=IR
          XIR0=0.0
        ENDIF
      ENDIF
C  FINISH UP DEFN OF R INTERPOLATION
      XIR0C=1.0-XIR0
      IR0P=MIN0((IR0+1),NF)
C
C  INTERPOLATED ECE TEMPERATURE
C
      ECNTER=XIT0C*(TE(IT0,IR0)*XIR0C+TE(IT0,IR0P)*XIR0) +
     >         XIT0*(TE(IT0P,IR0)*XIR0C+TE(IT0P,IR0P)*XIR0)
C
      RETURN
      END
C******************** END FILE ECNTER.FOR ; GROUP ECINTR ******************
