

(* cmd: /Applications/Mathematica.app/Contents/MacOS/MathKernel -script findBestShift.m sinograms/ 100 11 *)





perr = WriteString["stderr", ##]&;
eval = ToExpression[##]&;

ARGV = Drop[$CommandLine, 3]; 
Print[ARGV]
(* Print[Length[ARGV]] *)


(* input *)
sinoDir = ARGV[[1]];
shiftSlice = eval[ARGV[[2]]];
shift = eval[ARGV[[3]]];



(* read all sinogram files *)
filenamesSinograms = FileNames["sino_*", sinoDir]; 
numberFiles = Length[filenamesSinograms]; 
Print[numberFiles]; (*256*)

sinogram = Import[filenamesSinograms[[1]], {"Datasets", "sinogram"}];
{numberColumns, numberAngles} = Dimensions[sinogram];
Print[Dimensions[sinogram]]; (*{267, 202}*)
(* 256 shifted 11 to 267 *)


(* Find best center of rotation using ImagePad and reconstruction of a slice *)
sinogram = Import[filenamesSinograms[[shiftSlice]], {"Datasets", "sinogram"}];
sinogram = Image[sinogram, "Real"]; 

bestShift = {shift, 0}; 
gPad = InverseRadon[ImagePad[sinogram, {{0, 0}, bestShift}, 0], numberColumns]; 


padCOR = StringJoin["./", "padCOR.png"];
Export[padCOR, gPad, "PNG"];


Exit[]

