#! /bin/csh -f

#
# mksurfatlas
#
# Creates an atlas to use for surface registration
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/06 00:01:14 $
#    $Revision: 1.7 $
#
# 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: mksurfatlas,v 1.7 2007/01/06 00:01:14 nicks Exp $';

set atlas = ();
set target = ();
set subjects = ();
set surfval = ();
set surfvaldir = "label";
set regsurf = "sphere";
set hemi = ();

set force = 0;
set tmpdir = ();
set cleanup = 1;
set debug = 0;
set PrintHelp = 0;

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

# Parse the command-line arguments
goto parse_args;
parse_args_return:

# Check the command-line arguments
goto check_params;
check_params_return:

if(-e $atlas && $force) rm -f $atlas

set cmd = (mris_make_template -norot -annot aparc \
            -overlay $surfval 10 -overlay-dir $surfvaldir \
            $hemi $regsurf $subjects $atlas)
echo $cmd
$cmd
if($status) exit(1);

date
echo "mksurfatlas done"

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


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

  set flag = $argv[1]; shift;

  switch($flag)

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

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

    case "--sf":
      if($#argv < 1) goto arg1err;
      set subjectfile = $argv[1]; shift;
      if(! -e $subjectfile) then
        echo "ERROR: cannot find $subjectfile"
        exit 1;
      endif
      set subjects = ($subjects `cat $subjectfile`);
      breaksw

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

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

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

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

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

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

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

    case "--debug":
      set verbose = 1;
      set echo = 1; # turns on terminal echoing
      set debug = 1;
      breaksw

    default:
      echo "ERROR: flag $flag not recognized"
      exit 1;
      breaksw
  endsw

end

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

############--------------##################
check_params:
  if($#atlas == 0) then
    echo "ERROR: no atlas name specified"
    exit 1;
  endif
  if(-e $atlas && ! $force) then
    echo "ERROR: $atlas exists. Delete, rename, or run with -force"
    exit 1;
  endif
  if($#hemi == 0) then
    echo "ERROR: that thing got a hemi?"
    exit 1;
  endif
  if($#subjects == 0) then
    echo "ERROR: no subjects specified"
    exit 1;
  endif
  foreach subject ($subjects $target)
    if(! -e $SUBJECTS_DIR/$subject) then
      echo "ERROR: cannot find $SUBJECTS_DIR/$subject"
      exit 1;
    endif
    set sr = $SUBJECTS_DIR/$subject/surf/$hemi.$regsurf
    if(! -e $sr) then
      echo "ERROR: cannot find $sr"
      exit 1;
    endif
    set sv = $SUBJECTS_DIR/$subject/$surfvaldir/$hemi.$surfval
    if(! -e $sr) then
      echo "ERROR: cannot find $sv"
      exit 1;
    endif
  end
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 "USAGE: mksurfatlas"
  echo ""
  echo " --a atlas : saves results to this file (tif file)"
  echo ""
  echo " --h hemi"
  echo " --s subject <--s subject ...>"
  echo " --v surfval  : looks for subject/surfvaldir/hemi.surfval"
  echo ""
  echo " --d surfvaldir : default is label"
  echo " --r regsurf : default is sphere"
  echo ""
  echo " --version            : print version and exit"
  echo " --help               : print help and exit"
  echo " --debug              : turn on debugging"
  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 program has not been tested, is not supported, and you should not
use it.

Creates an atlas using mris_make_template. The atlas can then be used
to create the surface registration for each subject based on this
atlas.

A typical workflow for creating an fmri-based atlas would be something
like:

1. For each subject, run the functional analysis, map results to the
   surface, and save in subject/fmri/hemi.func.mgh

2. Choose one subject as the reference subject to create an initial
   atlas:

   mksurfatlas --a lh.func.atlas.init.tif --h lh --v func.mgh \
      --r sphere.reg --s refsubject

   This will create lh.func.atlas.init.tif

3. Create an initial registration surface based on this atlas,

   foreach subject (subject1 subject2 ...)
     mris_register -dist 0 -para 0 -annot aparc -remove_negative 1 -w 0 \
       -overlay func.mgh 10 -overlay-dir func \
        $SUBJECTS_DIR/$subject/surf/$hemi.sphere \
       lh.func.atlas.init.tif $SUBJECTS_DIR/$subject/surf/$hemi.funcreg.init
   end

   This will create $hemi.funcreg.init.

4. Create final atlas based on all subjects (similar to step 2)

   mksurfatlas --a lh.func.atlas.tif --h lh --v func.mgh \
     --r $hemi.funcreg.init --s subject1 --s subject2 --s ...

5. Create a final registration surface based on final atlas,

   foreach subject (subject1 subject2 ...)
     mris_register -dist 0 -para 0 -annot aparc -remove_negative 1 -w 0 \
       -overlay func.mgh 10 -overlay-dir func \
       $SUBJECTS_DIR/$subject/surf/$hemi.sphere \
       lh.func.atlas.tif $SUBJECTS_DIR/$subject/surf/$hemi.funcreg
   end

   This will create $hemi.funcreg which can then be used as
   you would $hemi.sphere.






