#! /bin/tcsh -f

#
# functwf-sess
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/09 22:41:17 $
#    $Revision: 1.2 $
#
# 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 VERSION = '$Id: functwf-sess,v 1.2 2007/01/09 22:41:17 nicks Exp $';

set inputargs = ($argv);
set DateStr = "`date '+%y%m%d%H%M'`"

set funcstem = ();
set fsd = bold;
set runlistfile = ();
set maskdir = masks;
set maskstem = ();
set outfile = ();
set hpf = ();
set pforder = -1;
set monly = 0;
set QOE = 1;
set MLF = ();
set svtev = 0;
set tevstem = ();
set cutends = 0;

set logunique = 0;
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

set PWD = `getpwdcmd`;
if($status) exit 1;

set SessList = `getsesspath $argv`;
if($status || $#SessList == 0) then
  getsesspath $argv 
  exit 1;
endif

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

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

echo "functwf-sess log file" >> $LF
echo $VERSION >> $LF
pwd           >> $LF
echo $0         >> $LF
echo $inputargs >> $LF
uname -a        >> $LF
date            >> $LF

set StartTime = `date`;

foreach sess ($SessList)
  set sessid = `basename $sess`;
  echo "$sessid -----------------------------------" |& tee -a $LF

  cd $sess
  if(! -e $fsd) then
    echo "ERROR: cannot find FSD $fsd"
    exit 1;
  endif
  cd $fsd

  if(! -e seq.info) then
    echo "ERROR: cannot find seq.info"
    exit 1;
  endif
  set TR = `cat seq.info | awk '{if($1 == "TR") print $2}'`;
   
  if($#runlistfile == 0) then
    set RunList = `getrunlist .`;
    if($status || $#RunList == 0) then
      echo "ERROR: $sess/$fsd has no runs"  |& tee -a $LF
      exit 1;
    endif
  else
    if(! -e $runlistfile) then
      echo "ERROR: ($sessid) could not find runlistfile $runlistfile" |& tee -a $LF
      exit 1;
    endif
    set RunList = (`cat $runlistfile`);
  endif
  echo "RunList = $RunList" |& tee -a $LF

  set okfile = functwf-sess-$$.ok
  rm -f $okfile
  if($#MLF == 0) set MLF = functwf-sess-$$.m
  rm -f $MLF
  echo "% matlab file for functwf-sess " >> $MLF
  echo "global QuitOnError" >> $MLF
  echo "QuitOnError = $QOE;" >> $MLF
  echo "" >> $MLF
  foreach run ($RunList)
    echo -n "r = fast_functwf(" >> $MLF

    set funcpath = $run/$funcstem
    echo -n "'-f','$funcpath','-tr','$TR'">> $MLF

    if($pforder > 0)   echo -n ",'-polyfit','$pforder'" >> $MLF
    if($#hpf != 0) echo -n ",'-hpf','$hpf'" >> $MLF
    if($cutends) echo -n ",'-cutends'" >> $MLF

    if($#maskstem != 0) then
      set maskpath = $maskdir/$maskstem
      echo -n ",'-m','$maskpath'">> $MLF
    endif

    if($#outfile != 0) then
      set outpath = $run/$outfile
      echo -n ",'-o','$outpath'">> $MLF
    endif

    if($#tevstem != 0) then
      set tevpath = $run/$tevstem
      echo -n ",'-tev','$tevpath'">> $MLF
    endif

    echo ");" >> $MLF
    echo "if(r) return; end" >> $MLF
    echo "clear all" >> $MLF
    echo "" >> $MLF

  end
  
  if(! $monly) then
    echo "fmri_touch('$okfile');" >> $MLF

    cat $MLF | matlab -nosplash |& tee -a $LF
    if(! -e $okfile) then
      echo "ERROR: in matlab execution" |& tee -a $LF
      echo "--------- matlab file -----------" |& tee -a $LF
      cat $MLF |& tee -a $LF
      exit 1;
    endif

    rm -f $MLF $okfile

  endif

end


set EndTime = `date`;

echo "" |& tee -a $LF
echo "Started at $StartTime" |& tee -a $LF
echo "Ended   at $EndTime"   |& tee -a $LF

echo "functwf-sess done"

echo ""

exit 0

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    case "-g":
    case "-s":
    case "-sf":
    case "-d":
    case "-df":
      shift;
      # ignore getsesspath arguments 
      breaksw

    case "-cwd":
      # ignore getsesspath arguments 
      breaksw

    case "-umask":
      if ( $#argv == 0) goto arg1err;
      umask $1; 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 funcstem"
    exit 1;
  endif

  if($svtev && $#tevstem == 0) set tevstem = $funcstem-tev


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

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

############--------------##################
usage_exit:
  echo ""
  echo "USAGE: functwf-sess"
  echo ""
  echo "Required Arguments:";
  echo "   -f funcstem : input stem"
  echo ""
  echo "Other Arguments (Optional)"
  echo "   -o    outfile : (default is funcstem.twf)"
  echo "   -tev  tevstem  : save temp eigvects as tevstem_000.bfloat"
  echo "   -svtev         : save temp eigvects as funcstem-tev_000.bfloat"
  echo "   -mask mask     : binary mask (fsd/masks/mask)"
  echo "   -hpf  cutoff   : high-pass filter cut off (sec), not with -polyfit"
  echo "   -polyfit order : fit and remove polynomial of order (not with -hpf)"
  echo "   -cutends       : dont include end slices"
  echo ""
  echo "Session Arguments (Required)"
  echo "   -sf sessidfile  ..."
  echo "   -df srchdirfile ..."
  echo "   -s  sessid      ..."
  echo "   -d  srchdir     ..."
  echo "   -fsd  FSD : functional subdirectory"
  echo "   -rlf  RunListFile "
  echo ""
  echo "Yet even more options"
  echo "   -help  : I need some info! Help! Not just any info! Heeelllp!"
  echo "   -debug : print out ridiculous amounts of verbiage"
  echo "   -monly mlf : only create matlab file"
  echo "   -umask   mask : unix file permission mask"
  echo "   -logunique : make a unique log file name"
  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

This will compute the autocorrelation function at each voxel. It 
can save this as a new functional volume. It can also save the
average autocorreation, average absolute autocorreation. It can
also compute the global whitening filter and whiten the data
with this filter.


