if [ $# -lt 6 ]; then 
echo ' ' 
echo 'regularize deformation field' 
echo ' ' 
echo 'usage:' 
echo ' '
echo 'LinuxRegularizeDeformationField inputdeformationfield outputjakfile outputdeformationfield X Y Z'
echo ' '
echo 'filename: the file generated by using LinuxCalculateJacobian'
echo 'the format of deformation field is not HAMMER field'
exit
fi

tempfilename=temp$$
tempfieldname=tempfield$$
mosrun -L LinuxResaveDeformationField ${1} $tempfieldname -d $4,$5,$6 
mosrun -L LinuxCalculateJacobian ${1} ${2} -d $4,$5,$6 > $tempfilename
A=`more $tempfilename`
echo $A
A=`echo $A | cut -d" " -f6`
echo $A
if [ $A -lt 0 ]; then
echo regularize field
echo LinuxAdaptiveRegularization3D -d $tempfieldname -r $4 -c $5 -s $6 -l 0.01 -u 28.0 -t 3 -o ${3}
mosrun -L LinuxAdaptiveRegularization3D -d $tempfieldname -r $4 -c $5 -s $6 -l 0.01 -u 28.0 -t 3 -o ${3}
mosrun -L LinuxResaveDeformationField ${3} ${3} -d $4,$5,$6
echo LinuxCalculateJacobian ${3} ${2} -d $4,$5,$6 > $tempfilename
mosrun -L LinuxCalculateJacobian ${3} ${2} -d $4,$5,$6 > $tempfilename
else
echo copy file
echo cp $1 $3	
cp $1 $3	
fi
rm $tempfilename
rm $tempfieldname

