function DATA=data_from_TL(TL,iisave,iifig,clim)
%
%
% 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, iisave=1; end
if nargin<3, iifig=1; end
if nargin<4, clim=[-100,-50]; end
%
ii_inc=0; ii_coh=0;
if ~isfield(TL,'tl_coh') | isempty(TL.tl_coh),
    ntl=1; tl_lab='Incoherent TL - dB'; ii_inc=1;
    [nrng,nzr,nzs,nbrg]=size(TL.tl_inc);
elseif ~isfield(TL,'tl_inc') | isempty(TL.tl_inc),
    ntl=1; tl_lab='Coherent TL - dB'; ii_coh=1;
    [nrng,nzr,nzs,nbrg]=size(TL.tl_coh);
else
    ntl=2; tl_lab='Coh/Inc TL - dB'; ii_inc=1; ii_coh=1;
    [nrng,nzr,nzs,nbrg]=size(TL.tl_coh);
end
DATA.ndim=5;
DATA.dims=[nrng,nzr,nzs,nbrg,ntl];
DATA.scales={TL.ranges(:)', TL.zr(:)', TL.zs(:)', TL.bearings(:)', [1:ntl]};
DATA.labels={'Range - km','Depth - m','Source Depth - m','Bearing - deg',tl_lab};
DATA.dlab='TL - dB';
DATA.data=zeros(DATA.dims);
DATA.title=TL.out_root;
if isfield(TL,'iidb'), DATA.iidB=TL.iidb; elseif isfield(TL,'iidB'), DATA.iidB=TL.iidB; else, DATA.iidB=1; end
jtl=0;
if ii_coh,
    jtl=jtl + 1;
    DATA.data(:,:,:,:,jtl)=TL.tl_coh;
end
if ii_inc,
    jtl=jtl + 1;
    DATA.data(:,:,:,:,jtl)=TL.tl_inc;
end
DATA.clim=clim; DATA.vdims=[2 1 3];
% Copy other fields to DATA:
loaded_fields=fieldnames(TL);
for j=1:length(loaded_fields),
	if ~any(strcmp(loaded_fields{j},{'tl_coh','tl_inc'})),
		expr=['DATA.',loaded_fields{j},'=TL.',loaded_fields{j},';']; eval(expr);
    end
end
%
if iisave, 
    str=['save ',DATA.out_root,'_PE DATA']; disp(str); eval(str);
end
if iifig, view5(DATA); end
