function SSP=ssp_from_xbt(edf_root,out_root,BATHY,iih_sort)
%
% Call SSP=ssp_from_xbt(edf_root,out_root,BATHY,iih_sort);
%
% Converts XBT data in EDF files to SSP file.
%
% Example call: SSP=ssp_from_xbt('T6',[GUI.adi_dir,'HCE02_XBT'],BATHY,1)
%
%
% 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<4, iih_sort=0; end
%
D=dir([edf_root,'*.EDF']); nfile=length(D);
%
iidiag=0;
Jday=zeros(nfile,1); Jhour=Jday; lat=Jday; long=Jday; hw=Jday; file_names=cell(nfile,1);
disp(['Processing ',int2str(nfile),' EDF Files ...']);
for jfile=1:nfile,
    disp(D(jfile).name);
    file_names{jfile}=D(jfile).name;
    fid=fopen(D(jfile).name); hdone=0; nline_skip=0;
    while ~hdone,
        tline=fgetl(fid); nline_skip=nline_skip + 1;
        if iidiag, disp([num2str(nline_skip),tline]); end
        if strncmp(tline,'Date',4),
            [data,count,err,next_idx]=sscanf(tline(16:end),'%f%c',5);
            month=data(1); day=data(3); year=data(4);
        elseif strncmp(tline,'Time',4),
            [data,count,err,next_idx]=sscanf(tline(16:end),'%f%c',5);
            hour=data(1); minute=data(3); second=data(4);
        elseif strncmp(tline,'Lati',4),
            [data,count,err,next_idx]=sscanf(tline(16:end),'%f%f%c',3);
            lat_deg=data(1); lat_min=data(2); iiNS=data(3);
        elseif strncmp(tline,'Long',4),
            [data,count,err,next_idx]=sscanf(tline(16:end),'%f%f%c',3);
            long_deg=data(1); long_min=data(2); iiEW=data(3);
        elseif strncmp(tline,'Depth (m)',9),
            hdone=1;
        end
    end
    fclose(fid);
    Jday(jfile)=date_to_jday(month,day,year);
    Jhour(jfile)=hour + minute/60 + second/3600;
    lat(jfile)=lat_deg + lat_min/60; if strcmp(char(iiNS),'S') lat=-lat; end
    long(jfile)=long_deg + long_min/60; if strcmp(char(iiEW),'W') long=-long; end
%
    fmt='%f%f%f';
    if iidiag, disp(['textread nline_skip = ',in2str(nline_skip)]); end
    [z,T,c]=textread(D(jfile).name,fmt,-1,'headerlines',nline_skip);
    hw(jfile)=min(z(end),interp2(BATHY.lat,BATHY.long,BATHY.hw.',lat(jfile),long(jfile)));
    ssp{jfile}=[z,c];
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;
if ~iih_sort,
	SSP.labels={'Depth - m','SSP Number'};
	SSP.scales={z_out, [1:nok]};
else
% Construct second set of scales:
    [h_sort,ii_sort]=sort(hw(iiok));
    iiok=iiok(ii_sort);
	SSP.labels={'Depth - m','Water Depth'};
	SSP.scales={z_out, h_sort};
end
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);
