#!/bin/tcsh
# script to make atlases from the UNC infant atlases contributed by Feng Shi
# this script specifically makes atlases from the CSF, white and gray segmentation and probability maps 

# make output datasets as small as possible
setenv AFNI_COMPRESSOR GZIP


@AfniEnv -set AFNI_SUPP_ATLAS_DIR /Users/dglen/UNC_Infant_Atlases

# first, the simple atlases with combined segmentation set to values of 10, 150, 250 for CSF,grey,white matter,resp.
# @Atlasize -space unc_infant_1yr -dset infant-1yr-seg.nii.gz -lab_file ./gwcsf.txt 1 0 -atlas_type G -lab_file_delim ';'
# @Atlasize -space unc_infant_2yr -dset infant-2yr-seg.nii.gz -lab_file ./gwcsf.txt 1 0 -atlas_type G -lab_file_delim ';'
# @Atlasize -space unc_infant_neo -dset infant-neo-seg.nii.gz -lab_file ./gwcsf.txt 1 0 -atlas_type G -lab_file_delim ';'

# now create probability map atlases for AFNI format for CSF, white and grey
# each map has to be scaled by the range set for each tissue type
foreach age ( neo 1yr 2yr )
   3dcalc -a infant-${age}-seg-csf.nii.gz -expr 'a/10' -prefix infant-${age}-seg-csf -datum float -overwrite
   3dcalc -a infant-${age}-seg-wm.nii.gz -expr 'a/250' -prefix infant-${age}-seg-wm  -datum float -overwrite
   3dcalc -a infant-${age}-seg-gm.nii.gz -expr 'a/150' -prefix infant-${age}-seg-gm  -datum float -overwrite
   3dbucket -prefix infant-${age}-pmaps -overwrite \
      infant-${age}-seg-csf+tlrc infant-${age}-seg-wm+tlrc. infant-${age}-seg-gm+tlrc.
   rm infant-${age}-seg-csf+tlrc infant-${age}-seg-wm+tlrc. infant-${age}-seg-gm+tlrc.
end

set si = 0
set nimloutpm = segs_pmap.niml
echo '<atlas_point_list' > $nimloutpm
echo ' ni_form="ni_group" >' >> $nimloutpm

foreach struct ( CSF white grey )
   echo '<ATLAS_POINT' >> $nimloutpm
   echo 'data_type="atlas_point"' >> $nimloutpm
   echo 'STRUCT='$struct >> $nimloutpm
   echo 'VAL='\"$si\" >> $nimloutpm
   echo 'OKEY='\"$si\" >> $nimloutpm
   echo 'GYoAR="0"' >> $nimloutpm
   echo 'COG='\" 0.000000 0.000000 0.000000\" >> $nimloutpm
   echo 'SB_LABEL='$struct' />' >> $nimloutpm
   set si = `ccalc -int -expr "$si + 1"`
end
echo '</atlas_point_list>' >> $nimloutpm

foreach age ( neo 1yr 2yr )
   3drefit -space unc_infant_${age} infant_${age}-pmaps+tlrc
   3drefit -sublabel 0 CSF -sublabel 1 white -sublabel 2 grey infant-${age}-pmaps+tlrc
   3drefit -cmap CONT_CMAP infant-${age}-pmaps+tlrc
   3drefit -atrint ATLAS_PROB_MAP 1 infant-${age}-pmaps+tlrc
   # put the atlas_point_list niml table in each dataset
   3drefit -atrstring ATLAS_LABEL_TABLE file:$nimloutpm infant-${age}-pmaps+tlrc
end


set atlasniml = infant_pmaps_atlas.niml
echo > $atlasniml
foreach age ( neo 1yr 2yr )
   echo '<ATLAS' >> $atlasniml
   echo '   atlas_name='\"UNC_infant_${age}_pmap\" >> $atlasniml
   echo '   dset_name='\"infant-${age}-pmaps+tlrc\" >> $atlasniml
   echo '   template_space='\"unc_infant_${age}\" >> $atlasniml
   echo '   description='\"Contains the probability maps for CSF, white and grey matter in $age infants\" >> $atlasniml
   echo '   comment='\"Please see infant AAL atlases for full citation info by Feng Shi, et al.\" >> $atlasniml
   echo ' ></ATLAS>'  >> $atlasniml
end
