% Define the path where your CSV files are located
files = dir('/home/juliechristo/PBL/dropsondes/*.csv');

% Initialize an empty cell array to store concatenated data
mlh_data = {};

% Loop through each CSV file
for i = 1:numel(files)
    file_path = fullfile(files(i).folder, files(i).name);
    
    % Read the CSV file
    table_data = readtable(file_path);
    
    % Convert table data to cell array
    cell_data = table2cell(table_data);
    
    % Concatenate columns into single-cell array
    mlh_data = [mlh_data; cell_data];
end

%-- Compute Statistics for each Method --
%comparing against the lidar-derived PBLH

%Lidar MLH
mlh_lidar = cell2mat(mlh_data(:,7));

%Parcel Method
mlh_parcel = cell2mat(mlh_data(:,2));
[nmb_parcel,nme_parcel,mb_parcel,me_parcel,rmse_parcel,corr_parcel] = calcStats_all(mlh_parcel,mlh_lidar);

%Theta Method
mlh_theta = cell2mat(mlh_data(:,3));
[nmb_theta,nme_theta,mb_theta,me_theta,rmse_theta,corr_theta] = calcStats_all(mlh_theta,mlh_lidar);

%Ri Method
mlh_ri = cell2mat(mlh_data(:,4));
[nmb_ri,nme_ri,mb_ri,me_ri,rmse_ri,corr_ri] = calcStats_all(mlh_ri,mlh_lidar);

%Qv Method
mlh_qv = cell2mat(mlh_data(:,5));
[nmb_qv,nme_qv,mb_qv,me_qv,rmse_qv,corr_qv] = calcStats_all(mlh_qv,mlh_lidar);

%Inversion Method
mlh_calvin = cell2mat(mlh_data(:,6));
[nmb_calvin,nme_calvin,mb_calvin,me_calvin,rmse_calvin,corr_calvin] = calcStats_all(mlh_calvin,mlh_lidar);

%Write statistics to output file
% Create a cell array to store the method names
methods = {'Parcel', 'Theta', 'Ri', 'Qv', 'Inversion'};

% Create a matrix to store the statistics
stats = [
    nmb_parcel, nme_parcel, mb_parcel, me_parcel, rmse_parcel, corr_parcel;
    nmb_theta, nme_theta, mb_theta, me_theta, rmse_theta, corr_theta;
    nmb_ri, nme_ri, mb_ri, me_ri, rmse_ri, corr_ri;
    nmb_qv, nme_qv, mb_qv, me_qv, rmse_qv, corr_qv;
    nmb_calvin, nme_calvin, mb_calvin, me_calvin, rmse_calvin, corr_calvin
];

% Define the file name
file_name = 'CPEX-CV_stats.txt';

% Open the file for writing
fileID = fopen(file_name, 'w');

% Write the column headers (statistics)
fprintf(fileID, 'Method\tNMB\tNME\tMB\tME\tRMSE\tCorr\n');

% Write the data
for i = 1:size(stats, 1)
    fprintf(fileID, '%s\t%f\t%f\t%f\t%f\t%f\t%f\n', methods{i}, stats(i, :));
end

% Close the file
fclose(fileID);

disp(['Results written to ' file_name]);



