


SP2GC State Plane (or whatever coordinates the model
uses) to Grid Coordinates
Usage: [i,j]=sp2gc(cdf,x_sp,y_sp,[iwater])
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
[include iwater=1 if you want only water points to
be considered]

0001 function [jj,ii]=sp2gc(cdf,x_sp,y_sp,iwater) 0002 % SP2GC State Plane (or whatever coordinates the model 0003 % uses) to Grid Coordinates 0004 % 0005 % Usage: [i,j]=sp2gc(cdf,x_sp,y_sp,[iwater]) 0006 % where cdf = netCDF 4D output file name 0007 % (x_sp,y_sp) = location in map coordinates 0008 % used by model 0009 % (i,j) = closest grid location in grid cell 0010 % coordinates 0011 % [include iwater=1 if you want only water points to 0012 % be considered] 0013 if(nargin==2), 0014 y_sp=x_sp(:,2); 0015 x_sp=x_sp(:,1); 0016 end 0017 if(nargin==3), 0018 iwater=0; 0019 end 0020 [d,x,y]=kslice(cdf,'depth'); 0021 % 0022 dind=find(~isnan(d)); 0023 ii=zeros(size(x_sp)); 0024 jj=ii; 0025 for i=1:length(x_sp); 0026 if(iwater), 0027 [index,dist]=nearxy(x(dind),y(dind),x_sp(i),y_sp(i)); 0028 index=dind(index); 0029 else 0030 [index,dist]=nearxy(x(:),y(:),x_sp(i),y_sp(i)); 0031 end 0032 [m,n]=size(x); 0033 ii(i)=floor((index-1)/m)+1; 0034 jj(i)=rem(index,m); 0035 if(jj(i)==0),jj(i)=m,end; 0036 end