#! /bin/csh -f

#
# mkfieldsign
#
# Original Author: Doug Greve
# CVS Revision Info:
#    $Author: greve $
#    $Date: 2007/09/21 23:31:28 $
#    $Revision: 1.8 $
#
# 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: mkfieldsign,v 1.8 2007/09/21 23:31:28 greve Exp $';

set inputargs = ($argv);

set eccendir = ();
set polardir = ();
set floatstem = ();
set realinfix = ();
set imaginfix = ();
set subject = ();
set fieldsignpath = ();
set fieldsignmaskpath = ();
set hemilist = ();
set angleoffset = 0;
set PrintHelp = 0;

# Print usage and exit if no args #
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;
  exit 1;
endif
##### Print out version info, if needed ####
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:

mkdir -p `dirname $fieldsignpath`;
mkdir -p `dirname $fieldsignmaskpath`;

setenv name         $subject
setenv eccendir     $eccendir
setenv polardir     $polardir
setenv floatstem    $floatstem
setenv realname     $realinfix
setenv complexname  $imaginfix

setenv angle_offset $angleoffset
unsetenv angle_cycles
unsetenv noexit

echo ""
foreach hemisphere ($hemilist)
  echo "--------.----------- $hemisphere --------.------------"
  setenv hemi $hemisphere;
  setenv patch occip.patch.flat
  setenv patchname occip.patch.flat

  # make sure that all the relevant w files are there #
  set wfile1 = `pwd`/$eccendir/$floatstem$realinfix-$hemi
  set wfile2 = `pwd`/$eccendir/$floatstem$imaginfix-$hemi
  set wfile3 = `pwd`/$polardir/$floatstem$realinfix-$hemi
  set wfile4 = `pwd`/$polardir/$floatstem$imaginfix-$hemi
  foreach wfile ($wfile1 $wfile2 $wfile3 $wfile4)
    set fname = `stem2fname $wfile`
    if($status) then
      echo "ERROR: cannot find $wfile"
      exit 1;
    endif
  end

  set patchpath = $SUBJECTS_DIR/$subject/surf/$hemi.$patch
  if(! -e $patchpath ) then
    echo "ERROR: cannot find $patchpath"
    exit 1;
  endif

  set mkfstcl = $FREESURFER_HOME/lib/tcl/mk-fieldsign.tcl;
  if(! -e $mkfstcl) then
    echo "ERROR: cannot find $mkfstcl"
    exit 1;
  endif

  setenv fs $fieldsignpath-$hemi
  setenv fm $fieldsignmaskpath-$hemi
  set cmd = (tksurfer -$name $hemi inflated -tcl $mkfstcl)
  pwd
  echo $cmd
  $cmd
  if($status) then
    echo "ERROR: tksurfer failed"
    exit 1;
  endif
  if(! -e $fs || ! -e $fm) then
    echo "ERROR: tksurfer failed when creating fieldsign"
    pwd
    echo $cmd
    echo ""
    exit 1;
  endif
  echo ""
  echo ""
end

echo "mkfieldsign COMPLETE"

exit 0

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

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

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

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

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

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

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

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

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

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

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

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

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

  set errs = 0;

  if($#eccendir == 0) then
    echo "ERROR: no eccen dir specified"
    set errs = 1;
  endif
  if($#polardir == 0) then
    echo "ERROR: no polar dir specified"
    set errs = 1;
  endif
  if($#floatstem == 0) then
    echo "ERROR: no float stem specified"
    set errs = 1;
  endif
  if($#realinfix == 0) then
    echo "ERROR: no real infix specified"
    set errs = 1;
  endif
  if($#imaginfix == 0) then
    echo "ERROR: no imaginary infix specified"
    set errs = 1;
  endif
  if($#subject == 0) then
    echo "ERROR: no subject specified"
    set errs = 1;
  endif
  if($#fieldsignpath == 0) then
    echo "ERROR: no field sign path specified"
    set errs = 1;
  endif
  if($#fieldsignmaskpath == 0) then
    echo "ERROR: no field sign mask path specified"
    set errs = 1;
  endif
  if($#hemilist == 0) set hemilist = (lh rh);

  if($errs) exit 1;

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

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

############--------------##################
usage_exit:
  echo "USAGE: mkfieldsign"
  echo "Options:";
  echo "   -eccen     dir"
  echo "   -polar     dir"
  echo "   -epbase    basename"
  echo "   -real      infix"
  echo "   -imag      infix"
  echo "   -subject   name"
  echo "   -hemi      hemisphere (rh or lh, def is both)"
  echo "   -angleoffset offset (0 to 1)"
  echo "   -fieldsign  path"
  echo "   -fieldsignmask  path"
  echo "   -umask umask   : set unix file permission mask"
  echo "   -version       : print version and exit"
  if( ! $PrintHelp) exit 1;
 
  echo ""
  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 constructs paths to the real and imaginary parts of the
eccentricity and polar according to the following rules:

$eccendir/$epbase$realinfix-$hemi.w
$eccendir/$epbase$imaginfix-$hemi.w
$polardir/$epbase$realinfix-$hemi.w
$polardir/$epbase$imaginfix-$hemi.w

This is a little oblique, but it is required because the old software
that handles this each of these components (ie, eccendir, epbase,
realinfix, imaginfix, and hemi). Note that this structure requires
that the eccen and polar be in different directories otherwise there
would be a name collision.

Eg, epbase = map, realinfix = real-, imaginfix = imag-, hemi = lh, 
resolves to map-real-lh.w and map-imag-lh.w.

The output will go in $fieldsign-lh.mgz and $fieldsignmask-lh.mgz
(note, there will also be files called simply $fieldsign and
$fieldsignmask). $fieldsign will be an overlay with values +1, -1, or
0. $fieldsignmask is magnitude of the HRF amplitude = 
sqrt(eccen^2 + polar^2).

Requires a patch called occip.patch.flat be in the subjects surf dir.
There is an angle offset parameter that is not clear.

This program calls tksurfer with the mk-fieldsign.tcl script. This
appears to use 50 iterations of surface smoothing.
