#!/bin/tcsh -f
# vsm-smooth

set VERSION = '$Id: vsm-smooth,v 1.1.2.2 2011/08/18 14:59:29 greve Exp $';

set vsm = ();
set vsmout = ();
set fwhm = ();

set tmpdir = ();
set cleanup = 1;
set LF = ();

set inputargs = ($argv);
set PrintHelp = 0;

if($#argv == 0) goto usage_exit;
set n = `echo $argv | grep -e -help | wc -l` 
if($n != 0) then
  set PrintHelp = 1;
  goto usage_exit;
endif
set n = `echo $argv | grep -e -version | wc -l` 
if($n != 0) then
  echo $VERSION
  exit 0;
endif

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

set outdir = `dirname $vsmout`
mkdir -p $outdir
pushd $outdir > /dev/null
set outdir = `pwd`;
popd > /dev/null

if($#tmpdir == 0) then
  if(-e /scratch)   set tmpdir = /scratch/tmpdir.vsm-smooth.$$
  if(! -e /scratch) set tmpdir = $outdir/tmpdir.vsm-smooth
endif
mkdir -p $tmpdir

set LF = /dev/null
if($#LF == 0) set LF = $outdir/vsm-smooth.log
if($LF != /dev/null) rm -f $LF

echo "Log file for vsm-smooth" >> $LF
date  | tee -a $LF
echo "" | tee -a $LF
echo "setenv SUBJECTS_DIR $SUBJECTS_DIR" | tee -a $LF
echo "cd `pwd`"  | tee -a $LF
echo $0 $inputargs | tee -a $LF
echo "" | tee -a $LF
cat $FREESURFER_HOME/build-stamp.txt | tee -a $LF
echo $VERSION | tee -a $LF
uname -a  | tee -a $LF

set vsmsm = $tmpdir/vsm.sm.nii
set cmd = (mri_fwhm --i $vsm --o $vsmsm --fwhm $fwhm --smooth-only)
echo $cmd | tee -a $LF
$cmd | tee -a $LF
if($status) exit 1;

set notvsm = $tmpdir/notvsm.nii
set cmd = (mri_binarize --abs --i $vsm --o $notvsm --min 1e-10 --inv)
echo $cmd | tee -a $LF
$cmd | tee -a $LF
if($status) exit 1;

set vsmsmmasked = $tmpdir/vsm.sm.masked.nii
set cmd = (mri_mask $vsmsm $notvsm $vsmsmmasked)
echo $cmd | tee -a $LF
$cmd | tee -a $LF
if($status) exit 1;

set cmd = (fscalc $vsm add $vsmsmmasked -o $vsmout)
echo $cmd | tee -a $LF
$cmd | tee -a $LF
if($status) exit 1;

if($cleanup) rm -rf $tmpdir

date | tee -a $LF
echo "vsm-smooth done" | tee -a $LF


exit 0

###############################################

############--------------##################
parse_args:
set cmdline = ($argv);
while( $#argv != 0 )

  set flag = $argv[1]; shift;
  
  switch($flag)

    case "--i":
    case "--vsm":
      if($#argv < 1) goto arg1err;
      set vsm = $argv[1]; shift;
      breaksw

    case "--o":
      if($#argv < 1) goto arg1err;
      set vsmout = $argv[1]; shift;
      breaksw

    case "--fwhm":
      if($#argv < 1) goto arg1err;
      set fwhm = $argv[1]; shift;
      breaksw

    case "--log":
      if($#argv < 1) goto arg1err;
      set LF = $argv[1]; shift;
      breaksw

    case "--nolog":
    case "--no-log":
      set LF = /dev/null
      breaksw

    case "--tmp":
    case "--tmpdir":
      if($#argv < 1) goto arg1err;
      set tmpdir = $argv[1]; shift;
      set cleanup = 0;
      breaksw

    case "--nocleanup":
      set cleanup = 0;
      breaksw

    case "--cleanup":
      set cleanup = 1;
      breaksw

    case "--debug":
      set verbose = 1;
      set echo = 1;
      breaksw

    default:
      echo ERROR: Flag $flag unrecognized. 
      echo $cmdline
      exit 1
      breaksw
  endsw

end

goto parse_args_return;
############--------------##################

############--------------##################
check_params:

if($#vsm == 0) then
  echo "ERROR: must spec vsm"
  exit 1;
endif
if($#vsmout == 0) then
  echo "ERROR: must spec vsmout"
  exit 1;
endif
if($#fwhm == 0) then
  echo "ERROR: must spec fwhm"
  exit 1;
endif

goto check_params_return;
############--------------##################

############--------------##################
arg1err:
  echo "ERROR: flag $flag requires one argument"
  exit 1
############--------------##################
arg2err:
  echo "ERROR: flag $flag requires two arguments"
  exit 1
############--------------##################

############--------------##################
usage_exit:
  echo ""
  echo "vsm-smooth"
  echo "  --i vsm"
  echo "  --o vsm.sm"
  echo "  --fwhm fwhm"
  echo "  --tmpdir tmpdir"
  echo ""

  if(! $PrintHelp) exit 1;
  echo $VERSION
  cat $0 | awk 'BEGIN{prt=0}{if(prt) print $0; if($1 == "BEGINHELP") prt = 1 }'
exit 1;

#---- Everything below here is printed out as part of help -----#
BEGINHELP

Implements a masked smoothing in which the input (vsm) is unchanged in
voxels that have a non-zero value. In voxels with a zero value, the
value is replaced with vsm smoothed by the given amount. This will
likely only change the voxels that are near the edge of the non-zero
voxels. This is a simple way to extrapolate the non-zero voxels beyond
their range. This works well for a B0 distortion correction voxel
shift map (vsm), but it can be applied to other maps.
