function nav_from_drifter(dir_root,txt_root,z0,BATHY)
%
% Call nav_from_drifter(dir_root,txt_root)
%
% Example call: nav_from_drifter('ev*','*_edit.TXT',50.1,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.
FORMAT = '%s%2d%2d%f%2d%f%c%3d%f%c%*[^\n]';
n_samples=-1;
%
D=dir(dir_root); ndir=length(D);
Jday=260;
for jdir=1:ndir,
    if ~D(jdir).isdir, disp('Not a directory!!'); keyboard; return; end
    eval(['cd ',D(jdir).name]);
    D2=dir(txt_root); nfile=length(D2);
    if nfile>1, disp('Unexpected number of files'); keyboard; return; end
    delim=',';  if ~isempty(findstr(D(jdir).name,'ev411')), delim='\t'; end
    jfile=1;
    file_name=D2(jfile).name; disp(file_name);
	[A,hr,minutes,sec,lat_deg,lat_min,north,lon_deg,lon_min,east] = ... 
        textread(file_name,FORMAT,n_samples,'delimiter',delim,'emptyvalue',NaN);
    Jhour0=hr + minutes/60 + sec/3600;
    Jhour=unwrap(Jhour0,12);
    if jdir==1, Jhour_last=Jhour(1); end
    if Jhour(1)<Jhour_last -1, Jday=Jday + 1; disp('Jday incremented'); end
%
    iisg_ns=1; if strcmpi(north(1),'S'), iisg_ns=-1; end
    iisg_ew=1; if strcmpi(east(1),'W'), iisg_ew=-1; end			
	lat=iisg_ns*(lat_deg + lat_min/60);
	long=iisg_ew*(lon_deg + lon_min/60);
%
    disp(['Directory = ',D(jdir).name,'; File name = ',D2(jfile).name]);
    disp(['   Initial, Final hours = ',num2str(Jhour(1)),',',num2str(Jhour(end))])
    if 0,
        Jdayx=input(['Enter Julian day (<CR> to keep current value of ',int2str(Jday),'): ']);
        if ~isempty(Jdayx), Jday=Jdayx; end
    end
    NAV.nav_file=sprintf('dvla_%s_%3.3i_h%2.2i_NAV',D(jdir).name(1:5),Jday,round(Jhour(1)));
%    keyboard
%    NAV.nav_file=['dvla_',D(jdir).name(1:5),'_',int2str(Jday),'_h', ...
%            int2str(Jhour(1)),'_NAV']; disp(NAV.nav_file),
    NAV.log_file=[D(jdir).name,'\',D2(jfile).name];
%	NAV.svg_root=SVGx.out_root;
    ii=find(~isnan(Jhour));
	NAV.jday=Jday;
	NAV.hour=Jhour(ii);
	NAV.min=(NAV.hour-NAV.hour(1) ) * 60;		% Minute after start of file
	NAV.lat=lat(ii);
	NAV.long=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);
	NAV.v=sqrt(vx.^2 + vy.^2) * (1000/3600);
	NAV.phi=atan2(vx,vy);
    str=['save ..\..\',NAV.nav_file,' NAV']; eval(str);
    eval(['cd ..']);
%
    Jhour_last=Jhour(end);
end
return

%
%*******************************************************************************
%  Read in the ARL Drifter Navigation
%*******************************************************************************
%
%
%
%  For Event 411 use white space delimiters (default) versus comma
%
% [A, hr, min, sec, lat_deg, lat_min, north, lon_deg, lon_min, west] = ... 
%      textread (file_name, FORMAT, n_time_samples, 'emptyvalue', NaN);
%
%********************************************************************************
%  Convert to "decimal" type
%********************************************************************************
%

