function [fid,cts_hdr]=cts_write_open(cts_root,Jday,Jhour,nrec,fs,f_bb,ii_type,val_type)
%
% Call [fid,cts_hdr]=cts_write_open(cts_root,Jday,Jhour,nrec,fs,f_bb,ii_type,val_type);
%
% INPUTS:
% cts_root      = Output file root for .cts file;
% Jday,Jhour    = Julian day,hour of starting sample;
% nrec          = Number of receivers;
% fs            = Sample rate in Hz;
% f_bb          = Basebanding frequency used to obtain complex time series;
% ii_type       = Type of data contained in file (0-10) [Types used so far: 1-5 (D,E,B,A,WF)];
% val_type      = Value representing level of the type of data (e.g. AN for ambient noise);
%
% NOTE: ii_type and val_type are useful when re-combining simulated CTS files (see cts_combine).
%
% OUTPUTS:
% fid           = File ID number of opened CTS file;
% cts_hdr       = CTS header written at start of output CTS file;
%
% 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<7, ii_type=1; end
if nargin<8, val_type=-999; end
%
cts_hdr=[0,Jday,Jhour,nrec,fs,f_bb,ii_type,-999*ones(1,10)];
if ii_type<0 | ii_type>10, error('Illegal type in cts_write_open'); end
% Types used so far: 1-6 (D,E,B,A,WF,INT);
cts_hdr(7+ii_type)=val_type;
cts_hdr(1)=length(cts_hdr);
cts_file=[cts_root,'.cts'];
% Open with 'ieee-be' (big endian) so that Unix Fortran can read more easily (03-08-01):
fid=fopen(cts_file,'w','ieee-be');
count=fwrite(fid,cts_hdr,'real*4');
%
% ...
%
% To write data:
% Creat pser as a complex array nrec (# receivers) by npt (# time samples) array:
%nprod=nrec*npt;
%zz=[reshape(real(pser),1,nprod); reshape(imag(pser),1,nprod)];		% 2 by nrec*npt real numbers
% Write out multiplexed in receiver number, with (real,imag) for each data point:
%count=fwrite(fid,zz,'real*4');
