subroutine rygrad(zdfdxi,zdfdth,zdfdr,zdfdy)
  !
  !  dmc apr 2007: this is called only from nbdebug/chkspd.for
  !                which arranges explicitly for xbjaco to be defined.
  !
  !  given the derivatives of f(xi,theta), evaluate the grad(f) vector
  !  in (r,y) cartesian space.
  !
  !  the jacobian elements from xbjaco(2,2) are used:
  !
  !  xbjaco(1,1) = (dr/dxi)    xbjaco(1,2) = (dr/dth)
  !  xbjaco(2,1) = (dy/dxi)    xbjaco(2,2) = (dy/dth)
  !
  !  this can be inverted in the usual way to yield:
  !
  !  d = (dr/dxi)*(dy/dth) - (dr/dth)*(dy/dxi)
  !
  !  ( dxi/dr  dxi/dy )  =  (  1/d*(dy/dth) -1/d*(dr/dth) )
  !  ( dth/dr  dth/dy )     ( -1/d*(dy/dxi)  1/d*(dr/dxi) )
  !
  !  from which (by use of chain rule) the df/dr and df/dy components
  !  are obtained.
  !
  !  the determinant is assumed nonzero without checking.
  !
  use nbi_com
  !
  !-----------------------------------
  !
  !  2x2 determinant
  !
  !============
  implicit none
  real(fp) :: zdfdth,zdfdr,zdfdy,zdfdxi,zdet
  !============
  zdet=xbjaco(1,1)*xbjaco(2,2) - xbjaco(1,2)*xbjaco(2,1)
  !
  zdfdr = (zdfdxi*xbjaco(2,2) - zdfdth*xbjaco(2,1))/zdet
  zdfdy = (-zdfdxi*xbjaco(1,2)+ zdfdth*xbjaco(1,1))/zdet
  !
  return
end subroutine rygrad
