close all
clear all

time_offset_from_intake = 0;

%Process lab-analyzed samples.
LabProcessedDataPath = '000_LabPrePost_Samples/';

files = dir([LabProcessedDataPath '*_summary_export.csv']);

plot_num = 0;
hists_per_page = 6;
page_num = 0;

bin_width = 2;

bins = 0:bin_width:120;
bins = bins(1:end-1)+bin_width/2;  %Convert specified bin edges to bin centers for use with hist.

ListFileNames{1} = '120-162942';    Identifier{1} = 'Nano-Pre';  
ListFileNames{2} = '120-164958';    Identifier{2} = 'Nano-Post';  
ListFileNames{3} = '120-173130';    Identifier{3} = 'Paragon-Pre';  
ListFileNames{4} = '120-175124';    Identifier{4} = 'Paragon-Post';  
ListFileNames{5} = '120-181349';    Identifier{5} = 'Iso-Pre';  
ListFileNames{6} = '120-183213';    Identifier{6} = 'Iso-Post';  
ListFileNames{7} = '120-185111';    Identifier{7} = 'Beach-Pre';  
ListFileNames{8} = '120-193151';    Identifier{8} = 'Chaet-Pre';  
ListFileNames{9} = '120-195002';    Identifier{9} = 'Chaet-Post';  
ListFileNames{10} = '120-200839';    Identifier{10} = 'Dun-Pre'; 
ListFileNames{11} = '120-204257';    Identifier{11} = 'Dun-Post';  
ListFileNames{12} = '120-210059';    Identifier{12} = 'Beach-Post';  

FC_filenames{1} = {'InstrumentIntercomparisons\fc-pump-test\nano-pre' ...
                   'InstrumentIntercomparisons\fc-pump-test\nano-post' ...
                  };  
FC_filenames{2} = {'InstrumentIntercomparisons\fc-pump-test\para-pre' ...
                   'InstrumentIntercomparisons\fc-pump-test\para-post' ...
                  };  
FC_filenames{3} = {'InstrumentIntercomparisons\fc-pump-test\iso-pre' ...
                   'InstrumentIntercomparisons\fc-pump-test\iso-post' ...
                  };                
FC_filenames{4} = {'InstrumentIntercomparisons\fc-pump-test\beach-pre' ...  
                  'InstrumentIntercomparisons\fc-pump-test\beach-post' ... 
                  }; 
FC_filenames{5} = {'InstrumentIntercomparisons\fc-pump-test\chaet-pre' ...
                   'InstrumentIntercomparisons\fc-pump-test\chaet-post' ...
                  };  
FC_filenames{6} = {'InstrumentIntercomparisons\fc-pump-test\dun-pre' ...
                   'InstrumentIntercomparisons\fc-pump-test\dun-post' ...
                  };  


SampleMap{1} = [1 2];  %Nano Pre and Post
SampleMap{2} = [3 4];  %Paragon Pre and Post
SampleMap{3} = [5 6];  %Iso Pre and Post
SampleMap{4} = [7 12];  %Beach Pre and Post
SampleMap{5} = [8 9];  %Chaet Pre and Post
SampleMap{6} = [10 11];  %Beach Pre and Post

SampleNames{1} = 'Nano';
SampleNames{2} = 'Paragon';
SampleNames{3} = 'Iso';
SampleNames{4} = 'Beach';
SampleNames{5} = 'Chaet';
SampleNames{6} = 'Dun';


for i =  1:length(files)
   i
   num_str{i} = files(i).name(1:10);
   
   hr_str{i} = files(i).name(5:6);
   min_str{i} = files(i).name(7:8);
   sec_str{i} = files(i).name(9:10);

   
   Lab_summary_data{i} = ReadSummaryExport([LabProcessedDataPath files(i).name]);
   
   Lab_ParticlesPerml(i) = Lab_summary_data{i}.ParticlesPerml 

   
   Lab_DateNumStartTime(i) = datenum(Lab_summary_data{i}.StartTime)+6/24-time_offset_from_intake/86400;  %Convert to UTC including 1Hr offset of FlowCam clock. (UTC is +7, but clock was one hour fast during April 2nd, 2014 cruise...)
   Lab_DateNumTime(i) = datenum(2014,4,3,str2double(hr_str{i}),str2double(min_str{i}),str2double(sec_str{i}))+6/24-time_offset_from_intake/86400; 
   
   Lab_SamplingSeconds(i) =  Lab_summary_data{i}.SamplingSeconds;
   
   
   [data{i}, headertext{i}] = xlsread([LabProcessedDataPath num_str{i} '_data_export.csv']);
   
   Lab_ESD{i} = data{i}(:,11);
   
   
   [Lab_nelements_ESD{i},Lab_centers_ESD{i}] = hist(Lab_ESD{i},bins)
     
   Lab_n_part_per_ml{i} = [Lab_ParticlesPerml(i)*Lab_nelements_ESD{i}/sum(Lab_nelements_ESD{i})];
   
   plot_num = plot_num+1;
   if(plot_num > hists_per_page) 
       plot_num = 1; 
   end
   if(plot_num == 1) 
     figure('Position', [705 72 1138 1426],'PaperSize',[11 17],'PaperOrientation','portrait','PaperPositionMode','auto','PaperPosition',[.5 .5 10.5 16.5])
   end
   subplot(hists_per_page/2,2,plot_num)
   bar(Lab_centers_ESD{i},Lab_n_part_per_ml{i},1);
   xlabel('Particle Size (ESD - microns)');
   ylabel('Particles per ml');
   title([Identifier{i} '   ' num2str(Lab_SamplingSeconds(i)) ' sec sample   Particles per ml = ' num2str(Lab_ParticlesPerml(i))]); ...
          
   %title([num2str(Lab_SamplingSeconds(i)) ' sec sample starting at ' datestr(Lab_DateNumTime(i)) '  Particles per ml = ' num2str(Lab_ParticlesPerml(i)) ...
   %       '\newline  ' Identifier{i}]);
      
   if(plot_num == hists_per_page) 
     page_num = page_num+1;
     print('-dpdf',['04302014_Lab_ESD_Histogram_Pg_' num2str(page_num)]);
   end
      
end


   maxsize(1) = 30;
   maxsize(2) = 60;
   maxsize(3) = 30;
   maxsize(4) = 30;
   maxsize(5) = 30;
   maxsize(6) = 30;

   
   
figure('Position', [705 72 1138 1426],'PaperSize',[11 17],'PaperOrientation','portrait','PaperPositionMode','auto','PaperPosition',[.5 .5 10.5 16.5])
for i = 1:6
ah = subplot(3,2,i);
set(ah,'FontSize',14);
bar(Lab_centers_ESD{SampleMap{i}(1)},[Lab_n_part_per_ml{SampleMap{i}(1)}'  Lab_n_part_per_ml{SampleMap{i}(2)}'],'grouped');
ylabel('Particles per ml');
xlabel('Size (ESD - microns)');
lh = legend([SampleNames{i} '-Pre  ' num2str(Lab_ParticlesPerml(SampleMap{i}(1))) ' ppml'],[SampleNames{i} '-Post  ' num2str(Lab_ParticlesPerml(SampleMap{i}(2))) ' ppml']);
set(lh,'FontSize',12);
xlim([0 maxsize(i)]);
title([SampleNames{i} ' size distributions']);



end
print('-dpdf',['04302014_ESD_PumpingComparisons']);  


for i = 1:6 
    for j = 1:2
      ESD = Lab_ESD{SampleMap{i}(j)}; 
      Lab_PPml = Lab_ParticlesPerml(SampleMap{i}(j));
      Lab_sampsecs = Lab_SamplingSeconds(SampleMap{i}(j));
      save(FC_filenames{i}{j},'ESD','Lab_PPml','Lab_sampsecs');
      clear ESD;
    end
end

   
