#! /bin/sh

#   This script is used to create the blurred intensity and gradient
#   volumes needed to make up the volumetric features for a general
#   set of model files needed for fitting with mritotal and nlfit.
#
#   All model files MUST be voxel-matched to _mask.mnc volume
#
#   Before running this script, the mni_autoreg package should be
#   installed.


# get program name
me=`basename $0`

MINC_COMPRESS_ORIG=${MINC_COMPRESS}

# check args
if [ $# -eq 0 ] ; then
   echo "usage: $me <base_filename>"
   echo "      $me -version"
   echo ""
   exit 1
fi

if [ $1 = "-version" ];
then
   echo "Package mni_autoreg, version 0.99.70"
   echo ""
   exit 0
fi

BASE=$1
echo "Using base filename $BASE"
echo "Note that your input volume *must* be in Talairach space for this to work"


# check for the input files
HIRES=$BASE.mnc
if [ ! -e $HIRES ];
then
   echo "Couldn't find input volume $HIRES"
   echo ""
   exit 1
fi


# subsample and pad onto a  2mm volume
PAD=${BASE}_pad.mnc
if [ -e $PAD ];
then
   echo "$PAD exists already..."
else
   echo "** Making $PAD from $HIRES by resampling on 2mm grid and padding by 16mm"
   export MINC_COMPRESS=0
   autocrop -clobber $HIRES $PAD -isostep 2 -isoexpand 16mm
   export MINC_COMPRESS=${MINC_COMPRESS_ORIG}
fi


# build the 16mm FWHM feature volumes.
BLURBASE=${BASE}_16
BLUR=${BLURBASE}_blur.mnc
MASK=${BLURBASE}_mask.mnc
if [ -e $BLUR -a -e $MASK ];
then
   echo "$BLUR and $MASK exist already..."
else
   echo "** Building 16mm data for $BASE"
   export MINC_COMPRESS=0
   mincblur -clobber $PAD tmp_16 -fwhm 16.0
   autocrop -clobber tmp_16_blur.mnc $BLUR -extend -16mm,-16mm -16mm,-16mm -36mm,-36mm
   export MINC_COMPRESS=${MINC_COMPRESS_ORIG}
   mincresample -clobber -nearest ${BASE}_mask.mnc ${MASK} -like ${BLUR}
   rm -f tmp_16*
fi


# build the 8mm FWHM feature volumes.
BLURBASE=${BASE}_8
BLUR=${BLURBASE}_blur.mnc
GRAD=${BLURBASE}_dxyz.mnc
MASK=${BLURBASE}_mask.mnc
if [ -e $BLUR -a -e $GRAD -a -e $MASK ];
then
   echo "$BLUR, $GRAD, and $MASK exist already..."
else
   echo "** Building 8mm data for $BASE"
   export MINC_COMPRESS=0
   mincblur -clobber $PAD tmp_8 -fwhm 8.0 -gradient
   autocrop -clobber tmp_8_blur.mnc $BLUR -extend -16mm,-16mm -16mm,-16mm -26mm,-26mm
   autocrop -clobber tmp_8_dxyz.mnc $GRAD -extend -16mm,-16mm -16mm,-16mm -26mm,-26mm
   export MINC_COMPRESS=${MINC_COMPRESS_ORIG}
   mincresample -clobber -nearest ${BASE}_mask.mnc ${MASK} -like ${BLUR}
   rm -f tmp_8*
fi


# pad by 4mm
PAD4=${BASE}_pad4.mnc
if [ -e $PAD4 ];
then
   echo "$PAD4 exists already..."
else
   echo "** Making $PAD4 from $HIRES by padding by 4mm"
   export MINC_COMPRESS=0
   autocrop -clobber $HIRES $PAD4 -isoexpand 4mm
   export MINC_COMPRESS=${MINC_COMPRESS_ORIG}
fi


# build the 4mm FWHM feature volume
BLURBASE=${BASE}_4
BLUR=${BLURBASE}_blur.mnc
GRAD=${BLURBASE}_dxyz.mnc
MASK=${BLURBASE}_mask.mnc
if [ -e $BLUR -a -e $GRAD -a -e $MASK ];
then
   echo "$BLUR, $GRAD, and $MASK exist already..."
else
   echo "** Building 4mm data for $BASE"
   export MINC_COMPRESS=0
   mincblur -clobber $PAD4 tmp_4 -fwhm 4.0 -gradient
   autocrop -clobber tmp_4_blur.mnc $BLUR -isoexpand -9mm
   autocrop -clobber tmp_4_dxyz.mnc $GRAD -isoexpand -9mm
   export MINC_COMPRESS=${MINC_COMPRESS_ORIG}
   mincresample -clobber -nearest ${BASE}_mask.mnc ${MASK} -like ${BLUR}
   rm -f tmp_4*
fi


# build the 2mm FWHM feature volume
BLURBASE=${BASE}_2
BLUR=${BLURBASE}_blur.mnc
GRAD=${BLURBASE}_dxyz.mnc
MASK=${BLURBASE}_mask.mnc
if [ -e $BLUR -a -e $GRAD -a -e $MASK ];
then
   echo "$BLUR, $GRAD, and $MASK exist already..."
else
   echo "** Building 2mm data for $BASE"
   export MINC_COMPRESS=0
   mincblur -clobber $PAD4 tmp_2 -fwhm 2.0 -gradient
   autocrop -clobber tmp_2_blur.mnc $BLUR -isoexpand -7mm
   autocrop -clobber tmp_2_dxyz.mnc $GRAD -isoexpand -7mm
   export MINC_COMPRESS=${MINC_COMPRESS_ORIG}
   mincresample -clobber -nearest ${BASE}_mask.mnc ${MASK} -like ${BLUR}
   rm -f tmp_2*
fi


# cleanup temporary files
rm -f $PAD $PAD4
