!--------------------------------------------------------------------
!  fnbint - flux surface fcn profile linear interpolation routine
!
real(fp) function fnbint(zxi,zf,idim1,idim2,invert,idummy,izi)
  !
  use nbi_com
  !
  !============
  implicit none
  integer :: idim1,idim2,invert,idummy,izi,ioff,imax,izip1
  !============
  real(fp) :: zxi,zxil,zdrat,zans,zansi
  !============
  real(fp) :: zf(idim1,idim2)
  !
  !  input:
  !
  !  zxi - flux coord location to which to linearly interpolate profile
  !  zf(idim1,idim2)  - profile to be interpolated -- variation is in
  !    dimension idim2
  !
  !    dimension idim1 spaces the data as desired
  !
  !   mj and mtbl are parameters in transp common:
  !
  !  if idim2 = mj its a plasma profile, not defined for xi.gt.1.0
  !  if imim2 = mtbl its a bloated profile, defined for xi.gt.1.0
  !
  !  invert - =1:  linear interpolation; -1: 1/(lin.int(1/f))
  !  idum - placeholder
  !
  !  output:
  !
  !  fnbint = interpolated profile value returned
  !
  !  izi = zone index into which the interpolation fell
  !        this will be in the plasma even if zxi.gt.1
  !
  !-----------------
  !
  !  range check  zxi normally btw 0 and 1; .gt. 1 is possible e.g.
  !  if n.b. ion or flr gyro crosses plasma bdy
  !
  if(zxi.lt.0.0_fp) then

     write(nonlin,9901) zxi
9901 format(' ?fnbint - passed "xi" coordinate .lt. 0.0: ',1pe12.5)
     !mg 04/22/2014
     izi=lcm1
     fnbint=zf(1,izi)
     return
  else if(zxi.gt.2.0_fp) then

     write(nonlin,9902) zxi
9902 format(' %fnbint - passed "xi" coordinate .gt. 2.0: ',1pe12.5)
     !mg 04/22/2014
     izi=lep1
     fnbint=zf(1,izi)
     return
  else if( ((idim2.ne.mj).and.(idim2.ne.mtbl)) .or. &
       (mj.eq.mtbl)                      ) then

     write(nonlin,9903) idim2
9903 format( &
          ' ?fnbint - dimension idim2=',i10,' .ne. known dimensions'/ &
          '   or common dimensions mj .eq. mtbl or undefined')
     !mg 04/22/2014 these settings may cause crash later on
     izi=0
     fnbint=0.0_fp
     return
  else
     !
     !  ok - regular or bloated profile?
     if(idim2.eq.mj) then
        ioff=lcentr
        zxil=min(zxi,1.0_fp)
        imax=ledge
     else
        ioff=1
        zxil=min(zxi,xminbm)
        imax=int(xminbm/dxi(lcentr,2)+0.5_fp)
     end if
     !
  end if
  !
  !  interpolation
  !
  izi=min(imax,(ioff+int(nzones*zxil)))
  izip1=izi+1
  !
  if(idim2.eq.mj) then
     zdrat=(zxil-xi(izi,1))/dxi(izi,2)
  else
     zdrat=(zxil-xiblo(izi+1))/dxi(lcentr,2)
  end if
  !
  if(invert.eq.1) then
     zans=zf(1,izi)+zdrat*(zf(1,izip1)-zf(1,izi))
  else
     zansi=1.0_fp/zf(1,izi)+ &
          zdrat*(1.0_fp/zf(1,izip1)-1.0_fp/zf(1,izi))
     zans=1.0_fp/zansi
  end if
  !
  fnbint=zans
  return
  !
end function fnbint
