#!/bin/tcsh
# rutviknimlpmap.csh
#
# read notes from AFNI header of dataset and make NIML atlas point list
#

set headin = "desikan_killiany.desai_prob.at_TT_N27+tlrc"
set headout_pre = "TT_dkpmap"
set mpmin = "desikan_killiany.desai_prob.at_subcortical_TT_N27.bl6.max.nii.gz"
set mpmout = "TT_desai_dk_mpm"
set nimlout = dkpmap.niml
set nimloutmpm = dkmpm.niml

# set headin = "fs.subcortical.desai_prob.at_TT_N27+tlrc"
# set headout_pre = "TT_fs_desai_pmap"
# set nimlout = TT_fs_desai_pmap.niml

echo '<atlas_point_list' > $nimlout
echo ' ni_form="ni_group" >' >> $nimlout

set nv = `3dnvals $headin`
@ nv -- 
# replace sub-brick label for every sub-brick with structure name
foreach sb (`count -digits 1 0 $nv 1`)
  set lbi = `3dNotes $headin |grep "Brik $sb\:"`
  3drefit -sublabel $sb $lbi[3] $headin
  # write out NIML point structure
  # the STRUCT field contains the structure name
  # the SB_LABEL field contains the sub-brick label that is associated with
  #  this structure
  echo '<ATLAS_POINT' >> $nimlout
  echo 'data_type="atlas_point"' >> $nimlout
  echo 'STRUCT='$lbi[3] >> $nimlout
  echo 'VAL='\"$sb\" >> $nimlout
  echo 'OKEY='\"$sb\" >> $nimlout
  echo 'GYoAR="0"' >> $nimlout
  echo 'COG=" 0.000000 0.000000 0.000000"' >> $nimlout
  echo 'SB_LABEL='$lbi[3]' />' >> $nimlout
end
echo '</atlas_point_list>' >> $nimlout

3dcalc -a $headin -prefix $headout_pre -expr 'a/100' -datum byte -overwrite

# the compressed file takes much less space because the prob. maps
#  are mostly zero (1/200 of original byte data). 
# Delete original uncompressed data
gzip ${headout_pre}+tlrc.BRIK
rm ${headout_pre}+tlrc.BRIK

3drefit -atrstring ATLAS_LABEL_TABLE file:$nimlout \
  ${headout_pre}+tlrc
3drefit -cmap CONT_CMAP ${headout_pre}+tlrc
3drefit -atrint ATLAS_PROB_MAP 1 ${headout_pre}+tlrc



# create local NIML text file or modify global AFNI_atlas_spaces.niml to
# include new atlases. See the global file for an example of an atlas
# structure
# setenv AFNI_LOCAL_ATLAS AFNI_local_spaces.niml 
# atlas should now be used by whereami and show up in its list of atlases
# whereami -show_atlases

# note dataset may be byte, short or float values
# if the data is unscaled byte or short, then the values are divided by 250
# as the default (used with CA_N27_PM)
# byte values take up much less space, of course, than float and are faster
# to load. Accuracy is limited to 1/255 at most, but the precision of 
# whereami output is 0.01 anyway. The float values take up 2.8 GB, so even the
# byte storage is 700 MB. This presents a much bigger issue for inclusion with
# AFNI distribution and the possibility for using a dual standard for atlases
# where atlases are considered basic or preferred versus supplemental atlases.
# Perhaps using the supplemental NIML file will be good enough for this purpose.
# Rick suggested generating the MPM atlas from these datasets, and generating
# the atlases from the fcon1000 and Freesurfer segmentation

# these additions were done to test byte and short atlases

# 3dcalc -a ddpmap+tlrc. -datum byte -expr a -prefix ddpmapb
# 3dcalc -a ddpmap+tlrc. -datum short -expr a -prefix ddpmaps

# these are the entries for the different "DD" pmaps in the AFNI_local_spaces.niml

#<atlas_dataset
#   atlas_name="Destrieux-Desai PMaps"
#   dset_name="ddpmap+tlrc"
#   template_space="TT_N27"
#   description="Contains 75 cortical areas in each hemisphere,including both gyri and sulci"
#   comment="Described in Destrieux et al., Neuroimage 2010 (53) pp. 1-15."
#></atlas_dataset>
#
#<atlas_dataset
#   atlas_name="Destrieux-Desai PMaps-byte"
#   dset_name="ddpmapb+tlrc"
#   template_space="TT_N27"
#   description="Contains 75 cortical areas in each hemisphere,including both gyri and sulci"
#   comment="Described in Destrieux et al., Neuroimage 2010 (53) pp. 1-15."
#></atlas_dataset>
#
#<atlas_dataset
#   atlas_name="Destrieux-Desai PMaps-short"
#   dset_name="ddpmaps+tlrc"
#   template_space="TT_N27"
#   description="Contains 75 cortical areas in each hemisphere,including both gyri and sulci"
#   comment="Described in Destrieux et al., Neuroimage 2010 (53) pp. 1-15."
#></atlas_dataset>


#This is an example of output using this atlas with whereami. This works the same for 
#short, float, byte and "wv2" (genx functions) or modified non-wv2 functions. The CA_N27_PM also
#gives the same results consistent across wv2 and non-wv2. 
#
#Warning messages have been reduced. Atlases are now marked as found using an atlas_found field in
#the ATLAS structure with a macro ATL_FOUND(atl) to simplify access. The value is -1,0 or 1 with
#-1 for not found, 1 for found, 0 for not searched yet.
