#! /bin/csh -f
### version 0.1a -- 3/09

### defaults
set patchsurf = smoothwm
set hemilist = "lh rh"
set surflist = "orig smoothwm inflated white graymid pial"
set patchlist = "occip full"
set configlist = "flat 3d"

### help
if ($#argv != 3) then
  echo ""
  echo "use: scalesurfaces <subject> <scalefact> <"all" or single-surf,patch>"
  echo ""
  echo "  isotropically scale all/one surf/patch for subject by scalefact"
  echo ""
  echo "  N.B.: permanently alters one/all surfaces/patches for a subject"
  echo "        mri/* volumes must be separately scaled to restore alignment"
  echo ""
  echo "  e.g., scalesurfaces fredd-hires-re 0.75 all"
  echo "        scalesurfaces fredd-hires-re 0.75 lh.inflated"
  echo "        scalesurfaces fredd-hires-re 0.75 rh.occip.patch.flat"
  echo ""
  echo "  allowed hemispheres:    ($hemilist)"
  echo "  allowed patch configs:  ($configlist)"
  echo "  def surf list for all:  ($surflist)"
  echo "  def patch list for all: ($patchlist)"
  echo "  single surf,patch: any surf suffix, patch infix OK"
  echo ""
  exit
endif

### args, errors
set name = $1
set scale = $2
set onefile = $3
set tmpscript  = /tmp/TmpTclScript.$$
set tmpscript2 = /tmp/TmpTclScript2.$$
onintr cleanup
if (! $?SUBJECTS_DIR) then
  echo "scalesurfaces: ### env var SUBJECTS_DIR not set"
  exit
endif
if (! -e $SUBJECTS_DIR/$name) then
  echo "scalesurfaces: ### subject $name  ...not found"
  exit
endif

### always show all surfs/patches found
set foundsurflist = ""
set foundpatchlist = ""
foreach hemi ($hemilist)
  foreach surf ($surflist)
    if (-e $SUBJECTS_DIR/$name/surf/$hemi.$surf) then
      set foundsurflist = "$foundsurflist $hemi.$surf"
    endif
  end
  foreach patch ($patchlist)
    foreach config ($configlist)
      if (-e $SUBJECTS_DIR/$name/surf/$hemi.$patch.patch.$config) then
        set foundpatchlist = "$foundpatchlist $hemi.$patch.patch.$config"
      endif
    end
  end
end
if ("$foundpatchlist" != "") then
  foreach hemi ($hemilist)
    if (! -e $SUBJECTS_DIR/$name/surf/$hemi.$patchsurf) then
      echo "scalesurfaces: ### need $patchsurf to read patches  ...quitting"
      exit
    endif
  end
endif
echo ""
echo "found these surfaces and patches for ${name}:"
echo ""
foreach surf ($foundsurflist)
  echo "  $surf"
end
foreach patch ($foundpatchlist)
  echo "  $patch"
end
echo ""

### if single surf, sanity check name (but allow any patch infix)
if ("$onefile" != "all") then
  # get hemi (surf or patch)
  set hemipref = notfound
  foreach hemi ($hemilist)
    if ("$onefile" =~ "$hemi.*") set hemipref = $hemi
  end
  if ($hemipref == "notfound") then
    echo "scalesurfaces: ### don't recognize hemi: $onefile ...quitting"
    exit
  endif
  set hemilist = $hemipref
  if ("$onefile" =~ "*.patch.*") then
    ### patch
    set surflist = ""
    # get patch infix
    set patchinfix = notfound
    foreach patch ($patchlist)
      if ("$onefile" =~ "?h.$patch.patch.*") set patchinfix = $patch
    end
    if ($patchinfix == "notfound") then
      echo "scalesurfaces: ### patch: $onefile not recognized ...continuing"
      set patchinfix = `echo $onefile | awk -F . '{print $2}'`
    endif
    set patchlist = $patchinfix
    # get config
    set configsuff = notfound
    foreach config ($configlist)
      if ("$onefile" =~ "?h.*.patch.$config") set configsuff = $config
    end
    if ($configsuff == "notfound") then
      echo "scalesurfaces: ### don't recognize config: $onefile ..quitting"
      exit
    endif
    set configlist = $configsuff
    # check patchsurf
    if (! -e $SUBJECTS_DIR/$name/surf/$hemilist.$patchsurf) then
      echo \
        "scalesurfaces: ### need $patchsurf to read patch $onefile ...quitting"
      exit
    endif
  else
    ### surf
    # get surf suffix
    set surfsuff = notfound
    foreach surf ($surflist)
      if ("$onefile" =~ "?h.$surf") set surfsuff = $surf
    end
    if ($surfsuff == "notfound") then
      echo "scalesurfaces: ### surface: $onefile not recognized ...continuing"
      set surfsuff = `echo $onefile | awk -F . '{print $2}'`
    endif
    set surflist = $surfsuff
    set patchlist = ""
    set configlist = ""
  endif
endif

### allow escape
if ("$onefile" == "all") then
  echo -n "Permanently scale *all* $name surfaces by $scale? ('y' or 'n') "
else
  echo -n "Permanently scale $name surf $onefile by $scale? ('y' or 'n') "
endif
set resp = "$<"
echo ""
if ("$resp" != "yes" && "$resp" != "y") then
  echo "scalesurfaces: ${name}'s surfaces unchanged"; echo ""
  exit
endif

### run surf script on all surfaces, make/run patch-specific scripts
# make surf script
echo "open_window"                              > $tmpscript
echo "read_binary_curv"                        >> $tmpscript
echo "make_lateral_view"                       >> $tmpscript
echo "set cslope 3.0"                          >> $tmpscript
echo "redraw"                                  >> $tmpscript
echo "really_scale_brain $scale $scale $scale" >> $tmpscript
echo "redraw"                                  >> $tmpscript
echo 'set outsurf $insurf'                     >> $tmpscript
echo "write_binary_surface"                    >> $tmpscript
echo "exit"                                    >> $tmpscript
foreach hemi ($hemilist)
  foreach surf ($surflist)
    if (! -e $SUBJECTS_DIR/$name/surf/$hemi.$surf) then
      #echo "### surf $hemi.$surf for $name not found ...skipping"
      continue
    endif
    echo "tksurfer -${name} $hemi $surf -tcl $tmpscript"
    tksurfer -${name} $hemi $surf -tcl $tmpscript
  end
  foreach patch ($patchlist)
    foreach config ($configlist)
      if (! -e $SUBJECTS_DIR/$name/surf/$hemi.$patch.patch.$config) then
        #echo "### $hemi.$patch.patch.$config not found ..skipped"
        continue
      endif
      # make patch script
      echo "open_window"                               > $tmpscript2
      echo "read_binary_curv"                         >> $tmpscript2
      echo "setfile patch $hemi.$patch.patch.$config" >> $tmpscript2
      echo "read_binary_patch"                        >> $tmpscript2
      echo "restore_zero_position"                    >> $tmpscript2
      echo "rotate_brain_x -90"                       >> $tmpscript2
      echo "set cslope 3.0"                           >> $tmpscript2
      echo "redraw"                                   >> $tmpscript2
      echo "really_scale_brain $scale $scale $scale"  >> $tmpscript2
      echo "redraw"                                   >> $tmpscript2
      echo "write_binary_patch"                       >> $tmpscript2
      echo "exit"                                     >> $tmpscript2
      echo "tksurfer -${name} $hemi $patchsurf -tcl $tmpscript2"
      tksurfer -${name} $hemi $patchsurf -tcl $tmpscript2
    end
  end
end
echo ""

cleanup:
echo "done"
rm -f $tmpscript $tmpscript2

