subroutine nbsplb(zxii,zthi,iderivs,zbi,zbith,zbixi,zbith2,zbixi2,zbixith)
 
  use iso_c_binding, only: fp => c_double
  use nbspline_mod
 
  !  use bicubic splines to evaluate what was done by the legacy
  !  bintrp routine
 
  implicit none
  real(fp), intent(in) :: zxii,zthi           ! mag. coordinates in
  integer, intent(in) :: iderivs          ! =0: value only; =1: val & derivs.
  real(fp), intent(out) :: zbi,zbith,zbixi    ! b,db/dth,db/dxi
  real(fp), intent(out) :: zbith2,zbixi2,zbixith ! d2b/dth2,d2b/dxi2,d2b/dxidth
 
  !
 
  real(fp) zxi,zth,zval(6)
  integer iwantb(6),iwant0(6),ierr
  real(fp) zthi8,zth8
 
  data iwant0 /1,0,0,0,0,0/
  data iwantb /1,1,1,1,1,1/

  integer ith,ixi
  real(fp) xiparam(1),thparam(1)
 
  !---------------------------------------------
  ! normalize range
 
  zxi=max(xi(1),min(xi(nxi),zxii))
  if((zthi.ge.th(1)).and.(zthi.le.th(nth))) then
     zth=zthi
  else
     if(zthi.lt.th(1)) zth=zthi+thmax
     if(zthi.gt.th(nth)) zth=zthi-thmax
     if((zth.lt.th(1)).or.(zth.gt.th(nth))) then
        zthi8=zthi
        zth8=mod(zthi8,thmax)
        if(zthi8.lt.0) zth8=zth8+thmax
        zth=zth8
        zth=max(th(1),min(th(nth),zth))
     endif
  endif
 
  ixi=min( (nxi-1), int(1+xspani*(zxi-xi(1))*(nxi-1)) )
  if(xi(ixi).gt.zxi) then
     ixi=ixi-1
  endif
  if(xi(ixi+1).lt.zxi) then
     ixi=min((nxi-1),(ixi+1))
  endif
  xiparam=(zxi-xi(ixi))/(xi(ixi+1)-xi(ixi))
 
  ith=min( (nth-1), int(1+thspani*(zth-th(1))*(nth-1)) )
  if(th(ith).gt.zth) then
     ith=max(1,(ith-1))
  endif
  if(th(ith+1).lt.zth) then
     ith=min((nth-1),(ith+1))
  endif
  thparam=(zth-th(ith))/(th(ith+1)-th(ith))
 
  nbsii=ith
  nbsjj=ixi
 
  if(iderivs.eq.0) then
     call fvbicub(iwant0,1,1,zval,nbsii,nbsjj,thparam,xiparam, &
          hth,hthi,hxi,hxii,bspl,nth,nxi)
  else
     call fvbicub(iwantb,1,1,zval,nbsii,nbsjj,thparam,xiparam, &
          hth,hthi,hxi,hxii,bspl,nth,nxi)
  endif
 
  zbi=zval(1)
  if(iderivs.ne.0) then
     zbith=zval(2)
     zbixi=zval(3)
     zbith2=zval(4)
     zbixi2=zval(5)
     zbixith=zval(6)
  endif
 
end subroutine nbsplb
! 11sep2004 fgtok -s r8_precision.sub nubeam_test_r8.sub "r8con.csh conversion"
! 11sep2004 fgtok
