#!/bin/bash


#information at: http://www.nitrc.org/projects/art
# really not too much information! also run executables with -h for a little more information
# exemple d'execució: 

#export ARTHOME=/home/yago/Desktop/ImatgeMedica/3D/RegistrationWithPreprocess3/ART

#definim parametres:
sourceDir=$1
fixed=$2
moving=$3 
outputDir=$4
iterations=$5
mRLevels=$6
regType=$7
lesionsBasal=$8
lesions12M=$9

#echo "iteracio $i de moment $itS"

iterationsAFF=$(( iterations / 2 ))

# Decide wheteher this is a temporal register
if [[ $regType = ATLAS || $regType = ATLASH ]]
then
	temporalRegister=false
else
	temporalRegister=true
fi

echo "********************************************************************************************************** " 
echo "* Testadort ART: Parametres de la prova:"
echo "* Imatge Fixa: $fixed"
echo "* Imatge Mobil: $moving"
echo "* Prefix directori s: $outputDir"
echo "* iteracions inicialitzacio Afi (/2): $iterationsAFF , el metode no flexible no se sap gaire quantes en fa... "
echo "* nivells: $mRLevels  "
echo "********************************************************************************************************** " 

echo " " 
echo "                        **************************************************************************************************** " 
echo "                                 PROVANT Registre ART "		  
echo " executo:	"
echo "                        **************************************************************************************************** " 


echo "vaig a fer $sourceDir/ART/3dwarper -sub $fixed -trg $moving -cubicspline -iter 8 -u camp.nii -o outputART.nii"
$sourceDir/ART/3dwarper -sub $fixed -trg $moving -cubicspline -u camp.nii -o outputART.nii #> sART$outputDir.txt
#$sourceDir/ART/3dwarper -sub $fixed -trg $moving -acpc -cubicspline -iter 8 -u camp.nii -o outputART.nii #> sART$outputDir.txt


#OJU!!!!!!!!!!!!
#Error: Sorry, but this program can only handle image types `short' and `unsigned char'
#fslmaths input.nii output.nii -odt type
#Possible datatypes are: char short int float double


mkdir filesART\_$outputDir

#compute moved ground truth
if ( "$temporalRegister" -eq true ) ; then
	cp $lesionsBasal ./lesionsBasal.nii
	cp $lesions12M ./lesions12M.nii
	$sourceDir/ART/applywarp3d -w camp.nii lesions12M.nii >> sART$outputDir.txt
	mv lesions12M.nii ./deformedLesions.nii
fi

# compute difference images
$sourceDir/imageDifference $fixed outputART.nii diffART"After".nii
if ( "$temporalRegister" -eq true ) ; then
$sourceDir/imageDifference $fixed $moving diffART"Before"$outputDir.nii
fi

#AT THIS MOMENT, WE ARE UNABLE TO COMPUTE DEFORMATION FIELD (it is stored in camp.nii but we cannot interpret it)

#move dicom files
mv "diff"*.nii filesART\_$outputDir
mv outputART.nii filesART\_$outputDir

#move lesion information
if ( "$temporalRegister" -eq true ) ; then
mv lesionsBasal.nii filesART\_$outputDir
mv lesions12M.nii filesART\_$outputDir
mv deformedLesions.nii filesART\_$outputDir 
fi

#move the rest
mv sART$outputDir.txt filesART\_$outputDir
mv camp.nii filesART\_$outputDir

