#!/bin/tcsh -f

if ("$1" == "-help" || "$1" == "-h" || "$2" == '') goto HELP

set input = $1
set afni_prefix = $2
set neuro_ori = 0

set strt_dir = "$PWD"

if ("$input:t" == "$input:h") then
   set fdir = ./
   set fname = "$input:t"
else 
   if ( -d $input ) then
      set fdir = "$input"
      set fname = ""
   else
      set fdir = "$input:h"
      set fname = "$input:t"
   endif
endif
cd "$fdir"
set fdir = "$PWD"
cd -

if ("$afni_prefix:t" == "$afni_prefix:h") then
   set out_dir = "$PWD"
   set afni_prefix = "$afni_prefix:t"
else 
   set out_dir = $afni_prefix:h
   set afni_prefix = $afni_prefix:t
endif

if ( ! -d "$out_dir") mkdir -p "$out_dir"
if ( ! -d "$out_dir") then
   echo "No output directory $out_dir"
   goto END
endif
cd "$out_dir"
set out_dir = "$PWD"
cd -


if ("$fname" == "") then
   set fname = "COR-"
endif

if ("$fname:e" == "mgz") then
   cd "$fdir"
   source $FREESURFER_HOME/FreeSurferEnv.csh
   mri_convert -ot cor $fname "$out_dir"
   set fdir = "$out_dir"
   cd "$strt_dir"
endif

#COR to brik
   cd "$fdir"
   set cor_files = ( `echo COR-???` ) >& /dev/null 
    if ( $status ) then
	set endstr = "failure: did not find COR files under '$fdir'"
    endif
   # create BRIK with to3d

    #SLAB changed by ZSS Mar 12 04. was 0.5 mm off. Bug pointed
    #out by A. Thomas. Good info found here:
    #www.wideman-one.com/gw/brain/fs/coords/fscoords.htm
    
      #New block added for reading COR images that are no longer 256x256x256 / 1mm cubic voxels ZSS Oct 20 04
      
      set Sr = `grep -w imnr0 COR-.info`
      set i0 = $Sr[$#Sr]
      set Sr = `grep -w imnr1 COR-.info`
      set i1 = $Sr[$#Sr]
      set Sr = `grep -w fov COR-.info`
      set FOV = `ccalc -eval $Sr[$#Sr] \* 1000`
      set Sr = `grep -w x COR-.info | grep -v xform`
      set nx = $Sr[$#Sr]
      set Sr = `grep -w y COR-.info`
      set ny = $Sr[$#Sr]
      set Sr = `grep -w thick COR-.info`
      set zthick = `ccalc -eval $Sr[$#Sr] \* 1000`
      set Sr = `grep -w psiz COR-.info`
      set psize = `ccalc -eval $Sr[$#Sr] \* 1000`

      set xSLAB0 = `ccalc -eval $nx \* $psize / 2`
      set xSLAB1 = `ccalc -eval $xSLAB0 - $psize`
      set ySLAB0 = `ccalc -eval $ny \* $psize / 2`
      set ySLAB1 = `ccalc -eval $ySLAB0 - $psize`
      set zSLAB0 = `ccalc -eval '('$i1 - $i0 + 1')' \* $zthick / 2`
      set zSLAB1 = `ccalc -eval $zSLAB0 - $zthick`
     
    if ( $neuro_ori == 1 ) then
      to3d -prefix $afni_prefix -xSLAB $xSLAB0'L'-$xSLAB1'R' -ySLAB $ySLAB0'S'-$ySLAB1'I' -zSLAB $zSLAB0'P'-$zSLAB1'A' '3Db:0:0:'$nx':'$ny':1:COR-???'
    else
	   to3d -prefix $afni_prefix -xSLAB $xSLAB0'R'-$xSLAB1'L' -ySLAB $ySLAB0'S'-$ySLAB1'I' -zSLAB $zSLAB0'P'-$zSLAB1'A' '3Db:0:0:'$nx':'$ny':1:COR-???'
    endif

    cd "$strt_dir"
    if ("$fdir" != "$out_dir") then
      mv "$fdir"/$afni_prefix* "$out_dir"
    endif
    
    
goto END

HELP:
   echo "Usage: `basename $0` <FSvoldata> <Prefix>"
   echo "A script to convert COR- or .mgz files from FreeSurfer."
   echo " DO NOT use this script for general purpose .mgz conversions"
   echo " Use mri_convert instead."
   echo "Example 1: Taking COR- images in mri/orig to BRIK volume"
   echo "      @SUMA_FSvolToBRIK mri/orig test/cor_afni"
   echo ""
   echo "Example 2: Taking .mgz volume to BRIK volume"
   echo "      @SUMA_FSvolToBRIK mri/aseg.mgz test/aseg_afni"
   echo ""
   echo "To view segmented volumes in AFNI, use the FreeSurfer"
   echo "color scale by doing:"
   echo "   Define Overlay --> Pos? (on)"
   echo "   Choose continuous (**) colorscale"
   echo "   Right Click on colorscale --> Choose Colorscale"
   echo "   Select FreeSurfer_Seg_255"
   echo "   Set Range to 255"
   echo ""
   goto END
   
END:
   
