%% ===== BEC-Droplets/Stripes: Combined Mean & Variance Across α =====
clear; clc; close all;

% ===== User Settings =====
dataFiles = { ...
    'C:\Users\Karthik-OfficePC\Documents\GitRepositories\Calculations\Data-Analyzer\+Scripts\Comparison\Cumulants\BECToDroplets_MeanVariance_AllThetaData.mat', ...
    'C:\Users\Karthik-OfficePC\Documents\GitRepositories\Calculations\Data-Analyzer\+Scripts\Comparison\Cumulants\BECToStripes_MeanVariance_AllThetaData.mat' ...
};

desiredTheta = [pi/6, pi/3];   % radians

% Use your pi-fraction formatter for labels
thetaLabels = arrayfun(@(th) ...
    ['\theta = ' strrep(thetaToPiStr(th), '\\pi', '\pi')], ...
    desiredTheta, 'UniformOutput', false);


fontName = 'Bahnschrift';
fontSize = 14;

% ===== Load Cumulant Data =====
allCumulants = [];
for i = 1:numel(dataFiles)
    S = load(dataFiles{i});
    data = S.cumulantData;

    if isempty(allCumulants)
        allCumulants = data;
    else
        allCumulants = [allCumulants, data]; %#ok<AGROW>
    end
end

% ===== Colormap (coolwarm trimmed) =====
fullCmap = Colormaps.coolwarm(256);
blueSide = fullCmap(1:100,:);
redSide  = fullCmap(157:end,:);
trimmedCmap = [blueSide; redSide];
indices = round(linspace(1, size(trimmedCmap,1), size(allCumulants,2)));
cmap = trimmedCmap(indices,:);

% ===== Plot & Save One Figure Per θ =====
saveDir = fileparts(mfilename('fullpath'));

for t = 1:numel(desiredTheta)
    
    fig = figure(t);
    set(fig, 'Color','w','Position',[100 100 1250 550]);
    tLayout = tiledlayout(1,2,'TileSpacing','Compact','Padding','Compact');
    sgtitle(sprintf('%s', thetaLabels{t}), ...
            'FontName',fontName,'FontSize',fontSize+4);

    % --- κ1 Mean ---
    ax1 = nexttile; hold(ax1,'on');
    for s = 1:size(allCumulants,2)
        plot(ax1, allCumulants(t,s).scan_vals, allCumulants(t,s).mean, '-o', ...
            'Color', cmap(s,:), 'LineWidth', 2, 'MarkerSize', 8, ...
            'MarkerFaceColor', cmap(s,:), 'DisplayName', allCumulants(t,s).label);
    end
    xlabel(ax1,'B (G)'); ylabel(ax1,'\kappa_1');
    title(ax1,'Mean'); grid(ax1,'on');
    legend(ax1,'Location','northeast');
    set(ax1,'FontName',fontName,'FontSize',fontSize);

    % --- κ2 Variance ---
    ax2 = nexttile; hold(ax2,'on');
    for s = 1:size(allCumulants,2)
        plot(ax2, allCumulants(t,s).scan_vals, allCumulants(t,s).variance, '-o', ...
            'Color', cmap(s,:), 'LineWidth', 2, 'MarkerSize', 8, ...
            'MarkerFaceColor', cmap(s,:), 'DisplayName', allCumulants(t,s).label);
    end
    xlabel(ax2,'B (G)'); ylabel(ax2,'\kappa_2');
    title(ax2,'Variance'); grid(ax2,'on');
    legend(ax2,'Location','northeast');
    set(ax2,'FontName',fontName,'FontSize',fontSize);
end

%% ===== Droplets-Stripes: Combined Mean & Variance Across α =====
clear; clc; close all;

% ---- User: path to saved .mat from previous script ----
% loadFile = 'C:\Users\Karthik-OfficePC\Documents\GitRepositories\Calculations\Data-Analyzer\+Scripts\Comparison\Cumulants\DropletsToStripes_MeanVariance_AllThetaData.mat';
loadFile = 'C:\Users\Karthik-OfficePC\Documents\GitRepositories\Calculations\Data-Analyzer\+Scripts\Comparison\Cumulants\StripesToDroplets_MeanVariance_AllThetaData.mat';

S = load(loadFile);
cumulantData = S.cumulantData;
options = S.options;

N_theta = size(cumulantData,1);
N_data  = size(cumulantData,2);

% --- Colormap (trimmed coolwarm) ---
fullCmap = Colormaps.coolwarm(256);
blueSide = fullCmap(1:100,:);
redSide  = fullCmap(157:end,:);
trimmedCmap = [blueSide; redSide];
cmap = trimmedCmap(round(linspace(1,size(trimmedCmap,1),N_data)), :);

for t = 1:N_theta

    % ===== θ label in π format =====
    thetaRad = cumulantData(t,1).theta;
    
    thetaStr = thetaToPiStr(thetaRad);            % 'pi/3', '2pi/3', etc.
    thetaStr = strrep(thetaStr,'\\pi','\pi');     % latex escape fix
    
    fig = figure(t);
    set(fig, 'Color','w','Position',[100 100 1200 550]);
    tl = tiledlayout(1,2,'TileSpacing','Compact','Padding','Compact');
    sgtitle(['\theta = ' thetaStr], ...
        'FontName', options.font, 'FontSize', 18);

    % --- Mean κ1 ---
    ax1 = nexttile; hold(ax1,'on');
    for d = 1:N_data
        plot(ax1, cumulantData(t,d).scan_vals, cumulantData(t,d).mean, '-o', ...
            'Color', cmap(d,:), 'LineWidth', 2, 'MarkerSize', 8, ...
            'MarkerFaceColor', cmap(d,:), 'DisplayName', cumulantData(t,d).label);
    end
    xlabel(ax1, '\alpha (degrees)', 'FontName', options.font, 'FontSize', 14);
    ylabel(ax1, '\kappa_1', 'FontName', options.font, 'FontSize', 14);
    title(ax1, 'Mean', 'FontName', options.font, 'FontSize', 16);
    grid(ax1,'on'); legend(ax1,'Location','northwest','FontSize',12);
    set(ax1,'FontName',options.font,'FontSize',14);

    % --- Variance κ2 ---
    ax2 = nexttile; hold(ax2,'on');
    for d = 1:N_data
        plot(ax2, cumulantData(t,d).scan_vals, cumulantData(t,d).variance, '-o', ...
            'Color', cmap(d,:), 'LineWidth', 2, 'MarkerSize', 8, ...
            'MarkerFaceColor', cmap(d,:), 'DisplayName', cumulantData(t,d).label);
    end
    xlabel(ax2, '\alpha (degrees)', 'FontName', options.font, 'FontSize', 14);
    ylabel(ax2, '\kappa_2', 'FontName', options.font, 'FontSize', 14);
    title(ax2, 'Variance', 'FontName', options.font, 'FontSize', 16);
    grid(ax2,'on'); legend(ax2,'Location','northwest','FontSize',12);
    set(ax2,'FontName',options.font,'FontSize',14);
end

%% ===== Helper function to format theta in terms of pi =====
function thetaStr = thetaToPiStr(thetaRad)
    % Convert theta in radians to a string like 'pi/3' or 'pi' or '2*pi/3'
    [N,D] = rat(thetaRad/pi,1e-6); % rational approximation
    if N == 0
        thetaStr = '0';
    elseif D == 1
        if N==1
            thetaStr = '\\pi';
        else
            thetaStr = sprintf('%d\\pi',N);
        end
    else
        if N == 1
            thetaStr = sprintf('\\pi/%d',D);   % omit 1 in numerator
        else
            thetaStr = sprintf('%d\\pi/%d',N,D);
        end
    end
end