function [CFVN,CF]=data_cfar_sdn(DATA,kdim,cfar_parms,iiview);
%
% Performs a censored-mean, normed-variance CFAR normalization on kdim'th dimension of 
% DATA.data;
%
% Call [CFVN,CF]=data_cfar_sdn(DATA,kdim,cfar_parms,iiview);
%
% DATA          = Input data structure;
% kdim          = Dimension along which to CFAR (usually time);
% cfar_parms    = [T_avg1,T_guard,T_avg2,pct_cens,mean_fac,ii_mean], where
%                 T_avg1    = Averaging time in past in hours;
%                 T_guard   = Guard band time interval around output points in hours;
%                 T_avg2    = Averaging time in future in hours;
%                 pct_cens  = Percentage at top to censor before mean;
%                 mean_fac  = Factor (multiplied by mean of pct_cens) to compute censor level;
%                 ii_mean   = 0 for median, 1 mean;
% iiview        = 1 to view output 1; 2 to view output 2
% CFVN          = Output 1:  cfar variance norm (SDN lookup)
% CF            = Output 2:  cfar (no SDN lookup)
%
%
% 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<2,
    error('Must give at least DATA structure and kdim as first two inputs to data_cfar');
elseif nargin<3,
    disp(['Dimension labels: ']); disp(sprintf('   %s   ',DATA.labels{1:DATA.ndim}));
    disp(['Dimension limits: ']);
    str=[];
    for jdim=1:DATA.ndim,
        str=[str,'   ',sprintf('[%1.2f %1.2f]',DATA.scales{jdim}([1,end]))];
    end
    disp(str)
    kdim=    str2num(input(['        Enter dimension number over which to CFAR: '],'s'));
    T_parms= str2num(input(['           Enter T_avg1, T_guard, T_avg2 in hours: '],'s'));
    if length(T_parms(:))~=3, error('Must be 3-element vector'); end
    if isempty(T_parms), T_parms=[12,2,2]*200/3600; end
    T_avg1=T_parms(1); T_guard=T_parms(2); T_avg2=T_parms(3);
    pct_cens=str2num(input(['              Percentage at top to censor (def=0): '],'s'));
    if isempty(pct_cens), pct_cens=0; end
    mean_fac=str2num(input(['              Factor to multiply mean (def=4): '],'s'));
    if isempty(mean_fac), mean_fac=4; end
    ii_mean =str2num(input(['                     Median(0) or Mean(1) (def=0): '],'s'));
    if isempty(ii_mean), ii_mean=0; end
    cfar_parms=[T_avg1,T_guard,T_avg2,pct_cens,mean_fac,ii_mean];
    iiview=  str2num(input(['             Auto view output? (1=yes,0=no,def=1): '],'s'));
    if isempty(iiview), iiview=1; end
elseif isempty(cfar_parms), cfar_parms=[[12,2,2]*200/3600, 15, 4, 1];
elseif nargin<4, iiview=1;
end
if isempty(iiview), iiview=1; end
%
SDN=[];
eval('load data_cfar_sdn_SDN');
if isempty(SDN), error('Must place data_cfar_sdn_SDN.mat somewhere in Matlab search path'); end
nu0=SDN.scales{2}(1); dnu=diff(SDN.scales{2}([1,2])); nnu=SDN.dims(2);
dB0=SDN.scales{1}(1); ddB=diff(SDN.scales{1}([1,2])); ndB=SDN.dims(1);
%
T_avg1=cfar_parms(1); T_guard=cfar_parms(2); T_avg2=cfar_parms(3); 
pct_cens=cfar_parms(4); mean_fac=cfar_parms(5); ii_mean=cfar_parms(6);
%
CFVN=DATA;
CF=DATA;
for jdim=1:7, KD{jdim}=1; end
for jdim=1:DATA.ndim, KD{jdim}=[1:DATA.dims(jdim)]; end
%
pct_take=1 - .01*pct_cens; iirep=ones(1,7);
time_ax=DATA.scales{kdim}; ntime=DATA.dims(kdim); dt=diff(time_ax([1,end]))/max(ntime-1,1);
nt_avg1=round(T_avg1/dt); nt_guard=round(T_guard/dt); nt_avg2=round(T_avg2/dt);
nt_guard1=nt_guard/2; nt_guard2=nt_guard - nt_guard1;
nt_low=nt_avg1 + nt_guard1; nt_high=nt_avg2 + nt_guard2;
data0=10.^(DATA.data/10);
dims_data=DATA.dims; dims_data(end+1:7)=1; dims_data(kdim)=0; 
[dims_loop,jdim_loop]=sort(dims_data);
% for jtime=1:ntime,
%     if mod(jtime,10)==1, disp(['Starting jtime = ',int2str(jtime),' of ',int2str(ntime)]); pause(.001); end
%     ntmin=max(1,jtime-nt_low); ntmax=ntmin+nt_low+nt_high;
%     if ntmax>ntime, ntmax=ntime; ntmin=max(1,ntmax-nt_low-nt_high); end
%     ii=[ntmin:jtime-nt_guard1-1,jtime+nt_guard2+1:ntmax];
%
%     npt_avg=fix((1 - .01*pct_cens)*length(ii));
    for j2=1:dims_data(jdim_loop(2)),
        KD{jdim_loop(2)}=j2;
        for j3=1:dims_data(jdim_loop(3)),
            KD{jdim_loop(3)}=j3;
            for j4=1:dims_data(jdim_loop(4)),
                KD{jdim_loop(4)}=j4;
                for j5=1:dims_data(jdim_loop(5)),
                    KD{jdim_loop(5)}=j5;
                    for j6=1:dims_data(jdim_loop(6)),
                        KD{jdim_loop(6)}=j6;
                        for j7=1:dims_data(jdim_loop(7)),
                            KD{jdim_loop(7)}=j7;
%                             KD{kdim}=ii;
                            KD{kdim}=1:ntime;
                            data=squeeze(data0(KD{1},KD{2},KD{3},KD{4},KD{5},KD{6},KD{7}));
%                             [data_sort]=sort(data);
%                             data_avg=mean(data_sort(1:npt_avg));
%                             indx_use=find(data<mean_fac*data_avg);
%                             mu=mean(data(indx_use)); 
%                             if mu~=1, sigma=std(data(indx_use)); else, sigma=1; end
%                             nu=max(1,min(20,(mu/sigma)^2));
%                             KD{kdim}=jtime;
%                             dval=10*log10(data0(KD{1},KD{2},KD{3},KD{4},KD{5},KD{6},KD{7})/mu);
%                             jnu=round((nu-nu0)/dnu)+1;
%                             jdB=min(ndB,max(1,round((dval-dB0)/ddB)+1));
							[CFVN.data(KD{1},KD{2},KD{3},KD{4},KD{5},KD{6},KD{7}), ...
                             CF.data(KD{1},KD{2},KD{3},KD{4},KD{5},KD{6},KD{7})] ...
                            = data_cfar_sdn_mex( ...
								data, ...	% 1 real*8 (ntime)
								int32(ntime), ...		% 2 integer*4
								int32(nt_low), ...		% 3 integer*4
								int32(nt_high), ...		% 4 integer*4
								int32(nt_guard1), ...	% 5 integer*4
								int32(nt_guard2), ...	% 6 integer*4
								pct_cens, ...		% 7 real*8
								mean_fac, ...		% 8 real*8
								SDN.data, ...			% 9 real*8 (ndB,nnu)
								int32(ndB), ...			% 10 integer*4
								int32(nnu), ...			% 11 integer*4
								dB0, ...			% 12 real*8
								ddB, ...			% 13 real*8
								nu0, ...			% 14 real*8
								dnu);			% 15 real*8

%                             SP.data(KD{1},KD{2},KD{3},KD{4},KD{5},KD{6},KD{7})=SDN.data(jdB,jnu);
                        end
                    end
                end
            end
        end
    end
% end    
%
CFVN.dlab=['CFAR - dB']; CFVN.clim=[0,20]; 
if CFVN.title(end)==']', CFVN.title=[CFVN.title(1:end-1),'N]']; else, CFVN.title=[CFVN.title,'[N]'];end
CF.dlab=['CFAR - dB']; CF.clim=[0,20]; 
if CF.title(end)==']', CF.title=[CF.title(1:end-1),'N]']; else, CF.title=[CF.title,'[N]'];end
if iiview==1,
    view5(CFVN);
elseif iiview==2
    view5(CF);
end
