function SP=data_cfar_var(DATA,kdim,cfar_parms,kdim_lims,iiview);
%
% Performs a variance normalization on kdim'th dimension of DATA.data;  Not finalized - does a
% variance in dB space.
%
% Call SP=data_cfar(DATA,kdim,T_avg,iiview);
%
% DATA          = Input data structure;
% kdim          = Dimension along which to CFAR (usually time);
% cfar_parms    = [T_avg], where
%                 Tavg = Averaging time in hours (T_avg=0 means to use entire period);
% 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'));
    T_avg=   str2num(input(['                    Enter averaging time in hours: '],'s'));
    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];
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); 
%
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
%
ii_avg=5;
for jdim=1:DATA.ndim, dim_lims{jdim}=[]; end; dim_lims{kdim}=kdim_lims;
%
SP=DATA; SP.iidB=0; %%SP.data=10.^(SP.data/10);
MED=data_avg2(SP,kdim,4,[-T_avg,-T_avg/4,0],dim_lims,0,0);  % MED.data left in dB
VAR=data_avg2(SP,kdim,ii_avg,[-T_avg,-T_avg/4,0],dim_lims,0,0);  % VAR.data left dB
%
dims_temp=[kdim,1:kdim-1,kdim+1:VAR.ndim];
if kdim==1,
    data_med1=permute(MED.data,dims_temp);
    data_var1=permute(VAR.data,dims_temp);
    if length(VAR.scales{kdim})==1,
        data_med2=reshape(repmat(data_med1,DATA.dims(kdim),1),DATA.dims(dims_temp));
        data_var2=reshape(repmat(data_var1,DATA.dims(kdim),1),DATA.dims(dims_temp));
    else
        data_med2=interp1(VAR.scales{kdim},data_med1,DATA.scales{kdim},'*linear','extrap');
        data_var2=interp1(VAR.scales{kdim},data_var1,DATA.scales{kdim},'*linear','extrap');
    end
else
    data_med1=permute(MED.data,dims_temp);
    data_var1=permute(VAR.data,dims_temp);
    if length(VAR.scales{kdim})==1,
        data_med2=reshape(repmat(data_med1,DATA.dims(kdim),1),DATA.dims(dims_temp));
        data_var2=reshape(repmat(data_var1,DATA.dims(kdim),1),DATA.dims(dims_temp));
    else
        data_med2=reshape(interp1(MED.scales{kdim},data_med1,DATA.scales{kdim},'*linear','extrap'), ...
            DATA.dims(dims_temp));
        data_var2=reshape(interp1(VAR.scales{kdim},data_var1,DATA.scales{kdim},'*linear','extrap'), ...
            DATA.dims(dims_temp));
    end
    [dummy,dims_back]=sort(dims_temp); 
end
med2=permute(data_med2,dims_back); var2=permute(data_var2,dims_back);
SP.data=(SP.data - med2) ./ var2;
% SP.data=10*log10(SP.data); 
SP.iidB=1;
SP.dlab=['VAR CFAR - dB']; SP.clim=[0,15];
if iiview, view5(SP); end
