%  Plot the injection and sampling tracks in eddy (rotating) coordinates
% 
hold off
clf

load Z:\oc415_2\adcp\Rebecca\tow_tracks.mat
load Z:\oc415_2\sled\med3_pos.mat
mlat3 = mlat;
mlon3 = mlon;
mday3 = m_st;

% Define eddy center as a function of time (given in year day)

% For the purposes of this exercise, we will assume the center 
% be stationary during the 18 hour period of injection, at:
% (30.28, -67.29)
% We further assume that it is moving along the following track 
% during sampling:
% y_c = -0.02*time + 34.285;
% x_c = -0.0325*time - 60.7619;

lonc_s3 = -0.0325*day_s3 - 60.7619;
latc_s3 = -0.02*day_s3 + 34.285;
rlon_s3 = lon_s3 - lonc_s3;
rlat_s3 = lat_s3 - latc_s3;
rx_s3 = rlon_s3*0.866*1.11*10^2;
ry_s3 = rlat_s3*1.11*10^2;
% % For sampling runs, convert medusa positions as well
% mlonc_s3 = -0.0325*mday3 - 60.7619;
% mlatc_s3 = -0.02*mday3 + 34.285;
% rmlon_s3 = mlon3 - mlonc_s3;
% rmlat_s3 = mlat3 - mlatc_s3;
% mx_s3 = rmlon_s3*0.866*1.11*10^2;
% my_s3 = rmlon_s3*1.11*10^2;
for jj = 1:length(mday3)
    for ii = 1:length(day_s3)
        del_t(ii,jj) = abs(day_s3(ii) - mday3(jj));
    end
    index(jj) = find(del_t(:,jj) == min(del_t(:,jj)));
end



% express all velocities in polar coordinates wrt center
% then convert radius from lat/lon units to meters
radius_s3 = sqrt((rx_s3).^2 + (ry_s3).^2);
theta_s3 = atan2(rx_s3,ry_s3);
% mradius_s3 = sqrt((mx_s3).^2 + (my_s3).^2);
% mtheta_s3 = atan2(mx_s3, my_s3);

% Correct theta for the rotation of the eddy, where we assume 
% that the eddy started rotating on yearday 0, and has been 
% rotating at a speed of 6.5 days per rotation ever since
omega = 1.12*10^(-5)*86400;
theta_rot_s3 = theta_s3 + omega*day_s3;
% mtheta_rot_s3= mtheta_s3+ omega*mday3;

% Select the medusa samples that contained SF6
%index_ms3 = [36:41 48:50];
index_ms3 = [1:50];

h2=polar(theta_rot_s3, radius_s3,'r');
set(h2, 'LineWidth', 2)
hold on
% polar(mtheta_rot_s3(index_ms3), mradius_s3(index_ms3), 'k+')
polar(theta_rot_s3(index), radius_s3(index), 'k+')
title({'Injection and Sampling Tracks in Eddy Coordinates';'Oceanus 415-2, 23Jul05-1Aug05'; 'Up is NORTH at 0200, 3Aug05 (214.2)'})
text(7, -7, 'S3')
hold off

