function [jj,ii]=sp2gc(cdf,x_sp,y_sp)
% SP2GC State Plane (or whatever coordinates the model 
%   uses) to Grid Coordinates
% 
%    Usage: [i,j]=sp2gc(cdf,x_sp,y_sp)
%       where  cdf = netCDF 4D output file name 
%              (x_sp,y_sp) = location in map coordinates
%                   used by model
%              (i,j) =  closest grid location in grid cell
%                      coordinates
%
if(nargin==2),
 y_sp=x_sp(:,2);
 x_sp=x_sp(:,1);
end
[name,xpos]=mcdinq(cdf,'xpos');
[name,ypos]=mcdinq(cdf,'ypos');
%
x=mcvgt(cdf,'x',[0 0],[ypos xpos]);
y=mcvgt(cdf,'y',[0 0],[ypos xpos]);
ii=zeros(size(x_sp));
jj=ii;
for i=1:length(x_sp);
 [index,dist]=nearxy(x(:),y(:),x_sp(i),y_sp(i)); 
 [m,n]=size(x);
 jj(i)=floor((index-1)/m)+1;
 ii(i)=rem(index,m);
 if(ii(i)==0),ii(i)=m,end;
end
