%script to extract analyzed data from ws6*.MAT files and generate plots


%% Load Data
pn_ws6='Results2/MAT'; %path to MAT files generated by plot_ws6
xlsfile='Results2/ptvpiv_20130206_bottom.xlsx';
sheet1='1006_ws6Analysis';
sheet2='ADV';
sheet3='burst_avg_bottom_only'

s=loadxls_struct(xlsfile,sheet1); %load input from xls as struct
adv=loadxls_struct(xlsfile,sheet2); %load input from xls as struct
bottom=loadxls_struct(xlsfile,sheet3);
in=logical([s.flag1,s.flag2,s.flag3,s.flag4]); %only requested and valid cases
I=find(all(in,2)); %gives indices to requested cases
clear a
for k=1:length(I);
    K=I(k); %index into struct: s
    matfile=fullfile(pn_ws6,strrep(s.file_ptv{K},'ptv_','ws6_'));
    a(k)=load(matfile);
end

%Time Data
%Construct PICS time from filename
fn={a.filename}'; %store the filenames in a cell array
dstr=regexp(fn,'\d*','match','once'); %extract all numeric content from cell array
mtime=datenum(dstr,'yyyymmddHHMMSS'); %convert string to serial time with yyyymmddHHMMSS format
%datestr(mtime) %checks the conversion
%Construct ADV time from time string
adv.mtime=datenum(adv.time);
(bottom.start_time)
bottom.mtime=datenum(bottom.start_time)
%% Plot Data d50m
figure(5);
picsfield='d50m';
%plot(mtime,[a.(picsfield)],'bo',adv.mtime,adv.ws_adv,'rx',...
%    'LineWidth',2)
plot(mtime,[a.(picsfield)],'bo',  'LineWidth',2)
datetick('x')
% set(gca,'YLim',[0,1])
%ylabel('\itW_s\rm  [mm\cdots^{ -1}]') %settling velocity d50 label
ylabel('\itd_{50}\rm  [\mum]')  %mass d50 label
xlabel('time')
legend({['pics-',picsfield],'adv'},'Interpreter','none')

%% Plot Data ws50m
figure(8);
picsfield='ws50m';
plot(mtime,[a.(picsfield)],'bo',adv.mtime,adv.ws_adv,'rx',...
    'LineWidth',2)
%plot(mtime,[a.(picsfield)],'bo',  'LineWidth',2)

datetick('x')
% set(gca,'YLim',[0,1])
ylabel('\itW_s\rm  [mm\cdots^{ -1}]') %settling velocity d50 label
%ylabel('\itd_{50}\rm  [\mum]')  %mass d50 label
xlabel('time')
legend({['pics-',picsfield],'adv'},'Interpreter','none')

WS50m_pics(1,:)=mtime;
WS50m_pics(2,:)=[a.(picsfield)];

save WS50m_pics.txt  WS50m_pics -ascii


%% Plot Data velocity
figure(7);
%picsfield='ws50m';
plot(bottom.mtime,bottom.u,'rx',...
    'LineWidth',2)
%plot(mtime,[a.(picsfield)],'bo',  'LineWidth',2)
datetick('x')
% set(gca,'YLim',[0,1])
ylabel('Velocity (cm/sec)') %settling velocity d50 label
%ylabel('\itd_{50}\rm  [\mum]')  %mass d50 label
xlabel('time')
legend({'adv- U'},'Interpreter','none')

%% Plot Data TSS percent pellets
figure(9);
%picsfield='ws50m';
plot(bottom.mtime,bottom.TSS_percent_pellets,'rx',...
    'LineWidth',2)
%plot(mtime,[a.(picsfield)],'bo',  'LineWidth',2)
datetick('x')
% set(gca,'YLim',[0,1])
ylabel('TSS percent pellets') %settling velocity d50 label
%ylabel('\itd_{50}\rm  [\mum]')  %mass d50 label
xlabel('time')
legend({'percent pellets'},'Interpreter','none')

%% Plot Data ws50m_floc, ws50m_bedagg,ws50m_primary
figure(11);
picsfield1='ws50m_floc';
picsfield2='ws50m_bedagg';
picsfield3='ws50m_primary';
%plot(mtime,[a.(picsfield)],'bo',adv.mtime,adv.ws_adv,'rx',...
%    'LineWidth',2)
plot(mtime,[a.(picsfield1)],'bo',  'LineWidth',2)
hold on
plot(mtime,[a.(picsfield2)],'ro',  'LineWidth',2)
plot(mtime,[a.(picsfield3)],'go',  'LineWidth',2)

datetick('x')
% set(gca,'YLim',[0,1])
ylabel('\itW_s\rm  [mm\cdots^{ -1}]') %settling velocity d50 label
%ylabel('\itd_{50}\rm  [\mum]')  %mass d50 label
xlabel('time')
legend({['pics-',picsfield1],['pics-',picsfield2],['pics-',picsfield3]},'Interpreter','none')

%% Plot Data ws50m_floc, d50m_bedagg,ws50m_primary
figure(12);
picsfield1='d50m_floc';
picsfield2='d50m_bedagg';
picsfield3='d50m_primary';
%plot(mtime,[a.(picsfield)],'bo',adv.mtime,adv.ws_adv,'rx',...
%    'LineWidth',2)
plot(mtime,[a.(picsfield1)],'bo',  'LineWidth',2)
hold on
plot(mtime,[a.(picsfield2)],'ro',  'LineWidth',2)
plot(mtime,[a.(picsfield3)],'go',  'LineWidth',2)

datetick('x')
% set(gca,'YLim',[0,1])
%ylabel('\itW_s\rm  [mm\cdots^{ -1}]') %settling velocity d50 label
ylabel('\itd_{50}\rm  [\mum]')  %mass d50 label
xlabel('time')
legend({['pics-',picsfield1],['pics-',picsfield2],['pics-',picsfield3]},'Interpreter','none')

