%% Parameters

% === Define folders and settings ===

baseFolder = '//DyLabNAS/Data/TwoDGas/2025/04/';

dates = ["01", "02"];  % Example: three folders
runs = {
    ["0059", "0060", "0061"],                        
    ["0007", "0008", "0009", "0010", "0011"]         
};

options.scan_parameter     = 'rot_mag_fin_pol_angle';
options.scan_groups        = 0:10:50;
options.cam                = 5;

% Image cropping and alignment
options.angle              = 0;
options.center             = [1285, 2100];
options.span               = [200, 200];
options.fraction           = [0.1, 0.1];

% Imaging and calibration parameters
options.pixel_size         = 5.86e-6;         % in meters
options.magnification      = 23.94;
options.removeFringes      = false;
options.ImagingMode        = 'HighIntensity';
options.PulseDuration      = 5e-6;

% Fourier analysis: Radial
options.theta_min          = deg2rad(0);
options.theta_max          = deg2rad(180);
options.N_radial_bins      = 500;
options.Radial_Sigma       = 2;
options.Radial_WindowSize  = 5;  % Must be odd

% Fourier analysis: Angular
options.r_min              = 10;
options.r_max              = 20;
options.k_min              = 1.2771;    % in μm⁻¹
options.k_max              = 2.5541;    % in μm⁻¹
options.N_angular_bins     = 180;
options.Angular_Threshold  = 75;
options.Angular_Sigma      = 2;
options.Angular_WindowSize = 5;

% Optional visualization / zooming
options.zoom_size          = 50;

% Optional flags or settings struct
options.skipUnshuffling    = false;
options.skipPreprocessing  = true;
options.skipMasking        = true;
options.skipIntensityThresholding = true;
options.skipBinarization   = true;

% === Loop through folders and collect results ===

results_all = [];

assert(length(dates) == length(runs), ...
    'Each entry in `dates` must correspond to a cell in `runs`.');

for i = 1:length(dates)
    currentDate = dates(i);
    currentRuns = runs{i};
    
    for j = 1:length(currentRuns)
        runID = currentRuns(j);
        folderPath = fullfile(baseFolder, currentDate, runID);
        
        if ~endsWith(folderPath, filesep)
            options.folderPath = [char(folderPath) filesep];
        else
            options.folderPath = char(folderPath);
        end 
        
        try
            % Unpack options struct into name-value pairs
            args = [fieldnames(options), struct2cell(options)]';
            args = args(:)';
            
            results = analyzeFolder(args{:});
            results_all = [results_all; results];
        catch ME
            warning("Error processing %s/%s: %s", currentDate, runID, ME.message);
        end
    end
end


%%  Plotting heatmap of mean_max_g2_values

N_x = length(options.scan_groups);
N_y = length(results_all);

BFields = [2.35, 2.15, 2.0, 1.85, 1.7, 1.55, 1.4, 1.35];

% Preallocate
g2_matrix = zeros(N_y, N_x);

for i = 1:N_y
    for j = 1:N_x
        g2_matrix(i, j)    = results_all(i).mean_max_g2_values(j);
    end
end

% Plot heatmap

font                        = 'Bahnschrift';

figure(1)
clf
set(gcf,'Position',[50 50 950 750])
imagesc(options.scan_groups, BFields, g2_matrix);
colormap(sky);
clim([0, 1])
set(gca, 'FontSize', 14, 'YDir', 'normal');
hXLabel = xlabel('\alpha (degrees)', 'Interpreter', 'tex');
hYLabel = ylabel('BField (G)', 'Interpreter', 'tex');
hTitle  = title('$\mathrm{max}[g^{(2)}_{[50,70]}(\delta\theta)]$', 'Interpreter', 'latex');
set([hXLabel, hYLabel], 'FontSize', 14)
set(hTitle, 'FontSize', 16, 'FontWeight', 'bold');  % Set font and size for title
colorbar;

%% Heat map of radial spectral contrast

N_x     = length(options.scan_groups);
N_y     = length(results_all);

BFields = [2.35, 2.15, 2.0, 1.85, 1.7, 1.55, 1.4, 1.35];

% Preallocate
radial_spectral_contrast_matrix = zeros(N_y, N_x);

for i = 1:N_y
    for j = 1:N_x
        radial_spectral_contrast_matrix(i, j)    = results_all(i).radial_spectral_contrast(j);
    end
end

% Plot heatmap

font    = 'Bahnschrift';

figure(3)
clf
set(gcf,'Position',[50 50 950 750])
imagesc(options.scan_groups, BFields, radial_spectral_contrast_matrix);
colormap(sky);
clim([0 0.008])
set(gca, 'FontSize', 14, 'YDir', 'normal');
hXLabel = xlabel('\alpha (degrees)', 'Interpreter', 'tex');
hYLabel = ylabel('BField (G)', 'Interpreter', 'tex');
hTitle  = title('Radial Spectral Contrast');
set([hXLabel, hYLabel], 'FontSize', 14)
set(hTitle, 'FontName', font, 'FontSize', 16, 'FontWeight', 'bold');  % Set font and size for title
colorbar;
