%% Movie of Drifter Deployment
% syntax: plotdeploymentmovie('Driftersessionjune 11 morning.mat',1)

function plotdeploymentmovie(filename,dep_nr)

load(filename) % load drifterdata

[XI,YI,ZI]=loadbathy; % load bathy


ndrifters=length(deployments(dep_nr,1).drifterdata);

for di=1:ndrifters

% delta s=sqrt(dx^2+dy^2)
deployments(dep_nr,1).drifterdata{1,di}(:,4)=sqrt(gradient(deployments(dep_nr,1).drifterdata{1,di}(:,1)).^2+gradient(deployments(dep_nr,1).drifterdata{1,di}(:,2)).^2);
% u = delta s / dt
u=deployments(dep_nr,1).drifterdata{1,di}(:,4)./gradient(deployments(dep_nr,1).drifterdata{1,di}(:,3));
u_smooth=filtfilt(ones(1,10)/10,1,u);
deployments(dep_nr,1).drifterdata{1,di}(:,5)=u_smooth;
end



% make legend
legendstr=[];
tstart=[];
tend=[];
for drift_nr=1:ndrifters
    legendstr=[legendstr ; num2str(deployments(dep_nr,1).raw.Blueloggers(drift_nr))];

    tstart=min([tstart min(deployments(dep_nr,1).drifterdata{1,drift_nr}(:,3))]);
    tend=max([tend max(deployments(dep_nr,1).drifterdata{1,drift_nr}(:,3))]);
end


jj=0; % counter frames
h=figure(55);
set(h,'PaperSize',[20.98 29.68],'Color',[1 1 1]);

hold all
%plotbathy(XI,YI,ZI)
          %% Clearly mark release point
         for drift_nr=1:ndrifters
            plot(deployments(dep_nr,1).drifterdata{drift_nr}(1,1),deployments(dep_nr,1).drifterdata{drift_nr}(1,2),'.k', 'markersize',15)
         end
   
Colorvect='cwgbmkyr';
         
for t=tstart:2:tend
   percentage=( t-tstart)/(tend-tstart)*100
    for drift_nr=1:ndrifters
        t_ind=find (deployments(dep_nr,1).drifterdata{1,drift_nr}(:,3) == t);
        if t_ind
        figure(55)
        %    scatter(deployments(dep_nr,1).drifterdata{drift_nr}(t_ind,1),deployments(dep_nr,1).drifterdata{drift_nr}(t_ind,2),3,deployments(dep_nr,1).drifterdata{drift_nr}(t_ind,5),'filled');
        plot(deployments(dep_nr,1).drifterdata{drift_nr}(t_ind,1),deployments(dep_nr,1).drifterdata{drift_nr}(t_ind,2),'--.','Color',Colorvect(drift_nr))
        end
 caxis([0.2 0.7])
        legend(legendstr)
        axis equal
        ylabel('Y RD in m')
        xlabel('X RD in m')
     
        %xlim([68600 68900])  
        xlim([68300 68750])  
        
       % ylim([447200 447500])
        ylim([447000 447300])  
        title(['Drifter session ' deployments(dep_nr,1).name ',  t =  ' num2str(t-tstart) ' s'])        
    end
    %%% Save frames 4 movie
jj=jj+1;
%M(jj)=getframe(gcf)
end


%make_movie_gif(M,5,'movingdrifters.gif',100)





function [XI,YI,ZI]=loadbathy

disp('loading bathy...');
A_2=load('XYZ_RD2009_06_09.mat');
B_2=load('XYZ_RD2009_06_11.mat');

XYZjet=A_2.XYZ_RD;
XYZloop=B_2.GPSdata_RD(:,1:3);

XYZ=[XYZloop];%XYZ=[ XYZjet ; XYZloop];

%%% select area of interest 
if 1
    ind=find (XYZ(:,3)< 1.5 & XYZ(:,3)>-3.2);
    XYZ=XYZ(ind,:);
end

gridsize=4;
[XI,YI] = meshgrid(min(XYZ(:,1)):gridsize:max(XYZ(:,1)), min(XYZ(:,2)):gridsize:max(XYZ(:,2)));
%ZI = gridfit(XYZ(:,1),XYZ(:,2),XYZ(:,3),XI(1,:),YI(:,1),'smoothness',0.3); %% smoothed Zvalues 
ZI = griddata(XYZ(:,1),XYZ(:,2),XYZ(:,3),XI,YI);




function plotbathy(XI,YI,ZI)

pcolor(XI,YI,ZI)

hold on;


[C,h] = contour(XI,YI,ZI,'--k');
set(h,'LevelList',[ -2.1:0.3:4],'TextList',[-2.1:0.3:4])
clabel(C,h,'LabelSpacing',72,'FontSize',10,'Color','k','Rotation',0) ;


%
shading interp
axis auto
axis equal
caxis([-2.1 2])
colorbar
