function [position,range,bearing,time]=EMATT_track(fname,deploy,starttime);
% [position,range,bearing,time]=EMATT_track(fname,deploy,starttime);
% NOTE: ***** REQUIRES MAPPING TOOLBOX FUNCTIONS ******
%
% INPUTS: fname         filename of the EMATT exported CSV data file
%         deploy        [latitude longitude] position (decimal degrees) of the
%                       EMATT deployment location
%         starttime    activation time (datenumber format) of the EMATT
%                       deployment
% OUTPUTS: position     [Lat Lon] position coordinates based on EMATT
%                       programming (assumes no current drift)
%          time         datenumber time vector for each position
%
%   example:  position=EMATT_track('DelMonte_RPS1.csv',[36+37.948/60 -121-51.957/60],datenum(2010,8,6,16,28,22));
% 
% Chris Miller
% 8/24/10

%12/28/10 - coverted to function form from original code
%% EMATT2 deployment location
% n=importdata('DelMonte_RPS1.csv');
% deploy=[36+37.948/60 -121-51.957/60];
% starttime=datenum(2010,8,06,16,28,22);    % datenumber for deployment time (UTC)
%%
n=importdata(fname);
data=n.data;
HH=data(:,1)+data(:,2)/60+data(:,3)/3600;   % Decimal hours for EMATT geometry
dHH=HH(2)-HH(1);        % delta time (hours) - should be 2 second interval
time=starttime+HH/24;   % time (datenumber format) for EMATT run
depth=data(:,4);        % EMATT depth (ft)
course=data(:,5);       % EMATT course (True if deviation is in data(:,7))
speed=data(:,6);        % EMATT speed (kts)

% plot depth vs. time for EMATT track
plot(time,-depth)
datetick('x',21);grid;
position=deploy;
% use reckon command to determine Lat/Lon vs. time
for i=2:length(time),   % loop over all times
    [position(i,1),position(i,2)]=reckon(position(i-1,1),position(i-1,2),nm2km(speed(i-1)*dHH),course(i-1),almanac('Earth','wgs84'));
end;

% Now calculate range/bearing from initial drop point (assumption of no buoy drift)
for i=1:length(time),
    [range(i),bearing(i)] = distance(position(1,1),position(1,2),position(i,1),position(i,2),almanac('Earth','wgs84'));
end;

