function stat=cts_write(fid,cts,iiclose)
%
% Call stat=cts_write(fid,cts,iiclose);
%
% where
% fid       = file ID number (output of cts_write_open);
% cts       = matrix of size [nrec,npt] containing npt complex time series samples at nrec receivers;
% iiclose   = 1 to close file after writing out cts;
%
% See:  cts_details, cts_open, cts_read, cts_write, cts_write_open, cts_combine, cts2fft, cts2spec, 
%       cts_from_real, cts_shot, cts_wf_make, fft2cts, mcs_gui.
%
%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
if nargin<3, iiclose=0; end
%
[nrec,npt]=size(cts);
nprod=nrec*npt;
zz=[reshape(real(cts),1,nprod); reshape(imag(cts),1,nprod)];		% 2 by nrec*npt real numbers
% Write out data multiplexed in receiver number, with (real,imag) for each data point:
count=fwrite(fid,zz,'real*4');
if count<2*nprod, stat=-1; else, stat=0; end
%
if iiclose, fclose(fid); end
