#!/bin/csh -f

#   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_reg package should be
#   installed.
#

if ( $#argv != 1 || $1 == "-help" ) then
    echo "usage: make_model base_filename"
    echo "   or: make_model [-version|-help]"
    exit 1
endif

if ( $1 == "-version" ) then
    echo "Package MNI AutoReg, version 0.98r"
    exit 0
endif

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

set HIRES=$BASE.mnc
if ( ! -e $HIRES ) then
   echo "Couldn't find input volume $HIRES"
   exit 1
endif



#
# subsample and pad onto a  2mm volume
#
set 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"
   autocrop  $HIRES $PAD -isostep 2 -isoexpand 16mm
endif

#
# build the 16mm FWHM feature volumes.
#

set BLURBASE=${BASE}_16
set BLUR=${BLURBASE}_blur.mnc

if (-e $BLUR) then
    echo "$BLUR exists already..."
else
   echo "** Building 16mm data for $BASE"

   mincblur $PAD tmp_16 -fwhm 16.0
   autocrop tmp_16_blur.mnc $BLUR -extend -16mm,-16mm -16mm,-16mm -36mm,-36mm -clobber
   mincresample -nearest  ${BASE}_mask.mnc ${BASE}_16_mask.mnc -like ${BLUR}

   rm tmp_16*
endif

#
# build the 8mm FWHM feature volumes.
#

set BLURBASE=${BASE}_8
set BLUR=${BLURBASE}_blur.mnc
set GRAD=${BLURBASE}_dxyz.mnc

if (-e $BLUR & -e $GRAD) then
    echo "$BLUR and $GRAD exist already..."
else
   echo "** Building 8mm data for $BASE"

   mincblur $PAD tmp_8 -fwhm 8.0 -gradient
   autocrop tmp_8_blur.mnc $BLUR -extend -16mm,-16mm -16mm,-16mm -26mm,-26mm -clobber
   autocrop tmp_8_dxyz.mnc $GRAD -extend -16mm,-16mm -16mm,-16mm -26mm,-26mm -clobber
   mincresample -nearest  ${BASE}_mask.mnc ${BASE}_8_mask.mnc  -like ${BLUR}

   rm tmp_8*
endif

#
# build the 4mm and 2mm FWHM feature volumes.
#
#

# pad by 4mm
#
set PAD4=${BASE}_pad4.mnc

if (-e $PAD4) then
    echo "$PAD4 exists already..."
else
   echo "** Making $PAD4 from $HIRES by padding by 4mm"
   autocrop  $HIRES $PAD4 -isoexpand 4mm
endif

set BLURBASE=${BASE}_4
set BLUR=${BLURBASE}_blur.mnc
set GRAD=${BLURBASE}_dxyz.mnc

if (-e $BLUR & -e $GRAD) then
    echo "$BLUR and $GRAD exist already..."
else
   echo "** Building 4mm data for $BASE"

   mincblur $PAD4 tmp_4 -fwhm 4.0 -gradient
   autocrop tmp_4_blur.mnc $BLUR -isoexpand -9mm -clobber
   autocrop tmp_4_dxyz.mnc $GRAD -isoexpand -9mm -clobber
   mincresample -nearest  ${BASE}_mask.mnc ${BASE}_4_mask.mnc  -like ${BLUR}

   rm tmp_4*
endif

set BLURBASE=${BASE}_2
set BLUR=${BLURBASE}_blur.mnc
set GRAD=${BLURBASE}_dxyz.mnc

if (-e $BLUR & -e $GRAD) then
    echo "$BLUR and $GRAD exist already..."
else
   echo "** Building 2mm data for $BASE"

   mincblur $PAD4 tmp_2 -fwhm 2.0 -gradient
   autocrop tmp_2_blur.mnc $BLUR -isoexpand -7mm -clobber
   autocrop tmp_2_dxyz.mnc $GRAD -isoexpand -7mm -clobber
   mincresample -nearest  ${BASE}_mask.mnc ${BASE}_2_mask.mnc -like ${BLUR}

   rm tmp_2*
endif


#
# cleanup temporary files
# 

rm $PAD $PAD4
