function TLh=hep_tl_convert(tl_files,f0,iidat,out_root)
%
% Call TLh=hep_tl_convert(tl_files,f0,iidat,out_root);
%
% tl_files      = cell array of ASCII files containing 2-column data of r,TL (pos #'s)
% f0            = center frequencies of TL (blank to input);
% iidat         = 1 for TL(r), 2=RL(t);
% out_root      = Ouput file root for _TLh.mat Matlab file containing nfreq by 1 data structure
%                 which can be viewed with view5;
%
%
% 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, f0=[]; end
if nargin<3, out_root=[]; end
%
if ischar(tl_files),
    D=dir(tl_files);
    nfreq=length(D); tl_files=cell(1,nfreq);
    for jfreq=1:nfreq,
        tl_files{jfreq}=D(jfreq).name;
    end
else
    nfreq=length(tl_files);
end
disp([int2str(nfreq),' files to be processed: ']); disp(char(tl_files));
disp(['f0 entered: ',num2str(f0(:)')]);
f0x=input('Enter new f0 (<CR> to keep original): ','s');
if ~isempty(f0x), f0=str2num(f0x), end
%
if length(f0)~=nfreq, disp('f0 and tl_files must have same dimensions'); end
%
if iidat==1,
    dlab='TL - dB'; xlab='Range - km'; sg=-1;
elseif iidat==2,
    dlab='Rev Level - dB//uPa^2'; xlab='Time - s'; sg=1;
end
TLh=cell(nfreq,1);
%
for jfreq=1:nfreq
	xx=load(tl_files{jfreq}); [npt,nc]=size(xx);
	TLh{jfreq}.file_name=tl_files{jfreq};
	TLh{jfreq}.dlab=dlab;
	TLh{jfreq}.data=[];
	TLh{jfreq}.ndim=2;
	TLh{jfreq}.dims=[npt nfreq];
	TLh{jfreq}.labels={xlab,'Center Frequency - Hz'};
	TLh{jfreq}.scales={xx(:,1)', [f0(jfreq)]};
	TLh{jfreq}.iidB=1;
%	flims=10.^(log10(f0(jfreq)) + log10(2)*[-delf_oct/2,delf_oct/2]);
	TLh{jfreq}.data=sg*xx(:,2);
end
%
if ~isempty(out_root),
    str=['save ',out_root,'_TLh TLh']; disp(str); eval(str);
else
    disp('Output file root missing, so TLh not saved in _TLh.mat file.'); 
end
