load('../../matlab/checkwarp_dump');

select = [1 4 9 10 13 14];
trials = [3:10 13 17 22 24 26 27];
select_k = trials(select);

x = [-0.5 0 0.5; -0.5 0 0.5; -0.5 0 0.5]';
y = [-0.5 -0.5 -0.5;0 0 0;0.5 0.5 0.5]';

maxshift = zeros(1,6);
minshift = zeros(1,6);
meanshift = zeros(1,6);
for k = 1:length(select)
    deformation = ps{select(k)}(1) + x*ps{select(k)}(2) + y*ps{select(k)}(3) + x.^2*ps{select(k)}(4) + x.*y*ps{select(k)}(5) + y.^2*ps{select(k)}(6);
    maxshift(k) = max(deformation(:));
    minshift(k) = min(deformation(:));
    meanshift(k) = mean(deformation(:));
end

newNz = 200;
minNz = 80;

z = [max(round(meanshift) - newNz/2+1,round(minshift) - minNz/2+1);...
min(round(meanshift) + newNz/2,round(maxshift) + minNz/2)];

n = 6;
    k = select_k(n);
    
    cmd0 = sprintf('trimvol -nx 4096 -ny 4096 -z %i,%i -rx -f Dummy_fbp_full.rec Dummy_fbp.rec',z(1,n),z(2,n));
    cmd1 = sprintf('trimvol -nx 4096 -ny 4096 -z %i,%i -rx -f Dummy_iTCTF_int16_fbp_full.rec Dummy_iTCTF_fbp.rec',z(1,n),z(2,n));
	
    fprintf('%s\n',cmd0);
    dos(cmd0);
	fprintf('%s\n',cmd1);
    dos(cmd1);
