function [i,j,dist] = roms_closest(grd,xi,yi)
%       [I,J,dist] = roms_closest(grd,XI,YI)
%
%	finds I,J indicies of ROMS lon_rho and lat_rho coordinate arrays in the 
% grid structure that give the point closest to the input points(s) XI,YI.
%	The point(s) of interest XI,YI may be specified as a pair
%	points, a pair of vectors, or a matrix XY with two columns
%	i.e. [I,J]=CLOSEST(X,Y,XY).  This last option allows the
%	direct output of GINPUT to be used as the input XY,
%	e.g. [I,J]=CLOSEST(X,Y,GINPUT)
%
%	John Wilkin, 4 November 93

if isstruct(grd)~=1
  error('First input should be a roms grid structure (see roms_get_grid)')
end

X = grd.lon_rho;
Y = grd.lat_rho;

if nargin == 2
  yi = xi(:,2);
  xi = xi(:,1);
end

for k=1:length(xi)
  dist = abs( (xi(k)-X) + sqrt(-1)*(yi(k)-Y));
  [ii,jj] = findm2(dist==min(dist(:)));
  i(k) = ii;
  j(k) = jj;
  dist = min(dist(:));
end
