#! /bin/tcsh -f

#
# tdr-rcolmat
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/09 22:41:19 $
#    $Revision: 1.6 $
#
# 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: tdr-rcolmat,v 1.6 2007/01/09 22:41:19 nicks Exp $';

set inputargs = ($argv);

set fidmatlist = ();
set rcolmat    = ();
set dnorm      = 1;
set rsegthresh = 0.05;
set regmethod  = "svdpct";
set svdregpct  = 90;
set tikregfact = 0.1;
set boldweight = 0;
set monly = 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

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

set umask = `umask`;

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

echo "tdr-rcolmat log file" >> $LF
echo $VERSION >> $LF
id            >> $LF
pwd           >> $LF
echo $0        >> $LF
echo $inputargs  >> $LF
uname -a      >> $LF
date          >> $LF

set StartTime = `date`;

if(! $monly) then
  set MLF = $outdir/run_tdr_rcolmat.m
  rm -f $MLF
endif

#---------------------------------------------#
tee -a > /dev/null $MLF  <<EOF
  fidmatfilelist  = splitstring('$fidmatlist');
  rcolmatfile = '$rcolmat';
  dnorm = [$dnorm];
  boldweight = [$boldweight];
  rthreshfid = [$rsegthresh];
  regmethod = '$regmethod'; % tikhonov or svdpct
  svdregpct = [$svdregpct];
  tikregfact = [$tikregfact];
  tdr_rcolmat;
EOF
#---------------------------------------------#

if(! $monly) then
  cat $MLF >> $LF
  cat $MLF | matlab -display iconic -nojvm -nosplash | & tee -a $LF
  echo ""| & tee -a $LF
endif

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

echo "tdr-rcolmat done"


exit 0

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

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

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

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

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

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

    case "--reg":
      if ( $#argv == 0) goto arg1err;
      set regmethod = $argv[1]; shift;
      if($regmethod != svdpct && $regmethod != tikhonov) then
        echo "ERROR: regmethod = $regmethod, must be svdpct or tikhonov"
        exit 1;
      endif
      breaksw

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

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

    case "--nodnorm":
      set dnorm = 0;
      breaksw

    case "--bold":
      set boldweight = 1;
      breaksw

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

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

    case "--echo":
      set echo = 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($#fidmatlist == 0) then
    echo "ERROR: must specify fidmat"
    exit 1;
  endif
  foreach fidmat ($fidmatlist)
    if(! -e $fidmat ) then
      echo "ERROR: $fidmat does not exist"
      exit 1;
    endif
  end

  if($#rcolmat == 0) then
    echo "ERROR: must specify rcolmat"
    exit 1;
  endif
  set outdir = `dirname $rcolmat`;

  mkdir -p $outdir
  if($status) then
    echo "ERROR: making $outdir"
    exit 1;
  endif

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

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

############--------------##################
usage_exit:
  echo ""
  echo "USAGE: tdr-rcolmat"
  echo ""
  echo "  --fid fidmat <--fid fidmat>"
  echo "  --rcol    rcolmat"
  echo "  --nodnorm"
  echo "  --bold"
  echo "  --rsegthresh rthresh <$rsegthresh>"
  echo "  --reg  method : <svdpct> tikhonov"
  echo "  --svdpct svdregpct <90>"
  echo "  --tik    tikregfact <$tikregfact>"

  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

Computes the matrix that reconstructs the columns
