function cmean2(cdfsin,cdfout)
%function cmean2(cdfsin,cdfout)
%
% CMEAN2 computes mean concentration from step 0-2 of multiple cdf files
%   used for computing the mean from several monthly means.
%     
%  tind is the indices of time steps to average over (1 is first step)
% 

mexcdf('setopts',0);
cdfid2=mexcdf('open',cdfout,'write');
[m,n]=size(cdfsin);
cdfid1=mexcdf('open',cdfsin(1,:),'nowrite');
[nam,nx]=mexcdf('diminq',cdfid1,'xpos');
[nam,ny]=mexcdf('diminq',cdfid1,'ypos');
[nam,nz]=mexcdf('diminq',cdfid1,'zpos');
mexcdf('close',cdfid1);
for i=1:m,
  cdfid(i)=mexcdf('open',cdfsin(i,:),'nowrite');
end
for j=[1:nz-1],
  ctot1=zeros(nx,ny);
  ctot2=zeros(nx,ny);
  ctot3=zeros(nx,ny);
  for i=1:m,
    c1=mexcdf('varget',cdfid(i),'conc',[0 j-1 0 0],[1 1 ny nx]);
    c2=mexcdf('varget',cdfid(i),'conc',[1 j-1 0 0],[1 1 ny nx]);
    c3=mexcdf('varget',cdfid(i),'conc',[2 j-1 0 0],[1 1 ny nx]);
    ctot1=ctot1+c1;
    ctot2=ctot2+c2;
    ctot3=ctot3+c3;
  end
  cmean1=ctot1/m;
  cmean2=ctot2/m;
  cmean3=ctot3/m;
% store mean field in cdfout
  mexcdf('varput',cdfid2,'conc',[0 j-1 0 0],[1 1 ny nx],cmean1);
  mexcdf('varput',cdfid2,'conc',[1 j-1 0 0],[1 1 ny nx],cmean2);
  mexcdf('varput',cdfid2,'conc',[2 j-1 0 0],[1 1 ny nx],cmean3);
end
mexcdf('close',cdfid2);
for i=1:m,
  mexcdf('close',cdfid(i));
end
