function AS=ambi_run(cts_root,BW,nD,Dlims,T_int,ii_window,nzp_tau,iifig,iisave)
%
% Call AS=ambi_run(cts_root,BW,nd,Dlims,T_int,ii_window,nzp_tau,iifig,iisave);
%
% cts_root      = CTS file root;
% BW            = bandwidth of signal;
% nD            = Number of D (0=default);
% Dlims         = Dmax for [-Dmax,Dmax] or [Dmin,Dmax];
% T_int         = Integration time in s (0=1 PRI, -n=n*PRI);
% ii_window     = Time-domain window: 0 for none, 1 for Kaiser, 2 for Hann;
% nzp_tau       = Zero pad factor in time domain: pad by 2^nzp_tau;
% iifig         = 0 to not view, 1 to view in new figure, >1 to view in figure number iifig;
% iisave        = Save AS data file in [cts_root,'_AS'];
%
%
if nargin<1, cts_root=[]; end
if nargin<2 | isempty(BW), BW=30; end
if nargin<3 | isempty(nD), nD=0; end
if nargin<4 | isempty(Dlims), Dlims=.15; end
if nargin<5 | isempty(T_int), T_int=0; end
if nargin<6 | isempty(ii_window), ii_window=1; end
if nargin<7 | isempty(nzp_tau), nzp_tau=1; end
if nargin<8 | isempty(iifig), iifig=1; end
if nargin<9 | isempty(iisave), iisave=1; end
%
str=which('ambi_run');
wf_dir=[str([1:end-22]),'Waveforms\'];
if isempty(cts_root),
    cts_file=uigetfile([wf_dir,'*.cts'],'Choose CTS file: ');
    cts_root=cts_file(1:end-4),
    keyboard
end
CTS=cts_open([wf_dir,cts_root]);
PRI=CTS.npt/CTS.fs; if T_int==0, T_int=PRI; elseif T_int<0, T_int=abs(T_int)*PRI; end
cts=cts_read(CTS,-10,inf);
% Find decimating factor such that fs>=BW:
factors=[1,cumprod(factor(CTS.npt))];
fs_poss=CTS.fs./factors; ii=find(fs_poss>=BW);
fs=fs_poss(ii(end)); n_dec=round(CTS.fs/fs);

cts=cts(1:n_dec:end);

%%nd=100; Dlims=[-.2,.2]; ii_window=1; nzp_tau=0; T_int=200; 
NPRI=max(1,round(T_int/PRI)),
AS.title=cts_root;
AS.ndim=2; AS.dims=[];
AS.labels={'D','Delay'};AS.dlab='Ambiguity - dB';
[AS.scales{2},AS.scales{1},AS.data]=ambi_surf(fs,CTS.f_bb_shift,cts,NPRI,nD,Dlims,ii_window,nzp_tau);
AS.dims=size(AS.data);
%
AS.iidB=1;
Amax=max(AS.data(:)); 
AS.data=AS.data - Amax;
AS.clim=[-80,0];

if iisave,
    str=['save ',cts_root,'_AS AS']; disp(str); eval(str);
end
if iifig
    if iifig>1, figure(iifig); clf; iinewfig=0; else, iinewfig=1; end
    view5(AS,[],[],iinewfig);
end
%
% 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.
