function speed=surfspd(infile)
% speed=surfspd(infile)
% Calculates maximum surface speed by linearly extrapolating using the top two
% grid cells from the ECOM-SI model.  Infile is the netCDF file from
% which the surface velocities are to be calculated. This is an
% ECOM-SI-specific m-file.

[dummy,IM]=mcdinq(infile,'xpos');
[dummy,JM]=mcdinq(infile,'ypos');
[dummy,KB]=mcdinq(infile,'zpos');
[dummy,times]=mcdinq(infile,'time');
sigma=mcvgt(infile,'sigma',[0],[KB]);
f=(sigma(1)+sigma(2))/(sigma(1)-sigma(3));
speed=zeros(times,IM*JM);
for i=0:times-1;
  u1=mcvgt(infile,'u',[i 0 0 0],[1 1 JM IM]);
  u2=mcvgt(infile,'u',[i 0 0 0],[1 1 JM IM]);
  u = (u2-u1)*f+u1;
  u = remat(u,JM);
  ushift = zeros(u);
  ushift(1:IM-1,1:JM-1) = u(2:IM,2:JM);
  u = .5*(u+ushift);
  u = u(:)';
  v1= mcvgt(infile,'v',[i 1 0 0],[1 1 JM IM]);
  v2= mcvgt(infile,'v',[i 1 0 0],[1 1 JM IM]);
  v = (v2-v1)*f+v1;
  v = remat(v,JM);
  vshift = zeros(v);
  vshift(1:IM-1,1:JM-1) = v(2:IM,2:JM);
  v = .5*(v+vshift);
  v = v(:)';
  speed(i+1,:)=sqrt(u.*u+v.*v);
end
speed=remat(max(speed),JM);
