%%% Analyse Drifter deployment

clear all
clc

% load drifter data
list=dir('*RD*mat');
if length(list)==0
    error('no .MAT files found with RD in the filename')
else
    disp(['loading ' num2str(length(list)) ' .mat files' ])
    for j=1:length(list)
        load (list(j).name)
    end
end

Deploymenttimes  %% read in drifter deployment times

for dep_nr=1:ndeployments
    tempstr=num2str(deployments(dep_nr,1).raw.deploy_timesHHMMSS,'%06.0f');
    deployments(dep_nr,1).raw.deploy_times_smidnight=str2num(tempstr(:,1:2))*3600+str2num(tempstr(:,3:4))*60+str2num(tempstr(:,5:6));

    tempstr=num2str(deployments(dep_nr,1).raw.retrieval_timesHHMMSS,'%06.0f');
    deployments(dep_nr,1).raw.retrieval_times_smidnight=str2num(tempstr(:,1:2))*3600+str2num(tempstr(:,3:4))*60+str2num(tempstr(:,5:6));
end

for dep_nr=1:ndeployments;
    ndrifters=length(deployments(dep_nr,1).raw.Blueloggers);
   
    for drift_nr=1:ndrifters
        nr=deployments(dep_nr,1).raw.Blueloggers(drift_nr);
        Drifterdata=eval(['BlueLoggerRD' num2str(nr)]);
        
        t_dep=deployments(dep_nr,1).raw.deploy_times_smidnight(drift_nr);
        t_ret=deployments(dep_nr,1).raw.retrieval_times_smidnight(drift_nr);
        ind=find(Drifterdata(:,3)>t_dep & Drifterdata(:,3)<t_ret); 
        deployments(dep_nr,1).drifterdata{drift_nr}=Drifterdata(ind,:);
    end

end
filename=['Driftersession' deployments(1,1).name '.mat'];
   save(filename, 'deployments')
disp([filename ' file saved'] )

% 
% t_dep=min(deployments(dep_nr,1).retrieval_timesHHMMSS)
% t_ret=max(deployments(dep_nr,1).retrieval_timesHHMMSS)
% 

% 
