function nav_from_ssn(txt_root,adi_dir,z0,BATHY)
%
% Call nav_from_ssn(xls_root,adi_dir,z0,BATHY);
%
% Converts Excel files to NAV files
%
% Example call:     nav_from_indy('Sept','Indy',45,BATHY);
%
% 10/7/2002 11:50	41 23.094 N	72 05.416 W	166 T	5.9
%
% 10/28/2002	2:30:05	39	44.101	N	72	14.284	W	39.73502	72.23807
%
% 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);
%
fmt = '%f%c%f%f%f';
fmt = '%f%c%f%c%f %f%c%f%c%f %f%f%c %f%f%c %f%f';
%
for jfile=1:nfile,
    disp(D(jfile).name);
    [month,sl1,day,sl2,year,hour,colon1,minute,colon2,sec,lat_deg,lat_min,NS,long_deg,long_min,EW,lat,long]= ...
        textread(D(jfile).name,fmt,-1);
    Jday=date_to_Jday(month,day,year);
    iisg_ns=1; if strcmp(NS(1),'S'), iisg_ns=-1; end
    iisg_ew=1; if strcmp(EW(1),'W'), iisg_ew=-1; end
    NAV.nav_file=[D(jfile).name(1:10),'_',int2str(Jday(1)),'_NAV'];
    NAV.log_file=D(jfile).name;
	NAV.jday=Jday(1);
	hour=(Jday - Jday(1))*24 + hour + minute/60;
    [hour_sort,ii]=unique(hour);
    ii=find(diff(hour)>0);
    NAV.hour=hour(ii);
	NAV.min=(NAV.hour-NAV.hour(1) ) * 60;		% Minute after start of file
	NAV.lat=iisg_ns*(lat_deg(ii) + lat_min(ii)/60);
	NAV.long=iisg_ew*(long_deg(ii) + long_min(ii)/60);
%
%for jfile=1:nfile,
%    month='Oct'; day=25; year=2002;
%    [hour,colon,minute,long,lat]=textread(D(jfile).name,fmt,-1);
%    Jday=date_to_Jday(month,day,year);
%    npt=length(hour); Jday=repmat(Jday,[npt,1]);
%    ii_day_jump=find(diff(hour)<-1),
%    for jday=1:length(ii_day_jump),
%        jspan=ii_day_jump(jday)+1;
%        Jday(jspan:end)=Jday(jspan:end) + 1;
%    end
        
%    iisg_ns=1; 
%    iisg_ew=-1; 
%    NAV.nav_file=[D(jfile).name(1:10),'_',month,'_',int2str(day),'_',int2str(Jday(1)),'_NAV'];
%    NAV.log_file=D(jfile).name;
%	NAV.jday=Jday(1);
%	hour=(Jday - Jday(1))*24 + hour + minute/60;
%    [hour_sort,ii]=unique(hour);
%    ii=find(diff(hour)>0); length(ii), length(hour),
%    NAV.hour=hour(ii);
%	NAV.min=(NAV.hour-NAV.hour(1) ) * 60;		% Minute after start of file
%	NAV.lat=iisg_ns*(lat(ii));
%	NAV.long=iisg_ew*(long(ii));
	NAV.bathy_root=BATHY.bathy_root;
	[NAV.xkm,NAV.ykm]=bathy_ll2xy(NAV.lat,NAV.long);
    NAV.z=z0*ones(size(NAV.xkm));
	vx=gradient(NAV.xkm,NAV.hour); vy=gradient(NAV.ykm,NAV.hour);
	v=sqrt(vx.^2 + vy.^2) * (1000/3600);
    NAV.v=v;
    NAV.phi=atan2(vx,vy);
%
    str=['save ',adi_dir,NAV.nav_file,' NAV']; disp(str); eval(str);
end
