function DATA=hep_tl_unique(A,B,out_root,delr);
%
% Call DATA=hep_tl_unique(A,B,delr);
%
%
% 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<3, out_root=[]; end
if nargin<4, delr=.2; end
%
if ischar(A), A=load(A); end
if ischar(B), B=load(B); end
%
[nr,nc]=size(A.data);
R=[A.scales{1}(:);B.scales{1}(:)];
TL=[A.data;B.data];
kk=find(~isnan(R));
R=R(kk);
TL=TL(kk,:);
% Find unique ranges to within +- .2 km:
fac=1/delr;
[Ru,ii,jj]=unique(round(abs(R)*fac)/fac); nu=length(Ru);
TLu=NaN*ones(nu,nc);
for jr=1:nu
    kk=find(jj==jr);
    TLu(jr,:)=mean(TL(kk,:),1);
    for jc=1:nc,
        TLx=TL(kk,jc);
        mm=find(~isnan(TLx));
        if ~isempty(mm),
            TLu(jr,jc)=mean(TLx(mm));
        else
            TLu(jr,jc)=NaN;
        end
    end
end
for jr=1:nu,
    mm=find(isnan(TLu(jr,:)));
    for jm=1:length(mm),
        jc=mm(jm);
        ii=find(~isnan(TLu(:,jc)));
        TLu(jr,jc)=interp1(Ru(ii),TLu(ii,jc),Ru(jr),'linear','extrap');
%%        disp([jr,mm,jm,jc]); TLu(jr,jc), keyboard
    end
end
%
DATA=A;
DATA.title=[A.title,' & ',B.title];
DATA.scales{1}=Ru;
DATA.labels{1}='|Range| - km';
DATA.data=TLu;
DATA.dims=size(TLu);
%
if isempty(out_root), 
    out_root=[A.title(1:min(8,end)),B.title(1:min(8,end))],
end
str=['save ',out_root,' DATA']; disp(str); eval(str);
