function transport(cdfin,cdfout,tind)
%function transport(cdfin,cdfout,tind)
%
% TRANSPORT computes the tranport of u,v,w over time steps tind 
%    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
%  
%  tind is the indices to do the tranport calculation (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');

h=mexcdf('varget',cdfid1,'depth',[0 0],[ny nx]);
h=replace(h,-99999.,0);
ind=find(h>0);
hmean=mean(h(ind))
wmean=zeros(nx,ny);
for i=tind,
  utot=zeros(nx,ny);
  vtot=zeros(nx,ny);
  for j=[1:nz-1],
    u=mexcdf('varget',cdfid1,'u',[i-1 j-1 0 0],[1 1 ny nx]);
    utot=utot+u;

    v=mexcdf('varget',cdfid1,'v',[i-1 j-1 0 0],[1 1 ny nx]);
    vtot=vtot+v;
  end
  umean=utot.*h/(hmean*(nz-1));
  vmean=vtot.*h/(hmean*(nz-1));
% store mean,max,min u field in 1st and 2nd levels of time step tind
  mexcdf('varput',cdfid2,'u',[i-1 0 0 0],[1 1 ny nx],umean);
  mexcdf('varput',cdfid2,'u',[i-1 1 0 0],[1 1 ny nx],umean);
  mexcdf('varput',cdfid2,'v',[i-1 0 0 0],[1 1 ny nx],vmean);
  mexcdf('varput',cdfid2,'v',[i-1 1 0 0],[1 1 ny nx],vmean);
  mexcdf('varput',cdfid2,'w',[i-1 0 0 0],[1 1 ny nx],wmean);
  mexcdf('varput',cdfid2,'w',[i-1 1 0 0],[1 1 ny nx],wmean);
end
mexcdf('close',cdfid1);
mexcdf('close',cdfid2);
