clear;
clc;

% uncs = 0;  %for figure 6
uncs = 1.2;  %for figure 7
scale = 0.75;

X = readtable(fullfile(pwd, 'sora_data_scored.xlsx'));
fprintf('culmen  %f  %f\n', min(X.Cul), max(X.Cul));
fprintf('tarsus  %f  %f\n', min(X.Tar), max(X.Tar));
unc_suffix = '';

tar1 = 38.2;
cul1 = (82.24427269 - 0.81710151*tar1) / 2.76535596;
tar2 = 26.5;
cul2 = (82.24427269 - 0.81710151*tar2) / 2.76535596;
if uncs > 0
    filename = fullfile(pwd, 'Plots', sprintf('Figure7_scatter_all_sora%s.png', unc_suffix));
    %fast uncertainty patches
    ptar1 = tar1;
    pcul1 = (-uncs + 82.24427269 - 0.81710151*ptar1) / 2.76535596;
    ptar2 = tar1;
    pcul2 = (uncs + 82.24427269 - 0.81710151*ptar2) / 2.76535596;
    ptar3 = tar2;
    pcul3 = (uncs + 82.24427269 - 0.81710151*ptar3) / 2.76535596;
    ptar4 = tar2;
    pcul4 = (-uncs + 82.24427269 - 0.81710151*ptar4) / 2.76535596;
    unc = zeros(5, 2);
    unc(1, 1) = pcul1;
    unc(1, 2) = ptar1;
    unc(2, 1) = pcul2;
    unc(2, 2) = ptar2;
    unc(3, 1) = pcul3;
    unc(3, 2) = ptar3;
    unc(4, 1) = pcul4;
    unc(4, 2) = ptar4;
    unc(5, 1) = pcul1;
    unc(5, 2) = ptar1;
    unc_suffix = sprintf('_%.2f', uncs);
else
    filename = fullfile(pwd, 'Plots', sprintf('Figure6_scatter_all_sora%s.png', unc_suffix));
end

W = 600 * scale;
H = 525 * scale;
axis_fontsize = 14 * scale;
lgn_fontsize = 11 * scale;
mm = [0 0 1];
mf = [0.7 0 0];
fm = [0 0 0.7];
ff = [1 0 0];

X2018 = X(X.CaptureYear==2018, :);
idG = X2018.score > 0;
idD = X2018.DNA_male;
idMM2018 = idG & idD;
idMF2018 = idG & ~idD;
idFM2018 = ~idG & idD;
idFF2018 = ~idG & ~idD;

X2020 = X(X.CaptureYear==2020, :);
idG = X2020.score > 0;
idD = X2020.DNA_male;
idMM2020 = idG & idD;
idMF2020 = idG & ~idD;
idFM2020 = ~idG & idD;
idFF2020 = ~idG & ~idD;

fig = figure('Units','pixels', 'Position',[300, 300, W, H]);
hold on
if uncs > 0
    patch(unc(:,1), unc(:,2), 'k', 'FaceAlpha',0.1, 'EdgeColor','none');
end

p = [];
if sum(idMM2018) > 0
    p1 = scatter(X2018.Cul(idMM2018), X2018.Tar(idMM2018), 27 * scale, 'o', 'filled', 'MarkerFaceColor',mm,...
        'MarkerEdgeColor','none', 'DisplayName','2018 M correct calls');
    p = [p p1];
end
if sum(idFM2018) > 0
    p1 = scatter(X2018.Cul(idFM2018), X2018.Tar(idFM2018), 27 * scale, '^', 'filled', 'MarkerFaceColor',fm,...
        'MarkerEdgeColor','none', 'DisplayName','2018 M missed calls');
    p = [p p1];
end
if sum(idFF2018) > 0
    p1 = scatter(X2018.Cul(idFF2018), X2018.Tar(idFF2018), 33 * scale, 's', 'filled', 'MarkerFaceColor',ff,...
        'MarkerEdgeColor','none', 'DisplayName','2018 F correct calls');
    p = [p p1];
end
if sum(idMF2018) > 0
    p1 = scatter(X2018.Cul(idMF2018), X2018.Tar(idMF2018), 33 * scale, 'd', 'filled', 'MarkerFaceColor',mf,...
        'MarkerEdgeColor','none', 'DisplayName','2018 F missed calls');
    p = [p p1];
end
if sum(idMM2020) > 0
    p1 = scatter(X2020.Cul(idMM2020), X2020.Tar(idMM2020), 25 * scale, 'o',...
        'MarkerEdgeColor',mm, 'LineWidth',1, 'DisplayName','2020 M correct calls');
    p = [p p1];
end
if sum(idFM2020) > 0
    p1 = scatter(X2020.Cul(idFM2020), X2020.Tar(idFM2020), 25 * scale, '^',...
        'MarkerEdgeColor',fm, 'LineWidth',1, 'DisplayName','2020 M missed calls');
    p = [p p1];
end
if sum(idFF2020) > 0
    p1 = scatter(X2020.Cul(idFF2020), X2020.Tar(idFF2020), 30 * scale, 's',...
        'MarkerEdgeColor',ff, 'LineWidth',1, 'DisplayName','2020 F correct calls');
    p = [p p1];
end
if sum(idMF2020) > 0
    p1 = scatter(X2020.Cul(idMF2020), X2020.Tar(idMF2020), 30 * scale, 'd',...
        'MarkerEdgeColor',mf, 'LineWidth',1, 'DisplayName','2020 F missed calls');
    p = [p p1];
end

line([cul1 cul2], [tar1 tar2], 'Color','k');
hold off
xlim([16.5 24.2]);
xticks([16 17 18 19 20 21 22 23 24]);
ylim([26.5 38.2]);
yticks([29 30 31 32 33 34 35 36 37 38]);
xlabel('Culmen length (mm)');
y = ylabel('Tarsometatarsus length (mm)');
pos = y.Position;
pos(1) = 15.9;
y.Position = pos;
% title('All Sora');
box on
set(gca, 'FontSize',axis_fontsize);
legend(p, 'Location','southwest', 'FontSize',lgn_fontsize, 'NumColumns',2);
SaveFigure(fig, filename);
% close(fig);
