function CTS=cts_open(cts_root);
%
% Call CTS=cts_open(cts_root);
%
% INPUTS:
% cts_root      = Root of .cts file.  Also searches for cts_root by itself.
%
% OUTPUTS:
% CTS           = Structure containing information on CTS file:
%   fs              = Sample rate;
%   fid_cts         = File ID number; 
%   header          = Entire header;
%   nrec            = Number of receiver;
%   Jday            = Julian day of start time; 
%   Jhour1,Jhour2   = Start, end hours of day Jday;
%   npt             = Number of samples in file;
%   ieee            = 'le' or 'be' for ieee big or little endian;
%
% 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.
%
if nargin<1, cts_root=[]; end
% NOTE: Try 'ieee-le' first and then 'ieee-be':
if isempty(cts_root),
    [cts_file, pathname] = uigetfile('*.cts', 'Pick an CTS file ...');
    if isempty(cts_file), disp('Returning.'); end
    cts_root=cts_file(1:end-4);
end
cts_file=[cts_root,'.cts'];
if ~exist(cts_file,'file'),
    cts_file=cts_root;
    if ~exist(cts_file,'file'),
        CTS.fid_cts=-1;
        disp(['Could not find CTS file ',cts_file,' or ',[cts_root,'.cts'],'  in directory ',pwd]);
	    return
    end
end
% NOTE: Try 'ieee-le' first and then 'ieee-be':
[fid_cts,msg]=fopen(cts_file,'r','ieee-le');	% Open file assuming PC wrote it
ieee='le';
if fid_cts<0,
	fid_cts,msg,
	error(['Trouble opening CTS file ',cts_file,' in directory ',pwd]);
	return
end
status=fseek(fid_cts,0,'eof');				% Go to end of file
nbyte_file=ftell(fid_cts);					% # bytes in file
status=fseek(fid_cts,0,'bof');				% Rewind
%
% Read in cts file header:
fmt='real*4'; nbyte_sample=8;		% Two real*4 numbers (real and imag) for each sample
[nword_hdr,count]=fread(fid_cts,1,fmt);
if nword_hdr<6 | round(nword_hdr)~=nword_hdr | nword_hdr>1000,
	fclose(fid_cts);
% Try opening file assuming UNIX or Mac wrote it:
	[fid_cts,msg]=fopen(cts_file,'r','ieee-be');
	[nword_hdr,count]=fread(fid_cts,1,fmt);  ieee='be';
	if nword_hdr<6 | round(nword_hdr)~=nword_hdr | nword_hdr>1000,
		disp('BAD CTS FILE HEADER: nword_hdr = '); disp(nword_hdr); return
%	else
%		disp('CTS file found to be created from UNIX or Mac (''ieee-be'').');
	end
end
%
fseek(fid_cts,0,-1);
[cts_hdr,count]=fread(fid_cts,nword_hdr,fmt);
%disp(num2str(cts_hdr)),
Jday=cts_hdr(2); Jhour=cts_hdr(3);
nrec=cts_hdr(4); fs=cts_hdr(5); f_bb_shift=cts_hdr(6); 
if nword_hdr>6, fac_bb=cts_hdr(7); end;
%
nsamp_file=(nbyte_file - nword_hdr*4)/(nrec*nbyte_sample);
Thour_file=nsamp_file/fs/3600;
Jhour_end=Jhour + Thour_file;

CTS.cts_file=cts_file;
CTS.fid_cts=fid_cts;
CTS.header=cts_hdr;
CTS.nrec=nrec;
CTS.Jday=Jday; CTS.Jhour1=Jhour; CTS.Jhour2=Jhour_end;
CTS.T_lims=[Jhour Jhour_end];
CTS.fs=fs; CTS.f_bb_shift=f_bb_shift;
CTS.npt=(nbyte_file/4 - nword_hdr)/(2*nrec);
CTS.ieee=ieee;
%
% 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.
