#!/usr/bin/env octave-cli

addpath("../../octave/");
defaults;

# Files to be processed and their respective labels
# Initialize transposed to allow for easier editing
# The number gives the line in the draw-comparison.csv file
files_tr = {
	"prismatic-n16-_loosed.log.csv" "prismatický luk" "b" 2
	"pyramidal-n16-_loosed.log.csv" "jehlanový luk" "r" 3
};
files = files_tr';

function out = process(file, output, draw)
	values = csvread(file{1,1});
	values(1,:) = [];
	
	time = values(:, 1);
	string_pos = values(:, 2);
	Ep_bow = values(:, 3) + values(:, 4) + values(:, 5);
	Ek_bow = values(:, 6) + values(:, 7) + values(:, 8);
	head_pos = values(:, 9);
	velocity = values(:, 10);
	Ek_arrow = values(:, 11) + values(:, 12) + values(:, 13);
	deltaEp = draw(file{4,1}, 5);
	efficiency = Ek_arrow(end)/deltaEp;
	
	plot(time, -velocity, file{3,1});
	
	# Even though it is a cell array, use square brackets when concatenating:
	line = {file{2,1}, Ep_bow(end), Ek_bow(end), Ek_arrow(end), velocity(end), efficiency};
	fprintf(output, "%s, %f, %f, %f, %f, %f\n", line{:});
endfunction

draw = csvread("draw-comparison.csv");

fid = fopen("loose-comparison.csv", "w");
header = {"typ", "Ep_bow", "Ek_bow", "Ek_arrow", "v_arrow", "efficiency"};
fprintf(fid, "%s, %s, %s, %s, %s, %s\n", header{:});

clf;
hold on;
for file = files
	process(file, fid, draw);
endfor
h = legend(files(2,:));
legend_settings(h);
legend("location", "southeast");
xlabel('$t\ [\second]$');
ylabel('$v\ [\metre\per\second]$');
#xlim([0 0.8]);
grid on;
box off;
hold off;
#set(gca,'xminortick','on')
#disp(get (gca, "xtick"));
#disp(get (gca, "xticklabel"));
deccomma;

print("xsection-loose", "-depslatex", "-color");
fclose(fid);
