!-----------------------------------------------------------------------
!  nb_locord  -- binary search routine
!
integer function nb_locord(zdata,iorder,inum,ztest)
  !
  !  input:
  !============
  use iso_c_binding, only: fp => c_double
  implicit none
  integer :: inum,ilo,ihi,itry
  !============
  real(fp) :: zdata(inum)  ! data to be searched
  integer :: iorder(inum)  ! ordering on data searched
  real(fp) :: ztest	  ! search value
  !
  !  output:
  !       nb_locord= pointer to ordering of ztest with respect to passed
  !         data zdata as ordered by iorder
  !
  !       if nb_locord=1, ztest is .lt. zdata(iorder(1))
  !       if nb_locord=5, ztest is .ge. zdata(iorder(4)) and
  !                             .lt. zdata(iorder(5))
  !       if nb_locord=inum+1, ztest is .ge. zdata(iorder(inum))
  !
  !-------------------------------------
  !
  if(ztest.ge.zdata(iorder(inum))) then
     nb_locord=inum+1
     return
  else
     !
     ilo=1
     ihi=inum
     !
     do while (ilo.le.ihi)
        if(ilo.eq.ihi) then
           nb_locord=ihi
           return
        end if
        !
        itry=(ilo+ihi)/2
        !
        if(ztest.lt.zdata(iorder(itry))) then
           ihi=itry
        else
           ilo=itry+1
        end if
     end do
  end if
end function nb_locord
