close all
clear all


IFCB_filenames{1} = {'4-2-2014-stream\features\p1-upcast\b1-s1\D20140402T165049_IFCB102_features.csv' ...
                     '4-2-2014-stream\features\p1-upcast\b3-s2\D20140402T165654_IFCB102_features.csv' ...
                     };  %Cast 1
IFCB_filenames{2} =  {'4-2-2014-stream\features\p2-upcast\b1-s1\D20140402T180557_IFCB102_features.csv' ...
                     '4-2-2014-stream\features\p2-upcast\b4-s2\D20140402T181443_IFCB102_features.csv' ...
                     '4-2-2014-stream\features\p2-upcast\b5-s3\D20140402T182158_IFCB102_features.csv' ...
                     '4-2-2014-stream\features\p2-upcast\b11-s4\D20140402T183008_IFCB102_features.csv' ...
                     };  %Cast 2
IFCB_filenames{3} =  {'4-2-2014-stream\features\p3-upcast\b1-s1\D20140402T194827_IFCB102_features.csv' ...
                     '4-2-2014-stream\features\p3-upcast\b2-s2\D20140402T195404_IFCB102_features.csv' ...
                     '4-2-2014-stream\features\p3-upcast\b3-s3\D20140402T195854_IFCB102_features.csv' ...
                     };  %Cast 3
IFCB_filenames{4} = {'4-2-2014-stream\features\p4-upcast\b1-s1\D20140402T210036_IFCB102_features.csv' ...
                     '4-2-2014-stream\features\p4-upcast\b2-s2\D20140402T210544_IFCB102_features.csv' ...
                     '4-2-2014-stream\features\p4-upcast\b3-s3\D20140402T210915_IFCB102_features.csv' ...
                     '4-2-2014-stream\features\p4-upcast\b4-s4\D20140402T211205_IFCB102_features.csv' ...
                     };  %Cast 4

IFCB_PixelsPerMicron = 3.4;

for i = 1:length(IFCB_filenames)  %i = cast number
    
    for j = 1:length(IFCB_filenames{i})  %j = stop number
    
      [data, headertext] = xlsread(IFCB_filenames{i}{j});
    
      IFCB_ESD{i}{j} = data(:,9)/IFCB_PixelsPerMicron;
      hr = IFCB_filenames{i}{j}(end-26:end-25);
      min = IFCB_filenames{i}{j}(end-24:end-23);
      sec = IFCB_filenames{i}{j}(end-22:end-21);
      IFCB_datenum{i}{j} = datenum(2014,4,2,str2double(hr),str2double(min),str2double(sec));
    
      filenm = ['FlowCam/c' num2str(i) '_s' num2str(j) '.csv'];
      if(exist(filenm))
        [data, headertext] = xlsread(filenm);
    
        FC_ESD{i}{j} = data(:,2);
        FC_PartPerMl{i}{j} = data(1,3);
        FC_Depth{i}{j} = data(1,4);
      else
        FC_ESD{i}{j} = [];
        FC_PartPerMl{i}{j} = 0;
      end
      
      
    end
    
end

bins = 0:2:30;

IFCB_VolumeAnalyzed = .4;  %ml

for i = 1:length(IFCB_filenames)  %i = cast number
  for j = 1:length(IFCB_filenames{i})  %j = stop number;  %Stop Number
  idx = find(IFCB_ESD{i}{j} > 0);  %Skip zeros
  [nelements_IFCB_ESD{i}{j},centers_IFCB_ESD{i}{j}] = hist(IFCB_ESD{i}{j}(idx),bins);
  npartperml_IFCB_ESD{i}{j} = (nelements_IFCB_ESD{i}{j})/IFCB_VolumeAnalyzed;

  [nelements_FC_ESD{i}{j},centers_FC_ESD{i}{j}] = hist(FC_ESD{i}{j},bins);
  npartperml_FC_ESD{i}{j} = nelements_FC_ESD{i}{j}*FC_PartPerMl{i}{j}/sum(nelements_FC_ESD{i}{j});
  depth{i}(j) = -10*j;  %Temporary example, need to fill in with actual depth.
  %figure
  %bar(centers_IFCB_ESD{i}{j},nelements_IFCB_ESD{i}{j});
  end
end


for i = 1:length(IFCB_filenames)  %i = cast number
figure('Position',[779 88 1225 1417],'PaperSize',[11 17],'PaperOrientation','portrait','PaperPositionMode','auto','PaperPosition',[.5 .5 10.5 16.5])
  for j = 1:length(IFCB_filenames{i})  %j = stop number;  %Stop Number
    subplot(2,2,j)
      bar(centers_IFCB_ESD{i}{j}-2,[npartperml_IFCB_ESD{i}{j}'  npartperml_FC_ESD{i}{j}'],'grouped');
      set(gca,'FontSize',13);
      xlabel('ESD (microns)')
      ylabel('Particles per ml Observed');
      title(['Cast ' num2str(i) ' - Stop ' num2str(j)  '   ' num2str(FC_Depth{i}{j},3)  'm   2 micron bins']);
      lh = legend(['IFCB Results - ' num2str(sum(npartperml_IFCB_ESD{i}{j})) ' Particles per ml'],['FlowCam Results - ' num2str(sum(FC_PartPerMl{i}{j})) ' Particles per ml'],'Location','NorthEast');
      set(lh,'FontSize',9);
      print('-dpdf',['ComparisonHistograms_Cast' num2str(i)]);
  end
end





