#!/bin/tcsh
# script used to update from previous version
# not necessary for public consumption but may be of interest

set inprefix = D99_atlas_1.2a.nii.gz
set outprefix = D99_atlas_1.2b
# save output volumes as compressed NIFTI datasets
set postfix = .nii.gz
set labeltable = labeltablesorted.txt
rm ${outprefix}*

# just copy for this version
3dcopy ${inprefix} ${outprefix}${postfix}

# now keep just right side, just to compute center of mass of each region
3dcalc -a ${outprefix}${postfix} -RAI -expr 'a*NOT(step(x))' -prefix ${outprefix}_right${postfix} -overwrite
# and the left just for surfaces
3dcalc -a ${outprefix}${postfix} -RAI -expr 'a*(step(x))' -prefix ${outprefix}_left${postfix} -overwrite

## compute centers of mass and make atlas labels
@Atlasize -lab_file $labeltable 1 0 -centers -space D99_Macaque -atlas_name D99_Macaque_right \
   -atlas_description "Saleem D99 macaque right side - manually edited from surface based"    \
   -dset ${outprefix}_right${postfix}


# copy the labels from the right side atlas that has the centers of mass
3drefit -copytables  ${outprefix}_right${postfix} ${outprefix}${postfix}
# copy the labels from the right side atlas that has the centers of mass
3drefit -copytables  ${outprefix}_right${postfix} ${outprefix}_left${postfix}

# remove the history from each of the atlas dataset
3drefit -denote ${outprefix}${postfix}
3drefit -denote ${outprefix}_right${postfix}

# make surfaces for right side
mkdir right_surfaces
cd right_surfaces
rm ${outprefix}_right*.gii ${outprefix}_right*.niml.dset
IsoSurface -isorois+dsets -input ../${outprefix}_right${postfix} -o ${outprefix}_right.gii \
   -remesh 0.1 -Tsmooth 0.1 100
cd ..

# make surfaces for left side
mkdir left_surfaces
cd left_surfaces
rm ${outprefix}_left*.gii ${outprefix}_left*.niml.dset
IsoSurface -isorois+dsets -input ../${outprefix}_left${postfix} -o ${outprefix}_left.gii \
   -remesh 0.1 -Tsmooth 0.1 100
cd ..

# update left,right GIFTI fields
# this allows "walnut" prying and left/right hemisphere hiding
cd wholebrain_surfaces
foreach surf ( *v2.L.*.gii )
   gifti_tool -mod_DA_meta AnatomicalStructurePrimary CortexLeft -infile $surf -write_gifti $surf
end

foreach surf ( *v2.R.*.gii )
   gifti_tool -mod_DA_meta AnatomicalStructurePrimary CortexRight -infile $surf -write_gifti $surf
end

cd ..

# manually edit CustomAtlases.niml to have description with citation information

# remainder instructions are not executed - just for reference from previous versions
#
#
#
#
## 3dcopy ${inprefix}+tlrc ${outprefix}
## @Atlasize -lab_file $labeltable 1 0 -centers -space D99_Macaque -atlas_name D99_Macaque \
##    -atlas_description "Saleem D99 macaque - manually edited from surface based"        \
##    -dset ${outprefix}+tlrc.
#
## previous version was not symmetric in left-right
## 3dZeropad -overwrite -R 1 -prefix D99_atlas_1.1c_zp D99_atlas_1.1c+tlrc.
#
#
#
#
#
#
#
#
## flip to make symmetric using right side as basis for drawn side
#3dLRflip -overwrite -prefix lr_flip ${inprefix}+tlrc.
## left side derived from flipped right side
#3dcalc  -overwrite  -a ${inprefix}+tlrc -b lr_flip+tlrc. -RAI \
#    -expr 'a*NOT(step(x))+b*(step(x))' -prefix ${outprefix}
#
## put label table on symmetric atlas
#3drefit -copytables ${inprefix}+tlrc  ${outprefix}+tlrc.
#
#
## now keep just right side, just to compute center of mass of each region
#3dcalc -a ${outprefix}+tlrc. -RAI -expr 'a*NOT(step(x))' -prefix ${outprefix}_right -overwrite
#3drefit -copytables ${inprefix}+tlrc  ${outprefix}_right+tlrc.
#
## 3drefit -copytables ${outprefix}_sym+tlrc D99_atlas_right+tlrc.
#
## update the atlas with the center of mass labeltable from the right side
## delete the first temporary version of this output atlas we just created
## rm ${outprefix}+tlrc.*
#
## use symmetric version - now just keep "sym" name for temporary original smoothness
##3drename ${outprefix}_sym+tlrc ${outprefix}
##3drefit -copytables D99_atlas_right+tlrc ${outprefix}+tlrc.
#
## Get modal value in 0.5 mm radius sphere at each voxel
## 3dLocalstat -overwrite -nbhd 'SPHERE(0.5)' -stat mode -prefix  ${outprefix}_mode -input ${outprefix}_sym+tlrc
#
## ***** stop using the previous claustrum!!!!!!
##
## use claustrum from previous processing
## 3dcalc -a ${outprefix}_mode+tlrc. -b claus_rs+tlrc. -c claus_left+tlrc. -overwrite \
##   -expr 'a*(not(b+c+equals(a,176)))+176*step(b+c)' -prefix ${outprefix} -datum byte -nscale
#
## use original (not modally smoothed) data from hippocampus (CA1 CA2 CA3 CA4 DG preS proS )
##set nosmoothlist = ""
##set roisearchinit = 0
##foreach roi ( CA1 CA2 CA3 CA4 DG preS proS )
##   set roisearch = `grep $roi $labeltable`
##   if ("$roisearchinit" == "0") then
##      set nosmoothlist = $roisearch[1]
##      set roisearchinit = 1
##   else
##      set nosmoothlist = ( $nosmoothlist,$roisearch[1] )
##   endif
##end
##
### combine mostly modally smoothed and some unsmoothed region data
##3dcalc -a ${outprefix}_mode+tlrc. -b ${outprefix}_sym+tlrc      \
##   -expr "a*not(amongst(b,$nosmoothlist))+b*amongst(b,$nosmoothlist)" \
##   -prefix  ${outprefix} -datum byte -nscale -overwrite
##
##
### extract surfaces from right side
### make right-only atlas, for getting center-of-mass and right surfaces
##3dcalc -datum byte -nscale -a ${outprefix}+tlrc. \
##   -expr 'a*step(-x)' -RAI -prefix ${outprefix}_smooth_right+tlrc -overwrite
##
#
## note some regions will need to be manually edited after smoothing
##  namely Striatum, V1, GP
##  both V1 and GP have small gaps that need to be reinserted.
##  GP needs a gap between the internal and external
##  Could try with threshold from template for these, but manual edit is fairly easy (Saleem)
#
