#! /bin/csh -f

#
# fwhmest-sess - sess front end for FSL's smoothest
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/09 22:41:17 $
#    $Revision: 1.4 $
#
# Copyright (C) 2002-2007,
# The General Hospital Corporation (Boston, MA). 
# All rights reserved.
#
# Distribution, usage and copying of this software is covered under the
# terms found in the License Agreement file named 'COPYING' found in the
# FreeSurfer source code root directory, and duplicated here:
# https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferOpenSourceLicense
#
# General inquiries: freesurfer@nmr.mgh.harvard.edu
# Bug reports: analysis-bugs@nmr.mgh.harvard.edu
#


set inputargs = ($argv);
set VERSION = '$Id'
set PrintHelp = 0;

set nolog     = 0;
set funcstem  = ();
set outfile   = ();
set fsd       = bold;
set rlf       = ();
set mask      = brain;
set PolyOrder = ();

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

goto parse_args;
parse_args_return:

set SessList = `getsesspath $inputargs`;
if($status || $#SessList == 0) then
  getsesspath $inputargs 
  echo "ERROR: cannot find any sessions" 
  exit 1;
endif

goto check_params;
check_params_return:

##### Create a log file ######
if(! $nolog) then
  set logdir = `pwd`/log;
  mkdir -p $logdir
  if(! -e $logdir) then
    echo "WARNING: could not create $logdir"
    set LF = /dev/null
  else
    set LF = $logdir/fwhmest-sess.log
    if(-e $LF) mv $LF $LF.old
  endif
else
  set LF = /dev/null
endif

echo "Logfile is $LF"
rm -f $LF;
touch $LF;
echo ------------------------------------------ >> $LF
echo "fwhmest-sess $VERSION" >> $LF
uname -a >> $LF
hostname >> $LF
date >> $LF
pwd >> $LF
echo $0 >> $LF
echo $inputargs >> $LF

set outfilebase = `basename $outfile`;
set outfiledir = `dirname $outfile`;
mkdir -p $outfiledir
pushd $outfiledir > /dev/null
set outfiledir = `pwd`;
popd  > /dev/null
set outfile = $outfiledir/$outfilebase

if(-e $outfile) mv $outfile $outfile.bak 
echo "outfile is $outfile"

echo "# date `date`"  >> $outfile
echo "# funcstem    $funcstem"  >> $outfile
echo "# maskstem    $mask"      >> $outfile
echo "# polyfit     $PolyOrder" >> $outfile
echo "# RunListFile $rlf"       >> $outfile
echo "#"   >> $outfile
echo "# Subject Run FWHMx FWHMy FWHMz FWHMxyz" >> $outfile

set StartTime = `date`;

## Loop through each session ##
foreach sess ($SessList)

  set sessid = `basename $sess`;
  echo " " |& tee -a $LF
  echo "Session: $sess ----------------" |& tee -a $LF
  date  |& tee -a $LF
  pushd $sess/$fsd > /dev/null

  set maskstem = masks/$mask

  set runlist = `getrunlist . $rlf`;
  foreach run ($runlist)
    set instem = $run/$funcstem
    set cmd = (fwhmest --i $instem --mask $maskstem)
    if($#PolyOrder) set cmd = ($cmd --polyfit $PolyOrder)
    echo "-------------------------------------" |& tee -a $LF
    date |& tee -a $LF
    pwd  |& tee -a $LF
    echo $cmd |& tee -a $LF
    $cmd |& tee -a $LF
    if($status) exit 1;

    #set fwhm = `cat $instem.fwhm.dat`;
    #set DLH = $fwhm[2];
    #set Volume = $fwhm[4];
    #set Resels = $fwhm[6];
    #echo "$sessid $run  $DLH $Resels $Volume" | tee -a $outfile

    set tmp = `cat $instem.fwhm.dat | grep FWHM | grep mm`;
    set FWHMx = $tmp[3];
    set FWHMy = $tmp[7];
    set FWHMz = $tmp[11];
    set FWHMxyz = `echo "$FWHMx * $FWHMy * $FWHMz" | bc -l `;
    set dd = ($sessid $run $FWHMx $FWHMy $FWHMz $FWHMxyz)
    printf "%-15s %s %6.4f %6.4f %6.4f %6.4f\n" $dd | tee -a $outfile
  end

end

set EndTime = `date`;

echo " " | tee -a $LF
echo " " | tee -a $LF
echo "Started at $StartTime" | tee -a $LF
echo "Ended   at $EndTime"   | tee -a $LF
echo " " | tee -a $LF
echo "done fwhmest-sess" | tee -a $LF

exit 0;
############################################################

############--------------##################
parse_args:

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

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

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

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

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

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

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

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

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

    case "--umask":
      if ( $#argv == 0) goto arg1err;
      umask $1; shift;
      breaksw

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

    case "-cwd":
      breaksw

    case "-s":
    case "-sf":
    case "-df":
    case "-d":
    case "-g":
      shift;
      breaksw

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

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

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

  if($#funcstem == 0) then
     echo "ERROR: must specify input func stem";
     exit 1
  endif
  if($#outfile == 0) then
     echo "ERROR: must specify output file";
     exit 1
  endif

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


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

#--------------------------------------------------------------------#
usage_exit:
  echo "fwhmest-sess"
  echo ""
  echo "  --i funcstem "
  echo "  --o outfile "
  echo ""
  echo "  --polyfit N     : default is -1 (no detrending)"
  echo "  --rlf rlf : run list file"
  echo "  --fsd fsd : func sub dir (bold)"
  echo "  --mask maskstem : default is brain"
  echo "   -umask umask          : set unix file permission mask"
  echo "   -version              : print version and exit"
  echo ""

  if($PrintHelp) \
  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

Front-end for FSLs smoothest. Estimates the smoothness of a data
set. 
