%% --- User chooses which dataset to load ---
datasetIdx                = 1;   % <-- change this to 1, 2, 3, ...
datasetName               = sprintf('Dataset_%d', datasetIdx);

% --- Base directory selection ---
useLocalBaseDir           = true;  % <-- set true to use script location, false to use manual path

if useLocalBaseDir
    % Use folder where this script is located
    thisScriptPath        = mfilename('fullpath');
    [thisScriptDir, ~, ~] = fileparts(thisScriptPath);
    baseDir               = fullfile(thisScriptDir, 'Results');
else
    % Use manually specified folder
    baseDir               = 'E:\Results - Experiment\202508\BECToDropletsToStripes\';
end

% --- Build paths ---
dataFile                  = fullfile(baseDir, 'SavedData', [datasetName '.mat']);
figSaveDir                = fullfile(baseDir, 'SavedFigures', datasetName);

% --- Load dataset ---
data                      = load(dataFile);

% --- Ensure figure folder exists ---
if ~exist(figSaveDir, 'dir')
    mkdir(figSaveDir);
end

% --- Access dataset struct dynamically ---
datasetStruct             = data.(datasetName);
compiled_results          = datasetStruct.results;
scan_parameter_values     = datasetStruct.scan_parameter_values;
scan_reference_values     = datasetStruct.scan_reference_values;

% --- Load options used during analysis ---
options                   = datasetStruct.options;
options.font              = 'Bahnschrift';   % override if needed
options.skipSaveFigures   = false;           % override if needed

%% ------------------ Plot Heatmaps ------------------

% Heatmap of radial_spectral_contrast
Plotter.plotHeatmap(compiled_results.spectral_analysis_results, scan_parameter_values, scan_reference_values, 'radial_spectral_contrast', ...
    'Colormap', @Colormaps.coolwarm, ...
    'ColorScale', 'log', ...
    'XLabel', '\alpha (degrees)', ...
    'YLabel', 'B (G)', ...
    'Title', 'Radial Spectral Contrast', ...
    'FontName', options.font, ...
    'FigNum', 9, ...
    'SaveFileName', 'Heatmap_RadialSpectralContrast.fig', ...
    'SaveDirectory', options.saveDirectory);

%% Heatmap of mean_max_g2_values
Plotter.plotHeatmap(compiled_results.custom_g_results, scan_parameter_values, scan_reference_values, 'mean_max_g2', ...
    'PlotDirectly', true, ...
    'Colormap', @Colormaps.coolwarm, ...
    'ColorScale', 'linear', ...
    'XLabel', '\alpha (degrees)', ...
    'YLabel', 'B (G)', ...
    'Title', '$\mathrm{max}[g^{(2)}_{[50,70]}(\delta\theta)]$', ...
    'FontName', options.font, ...
    'FigNum', 10, ...
    'SaveFileName', 'Heatmap_MaxG2.fig', ...
    'SaveDirectory', options.saveDirectory);

%% Heatmap of mean g2 values as specific theta

Plotter.plotG2MeanHeatmap(compiled_results.full_g2_results, pi/2, ...
    scan_reference_values, ...
    'Colormap', @Colormaps.coolwarm, ...
    'CLim', [0 1.0], ...
    'ColorScale', 'log', ...
    'XLabel', '\alpha (degrees)', ...
    'YLabel', 'B (G)', ...
    'Title', options.titleString, ...
    'FontName', options.font, ...
    'FigNum', 11, ...
    'SaveFileName', 'Heatmap_MeanG2.fig', ...
    'SaveDirectory', options.saveDirectory);

%% G2 curves across phase diagram

Plotter.plotG2CurvesMultiParam(compiled_results.full_g2_results, ...
    scan_parameter_values, scan_reference_values, ...
    [2.45, 2.35, 2.32, 2.28], ...
    [0, 10, 20, 30, 40], ...
    'FigNum', 12, ...
    'FontName', options.font, ...
    'FontSize', 14, ...
    'Title', options.titleString, ...
    'Param1Name', 'B', ...
    'Param2Name', '\alpha', ...
    'HighlightEvery', 5, ...          
    'SkipSaveFigures', false, ...
    'SaveFileName', 'G2Curves_PhaseDiagram.fig', ...
    'SaveDirectory', options.saveDirectory);

