function SSP=ssp_from_xbt_excel(xls_root,out_root,BATHY)
%
% Call SSP=ssp_from_xbt(xls_root,out_root,BATHY);
%
% Converts Excel files to SSP file.
%
% Example call:     SSP=ssp_from_xbt('TL','TL_run',BATHY)
%
%
% 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.
D=dir([xls_root,'*.xls']); nfile=length(D);
%
Jday=zeros(nfile,1); Jhour=Jday; lat=Jday; long=Jday; hw=Jday; file_names=cell(nfile,1);
for jfile=1:nfile,
    disp(D(jfile).name);
    file_names{jfile}=D(jfile).name;
    [data,txt]=xlsread([D(jfile).name]);
    txt_date=txt{3,1}(18:27); month=str2num(txt_date(1:2)); day=str2num(txt_date(4:5)); year=str2num(txt_date(7:10));
    Jday(jfile)=date_to_jday(month,day,year);
    txt_time=txt{4,1}(18:25); hour=str2num(txt_time(1:2)); minute=str2num(txt_time(4:5)); sec=str2num(txt_time(7:8));
    Jhour(jfile)=hour + minute/60 + sec/3600;
    txt_lat=txt{6,1}(18:end); ii_north=strcmpi(txt_lat(end),'n');
    lat(jfile)=str2num(txt_lat(1:3)) + str2num(txt_lat(4:end-1))/60;
    if ~ii_north, lat(jfile)=-lat(jfile); end
    txt_long=txt{7,1}(18:end); ii_east=strcmpi(txt_long(end),'e');
    long(jfile)=str2num(txt_long(1:3)) + str2num(txt_long(4:end-1))/60;
    if ~ii_east, long(jfile)=-long(jfile); end
%
    hw(jfile)=min(data(end,1),interp2(BATHY.lat,BATHY.long,BATHY.hw.',lat(jfile),long(jfile)));
    ssp{jfile}=data(:,[1,5]);
end
%
hmin_xbt=1;
hmax_xbt=460;
hw=floor(hw*2)/2, hwmax=min(hmax_xbt,max(hw)),
z_out=unique([0:2:50,55:5:100,110:10:200,225:25:min(hwmax,500),600:100:hwmax,hw(:)']);
ii=find(z_out<=hwmax); z_out=z_out(ii); nz_out=length(z_out);
data=NaN*ones(nz_out,nfile); nz=zeros(1,nfile); svp_bad=zeros(1,nfile);
for j=1:nfile,
    ii=find(ssp{j}(:,1)>=hmin_xbt);
    data(:,j)=interp1(ssp{j}(ii,1),ssp{j}(ii,2),z_out(:),'linear','extrap');
    ii=find(z_out>hw(j)); data(ii,j)=NaN;
    if isempty(ii), nz(j)=nz_out; else, nz(j)=ii(1)-1; end
%    ii=find(data(:,j)<1460 | data(:,j)>1570); data(ii,j)=NaN;
%    iibad=find(isnan(data(1:nz(j),j))); nbad=length(iibad);
%    if nbad>0, svp_bad(j)=1; end
end
%
abs_time=Jday + Jhour/24;
[dummy,iiok]=sort(abs_time), nok=length(iiok),
SSP.ndim=2;
SSP.labels={'Depth - m','SSP Number'};
SSP.scales={z_out, [1:nok]};
SSP.dims=[nz_out, nok];
SSP.data=data(:,iiok); SSP.dlab='Sound Speed - m/s';
SSP.hw=hw(iiok);
SSP.nz=nz(iiok); %SSP.ssp_no=ssp_no(iiok);
SSP.lat=lat(iiok); SSP.long=long(iiok);
SSP.Jday=Jday(iiok); SSP.Jhour=Jhour(iiok);
SSP.file_names=file_names(iiok);
SSP.title=out_root;
SSP.clim=[min(SSP.data(:)),max(SSP.data(:))];
%
str=['save ',out_root,'_SSP SSP']; disp(str); eval(str);
