function [zout]=my_fun_blair(Z0);
% my_fun_blair  Use fminsearch for find best fit to eddy drifter data
% =========================================================================
% my_fun_blair  Version 1.0 02-Sep-2005
%
% Usage: 
%   my_fun_blair(Z0)
%
% Description:
%   This function computes a best fit of the drogued drifter data in
%   eddy A4 during Tracer 2 (28 Aug - 15 Sep 2005).
%   
%
% Input:
%   Z0=[x00 y00 r th0 omega u v]; where
%   x00 = estimate of intital latitude of eddy center (degrees)
%   y00 = estimate of intital longitude of eddy center (degrees)
%   r = radius of drifter track (m)
%   th0 = estimate of initial angle of track relative to true North (rad)
%   omega = estimate of rotation rate of the eddy (rad/s)
%   u = estimate of x-component of speed in the x-direction
%   v = extimate of y-component of speed in the y-direction
%
% Output:
%   zout = sum of the squares of the difference between the data and fit
%
% Called by:
%   fl_pos_fit_blair and fminsearch
%
% Calls:
%   n/a
%
% Author:
%   Cindy Sellers WHOI
%   Blair Greenan Bedford Institute of Oceanography
%   James Girton APL, University of Washington
%   Matlab 7
%   greenanb@mar.dfo-mpo.gc.ca
% =========================================================================
%

% History
% Version 1.0 02-Sep-2005

% reference latitude and longitude
lonref=69;
latref=30;
lat0 = 29.85;

% load float data
%load Drogue_52486_smoothed.txt
load Drogue_52484_smoothed.txt
%t = Drogue_52486_smoothed(:,1);
%lon=-Drogue_52486_smoothed(:,2);
%lat=Drogue_52486_smoothed(:,3);
t = Drogue_52484_smoothed(:,1);
lon=-Drogue_52484_smoothed(:,2);
lat=Drogue_52484_smoothed(:,3);

% convert to m
phi = lat0*pi/180; % rad
x=111000/cos(phi)*(lonref-lon); % m
y=111000*(lat-latref); % m
% generate a complex number represneting position
z=x+i*y;  

% reference time
t0 = t(1)-.001; % days
% convert to seconds
tsec_ref = (t-t0)*3600*24;

% extract info from input vector
x00   = Z0(1);
y00   = Z0(2);
r     = Z0(3);
th0   = Z0(4);
omega = Z0(5);
u     = Z0(6);
v     = Z0(7);

% function describing the rotation and translation of the eddy
zfun = ((x00+i*y00) + r * exp(i*(th0+omega*tsec_ref)) + (u+i*v)*tsec_ref ); 
% sum of squared differences
zout = sum((abs(z - zfun)).^2 ); 

% x,y,t - input data  (as z0)
% t0 - reference time (before all data)
% t-t0 is in seconds
% unknowns:
% u,v (w)- components of velocity of center
% w - u + (i * v);
% r - radius of orbit around center
% omega - angular velocity of orbit around center
% th0 - initial angle at t0
% x00, y00 - position at t0 (z00 - complex value of x00 + (i * y00))
