#!/bin/tcsh
set spacelist = ( TT MNI MNIa )
# copy the old atlas
foreach space ( $spacelist )
   rm ${space}_caez_ml_18_relabel+tlrc.*
   3dcopy ~/abin/${space}_caez_ml_18+tlrc. ${space}_caez_ml_18_relabel.nii.gz
end

# get the labels by themselves
3dinfo -atlas_points ~/abin/TT_caez_ml_18+tlrc. | grep "STRUCT=" | awk -F= '{print $2}' > mllabelsonly.txt

rm ml_index_label.txt

# get the index for that label
set nl = `wc -l mllabelsonly.txt`
foreach labeli (`count 1 $nl[1]`)
   set label = `head -$labeli mllabelsonly.txt|tail -1`
   set index = `3dinfo -atlas_points ~/abin/TT_caez_ml_18+tlrc. | grep -A 1 "$label" | grep VAL= | awk -F= '{print $2}' | tr -d \"`
   # replace the spaces with underscores
   set label = `echo $label| tr " " "_"`
   echo $index $label >> ml_index_label.txt
end


# make atlases for each space
foreach space ( $spacelist )
 @Atlasize -lab_file ml_index_label.txt 1 0 -lab_file_delim \"  \
   -atlas_type S -atlas_name ${space}_caez_ml_1.8 \
   -atlas_description "Updated Eickhoff-Zilles Macrolabel atlas" \
   -dset  ${space}_caez_ml_18_relabel.nii.gz -centers -centertype Icent -skip_novoxels
 rm ${space}_caez_ml_18+tlrc.*
 3dcopy ${space}_caez_ml_18_relabel.nii.gz ${space}_caez_ml_18
 3drefit -denote ${space}_caez_ml_18+tlrc
end
