%function parse_float_data(infile);

%%%
%%% Parse data from Coastal Profiling Float log files.
%%% Laughlin Barker - Summer Intern 2014
%%%

clear; close all

infile = 'C:\Users\lbarker\Documents\MATLAB\float data\final_cropped.xlsx';
[path,name,ext] = fileparts(infile);
cd(path);

[num,txt,raw] = xlsread(infile);

time = num(:,1);
pressure = num(:,4);
bellows = num(:,5);

state = raw(2:end,3);
descendMask = regexp(state, regexptranslate('wildcard','Descend*'));
ascendMask = regexp(state, regexptranslate('wildcard','Ascend*'));
descendLogical = ~cellfun(@isempty,descendMask);
ascendLogical = ~cellfun(@isempty,ascendMask);

[~, descendStart] = max(descendLogical);

missionLogical = ascendLogical + descendLogical;

pmask = ~isnan(pressure);
bmask = ~isnan(bellows);
tmask = ~isnan(time);

ptmask = pmask + tmask == 2;

ptmask(end) = [];

ptdivemask = ptmask + missionLogical == 2;

LPFVel = cell2mat(raw(2:end,21));
diffVel = cell2mat(raw(2:end,22));
LPFAcc = cell2mat(raw(2:end,23));
pressureavg = cell2mat(raw(2:end,24));
pressurevar = cell2mat(raw(2:end,25));
PIDSP = cell2mat(raw(2:end,26));
PIDPV = cell2mat(raw(2:end,27));
y1 = cell2mat(raw(2:end,28));
y2 = cell2mat(raw(2:end,29));
I = cell2mat(raw(2:end,30));

h = figure('DoubleBuffer','on');
set(h,'Position',[100 100 600 600]);
set(gca,'FontSize',20)
t = time(ptmask);
d = -pressure(ptmask);
hold on;
plot(t,d,'b-','LineWidth',2);
plot([100 465],[-7 -7],'r','LineWidth',2)
plot([482 855],[-4 -4],'r','LineWidth',2)
xlabel('Time (s)','FontSize',20);
ylabel('Depth (m)');
axis([0 950 -8 0])

%color bands for 7m hold
p1 = patch([100 465 465 100],[-8 -8 -6 -6],[0 1 0]);
p2 = patch([100 465 465 100],[-6 -6 -5 -5],[1 1 0])

%color bands for 4m hold
p3 = patch([482 855 855 482],[-5 -5 -3 -3],[0 1 0]);
p4 = patch([482 855 855 482],[-5 -5 -6 -6],[1 1 0])
p5 = patch([482 855 855 482],[-3 -3 -2 -2],[1 1 0])
set(p1,'FaceAlpha',0.5);
set(p2,'FaceAlpha',0.5);
set(p3,'FaceAlpha',0.5);
set(p4,'FaceAlpha',0.5);
set(p5,'FaceAlpha',0.5);

dot = line('XData',t(1),'YData',d(1),'Color','r','Marker','.','MarkerSize',30);
mkdir('figs');
tic
for i = 1:length(t)/2;
    set(dot,'XData',t(i*2),'YData',d(i*2))
    drawnow
    saveas(gcf,strcat('figs\',num2str(i),'.tiff'))
end
toc
%make a movie
cd(path);
 cd('figs');
 system('ffmpeg -i %d.tiff -r 24.1 -b 2M -bt 4M -s 1200x1200 -vcodec libx264 output.avi')


%%% make side by side video
%ffmpeg -i test.avi -i test2.avi -filter_complex "[0:v]setpts=PTS-STARTPTS, pad=iw*2:ih[bg]; [1:v]setpts=PTS-STARTPTS[fg]; [bg][fg]overlay=w" output.avi
%%%