#!/usr/bin/env octave-cli

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

# Files to be processed and their respective labels
# Initialize transposed to allow for easier editing
files_tr = {
	"prismatic-n16-_drawn.log.csv" "prismatický luk" "b"
	"pyramidal-n16-_drawn.log.csv" "jehlanový luk" "r"
};
files = files_tr';

function out = process(file, output)
	values = csvread(file{1,1});
	values(1,:) = [];
	disp(values(1,:));
	#values = valueswitheader(2:end, :);
	time = values(:, 1);
	draw = values(:, 2);
	force = values(:, 3);
	Ep_total = values(:, 4);

	plot(draw, -force, file{3,1});
	
	db = draw(end,1) - draw(1,1);
	Fd = force(end,1);
	Ep = Ep_total(end, 1);
	deltaEp = Ep_total(end, 1) - Ep_total(1, 1);
	# Even though it is a cell array, use square brackets when concatenating:
	line = {file{2,1}, db, Fd, Ep, deltaEp};
	fprintf(output, "%s, %f, %f, %f, %f\n", line{:});
endfunction

fid = fopen("draw-comparison.csv", "w");
header = {"typ", "db", "Fd", "Ep", "DeltaEp"};
fprintf(fid, "%s, %s, %s, %s, %s\n", header{:});

clf;
hold on;
for file = files
	process(file, fid);
endfor
h = legend(files(2,:));
legend_settings(h);
legend("location", "northwest");
xlabel('$d\ [\metre]$');
ylabel('$F\ [\newton]$');
xlim([0-eps 0.8+eps]);
deccomma;
grid on;
box off;
hold off;

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