function nav_from_ssbn(txt_root,out_root,height,BATHY)
%
% Call nav_from_ssbn(txt_root,out_root,height,BATHY);
%
% height        = height of sub in m;
%
% Converts SSBN text files to NAV files
%
% Example call:     nav_from_ssbn('day','SSBN',10,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([txt_root,'*.txt']); nfile=length(D);
%
for jfile=1:nfile,
%    S=importdata(D(jfile).name); nptx=size(S.data,1);
%    ii=[1:10:nptx];
%    Jday_all=rem(floor(S.data(ii,1)/1e6),1000); npt=length(Jday_all);
%    year=2000+floor(S.data(1,1)/1e9); 
%    Jday=Jday_all(floor(npt/2));
%    hour=rem(floor(S.data(ii,1)/1e4),100);
%    minute=rem(floor(S.data(ii,1)/1e2),100);
%    sec=rem(S.data(ii,1),100);
   	fp = fopen(D(jfile).name);
	
	%skip header
	for line = 1:6, fgetl(fp); end
	%Determine length of data
	Bsod = ftell(fp);
	fseek(fp,0,'eof'); Beof = ftell(fp);
	fseek(fp,Bsod,'bof');
	nlines = (Beof-1-Bsod)/57;
	if nlines ~= floor(nlines), Bsod, Beof, nlines, error('lines not 57 bytes long'); end
	%read 1 line at a time
	disp(['Reading lines from ',D(jfile).name])
	lines = zeros(nlines,55);
	for line = 1:nlines, lines(line,:) = fgetl(fp); end
	lines = char(lines);
	fclose(fp);
    ii=[1:5:nlines]; npt=length(ii);
%decode the data
	disp('decoding data')
    year=2000 + str2num(lines(1,6:7)),
    Jday_all = str2num(lines(ii,8:10));
    NAV.jday=Jday_all(floor(npt/2));
    [month,day]=date_from_Jday(NAV.jday,year);
    NAV.nav_file=[out_root,'_',month,'_',int2str(day),'_',int2str(NAV.jday),'_NAV'];
    NAV.log_file=D(jfile).name;
    hour=str2num(lines(ii,11:12));
	minute = str2num(lines(ii,13:14));
	sec = str2num(lines(ii,15:16));
	NAV.hour = (Jday_all-NAV.jday)*24 + hour + minute/60 + sec/3600;
	NAV.min=(NAV.hour-NAV.hour(1) ) * 60;		% Minute after start of file
	NAV.lat = str2num(lines(ii,17:24));
	NAV.long = str2num(lines(ii,26:34));
	NAV.bathy_root=BATHY.bathy_root;
	[NAV.xkm,NAV.ykm]=bathy_ll2xy(NAV.lat,NAV.long);
	NAV.z = -height/2 + str2num(lines(ii,42:47))*.3048;
% Smooth in x,y (?)
iiplot=0;
if iiplot,
    figure;
    plot_cfill(NAV.xkm,NAV.ykm,'o','r',5); hold on;
end
T_skip=1.5; mini=[NAV.min(1):T_skip:NAV.min(end)]'; size(T_skip),
xi=interp1(NAV.min,NAV.xkm,mini);
yi=interp1(NAV.min,NAV.ykm,mini);
T_out=10/60;
t_new=[NAV.min(1):T_out:NAV.min(end)]';
NAV.hour=NAV.hour(1) + t_new/60;
NAV.xkm=spline(mini,xi,t_new); NAV.ykm=spline(mini,yi,t_new);
[NAV.lat,NAV.long]=bathy_xy2ll(NAV.xkm,NAV.ykm);
NAV.z=spline(NAV.min,NAV.z,t_new);
NAV.min=t_new;

if iiplot,
    plot_cfill(NAV.xkm,NAV.ykm,'o','b',5); zoom on;
end
%
	hdg = str2num(lines(ii,48:55));
    NAV.v = str2num(lines(ii,36:41)) * .5144;
	NAV.phi=hdg*pi/180;   
    if 1,
    	vx=gradient(NAV.xkm,NAV.hour); vy=gradient(NAV.ykm,NAV.hour);
        NAV.v=sqrt(vx.^2 + vy.^2) * (1000/3600);
    	NAV.phi=atan2(vx,vy);
    else
		NAV.v=str2num(lines(ii,36:41)) * .5144;;
		vx=gradient(NAV.xkm,NAV.hour); vy=gradient(NAV.ykm,NAV.hour);
		NAV.phi=atan2(vx,vy);
    end
%
    str=['save ..\',NAV.nav_file,' NAV']; disp(str); eval(str);
end
