function [SP,MU]=data_cfar(DATA,kdim,cfar_parms,kdim_lims,iiview);
%
% Performs a median cfar normalization on kdim'th dimension of DATA.data;
%
% Call [SP,data3]=data_cfar(DATA,kdim,T_avg,iiview);
%
% DATA          = Input data structure;
% kdim          = Dimension along which to CFAR (usually time);
% cfar_parms    = [T_avg,pct_cens,ii_mean], where
%                 Tavg = Averaging time in hours (T_avg=0 means to use entire period);
%                 pct_cens  = Percentage at top to censor before median or mean;
%                 ii_mean   : 0=median, 1=mean, 2=max;
% iiview        = 1 to view output;
%
%
% 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<1,
    error('Must give at least DATA structure as first input to data_cfar');
elseif nargin<2,
    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'));
    if isempty(kdim), 
        kdim=find(strncmp(axlabs,'Time -',6) | strncmp(axlabs,'time -',6));
        if isempty(kdim) | length(kdim)>1, kdim=1; end
    end
    T_avg=   str2num(input(['          Enter averaging time in hours ([]==>all): '],'s'));
    if isempty(T_avg), T_avg=0; end
    pct_cens=str2num(input(['              Percentage at top to censor (def=0): '],'s'));
    if isempty(pct_cens), pct_cens=0; end
    ii_mean =str2num(input(['            Median(0), Mean(1), or Max(2) (def=0): '],'s'));
    if isempty(ii_mean), ii_mean=0; end
    kdim_lims=  str2num(input(['                         dim_lims for CFAR dim: '],'s'));
    if isempty(kdim_lims), kdim_lims=[]; end
    iiview=  str2num(input(['             Auto view output? (1=yes,0=no,def=1): '],'s'));
    if isempty(iiview), iiview=1; end
    cfar_parms=[T_avg,pct_cens,ii_mean];
elseif nargin<3 | isempty(cfar_parms), cfar_parms=[0,0,0];
elseif nargin<4, kdim_lims=[];
elseif nargin<5, iiview=1;
end
%
T_avg=cfar_parms(1); pct_cens=cfar_parms(2); ii_mean=cfar_parms(3);
%
if T_avg~=0,
    T_interval=diff(DATA.scales{kdim}([1,end])); T_rat=T_interval/T_avg;
    if T_rat<1.2, T_avg=0; end
end
%
if ii_mean==1, ii_avg=1; elseif ii_mean==0, ii_avg=4; elseif ii_mean==2, ii_avg=3; end
for jdim=1:DATA.ndim, dim_lims{jdim}=[]; end; dim_lims{kdim}=kdim_lims;
MED=data_avg2(DATA,kdim,ii_avg,[-T_avg,-T_avg/4,pct_cens],dim_lims,0,0); 
SP=DATA; MU=DATA;
dims_temp=[kdim,1:kdim-1,kdim+1:MED.ndim];
if kdim==1,
    data2=permute(MED.data,dims_temp);
    if length(MED.scales{kdim})==1,
        MU.data=reshape(repmat(data2,DATA.dims(kdim),1),DATA.dims(dims_temp));
    else
        MU.data=interp1(MED.scales{kdim},data2,DATA.scales{kdim},'*linear','extrap');
    end
    SP.data=SP.data - MU.data;
else
    data2=permute(MED.data,dims_temp);
    if length(MED.scales{kdim})==1,
        MU.data=reshape(repmat(data2,DATA.dims(kdim),1),DATA.dims(dims_temp));
    else
        MU.data=reshape(interp1(MED.scales{kdim},data2,DATA.scales{kdim},'*linear','extrap'), ...
            DATA.dims(dims_temp));
    end
    [dummy,dims_back]=sort(dims_temp);
    MU.data=permute(MU.data,dims_back);
    SP.data=SP.data - MU.data;
end
%
SP.dlab=['CFAR - dB']; if ii_mean==2, SP.clim=[-15 0]; else, SP.clim=[0,15]; end
if iiview, view5(SP); end
