#!/bin/csh -f
# mkmosaic-sess
#
set VERSION = '$Id: mkmosaic-sess,v 1.6 2000/08/30 23:00:56 greve Exp greve $';
set FASTVER = `cat $FMRI_ANALYSIS_DIR/docs/version`;
set inputargs = "$argv";

set map = sig;
set contrast   = ();
set analysis   = ();
set ScriptOnly = 0;
set umaskarg   = ();
set base     = ();
set baserun  = ();
set nolog = 0;
set space = native;
set spacesubdir = ();
set isxavgmethod = ();

if($#argv == 0) then
  goto usage_exit;
  exit 1;
endif

set n = `echo $argv | grep version | wc -l` 
if($n != 0) then
  echo $VERSION
  exit 0;
endif

## Look for nolog option ##
set n = `echo $argv | grep nolog | wc -l` 
if($n != 0) then
  set nolog = 1;
endif

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

echo "-----------------------------------------------------------"
echo "mkmosaic-sess logfile is $LF"
echo "-----------------------------------------------------------"

echo "mkmosaic-sess log file" >> $LF
echo $VERSION >> $LF
echo "FAST Version $FASTVER"  >> $LF
pwd           >> $LF
echo "$0"     >> $LF
echo "$inputargs"  >> $LF
uname -a      >> $LF
date          >> $LF
which mkmosaic >> $LF

goto parse_args;
parse_args_return:

set SessList = `getsesspath $inputargs`;
if($status || $#SessList == 0) then
  echo "ERROR: $SessList" |& tee -a $LF
  exit 1;
endif

goto check_params;
check_params_return:

## Get functional subdirectory from the info file ##
if($#base == 0) then
  set fsd = `cat $analysis/analysis.info | awk '{if($1 == "fsd") print $2}'`;
endif

## Prepare the script, if need be ##
if($ScriptOnly) then
  if(! -d scripts) mkdir scripts
  if( $#base == 0 ) then
    set scriptfile = $analysis/scripts/run-mkmosaic
  else
    set scriptfile = scripts/run-mkmosaic-$base
  endif
  echo "Script file is $srciptfile"
  rm -f $scriptfile
  touch $scriptfile
  echo "#\!/bin/csh -f" >> $scriptfile
  echo "cd .." >> $scriptfile
  chmod a+x $scriptfile
endif

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

  echo "Session: $sess ------------------" |& tee -a $LF

  ## mosaic the t1epi ##
  if($#base != 0) then
    set basedir = $sess/$base;
    if( ! -e $basedir ) then
      echo "ERROR: cannot find $basedir" |& tee -a $LF
      exit 1;
    endif
    if($#baserun == 0) then
      set rundir = `getfirstrundir-sess $basedir` 
      echo ------ $rundir ----------
      if($status) then
        echo "ERROR: problem with directory $basedir" |& tee -a $LF
        exit 1;
      endif
      set run = `basename $rundir`;
    else
      set run = `printf %03d $baserun`;
    endif
    set indir = $sess/$base/$run
    if(! -e $indir) then
      echo "ERROR: $indir does not exist" |& tee -a $LF
      exit 1;
    endif
    set instem = $indir/f
    set cmd1 = "mkmosaic $instem -heq"
    set cmd2 = ();
  else
    if($#contrast == 0) then
      ## mosaic the offset and hdr ##
      set instem = $sess/$fsd/$analysis/$spacesubdir/h-offset
      set cmd1 = "mkmosaic $instem"

      set instem = $sess/$fsd/$analysis/$spacesubdir/h
      set tmp = `grep Nh $instem.dat`;
      @ Nh = $tmp[2];
      if($Nh != 1) then
        set cmd2 = "  mkmosaic $instem"
      else
        set cmd2 = ();
      endif
    else
      ## mosaic the signficance maps ##
      set instem = $sess/$fsd/$analysis/$spacesubdir/$contrast/$map
      set cmd1 = "mkmosaic $instem";
      set cmd2 = ();
    endif
  endif

  df $sess |& tee -a $LF
  pwd |& tee -a $LF
  echo $cmd1 |& tee -a $LF
  echo $cmd2 |& tee -a $LF

  if($ScriptOnly) then
    echo $cmd1 >> $scriptfile
    echo $cmd2 >> $scriptfile
    echo " " >> $scriptfile
    echo " " >> $scriptfile
  else
    $cmd1 |& tee -a $LF
    if($status) then
      echo "ERROR: $cmd1" |& tee -a $LF
      exit 1;
    endif
    if($#cmd2 > 0) then
      $cmd2 |& tee -a $LF
      if($status) then
        echo "ERROR: $cmd2" |& tee -a $LF
        exit 1;
      endif
    endif
  endif

end # loop over SessList #

echo "mkmosaic-sess completed SUCCESSFULLY" |& tee -a $LF

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

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

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

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

    case "-t1epi":
      set base = t1epi;
      breaksw

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

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

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

    case "-map":
      if ( $#argv == 0) goto arg1err;
      set map = $argv[1]; shift;
      if($map != sig && $map != minsig && $map != iminsig && $map != t) then
        echo "ERROR: map must be either sig, minsig, iminsig, or t"|& tee -a $LF
        exit 1;
      endif
      breaksw

    case "-space":
      if ( $#argv == 0) goto arg1err;
      set space = $argv[1]; shift;
      if($space != tal && $space != native) then
        echo "ERROR: space must be either tal or native " |& tee -a $LF
        exit 1;
      endif
      breaksw

    case "-isxavg":
      if ( $#argv == 0) goto arg1err;
      set isxavgmethod = $argv[1]; shift;
      if($isxavgmethod != fixed && $isxavgmethod != random) then
        echo "ERROR: -isxavg must be either fixed or random"|& tee -a $LF
        exit 1;
      endif
      breaksw

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

    case "-scriptonly":
      set ScriptOnly = 1;
      breaksw

    case "-cwd":
      breaksw

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

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

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

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

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

    default:
      echo ERROR: Flag $flag unrecognized. |& tee -a $LF
      echo $cmdline
      exit 1
      breaksw
  endsw

end

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

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

  if ($#analysis == 0 && $#contrast != 0) then
     echo "ERROR: must specify -analysis with -contrast" |& tee -a $LF
     exit 1
  endif

  if ($#SessList == 0) then
    echo "ERROR: no sessions specified" |& tee -a $LF
    exit 1;
  endif

  if($#base == 0 && $#analysis == 0) then
     echo "ERROR: no source for mosaic specified"|& tee -a $LF
     exit 1
  endif

  if($#base != 0 && $#analysis != 0) then
    echo "ERROR: cannot specify -base and -analysis"|& tee -a $LF
    exit 1
  endif

  ## Determine whether Group or Individual Data ##
  @ nGroup = 0;
  foreach sess ($SessList)
    set sessinfo = $sess/session.info
    set IsGroup = `grep GroupAverage $sessinfo | wc -l`;
    if($IsGroup) @ nGroup = $nGroup + 1;
  end
  if($IsGroup && ($nGroup != $#SessList) ) then
    echo "ERROR: cannot mix individual and group sessions"
    exit 1;
  endif

  ## Group Contingencies ##
  if($IsGroup) then
    if($#isxavgmethod == 0) then
      echo "ERROR: must supply -isxavg method with group data" |& tee -a $LF 
      exit 1;
    endif
    if($isxavgmethod == "fixed") then
      set spacesubdir = $space-ffx
    else
      set spacesubdir = $space-rfx
    endif
  else
    if($space == "native") then
      set spacesubdir = ();
    else
      set spacesubdir = $space;
    endif
    if($#isxavgmethod != 0) then
      echo "ERROR: -isxavg can only be used with group data" |& tee -a $LF 
      exit 1;
    endif
  endif

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

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

############--------------##################
usage_exit:
  echo "USAGE: mkmosaic-sess"
  echo "Options:";
  echo "   -analysis  analysisname : name of session-level functional analysis";
  echo "   -contrast  contrastname : contrast name"
  echo "   -map       mapname      : <sig>, minsig, iminsig, t"
  echo "   -base      dirname      : make mosaic of data in base dir (eg, t1epi)"
  echo "   -baserun   run          : run of basedir (default is first)"
  echo "   -sf sessidfile  ..."
  echo "   -df srchdirfile ..."
  echo "   -s  sessid      ..."
  echo "   -d  srchdir     ..."
  echo "   -scriptonly    : don't run, just generate a script"
  echo "   -version       : print version and exit"
  echo "   -umask umask   : set unix file permission mask"
exit 1;
