#! /bin/bash
#  beast_normalize
#
#  Copyright 2011  Simon Fristed Eskildsen, Vladimir Fonov,
#   	      	    Pierrick Coupé, Jose V. Manjon
#
#  This file is part of mincbeast.
#
#  mincbeast is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  mincbeast is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with mincbeast.  If not, see <http://www.gnu.org/licenses/>.
#
#  For questions and feedback, please contact:
#  Simon Fristed Eskildsen <eskild@gmail.com> 

set -e
num_args=3
usage_string="<input mnc> <output mnc> <output xfm>"
options_string="\t-initial <xfm>\t\t# initialize with provided transformation\n\t-noreg\t\t\t# do not perform registration\n\t-non3\t\t\t# do not perform N3\n\t-intensity_norm <0|1|2>\t# default is 2 (try 1 if it crashes)\n\t-modeldir <path>\t# path to model\n\t-modelname <name>\t# default is mni_icbm152_t1_tal_nlin_sym_09c"

unset list
declare -a list
listcount=0

reg=1
n3=1
initxfm=""

OLDIFS=${IFS}
IFS=:
for path in ${MNI_DATAPATH}; do
  if [[ -d ${path}/icbm152_model_09c ]]; then
    modeldir=${path}/icbm152_model_09c
  fi
done
IFS=${OLDIFS}

modelname=mni_icbm152_t1_tal_nlin_sym_09c
intensitynorm=2

while [ 0 -eq 0 ]
do
  case "$1" in
  -nor*)
    reg=0
    shift
    ;;
  -non*)
    n3=0
    shift
    ;;
  -ini*)
    initxfm=$2
    shift 2
    ;;
  -int*)
    intensitynorm=$2
    shift 2
    ;;
  -modeldir)
    modeldir=$2
    shift 2
    ;;
  -modelname)
    modelname=$2
    shift 2
    ;;
  *)
    if [ "$1" != "" ]; then
	list=( "${list[@]}" "$1" )
	let listcount=listcount+1
	shift
    else
	break
    fi
  esac
done

# get number of entries
max=${#list[*]}

test $max -ne $num_args && echo -e "Usage: `basename $0` $usage_string\nOptions:\n$options_string" && exit 

input=${list[0]}
output=${list[1]}
xfm=${list[2]}

if [ ! -d $modeldir ]; then
    echo -e "Error!\tCannot find model directory."
    echo -e "\tEnvironment variable MNI_DATAPATH is empty or -modeldir is not set"
    echo -e "Usage: `basename $0` $usage_string\nOptions:\n$options_string"
    exit
fi

template=$modeldir/$modelname.mnc
mask=$modeldir/${modelname}_mask.mnc

tmp=`mktemp -d`

mincreshape -float -normalize $input $tmp/reshaped.mnc

if [ $reg -eq 1 ]; then
# step 1: no mask
    if [ "$initxfm" = "" ]; then
	if [ $n3 -eq 1 ]; then
	    nu_correct -iter 100 -stop 0.0001 -fwhm 0.1 $tmp/reshaped.mnc $tmp/nuc1.mnc
	else
	    ln -s $tmp/reshaped.mnc $tmp/nuc1.mnc
	fi
	if [ $intensitynorm -gt 1 ]; then
	    volume_pol --order 1 --min 0 --max 100 --noclamp $tmp/nuc1.mnc $template --expfile $tmp/file1.exp
	    minccalc $tmp/nuc1.mnc $tmp/normal1.mnc -expfile $tmp/file1.exp -short	
	else
	    cp $tmp/nuc1.mnc $tmp/normal1.mnc
	fi
	bestlinreg_s $tmp/normal1.mnc $template $tmp/lin1.xfm
	mincresample -like $template -transform $tmp/lin1.xfm $tmp/normal1.mnc $tmp/final1.mnc	
    else
	cp $initxfm $tmp/lin1.xfm
	ln -s $tmp/reshaped.mnc $tmp/final1.mnc
    fi    
else
    param2xfm $tmp/lin1.xfm
    ln -s $tmp/reshaped.mnc $tmp/final1.mnc
fi

# step 2: with mask
mincresample -invert -nearest -like $tmp/reshaped.mnc -transform $tmp/lin1.xfm $mask $tmp/mask.mnc -clob
if [ $n3 -eq 1 ]; then
    nu_correct -iter 100 -stop 0.0001 -fwhm 0.1 $tmp/reshaped.mnc $tmp/nuc2.mnc -mask $tmp/mask.mnc -clob
else
    ln -s $tmp/reshaped.mnc $tmp/nuc2.mnc
fi
if [ $intensitynorm -gt 0 ]; then
    volume_pol --order 1 --min 0 --max 100 --noclamp $tmp/nuc2.mnc $template --expfile $tmp/file2.exp --source_mask $tmp/mask.mnc --target_mask $mask --clobber
    minccalc $tmp/nuc2.mnc $tmp/normal2.mnc -expfile $tmp/file2.exp -short -clob
else
    cp $tmp/nuc2.mnc $tmp/normal2.mnc
fi
if [ $reg -eq 1 ]; then
    bestlinreg_s $tmp/normal2.mnc $template $tmp/lin2.xfm -source_mask $tmp/mask.mnc -target_mask $mask -clob
    mincresample -like $template -transform $tmp/lin2.xfm $tmp/normal2.mnc $output -sinc -clob
    cp $tmp/lin2.xfm $tmp/lin1.xfm
else
    cp $tmp/normal2.mnc $output

fi

cp $tmp/lin1.xfm $xfm

rm -r $tmp

