#! /bin/tcsh -f

#
# mkheadsurf
#
# REPLACE_WITH_ONE_LINE_SHORT_DESCRIPTION
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: nicks $
#    $Date: 2007/01/06 00:01:14 $
#    $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
#

# mkheadsurf

set VERSION = '$Id: mkheadsurf,v 1.2 2007/01/06 00:01:14 nicks Exp $';

set inputargs = ($argv);

umask 2;
set noseghead = 0;
set subjid = ();
set srcvol     = T1;
set headvol    = seghead;
set headsurf   = seghead;
set smheadsurf = smseghead;
set hemi = lh;
set thresh1 = 20;
set thresh2 = 20;
set nhitsmin = 2;
set fillval  = 255;

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;

goto parse_args;
parse_args_return:

goto check_params;
check_params_return:

set subjdir = $SUBJECTS_DIR/$subjid
mkdir -p $subjdir/scripts
set LF = $subjdir/scripts/mkheadsurf.log
if(-e $LF) mv $LF $LF.old
echo "INFO: log file is $LF"

echo "Log file for mkheadsurf" >> $LF
date >> $LF
pwd >> $LF
echo $0 >> $LF
echo $inputargs >> $LF
df $subjdir >> $LF

set StartTime = `date`;

if(! $noseghead ) then
  #----- Segment the head ---------#
  mkdir -p $subjdir/mri/$headvol
  set cmd = (mri_seghead );
  set cmd = ($cmd --invol    $subjdir/mri/$srcvol)
  set cmd = ($cmd --outvol   $subjdir/mri/$headvol)
  set cmd = ($cmd --fill     $fillval);
  set cmd = ($cmd --thresh1  $thresh1);
  set cmd = ($cmd --thresh2  $thresh2);
  set cmd = ($cmd --nhitsmin $nhitsmin);
  echo "--------------------------------" |& tee -a $LF
  date |& tee -a $LF
  pwd |& tee -a $LF
  echo $cmd |& tee -a $LF
  echo "--------------------------------" |& tee -a $LF
  $cmd |& tee -a $LF
  if($status) then
    echo "ERROR: mri_seghead" |& tee -a $LF
    exit 1;
  endif
else
  if(! -e $subjdir/mri/$headvol) then
    echo "ERROR: $subjdir/mri/$headvol does not exist, and you "
    echo "have not chosen to create it"
    exit 1;
  endif
endif

#----- Tessellate Head ---------#
set cmd = (mri_tessellate);
set cmd = ($cmd $subjdir/mri/$headvol $fillval)
set cmd = ($cmd  $subjdir/mri/$headvol/$hemi.$headsurf)
echo "--------------------------------" |& tee -a $LF
date |& tee -a $LF
pwd |& tee -a $LF
echo $cmd |& tee -a $LF
echo "--------------------------------" |& tee -a $LF
$cmd |& tee -a $LF
if($status) then
  echo "ERROR: mri_tessellate" |& tee -a $LF
  exit 1;
endif

#----- Smooth the tessellation ---------#
set cmd = (mris_smooth);
set cmd = ($cmd  $subjdir/mri/$headvol/$hemi.$headsurf)
set cmd = ($cmd  $subjdir/mri/$headvol/$hemi.$smheadsurf)
echo "--------------------------------" |& tee -a $LF
date |& tee -a $LF
pwd |& tee -a $LF
echo $cmd |& tee -a $LF
echo "--------------------------------" |& tee -a $LF
$cmd |& tee -a $LF
if($status) then
  echo "ERROR: mris_smooth" |& tee -a $LF
  exit 1;
endif
cp $subjdir/mri/$headvol/$hemi.$smheadsurf $subjdir/surf |& tee -a $LF

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

echo "mkheadsurf done"  |& tee -a $LF


exit 0

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

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

  set flag = $argv[1]; shift;

  switch($flag)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    case "-debug":
      set verbose = 1;
      set echo = 1;
      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($#subjid == 0) then
    echo "ERROR: no subjid specified"
    exit 1;
  endif

  if(! -e $SUBJECTS_DIR ) then
    echo "ERROR: $SUBJECTS_DIR does not exist"
    exit 1;
  endif

  if(! -e $SUBJECTS_DIR/$subjid ) then
    echo "ERROR: cannot find subject $subjid in $SUBJECTS_DIR"
    exit 1;
  endif

  if(! -e $SUBJECTS_DIR/$subjid/mri/$srcvol ) then
    echo "ERROR: cannot find volume $srcvol for $subjid"
    exit 1;
  endif

  if(! -w $SUBJECTS_DIR/$subjid/mri ) then
    echo "ERROR: cannot write to $SUBJECTS_DIR/$subjid/mri"
    exit 1;
  endif

  if(! -w $SUBJECTS_DIR/$subjid ) then
    echo "ERROR: cannot write to $SUBJECTS_DIR/$subjid"
    exit 1;
  endif

  mkdir -p $SUBJECTS_DIR/$subjid/surf

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

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

############--------------##################
usage_exit:
  echo ""
  echo "USAGE: mkheadsurf"
  echo ""
  echo "Required Arguments:";
  echo "  -subjid subjectid"
  echo ""
  echo "Other Arguments (Optional)"
  echo "   -noseghead : don't seg the head, just tess and smooth existing"
  echo "   -thresh1 threshold : default is $thresh1"
  echo "   -thresh2 threshold : default is $thresh2"
  echo "   -nhitsmin N        : default is $nhitsmin"
  echo "   -fillval value     : default is $fillval"
  echo "   -srcvol volid      : default is T1"
  echo "   -headvol volid     : default is seghead"
  echo "   -headsurf surfid   : default is seghead"
  echo "   -smheadsurf surfid : default is smseghead"
  echo "   -hemi hemi         : default is lh"
  echo "   -sd subjectsdir    : default is SUBJECTS_DIR"
  echo "   -umask umask       : default is 2 (ie, group and individ writable)"
  echo ""
  echo "See also: mri_seghead, mri_tessellate, and mris_smooth"
  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 segment and create a surface representation of the head that
can be displayed using tksurfer. The output is lh.smseghead and will
be stored in the subject's surf directory. There will also be a new
volume called seghead in the subject's mri directory. The volume will
be segmented and filled with 255. If the final head surface does not
look good enough, this volume can be edited (eg, tkmedit subjid
seghead -aux T1), then mkheadsurf can be re-run with -noseghead. The
segmenation surface can be loaded by File->Load Main Surface:
lh.smseghead.

For notes on setting the head segmentation parameters thresh1,
thresh2, and nhitsmin, see mri_seghead --help.
