close all
clear
clc

fname = 'all_data5.mat';

%select one realisation to use for example comparison
realisation_index = 1;

db_range = 60;

save_figures_as_png = 1;

%--------------------------------------------------------------------------

%Load fusion method and ROI details
run('fusion_method_list_for_FIGS');

%Load data file
load(fullfile(data_file_location, fname));

defects_to_do = 1; %SDH index
no_realisations = 0;

%convert ROI to indices
if ~isempty(xmin)
    ximin = min(find(data.x >= xmin));
else
    ximin = 1;
end
if ~isempty(xmax)
    ximax = max(find(data.x <= xmax));
else
    ximax = length(data.x);
end
if ~isempty(zmin)
    zimin = min(find(data.z >= zmin));
else
    zimin = 1;
end
if ~isempty(zmax)
    zimax = max(find(data.z <= zmax));
else
    zimax = length(data.z);
end

xplot = data.x(ximin:ximax);
zplot = data.z(zimin:zimax);

no_views = size(data.fe_defects, 3);
no_defects = length(defects_to_do);

%extract relevant regions of data from file
expt_noise = fn_expand_expt_data(data.expt_noise(zimin:zimax, ximin:ximax, :, :), no_realisations);
fe_data = data.fe_defects(zimin:zimax, ximin:ximax, :, defects_to_do);
expt_sdh = data.expt_sdh(zimin:zimax, ximin:ximax, :);

figure;
%plot axes
px = data.x(ximin:ximax) * 1e3;
pz = data.z(zimin:zimax) * 1e3;
norm_val = 0;
for vi1 = 1:7
    for vi2 = 1:3
        vi = (vi1 - 1) * 3 + vi2; %view number from subfigure indices
        for ii = 1:2 %1 - expt, 2 - FE
            subplot(7, 8, (vi1 - 1) * 8 + (vi2 - 1) * 3 + ii);
            switch ii
                case 1
                    tmp = expt_sdh(:, :, vi);
                    str = 'Expt.';
                case 2
                    tmp = fe_data(:, :, vi) + expt_noise(:, :, vi, realisation_index);
                    str = 'Synth.';
            end
            tmp = abs(tmp);
            if ~norm_val
                norm_val = max(tmp(:));
            end
            tmp = 20 * log10(tmp / norm_val);
            im = imagesc(px - px(1), pz - pz(1), tmp);
            axis equal; axis tight;
            text(0, 0, {' ', [' ', data.view_names{vi}]}, 'FontName', 'Calibri', 'Color', 'w', 'FontSize', 8);
            if vi1 == 1
                title(str);
            end
            if vi2 > 1 || ii > 1
                set(gca, 'YTickLabel', []);
            else
                ylabel('z (mm)');
            end
            if vi1 < 7 
                set(gca, 'XTickLabel', []);
            else
                xlabel('x (mm)');
            end
            caxis([-db_range, 0]);
        end
    end
end

axes('Position', [3/8, 1/40, 2/8, 1/40]); imagesc([0, db_range] - db_range, [0,1], linspace(0,1,100));set(gca, 'YTickLabel', []);%title('dB');

o.font = 'Calibri';
o.width = 0.16;
o.height = 0.16;
fn_prepare_figure(o)

if save_figures_as_png
    saveas(gcf, fullfile(fig_file_location,'Fig_S3'), 'png');
end