function speed(cdfin,cdfout,tind)
%function Speed(cdfin,cdfout,tind)
%
% SPEED computes average, max and min speed 
%    of a ecomsi model run.  cdfin is the ecomsi.cdf run, and
%    cdfout is a dummy ecomsi.cdf run that has at least three time
%    steps of varout that you don't mind writing over.  The mean value
%    is stored in the first time step, max in the 2nd time step, and
%    min in the 3rd time step 
%  
%  tind is the indices of time steps to average over (1 is first step)
cdfid1=mexcdf('open',cdfin,'nowrite');
cdfid2=mexcdf('open',cdfout,'write');

[nam,nx]=mexcdf('diminq',cdfid1,'xpos');
[nam,ny]=mexcdf('diminq',cdfid1,'ypos');
[nam,nz]=mexcdf('diminq',cdfid1,'zpos');

for j=[1:nz-1],
  wtot=zeros(nx,ny);
  wmax=-1.e35*ones(nx,ny);
  wmin=1.e35*ones(nx,ny);
  for i=tind,
    u=mexcdf('varget',cdfid1,'u',[i-1 j-1 0 0],[1 1 ny nx]);

    v=mexcdf('varget',cdfid1,'v',[i-1 j-1 0 0],[1 1 ny nx]);

    w=abs(u+sqrt(-1)*v);
    wmax=max(w,wmax);
    wmin=min(w,wmin);
    wtot=wtot+w;
  end
  wmean=wtot/length(tind);
% store speed in 1st, 2nd, 3rt time step of u field cdfout
  mexcdf('varput',cdfid2,'u',[0 j-1 0 0],[1 1 ny nx],wmean);
  mexcdf('varput',cdfid2,'u',[1 j-1 0 0],[1 1 ny nx],wmax);
  mexcdf('varput',cdfid2,'u',[2 j-1 0 0],[1 1 ny nx],wmin);
end
mexcdf('close',cdfid1);
mexcdf('close',cdfid2);
