#! /bin/csh -f
# modified most recently 23/11/04
# copied 6/6/08

# find out what sytem we have
set sys = `uname -s`
if ($sys == "Linux") then
  set byteorder = "LSB"
else
  set byteorder = "MSB"
endif

set path = ($path /myrtos/usr1/tmp/dhagler/bin-lnx \
  /myrtos/usr1/tmp/dhagler/tools/bfloats \
  /myrtos/usr1/tmp/dhagler/bin-all )

# newdefs
newdefs
setenv CSURF_DIR /kamares/usr/local/FreeSurfer0.9b
source $CSURF_DIR/../etc/FreeSurfer.cshrc cleaninstead
source $CSURF_DIR/FreeSurferDefs.csh

set vol2surf = /kamares/usr/local/FreeSurfer0.9b/bin/Linux/mri_vol2surf
set surf2surf = /kamares/usr/local/FreeSurfer0.9b/bin/Linux/mri_surf2surf


# set variables
set nfspace = /mochlos/usr0/fspace
set analname = sm16_multireachSurfave_15sub
set groupdir = /mochlos/usr1/tmp/ffilimon/$analname
set hemilist = (lh rh)
set paintsubj = flavia
set smoothsteps = 16
set dmax = 2
set dstep = 0.25


set sublist = (\
  flavia \
  donh \
  samh \
  joelc \
  ireneM \
  alanr \
  kazu \
  rueysong \
  jingjing \
  matthewL \
  adrienne \
  samanthaH \
  marty4T \
  samson \
  fawad \
)

#specifies number of subjects which to include
set newlist = (1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)

set condlist = (\
  reach \
  observe \
  imagine \
)

set twocondtestlist = (\
  reachVSobserve \
  observeVSimagine \
  reachVSimagine \
)

set testnamelist = ($condlist $twocondtestlist)
#1 = reach
#2 = observe
#3 = imagine

# testcond1list and testcond2list are for doing ttests
# The pairedflaglist is only 0 or 1 and tells whether to do a paired ttest.
# cnum lists have something to do with ANOVA tests
# have as many columns as there are total number of tests 
# = nr of conditions + nr of VScomparisons
# NOTE: the numbers from testcond1list will be compared with those from testcond2list
# specifically, the condition represented by e.g. 2 in cond1list will be subtracted from
# the number (condition) directly underneath in cond2list

set testcond1list   = (0 0 0 1 2 1)
set testcond2list   = (1 2 3 2 3 3)
set pairedflaglist  = (0 0 0 1 1 1)
set cnum1list = (1 2 1)
set cnum2list = (2 3 3)

# interpretation: cnum2 condition 1 will be compared with cnum1 condition 2, etc.

# one set of briks (one for each condition) per subject
# e.g. if you have three conditions, write three lines for each condition
# (same fspace (concat) directory (what you ran 3dDecon on) )
# -1 if not represented


set scandirlist = (\
 $nfspace/030520FF/image/multiblockreach-cat \
   $nfspace/030520FF/image/multiblockreach-cat \
   $nfspace/030520FF/image/multiblockreach-cat \
 $nfspace/030905DH/image/warp-multireach-cat \
   $nfspace/030905DH/image/warp-multireach-cat \
   $nfspace/030905DH/image/warp-multireach-cat \
 $nfspace/030927SH/image/multireachvreg-cat \
   $nfspace/030927SH/image/multireachvreg-cat \
   $nfspace/030927SH/image/multireachvreg-cat \
 $nfspace/031002JC/image/multireachvreg-cat \
   $nfspace/031002JC/image/multireachvreg-cat \
   $nfspace/031002JC/image/multireachvreg-cat \
 $nfspace/031004IM/image/warp-multireach-cat \
   $nfspace/031004IM/image/warp-multireach-cat \
   $nfspace/031004IM/image/warp-multireach-cat \
 $nfspace/031006AR/image/warp-multireach-cat \
   $nfspace/031006AR/image/warp-multireach-cat \
   $nfspace/031006AR/image/warp-multireach-cat \
 $nfspace/031006KM/image/warp-multireach-cat \
   $nfspace/031006KM/image/warp-multireach-cat \
   $nfspace/031006KM/image/warp-multireach-cat \
 $nfspace/031007RS/image/warp-multireachvreg-cat \
   $nfspace/031007RS/image/warp-multireachvreg-cat \
   $nfspace/031007RS/image/warp-multireachvreg-cat \
 $nfspace/031014JE/image/warp-multireach-cat \
   $nfspace/031014JE/image/warp-multireach-cat \
   $nfspace/031014JE/image/warp-multireach-cat \
 $nfspace/031016ML/image/warp-multireach-cat \
   $nfspace/031016ML/image/warp-multireach-cat \
   $nfspace/031016ML/image/warp-multireach-cat \
 $nfspace/031019AD/image/multireachvreg-cat \
   $nfspace/031019AD/image/multireachvreg-cat \
   $nfspace/031019AD/image/multireachvreg-cat \
 $nfspace/031025SH/image/warp-multireachvreg-cat \
   $nfspace/031025SH/image/warp-multireachvreg-cat \
   $nfspace/031025SH/image/warp-multireachvreg-cat \
 $nfspace/031030MS/image/warp-multireachvreg-cat \
   $nfspace/031030MS/image/warp-multireachvreg-cat \
   $nfspace/031030MS/image/warp-multireachvreg-cat \
 $nfspace/031118SY/image/multireachvreg-cat \
   $nfspace/031118SY/image/multireachvreg-cat \
   $nfspace/031118SY/image/multireachvreg-cat \
 $nfspace/031130FS/image/multireachvreg-cat \
   $nfspace/031130FS/image/multireachvreg-cat \
   $nfspace/031130FS/image/multireachvreg-cat \
)

***********************************************


***********************************************


# there need to be as many identical lines as the number of conditions
# sets stats brik list = the stats briks inside the directories specified above
# these are the stats briks that 3dDecon was run on
# smoothing?

set statsbriklist = (\
  multiblockreach-cat-stats+orig \
  multiblockreach-cat-stats+orig \
  multiblockreach-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  multireachvreg-cat-stats+orig \
  multireachvreg-cat-stats+orig \
  multireachvreg-cat-stats+orig \
  multireachvreg-cat-stats+orig \
  multireachvreg-cat-stats+orig \
  multireachvreg-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  warp-multireachvreg-cat-stats+orig \
  warp-multireachvreg-cat-stats+orig \
  warp-multireachvreg-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  warp-multireach-cat-stats+orig \
  multireachvreg-cat-stats+orig \
  multireachvreg-cat-stats+orig \
  multireachvreg-cat-stats+orig \
  warp-multireachvreg-cat-stats+orig \
  warp-multireachvreg-cat-stats+orig \
  warp-multireachvreg-cat-stats+orig \
  warp-multireachvreg-cat-stats+orig \
  warp-multireachvreg-cat-stats+orig \
  warp-multireachvreg-cat-stats+orig \
  multireachvreg-cat-stats+orig \
  multireachvreg-cat-stats+orig \
  multireachvreg-cat-stats+orig \
  multireachvreg-cat-stats+orig \
  multireachvreg-cat-stats+orig \
  multireachvreg-cat-stats+orig \
)


set nsliceslist = (\
  28 28 28 \
  28 28 28 \
  28 28 28 \
  28 28 28 \
  28 28 28 \
  28 28 28 \
  28 28 28 \
  28 28 28 \
  28 28 28 \
  28 28 28 \
  28 28 28 \
  28 28 28 \
  28 28 28 \
  28 28 28 \
  28 28 28 \
)

# this picks the appropriate briks out of the above list
# these are the coefficient sub-briks contained in the stats briks specified above, 
# which are a result of 3Ddeconvolve = #0 is reach_LC#0#_coef, #2 is observe_LC#0#_coef
# #4 is reach_LC##_coef - the other ones are Fstat files and VS files

set subbriklist = (\
  0 2 4 \
  0 2 4 \
  0 2 4 \
  0 2 4 \
  0 2 4 \
  0 2 4 \
  0 2 4 \
  0 2 4 \
  0 2 4 \
  0 2 4 \
  0 2 4 \
  0 2 4 \
  0 2 4 \
  0 2 4 \
  0 2 4 \
)

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


# create directory tree and extract stats
echo "### extracting stats..."
if (! -e $groupdir) then
  mkdir $groupdir
endif
set briknum = 1
set subnum = 1
foreach sub ($sublist)
  if ($newlist[$subnum] == 1) then
    # make subject directory
    if (! -e $groupdir/$sub) then
      mkdir $groupdir/$sub
    endif
    cd $groupdir/$sub

    # step through each condition
    foreach cond ($condlist)
      set scandir = $scandirlist[$briknum]
      set statsbrik = $statsbriklist[$briknum]
      set subbrik = $subbriklist[$briknum]
      echo cp $scandir/$statsbrik* $groupdir/$sub
      cp $scandir/$statsbrik* .
      forceMSB $statsbrik.BRIK
      if ($scandir != -1) then
        # convert subbrik to bfloat
        brik2bfloat $statsbrik $cond $subbrik
      endif
      @ briknum++
    end
  endif
  @ subnum++
end

# paint to surface
echo "### painting to surface..."
set briknum = 1
set subnum = 1
foreach sub ($sublist)
  if ($newlist[$subnum] == 1) then
    cd $groupdir/$sub
    # step through each condition
    foreach cond ($condlist)
      set scandir = $scandirlist[$briknum]
      set nslices = $nsliceslist[$briknum]
      if ($scandir != -1) then
        # interpolate to icos
        foreach hemi ($hemilist)
          paint \
            $cond'_'%03d.bfloat \
            $hemi \
            orig \
            $groupdir/$sub/$cond-$hemi.w \
            -nslices $nslices \
            -regdat $scandir/register.dat \
            -dmax $dmax -dstep $dstep
        end
      endif
      @ briknum++
    end
  endif
  @ subnum++
end

# interpolate and smooth onto spherical surface
echo "### interpolating and smoothing onto spherical surface..."
set briknum = 1
set subnum = 1
foreach sub ($sublist)
  if ($newlist[$subnum] == 1) then
    cd $groupdir/$sub

    # step through each condition
    foreach cond ($condlist)
      set scandir = $scandirlist[$briknum]
      if ($scandir != -1) then
        foreach hemi ($hemilist)
          set instem = $cond-$hemi
          set outstem = $cond-sph-sm$smoothsteps-$hemi
          # smooth on surface
          $surf2surf \
            --srcsubject $sub --srcsurfval $groupdir/$sub/$instem \
            --trgsubject ico --trgsurfval $groupdir/$sub/$outstem \
            --trgicoorder 7 --src_type w --trg_type bfloat \
            --hemi $hemi --frame 0 --mapmethod nnfr \
            --nsmooth $smoothsteps --noreshape
        end
      endif
      @ briknum++
    end
  endif
  @ subnum++
end

# convert spherical data to briks
echo "### converting to briks..."
set briknum = 1
set subnum = 1
foreach sub ($sublist)
  if ($newlist[$subnum] == 1) then
    cd $groupdir/$sub

    # step through each condition
    foreach cond ($condlist)
      set scandir = $scandirlist[$briknum]
      if ($scandir != -1) then
        foreach hemi ($hemilist)
          # resize to multislice 64x64
          bresize $cond-sph-sm$smoothsteps-$hemi \
                  -newx 64 -newy 64 \
                  -quiet \
                  -outstem $cond-sph-sm$smoothsteps-folded-$hemi

          # remove brik if it exists
          if (-e $cond-sph-folded-$hemi+orig.BRIK) then
            rm $cond-sph-sm$smoothsteps-folded-$hemi+orig.BRIK
            rm $cond-sph-sm$smoothsteps-folded-$hemi+orig.HEAD
          endif

          # convert to brik
          if ($byteorder == "LSB") then
            set swap = "-4swap"
          else
            set swap = ""
          endif
          to3d \
            -fbuc $swap\
            -prefix $cond-sph-sm$smoothsteps-folded-$hemi \
            -view orig \
            -xFOV 120L-R \
            -yFOV 120P-A \
            -zFOV 60S-I \
            3Df:0:0:64:64:1:$cond-sph-sm$smoothsteps-folded-$hemi'_???.bfloat'

          # resave as scaled shorts
          3dcalc -prefix temp \
                 -a $cond-sph-sm$smoothsteps-folded-$hemi+orig \
                 -expr a \
                 -fscale \
                 -datum short

          # replace brik with temp
          mv temp+orig.BRIK $cond-sph-sm$smoothsteps-folded-$hemi+orig.BRIK
          mv temp+orig.HEAD $cond-sph-sm$smoothsteps-folded-$hemi+orig.HEAD

          # remove folded bfloats
          rm $cond-sph-sm$smoothsteps-folded-$hemi'_'???.bfloat
          rm $cond-sph-sm$smoothsteps-folded-$hemi'_'???.hdr
        end
      endif
      @ briknum++
    end
  endif
  @ subnum++
end

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

# calculate t statistics with 3dttest
echo "### calculating averages and t stats with 3dttest..."
set testdir = $groupdir/ttest
if (! -e $testdir) then
  mkdir $testdir
endif
cd $testdir
set testnum = 1
foreach testname ($testnamelist)
  set testcond1 = $testcond1list[$testnum]
  set testcond2 = $testcond2list[$testnum]
  if ($pairedflaglist[$testnum] == 1) then
    set paired = 1
    set pairedflag = "-paired"
  else
    set paired = 0
    set pairedflag = ""
  endif
  foreach hemi ($hemilist)
    # remove brik if it exists
    if (-e $testname-sph-sm$smoothsteps-folded-$hemi+orig.BRIK) then
      rm $testname-sph-sm$smoothsteps-folded-$hemi+orig.BRIK
      rm $testname-sph-sm$smoothsteps-folded-$hemi+orig.HEAD
    endif

    set inlist1 = ()
    set inlist2 = ()
    set subnum = 1
    foreach sub ($sublist)
      @ briknum = (($subnum - 1) * $#condlist) + $testcond2
      if ($scandirlist[$briknum] == -1) then
        @ subnum++
        continue
      endif
      if ($testcond1 != 0) then
        @ briknum = (($subnum - 1) * $#condlist) + $testcond1
        if (($scandirlist[$briknum] == -1) && ($paired != 0)) then
          @ subnum++
          continue
        endif
      endif
      set indir = $groupdir/$sub
      if ($testcond1 != 0) then
        set instem = $condlist[$testcond1]-sph-sm$smoothsteps-folded-$hemi+orig
        set inlist1 = ($inlist1 $indir/$instem)
      endif
      set instem = $condlist[$testcond2]-sph-sm$smoothsteps-folded-$hemi+orig
      set inlist2 = ($inlist2 $indir/$instem)
      @ subnum++
    end
    if ($testcond1 != 0) then
      set inlist1 = (-set1 $inlist1)
    else
      set inlist1 = "-base1 0"
    endif
    set inlist2 = (-set2 $inlist2)
    
    3dttest -prefix $testname-sph-sm$smoothsteps-folded-$hemi $pairedflag $inlist1 $inlist2

    # convert to bucket format and relabel subbriks
    3dbucket -prefix temp -fbuc \
             $testname-sph-sm$smoothsteps-folded-$hemi+orig'[0]' \
             $testname-sph-sm$smoothsteps-folded-$hemi+orig'[1]'
    3drefit -sublabel 0 $testname':mean' \
            -sublabel 1 $testname':t-stat' \
            temp+orig
    mv temp+orig.BRIK $testname-sph-sm$smoothsteps-folded-$hemi+orig.BRIK
    mv temp+orig.HEAD $testname-sph-sm$smoothsteps-folded-$hemi+orig.HEAD
  end
  @ testnum++
end

# convert back to spherical
echo "### converting back to spherical..."
cd $testdir
set testnum = 1
foreach testname ($testnamelist)
  foreach hemi ($hemilist)
    # convert to bfloats
    forceMSB $testname-sph-sm$smoothsteps-folded-$hemi+orig.BRIK
    brik2bfloat $testname-sph-sm$smoothsteps-folded-$hemi+orig \
                $testname-mean-sph-sm$smoothsteps-folded-$hemi 0
    brik2bfloat $testname-sph-sm$smoothsteps-folded-$hemi+orig \
                $testname-tstat-sph-sm$smoothsteps-folded-$hemi 1

    # resize back to linear array - spherical surface
    bresize $testname-mean-sph-sm$smoothsteps-folded-$hemi \
            -nslices 41 -trunc 4094 -unfold -quiet \
            -outstem $testname-mean-sph-sm$smoothsteps-$hemi
    bresize $testname-tstat-sph-sm$smoothsteps-folded-$hemi \
            -nslices 41 -trunc 4094 -unfold -quiet \
            -outstem $testname-tstat-sph-sm$smoothsteps-$hemi

    # remove folded bfloats
    rm $testname-mean-sph-sm$smoothsteps-folded-$hemi'_'???.bfloat
    rm $testname-tstat-sph-sm$smoothsteps-folded-$hemi'_'???.bfloat
    rm $testname-mean-sph-sm$smoothsteps-folded-$hemi'_'???.hdr
    rm $testname-tstat-sph-sm$smoothsteps-folded-$hemi'_'???.hdr
  end
  @ testnum++
end

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

# calculate anova stats
echo "### calculating averages and t stats with 3dANOVA2..."
set testdir = $groupdir/anova
if (! -e $testdir) then
  mkdir $testdir
endif
cd $testdir

foreach hemi ($hemilist)
  # remove brik if it exists
  if (-e $analname-sph-sm$smoothsteps-folded-$hemi+orig.BRIK) then
    rm $analname-sph-sm$smoothsteps-folded-$hemi+orig.BRIK
    rm $analname-sph-sm$smoothsteps-folded-$hemi+orig.HEAD
  endif

  set condnum = 1
  set inlist = ()
  set outlist = ()
  foreach cond ($condlist)
    set subnum = 1
    foreach sub ($sublist)
      @ briknum = (($subnum - 1) * $#condlist) + $condnum
      if ($scandirlist[$briknum] == -1) then
        echo "condition $cond missing for subject $sub ... skipping"
        @ subnum++
        continue
      endif

      set indir = $groupdir/$sub
      set instem = $cond-sph-sm$smoothsteps-folded-$hemi+orig
      set inlist = ($inlist -dset $condnum $subnum $indir/$instem)
      @ subnum++
    end

    set outlist = ($outlist -amean $condnum $cond)
    @ condnum++
  end
  set contnum = 1
  foreach cont ($twocondtestlist)
    set cnum1 = $cnum1list[$contnum]
    set cnum2 = $cnum2list[$contnum]
    set outlist = ($outlist -adiff $cnum1 $cnum2 $cont)
    @ contnum++
  end

  3dANOVA2 -type 3 -alevels $#condlist -blevels $#sublist \
           $inlist \
           $outlist \
           -bucket $analname-sph-sm$smoothsteps-folded-$hemi
end

# convert back to spherical
echo "### converting back to spherical..."
cd $testdir
foreach hemi ($hemilist)
  forceMSB $analname-sph-sm$smoothsteps-folded-$hemi+orig.BRIK
  set testnum = 1
  foreach testname ($testnamelist)
    # convert to bfloats
    @ mnum = 2 * ($testnum - 1)
    @ tnum = $mnum + 1
    brik2bfloat $analname-sph-sm$smoothsteps-folded-$hemi+orig \
                $testname-mean-sph-sm$smoothsteps-folded-$hemi $mnum
    brik2bfloat $analname-sph-sm$smoothsteps-folded-$hemi+orig \
                $testname-tstat-sph-sm$smoothsteps-folded-$hemi $tnum

    # resize back to linear array - spherical surface
    bresize $testname-mean-sph-sm$smoothsteps-folded-$hemi \
            -nslices 41 -trunc 4094 -unfold -quiet \
            -outstem $testname-mean-sph-sm$smoothsteps-$hemi
    bresize $testname-tstat-sph-sm$smoothsteps-folded-$hemi \
            -nslices 41 -trunc 4094 -unfold -quiet \
            -outstem $testname-tstat-sph-sm$smoothsteps-$hemi

    # remove folded bfloats
    rm $testname-mean-sph-sm$smoothsteps-folded-$hemi'_'???.bfloat
    rm $testname-tstat-sph-sm$smoothsteps-folded-$hemi'_'???.bfloat
    rm $testname-mean-sph-sm$smoothsteps-folded-$hemi'_'???.hdr
    rm $testname-tstat-sph-sm$smoothsteps-folded-$hemi'_'???.hdr
    @ testnum++
  end
end

# apply threshold to brik and convert back to spherical
set thresh = 3.047
set threshinfix = thresh5x10-3
# corresponds to p=0.005
echo "### applying threshold and converting back to spherical..."
set testdir = $groupdir/anova
cd $testdir
foreach hemi ($hemilist)
  set testnum = 1
  foreach testname ($testnamelist)
    # remove brik if it exists
    if (-e $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.BRIK) then
      rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.BRIK
      rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.HEAD
    endif

    # apply threshold
    @ mnum = 2 * ($testnum - 1)
    @ tnum = $mnum + 1
    3dcalc -prefix $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi \
           -a $analname-sph-sm$smoothsteps-folded-$hemi+orig'['$mnum']' \
           -b $analname-sph-sm$smoothsteps-folded-$hemi+orig'['$tnum']' \
           -fscale -datum short \
           -expr "a*step(abs(b)-$thresh)"

    # convert to bfloats
    forceMSB $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.BRIK
    brik2bfloat $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig \
                $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi 0

    # resize back to linear array - spherical surface
    bresize $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi \
            -nslices 41 -trunc 4094 -unfold -quiet \
            -outstem $testname-mean-$threshinfix-sph-sm$smoothsteps-$hemi

    # remove folded bfloats and temp brik
    rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.BRIK
    rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.HEAD
    rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi'_'???.bfloat
    rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi'_'???.hdr
    @ testnum++
  end
end

# apply threshold to brik and convert back to spherical
set thresh = 3.666
set threshinfix = thresh10-3
# corresponds to p=0.001
echo "### applying threshold and converting back to spherical..."
set testdir = $groupdir/anova
cd $testdir
foreach hemi ($hemilist)
  set testnum = 1
  foreach testname ($testnamelist)
    # remove brik if it exists
    if (-e $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.BRIK) then
      rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.BRIK
      rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.HEAD
    endif

    # apply threshold
    @ mnum = 2 * ($testnum - 1)
    @ tnum = $mnum + 1
    3dcalc -prefix $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi \
           -a $analname-sph-sm$smoothsteps-folded-$hemi+orig'['$mnum']' \
           -b $analname-sph-sm$smoothsteps-folded-$hemi+orig'['$tnum']' \
           -fscale -datum short \
           -expr "a*step(abs(b)-$thresh)"

    # convert to bfloats
    forceMSB $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.BRIK
    brik2bfloat $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig \
                $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi 0

    # resize back to linear array - spherical surface
    bresize $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi \
            -nslices 41 -trunc 4094 -unfold -quiet \
            -outstem $testname-mean-$threshinfix-sph-sm$smoothsteps-$hemi

    # remove folded bfloats and temp brik
    rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.BRIK
    rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.HEAD
    rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi'_'???.bfloat
    rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi'_'???.hdr
    @ testnum++
  end
end

# apply threshold to brik and convert back to spherical
set thresh = 4.516
set threshinfix = thresh10-4
# corresponds to p=0.0001
echo "### applying threshold and converting back to spherical..."
set testdir = $groupdir/anova
cd $testdir
foreach hemi ($hemilist)
  set testnum = 1
  foreach testname ($testnamelist)
    # remove brik if it exists
    if (-e $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.BRIK) then
      rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.BRIK
      rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.HEAD
    endif

    # apply threshold
    @ mnum = 2 * ($testnum - 1)
    @ tnum = $mnum + 1
    3dcalc -prefix $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi \
           -a $analname-sph-sm$smoothsteps-folded-$hemi+orig'['$mnum']' \
           -b $analname-sph-sm$smoothsteps-folded-$hemi+orig'['$tnum']' \
           -fscale -datum short \
           -expr "a*step(abs(b)-$thresh)"

    # convert to bfloats
    forceMSB $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.BRIK
    brik2bfloat $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig \
                $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi 0

    # resize back to linear array - spherical surface
    bresize $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi \
            -nslices 41 -trunc 4094 -unfold -quiet \
            -outstem $testname-mean-$threshinfix-sph-sm$smoothsteps-$hemi

    # remove folded bfloats and temp brik
    rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.BRIK
    rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi+orig.HEAD
    rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi'_'???.bfloat
    rm $testname-mean-$threshinfix-sph-sm$smoothsteps-folded-$hemi'_'???.hdr
    @ testnum++
  end
end

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

# paint group results onto individual's surface
echo "### painting group results..."
set testdir = $groupdir/ttest
cd $testdir
foreach testname ($testnamelist)
  foreach hemi ($hemilist)
    set statsname = $testname-tstat-sph-sm$smoothsteps-$hemi
    $surf2surf \
      --srcsubject ico --srcsurfval $testdir/$statsname \
      --trgsubject $paintsubj --trgsurfval $testdir/$statsname.w \
      --trgfmt paint --hemi $hemi --frame 0 --mapmethod nnfr

    set statsname = $testname-mean-sph-sm$smoothsteps-$hemi
    $surf2surf \
      --srcsubject ico --srcsurfval $testdir/$statsname \
      --trgsubject $paintsubj --trgsurfval $testdir/$statsname.w \
      --trgfmt paint --hemi $hemi --frame 0 --mapmethod nnfr
  end
  @ statsnum++
end

echo "### painting group results..."
set testdir = $groupdir/anova
cd $testdir
foreach testname ($testnamelist)
  foreach hemi ($hemilist)
    set statsname = $testname-tstat-sph-sm$smoothsteps-$hemi
    $surf2surf \
      --srcsubject ico --srcsurfval $testdir/$statsname \
      --trgsubject $paintsubj --trgsurfval $testdir/$statsname.w \
      --trgfmt paint --hemi $hemi --frame 0 --mapmethod nnfr

    set statsname = $testname-mean-sph-sm$smoothsteps-$hemi
    $surf2surf \
      --srcsubject ico --srcsurfval $testdir/$statsname \
      --trgsubject $paintsubj --trgsurfval $testdir/$statsname.w \
      --trgfmt paint --hemi $hemi --frame 0 --mapmethod nnfr
  end
  @ statsnum++
end

set threshinfix = thresh5x10-3
echo "### painting group results..."
set testdir = $groupdir/anova
cd $testdir
foreach testname ($testnamelist)
  foreach hemi ($hemilist)
    set statsname = $testname-mean-$threshinfix-sph-sm$smoothsteps-$hemi
    $surf2surf \
      --srcsubject ico --srcsurfval $testdir/$statsname \
      --trgsubject $paintsubj --trgsurfval $testdir/$statsname.w \
      --trgfmt paint --hemi $hemi --frame 0 --mapmethod nnfr
  end
  @ statsnum++
end

set threshinfix = thresh10-3
echo "### painting group results..."
set testdir = $groupdir/anova
cd $testdir
foreach testname ($testnamelist)
  foreach hemi ($hemilist)
    set statsname = $testname-mean-$threshinfix-sph-sm$smoothsteps-$hemi
    $surf2surf \
      --srcsubject ico --srcsurfval $testdir/$statsname \
      --trgsubject $paintsubj --trgsurfval $testdir/$statsname.w \
      --trgfmt paint --hemi $hemi --frame 0 --mapmethod nnfr
  end
  @ statsnum++
end

set threshinfix = thresh10-4
echo "### painting group results..."
set testdir = $groupdir/anova
cd $testdir
foreach testname ($testnamelist)
  foreach hemi ($hemilist)
    set statsname = $testname-mean-$threshinfix-sph-sm$smoothsteps-$hemi
    $surf2surf \
      --srcsubject ico --srcsurfval $testdir/$statsname \
      --trgsubject $paintsubj --trgsurfval $testdir/$statsname.w \
      --trgfmt paint --hemi $hemi --frame 0 --mapmethod nnfr
  end
  @ statsnum++
end

echo "### finished"



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





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

to paint on multiple subjects: lars' loop:


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

# paint group results onto individual's surface

set subnum = 1
foreach sub ($sublist)
	set paintsubj = $sublist[$subnum]

	echo "### painting group results..."
	set testdir = $groupdir/ttest
	cd $testdir
	foreach testname ($testnamelist)
	  foreach infix ($infixlist)
	    foreach hemi ($hemilist)
	      set statsname = $testname-tstat-sph-sm$smoothsteps$infix-$hemi
	      $surf2surf \
	        --srcsubject ico --srcsurfval $testdir/$statsname \
	        --trgsubject $paintsubj --trgsurfval $testdir/$testname-tstat-$paintsubj-$hemi.w \
	        --trgfmt paint --hemi $hemi --frame 0 --mapmethod nnfr

 	     set statsname = $testname-mean-sph-sm$smoothsteps$infix-$hemi
 	     $surf2surf \
	        --srcsubject ico --srcsurfval $testdir/$statsname \
	        --trgsubject $paintsubj --trgsurfval $testdir/$testname-mean-$paintsubj-$hemi.w \
	        --trgfmt paint --hemi $hemi --frame 0 --mapmethod nnfr
 	   end
	  end
	end
	@ subnum++
end

echo "### finished"



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

#to view

set path = ($path /myrtos/usr1/tmp/dhagler/bin-lnx \
  /myrtos/usr1/tmp/dhagler/tools/bfloats \
  /myrtos/usr1/tmp/dhagler/bin-all)
  
newdefs
  
surfstats flavia <wfilenamewouthemi> <hemi> -fslope -fmid -fthresh -smooth

# e.g.:

surfstats flavia reach-mean-sph-sm50 lh -fslope 0.05 -fmid 10 -fthresh 6.7 -smooth 5

# e.g. 2:

surfstats flavia reach-tstat-sph-sm50 lh -fslope 0.1 -fmid 0 -fthresh 2.5 -smooth 5


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

