close all
clear all



BaseFolder = 'MonoChromaticComparisons';
ResultsFolder = 'latest_batch_results';

results_table = readtable(['../' BaseFolder '/' ResultsFolder '/batch_runs.log']);
table_size = size(results_table);

for i = 1:table_size(1)
  files = dir(['../' BaseFolder '/' ResultsFolder '/' results_table.pblogFilename{i} '/../matlab/*_raw.mat']);
  results_data{i} = load([files.folder '\' files.name]);

disp(num2str(i));
end

      A = [0.125, 0.125, 0.125, 0.125, 0.125, 0.125, 0.25, 0.25, 0.25, 0.25, 0.25, 0.5, 0.5,  0.5,  0.5, 0.75, 0.75, 0.75, 1.0,  1.0,  1.0,  1.0, 1.25];
      T = [4.0,     6.0,   8.0,  10.0,  12.0,  14.0,  6.0,  8.0, 10.0, 12.0, 14.0, 6.0, 8.0, 12.0, 14.0,  8.0, 12.0, 14.0, 8.0, 10.0, 12.0, 14.0, 14.0];

    WecSimResultsFilenames{1} = '0-25_4-00.mat';
    WecSimResultsFilenames{2} = '0-25_6-00.mat';
    WecSimResultsFilenames{3} = '0-25_8-00.mat';
    WecSimResultsFilenames{4} = '0-25_10-00.mat';
    WecSimResultsFilenames{5} = '0-25_12-00.mat';
    WecSimResultsFilenames{6} = '0-25_14-00.mat';

    WecSimResultsFilenames{7} = '0-50_6-00.mat';
    WecSimResultsFilenames{8} = '0-50_8-00.mat';
    WecSimResultsFilenames{9} = '0-50_10-00.mat';
    WecSimResultsFilenames{10} = '0-50_12-00.mat';
    WecSimResultsFilenames{11} = '0-50_14-00.mat';

    WecSimResultsFilenames{12} = '1-00_6-00.mat';
    WecSimResultsFilenames{13} = '1-00_8-00.mat';
    WecSimResultsFilenames{14} = '1-00_12-00.mat';
    WecSimResultsFilenames{15} = '1-00_14-00.mat';

    WecSimResultsFilenames{16} = '1-50_8-00.mat';    
    WecSimResultsFilenames{17} = '1-50_10-00.mat';    
    WecSimResultsFilenames{18} = '1-50_14-00.mat';    

    WecSimResultsFilenames{19} = '2-00_8-00.mat';    
    WecSimResultsFilenames{20} = '2-00_10-00.mat';    
    WecSimResultsFilenames{21} = '2-00_12-00.mat';    
    WecSimResultsFilenames{22} = '2-00_14-00.mat';    
    
    WecSimResultsFilenames{23} = '2-50_14-00.mat';    




WecSimResultsFolder = 'Andy_monochro_results';

for i = 1:length(A)

    WecSimResults = load(['../' WecSimResultsFolder '/' WecSimResultsFilenames{i}]);


figure
subplot(4,1,1)
t = (results_data{i}.XB_Time-results_data{i}.XB_Time(1))*24*3600;
plot(t,A(i)*cos(2*pi*t/T(i)));
hold all;
plot(t,results_data{i}.XB_Alt-mean(results_data{i}.XB_Alt)); 
plot(WecSimResults.time_array,WecSimResults.buoy_heave);
xlabel('time (s)');
ylabel('meters');
grid on;
title(['A = ' num2str(A(i)) 'm   T = ' num2str(T(i)) 's'])
legend('wave height','Gazebo Buoy Pos','WecSim Buoy Pos')

subplot(4,1,2)
t = (results_data{i}.SC_Time-results_data{i}.SC_Time(1))*24*3600;
pos = results_data{i}.SC_Range;
plot(t,results_data{i}.SC_Range)
hold all
plot(WecSimResults.time_array,39.4*WecSimResults.sim_pistonpos_values{1});
l_pos = length(results_data{i}.SC_Range);
%ylim([0.9*min(pos(l_pos/2:end)) 1.1*max(pos(l_pos/2:end))]);
xlabel('time (s)');
ylabel('Piston Pos (in)');
legend('Gazebo Piston Pos','WecSim Piston Pos')
grid on;

subplot(4,1,3)
t = (results_data{i}.SC_Time-results_data{i}.SC_Time(1))*24*3600;
pos = results_data{i}.SC_Range;
plot(t(2:end),diff(results_data{i}.SC_Range)./diff(t));
hold all
plot(WecSimResults.time_array,39.4*WecSimResults.sim_pistonvel_values{1});
l_pos = length(results_data{i}.SC_Range);
%ylim([0.9*min(pos(l_pos/2:end)) 1.1*max(pos(l_pos/2:end))]);
xlabel('time (s)');
ylabel('Piston Vel (in/s)');
legend('Gazebo Piston Vel','WecSim Piston Vel')
grid on;

subplot(4,1,4)
t = (results_data{i}.PC_Time-results_data{i}.PC_Time(1))*24*3600;
rpm = results_data{i}.PC_RPM;
plot(t,rpm)
hold all
plot(WecSimResults.time_array,WecSimResults.sim_motorrpm_values{1});
l_rpm = length(rpm);
%ylim([1.1*min(rpm(l_rpm/2:end)) 1.1*max(rpm(l_rpm/2:end))]);
xlabel('time (s)');
ylabel('RPM');
grid on;
legend('Gazebo RPM','WecSim RPM')



%print(['MonoChromaticResults_A' num2str(A) '_T' num2str(T(i)) '.pdf'],'-dpdf','-fillpage');
end
