
%class = 'Syn';classstr = char(class);
%class = 'Euks';classstr = char(class);
class = {'picoeuk' 'ultraeuk'}; classstr = 'pico_ultraeuk';
%class = 'nanoeuk'; classstr = char(class);

for expt = 12:12
switch expt
    case 12
        tableDilution = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Dilution12_28Apr2018_C164.csv');
        tableGrazer = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Grazer12_28Apr2018_C164.csv');     
    case 11
        tableDilution = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Dilution11_27Apr2018_C144.csv');
        tableGrazer = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Grazer11_27Apr2018_C144.csv');     
    case 10
        tableDilution = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Dilution10_26Apr2018_C132.csv');
        tableGrazer = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Grazer10_26Apr2018_C132.csv');   
    case 9
        tableDilution = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Dilution9_25Apr2018_C113.csv');
        tableGrazer = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Grazer9_25Apr2018_C113.csv');     
    case 8
        tableDilution = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Dilution8_24Apr2018_C101.csv');
        tableGrazer = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Grazer8_24Apr2018_C101.csv');
    case 7
        tableDilution = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Dilution7_23Apr2018_C84.csv');
        tableGrazer = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Grazer7_23Apr2018_C84.csv');
    case 6        
        tableDilution = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Dilution6_23Apr2018_C78.csv');
        tableGrazer = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Grazer6_22Apr2018_C78.csv');
    case 5
        tableDilution = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Dilution5_21Apr2018_C61.csv');
        tableGrazer = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Grazer5_21Apr2018_C61.csv');
    case 4        
        tableGrazer = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Grazer4_20Apr2018_C43.csv');
    case 3
        tableDilution = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Dilution3_20Apr2018.csv');
        tableGrazer = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Grazer3_19Apr2018_C30.csv');
    case 2
        tableGrazer = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Grazer2_18Apr2018_C17.csv');
    case 1
        tableGrazer = importfile_Attune_csv('\\multiproc\science_share\Grazing_AttuneFCM_results\SFD_AR29_Grazer1_17Apr2018_C6.csv');
end

DilutionFlag = 0;
if exist('tableDilution', 'var')
    DilutionFlag = 1;
    gateDilution = unique(tableDilution.Gate);

    for count = 1:length(gateDilution)
        ii = strmatch(gateDilution(count),tableDilution.Gate);
        temp = tableDilution.Concentration(ii);
        %if length(temp) == size(concentration,1)*2; %handle case where Euks appear twice
        if strcmp(gateDilution{count},'Euks')
            temp = temp(1:2:end);
        end
        concDilution(count,:) = temp;
    end
    sampleDilution = tableDilution.Sample(ii)'; 
    expDilution = tableDilution.Experiment(ii);
    WWDind = strmatch('WWD', sampleDilution);
    WWLind = strmatch('WWL', sampleDilution);
    M10Lind = strmatch('Micro10L', sampleDilution);
    M10Dind = strmatch('Micro10D', sampleDilution);
    if isempty(M10Lind)
        M10Lind = strmatch('microl', lower(sampleDilution));
        M10Dind = strmatch('microd', lower(sampleDilution));
    end
    [~,~,cDilution] = intersect(class, gateDilution);
    concDilution2plot = sum(concDilution(cDilution,:),1)*1000; %cells per ml
else
    expDilution{1} = 'No dilution data';
end

gateGrazer = unique(tableGrazer.Gate);

for count = 1:length(gateGrazer)
    ii = strmatch(gateGrazer(count),tableGrazer.Gate);
    temp = tableGrazer.Concentration(ii);
    if strcmp(gateGrazer{count},'Euks')
        temp = temp(1:2:end);
    end
    concGrazer(count,:) = temp;
end
sampleGrazer = tableGrazer.Sample(ii)';
sampleGrazer = regexprep(sampleGrazer, '_', '');
expGrazer = tableGrazer.Experiment(ii);

%Grazer9
switch expt
    case {9 10 11 12}
        WWIind = strmatch('WWInitial', sampleGrazer);
        M10Iind = strmatch('Micro10Initial', sampleGrazer);
    otherwise
        WWIind = strmatch('initial', lower(sampleGrazer));
end

WWDindG = strmatch('WWD', sampleGrazer);
WWLindG = strmatch('WWL', sampleGrazer);
MLindG = strmatch('microl', lower(sampleGrazer));
MDindG = strmatch('microd', lower(sampleGrazer));
DilutionBlankind =  [strmatch('Dilutionblank', sampleGrazer); strmatch('Dilution blank', sampleGrazer)];

[~,~,cGrazer] = intersect(class, gateGrazer);
concGrazer2plot = sum(concGrazer(cGrazer,:),1)*1000; % cells per ml

incTime = 24/24; %days
if DilutionFlag
    WWLmu = log(mean(concDilution2plot(WWLind))./mean(concGrazer2plot(WWIind)))/incTime;
    WWDmu = log(mean(concDilution2plot(WWDind))./mean(concGrazer2plot(WWIind)))/incTime;
    switch expt
        case 99 %{9 10 11 12}
            Micro10Lmu = log(mean(concDilution2plot(M10Lind))./mean(concGrazer2plot(M10Iind)))/incTime;
            Micro10Dmu = log(mean(concDilution2plot(M10Dind))/mean(concGrazer2plot(M10Iind)))/incTime
            DilutionComputed = mean(concGrazer2plot(M10Iind))./mean(concGrazer2plot(WWIind));
            WWLmu0 = WWLmu-(WWLmu-Micro10Lmu)/(1-DilutionComputed)*1;
            WWDmu0 = WWDmu-(WWDmu-Micro10Dmu)/(1-DilutionComputed)*1
            DilutionFactor = DilutionComputed;
        otherwise
            Micro10Lmu = log(mean(concDilution2plot(M10Lind))./mean(concGrazer2plot(WWIind)/10))/incTime;
            Micro10Dmu = log(mean(concDilution2plot(M10Dind))/mean(concGrazer2plot(WWIind)/10))/incTime;
            WWLmu0 = WWLmu-(WWLmu-Micro10Lmu)/.9*1; 
            WWDmu0 = WWDmu-(WWDmu-Micro10Dmu)/.9*1;
            DilutionFactor = .1;
    end;
else
    WWLmu = NaN;
    WWDmu = NaN;
    Micro10Lmu = NaN;
    Micro10Dmu = NaN;
    WWLmu0 = NaN;
    WWDmu0 = NaN;
end
WWLmuG = log(mean(concGrazer2plot(WWLindG))./mean(concGrazer2plot(WWIind)))/incTime;
WWDmuG = log(mean(concGrazer2plot(WWDindG))./mean(concGrazer2plot(WWIind)))/incTime;
MicroLmuG = log(mean(concGrazer2plot(MLindG))./mean(concGrazer2plot(WWIind)))/incTime;
MicroDmuG = log(mean(concGrazer2plot(MDindG))./mean(concGrazer2plot(WWIind)))/incTime;
MicromuGdiff = MicroLmuG-MicroDmuG; 
WWmuGdiff = WWLmuG-WWDmuG;
NetRate_table(expt,:) = table(expDilution(1), expGrazer(1), WWLmu, Micro10Lmu, WWLmuG, MicroLmuG, WWDmu, Micro10Dmu, WWDmuG, MicroDmuG, WWmuGdiff, MicromuGdiff,WWLmu0,WWDmu0)

scrsz = get(groot,'ScreenSize');
figure('Position', scrsz)
if DilutionFlag
    subplot(4,1,1)
    bar(concDilution2plot), set(gca, 'xticklabels', sampleDilution, 'fontsize', 10, 'fontsize', 10,'xticklabelrotation', 20)
    ylabel('Cells (ml^{-1})')
    title([classstr {expDilution{1}}], 'interpreter', 'none')
end
subplot(4,1,2)
bar(concGrazer2plot), set(gca, 'xtick', 1:length(sampleGrazer), 'xticklabels', sampleGrazer, 'fontsize', 10,'xticklabelrotation', 20)
ylabel('Cells (ml^{-1})')
subplot(4,2,5)
if DilutionFlag, plot(1, concDilution2plot(WWLind), 'b*'), end;
hold on
plot(1, concGrazer2plot(WWIind), 'r^')
%plot(1, concGrazer2plot(WWLindG), 'c+')
plot(1, concGrazer2plot(WWLindG), 'go')
text(.05, max(ylim)*.9, 'Light')
text(.1, max(ylim)*.75, 'Initial', 'color', 'r')
text(.1, max(ylim)*.6, 'Final dilution', 'color', 'b')
text(.1, max(ylim)*.45, 'Final grazer', 'color', 'g')
if DilutionFlag, plot(.1, concDilution2plot(M10Lind), 'b*'), end;
%plot(10, concGrazer2plot(M10Iind),'r.')
plot(DilutionFactor, concGrazer2plot(WWIind)/10, 'r^')
ylabel('Cells (ml^{-1})')
%xlabel('Whole water fraction')
xlim([0 1])
subplot(4,2,6)
plot([DilutionFactor 1], [Micro10Lmu WWLmu], '*-')
hold on
plot(0,WWLmu0, '*r')
xlim([0 1])
ylabel('Division rate (d^{-1})')
%xlabel('Whole water fraction')
subplot(4,2,7)
if DilutionFlag, plot(1, concDilution2plot(WWDind), 'b*'), end;
hold on
plot(1, concGrazer2plot(WWIind), 'r^')
plot(1, concGrazer2plot(WWDindG), 'go')
if DilutionFlag, plot(.1, concDilution2plot(M10Dind), 'b*'), end;
%plot(10, concGrazer2plot(M10Iind),'r.')
plot(.1, concGrazer2plot(WWIind)/10, 'r^')
text(.05, max(ylim)*.9, 'Dark')
text(.1, max(ylim)*.75, 'Initial', 'color', 'r')
text(.1, max(ylim)*.6, 'Final dilution', 'color', 'b')
text(.1, max(ylim)*.45, 'Final grazer', 'color', 'g')
ylabel('Cells (ml^{-1})')
xlabel('Whole water fraction')
xlim([0 1])
subplot(4,2,8)
plot([DilutionFactor 1], [Micro10Dmu WWDmu], '*-')
hold on
plot(0,WWDmu0, '*r')
xlim([0 1])
ylabel('Division rate (d^{-1})')
xlabel('Whole water fraction')
orient tall

figure 
bar([concGrazer2plot(DilutionBlankind) concGrazer2plot(WWIind)/10 concGrazer2plot(M10Iind) concGrazer2plot(WWIind)*.1+concGrazer2plot(DilutionBlankind)*.9]);
set(gca, 'xtick', 1:12, 'xticklabels', [sampleGrazer([DilutionBlankind; WWIind; M10Iind]) {'90:10 mixA'} {'90:10 mixB'} {'90:10 mixC'}], 'fontsize', 10,'xticklabelrotation', 20)
title([classstr {expDilution{1}}], 'interpreter', 'none')
%clear tableGrazer tableDilution concGrazer concDilution

end;

writetable(NetRate_table, ['Rates_' classstr '.csv'])
