function [DATA]=cts_extract(cts_root,T_int,t_lims,ph_lims);
%
% Call [DATA]=cts_extract(cts_root,T_int,t_lims,ph_lims);
%
% T_int		= FFT Integration time (T_int<0 ==> set T_int>=|T_int| such power of 2 NFFT);
% t_lims	= Time interval to output [t1,t2] in hours of day ([] = all time);
% ph_lims	= Phone numbers to output ([] = all phones);
%
% 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<2, T_int=10; end
if nargin<3 | isempty(t_lims), t_lims=[-24,48]; end
if nargin<4, ph_lims=[]; end
%
% Check for existence of input data file:
[data_file,cts_root,iif,path_back]=adi_check_file(cts_root,'.cts','CTS FILE');
if ~iif, disp('Exiting cts_extract ...'); return; end
%
t0 = clock;
CTS=cts_open(cts_root);
%
nrec=CTS.nrec; fs=CTS.fs; dt=1/fs; 
% For T_int<0, set T_int to get nT_int a power of 2:
if T_int<0, T_int=dt * 2^ceil(log2(-T_int/dt)); end
nT_int=round(T_int/dt); T_int=nT_int*dt;
if isempty(ph_lims),
   iirec=[1:nrec];
else
   [nr,nc]=size(ph_lims);
   if nr==1 & nc==2, iirec=[ph_lims(1):ph_lims(2)];
   elseif nc==1, iirec=ph_lims';
   else, disp('USER ERROR: ph_lims should be [jrmin,jrmax] or [jr1;jr2;...jrn]'); return;
   end
end
nrec_out=length(iirec);     %disp(iirec),
if any(iirec<1) | any(iirec>nrec),
	disp(ph_lims); error('Illegal receiver specification in cts2fft');
	return
end
rec_indx_re=(iirec-1)*2 + 1; rec_indx_im=rec_indx_re + 1;
% Position file to starting point:
nT_t1=round(3600*(t_lims(1)-CTS.Jhour1)/dt);
fmt='real*4'; nbyte_sample=8;
nbyte=nrec*nbyte_sample*nT_t1;		% nbyte_sample bytes per sample
if nbyte>0,
	fseek(CTS.fid_cts,nbyte,0);		% Seek from current position since cts file has header
else
	t_lims(1)=CTS.Jhour1;
end
% Adjust last requested time so that projected CPU times are valid:
t_lims(2)=min(t_lims(2),CTS.T_lims(2));
t_hr=t_lims(1);
ntime=floor(diff(t_lims)*3600/T_int);
%
DATA.title=cts_root;
DATA.ndim=3; DATA.dims=[nT_int,nrec_out,ntime];
DATA.labels={'Time - s', 'Receiver No', ['Time - hour of day ',int2str(CTS.Jday)]};
DATA.scales={[0:nT_int-1]*dt, iirec, t_hr + [0:ntime-1]*T_int/3600};
DATA.dlab='Pressure - \muPa'; DATA.iidB=0;
% Read in first column's worth for all receivers:
npt_read=2*nrec*nT_int;
for jtime=1:ntime,
    [pser,count]=fread(CTS.fid_cts,[2*nrec,nT_int],fmt);
	if count<npt_read, disp('Unexpected EOF in cts_extract'); keyboard; end
    DATA.data(:,:,jtime)=pser(rec_indx_re,:)' + i*pser(rec_indx_im,:)';		% pser is nT_int by nrec_out complex;
end
%
fclose(CTS.fid_cts);
disp(['# Times output: ',int2str(ntime)]);
ctime=etime(clock,t0)/60;
disp(['CPU Time = ',sprintf('%2.2f',ctime),' min.']);
%
% 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.
