#!/usr/bin/env bash

### TIR script
### for resting state fMRI processing
### generated by Tossi Ikuta <tikuta@go.olemiss.edu>
### by improving fcon_1000 script

### Usage 
### TIR_??.sh SessionName



## ###########
## VARIABLES
## ###########


                
                
######  File Name Variables
######  NEEDS TO BE CHANGED FOR EACH SET UP

fname="func"    # input name for functional series "func" will point to "func.nii.gz"
aname="anat"    # input name for anat series "anat" will point to "anat.nii.gz"



#pdir="/data/Mask/MNI2mm/"   # Directory name for the two priors below
pdir="/ptmp/r1770/bin/TIR"   # Directory name for the two priors below
wmpr=${pdir}/avg152T1_white_bin.nii.gz
                            # White Matter Prior
csfpr=${pdir}/avg152T1_csf_bin.nii.gz
     
                            # CSF Prior
                            
                            

NuiFSF="${pdir}/nuisance_NG.fsf"
                            # Full path to "nuisance_NG.fsf"
                            # this fsf will be used as a template to fit to each series
                            
#StandardT1=$(ls ${FSL_DIR}/data/standard/MNI152_T1_2mm_brain.nii.gz);
StandardT1=$(ls ${pdir}//MNI152_T1_2mm_brain.nii.gz);
                            # Full path to the standard brain
                            


###### Scan Related Parameters
###### Default Values

TR="2"      # TR in secs
FirstV="4"  # 4 will delete first four volumes


MotThresh=0.3;  # motion threshold (mm): see JD Poser 2015 Neuroimage for using 0.3mm
                # volumes with any motion above this threshold will be scrubbed
                
######  Smoothing Parameters
fwhm=6

                        

delete=0
                            
## ###########
## Arguments
## ###########


if [ $# -lt "1" ]; then show_help=1; fi
while getopts "hdf:s:t:" Option; do
 case $Option in
  h)
   show_help=1
   ;;
  d)
   delete=1;
   ;;
  f)
   FirstV="$OPTARG"
   echo "First ${FirstV} volumes will be deleted."
   ;;  
  s)
   fwhm="$OPTARG"
   echo "fwhm is ${fwhm}mm."
   ;;
  t)
   TR="$OPTARG"
   echo "TR is ${TR} secs."
   ;;
  esac
 done

shift $((OPTIND-1))

bsize=""



if [[ $show_help == "1" ]]; then
echo ""
echo "USAGE: ${0} [OPTION]... <InputDirs>
Options:
  -d  Delete intermediate functional files to reduce space usage (otherwise x25 data)
  -f  First volumes to be deleted (default=4)
  -s  Smoothing fwhm (in mm: default=6)
  -t  TR (secs: default=2)";
echo ""
echo "Example: ${0} -r SessionDir"
exit;
fi

dte=$(date +%m%d%H%M)


## ###########
## Argument VARIABLES
## ###########


sbj=${1}

sbjdir=${PWD}/${sbj}




## ###########
## Derived Variables
## ###########

######  Directory Names (no need to change)
fdir="${sbjdir}/func"       
adir="${sbjdir}/anat"
sdir="${sbjdir}/seg2mm"
rdir="${sbjdir}/reg2mm"
ndir="${sbjdir}/nui2mm"

sigma=$(echo "scale=10 ; ${fwhm}/2.3548" | bc)


######  Directory Names (no need to change)
InitialTotalV=$(fslinfo ${fdir}/${fname} | head -n 5 | tail -n 1 | xargs -n 1 | tail -n 1 )
VolN=$(echo "${InitialTotalV}-${FirstV}" | bc)
MiddleV=$(echo "(${VolN}-${FirstV})/2" | bc)
LastV=$(echo "${VolN}-1" | bc) 


refvol=0    # reference volume number. it is AFTER the first few volumes are deleted.
            # 0 will point to the very first volume after deleting first four (or your choice).
            # replace 0 by MiddleV to take the middle volume.





#############################################################
### ${cr} variable is the current latest functional file name"
cr="${fname}"
acr="${aname}"
#############################################################


## ###########
## END VARIABLES
## ###########






## ###########
## Anatomical Functions
## ###########

function aprep {
    echo ""
    echo "***************************************************"
    echo "***************  aFile Preperation  ***************"
    echo "***************************************************"

    pushd ${adir}

    echo ""
    echo "A1: deobliquing ${acr}.nii.gz"
    3drefit -deoblique ${acr}.nii.gz

    echo ""
    echo "A2: reorienting ${acr}.nii.gz to RPI"
    fslreorient2std  ${acr} ${acr}_RPI
    
    echo ""
    acr="${acr}_RPI"
    popd
}



function ssT1 {
    echo ""
    echo "***************************************************"
    echo "**************  skull stripping T1  ***************"
    echo "***************************************************"

    pushd ${adir}

    echo ""
    echo "ASt1: skull stripping ${acr}"
    3dSkullStrip -input ${acr}.nii.gz -o_ply ${acr}_surf.nii.gz

    echo ""
    echo "ASt2: skull stripping applied to ${acr}: ${acr}_brain.nii.gz"
    3dcalc -a ${acr}.nii.gz -b ${acr}_surf.nii.gz -expr 'a*step(b)' -prefix ${acr}_brain.nii.gz
    # this may be theoretically redundant
    


    echo ""
    acr="${acr}_brain"
    popd
}

## ###########
## END Anatomical Functions
## ###########



function funcprep {
    echo ""
    echo "***************************************************"
    echo "***************  fFile Preperation  ***************"
    echo "***************************************************"

    echo ""
    echo "F1: Converting nifti into float: ${cr}_FL"
    fslmaths ${cr} ${cr}_FL -odt float
    cr="${fname}_FL"
    

    echo ""
    echo "F2: Removing the first ${FirstV} volumes: ${cr}_FD"
    fslroi ${cr} ${cr}_FD ${FirstV} ${VolN}
    pr=${cr}
    if [ ${delete} -eq 1 ]; then echo "Deleting ${pr}"; rm ${pr}.nii.gz; fi
    cr="${cr}_FD"
    
    #refvol=$(echo "${refvol}-${FirstV}" | bc)
    #echo "F3: now the refvol number is ${refvol} - ${FirstV} : ${refvol}"

    echo ""
    echo "F3: reoriented to RPI to create: ${cr}_RPI"
    #3dresample -orient RPI -inset ${cr} -prefix RPI_${cr}
    fslreorient2std  ${cr} ${cr}_RPI
    pr=${cr}
    if [ ${delete} -eq 1 ]; then echo "Deleting ${pr}"; rm ${pr}.nii.gz; fi
    cr="${cr}_RPI"

    echo ""
    echo "F4: deoblique-ing for afni"
    3drefit -deoblique ${cr}.nii.gz

    echo ""
    echo "F5: 3dDespike, creating a BRIK"
    3dDespike -NEW ${cr}.nii.gz
    3dAFNItoNIFTI despike+orig
    gzip -c despike.nii > ${cr}_DSP.nii.gz
    pr=${cr}
    if [ ${delete} -eq 1 ]; then echo "Deleting ${pr}"; rm ${pr}.nii.gz; rm despike.nii; rm despike+orig.BRIK; rm despike+orig.HEAD; fi
    cr="${cr}_DSP"

    echo ""
    echo "F6: despiked one was created as ${cr}"
    #mkdir dsp
    #mv despike* dsp


    
    # deleted as of 18
    #echo ""
    #echo "F7: mask created as: ${cr}_mask"
    #fslmaths ${cr} -Tmin -bin ${cr}_mask -odt char
    #crfmask="${cr}_mask"

}


function mccr {
    echo ""
    echo "***************************************************"
    echo "***************  MOTION CORRECTION  ***************"
    echo "***************************************************"

    echo ""
    echo "M1: creating mcf directory and move MCF files"
    mkdir -p mcf

    echo ""
    echo "M2: running mcflirt to do MOTION CORRECTION on ${cr}: mcf/${cr}_MCF"
    #echo "(M2: first volume is used as reference (integers begin with 1 not 0 in mcflirt))"
    #mcflirt -in ${cr} -out mcf/${cr}_MCF -mats -plots -refvol ${refvol} -rmsrel -rmsabs
    mcflirt -in ${cr} -out mcf/${cr}_MCF -mats -plots -refvol 0 -rmsrel -rmsabs
    # https://fsl.fmrib.ox.ac.uk/fsl/fslwiki/MCFLIRT
    # Jenkinson, M., Bannister, P., Brady, J. M. and Smith, S. M. Improved Optimisation for the Robust and Accurate Linear Registration and Motion Correction of Brain Images. NeuroImage, 17(2), 825-841, 2002.


    echo ""
    echo "M3: moving mcf/${cr}_MCF to: ${cr}_MCF"
    mv mcf/${cr}_MCF.nii.gz .
    pr=${cr}
    if [ ${delete} -eq 1 ]; then echo "Deleting ${pr}"; rm ${pr}.nii.gz; fi
    cr="${cr}_MCF"

    par="${fdir}/mcf/${cr}.par"

    echo ""
    echo "M4: creating png images"
    fsl_tsplot -i mcf/${cr}.par -t 'MCFLIRT estimated rotations (radians)' -u 1 --start=1 --finish=3 -a x,y,z -w 640 -h 144 -o rot_${cr}.png 
    fsl_tsplot -i mcf/${cr}.par -t 'MCFLIRT estimated translations (mm)' -u 1 --start=4 --finish=6 -a x,y,z -w 640 -h 144 -o trans_${cr}.png 
    fsl_tsplot -i mcf/${cr}_abs.rms,mcf/${cr}_rel.rms -t 'MCFLIRT estimated mean displacement (mm)' -u 1 -w 640 -h 144 -a absolute,relative -o disp_${cr}.png 

    #cat MCF_${cr}_rel_mean.rms

}


function skstfunc {
    echo ""
    echo "***************************************************"
    echo "*********  skull stripping functional  ************"
    echo "***************************************************"

    echo ""
    echo "FSt1: 3dAutomask: ${cr}_mask.nii.gz"
    3dAutomask -prefix ${cr}_mask.nii.gz -dilate 1 ${cr}.nii.gz
    
    # below line added as of 18
    crfmask="${cr}_mask"
    
    echo ""
    echo "FSt2: 3dcalc: ${cr}_SS.nii.gz"
    3dcalc -a ${cr}.nii.gz -b ${cr}_mask.nii.gz -expr 'a*b' -prefix ${cr}_SS.nii.gz
    pr=${cr}
    if [ ${delete} -eq 1 ]; then echo "Deleting ${pr}"; rm ${pr}.nii.gz; fi
    cr="${cr}_SS"
}




function exfunc {
    echo ""
    echo "***************************************************"
    echo "*************   Example Functional  ***************"
    echo "***************************************************"

    echo ""
    echo "The volume ${refvol} is taken as an exammple Volume: EX_${cr}"
    fslroi ${cr} EX_${cr} ${refvol} 1
    exname="EX_${cr}"
}

function reg {
    echo ""
    echo "***************************************************"
    echo "****************  Registration   ******************"
    echo "***************************************************"
    echo ""
           
    afile="${adir}/${acr}"
    efile="${fdir}/${exname}"

    f2a="FuncToAnat.mat"
    a2f="AnatToFunc.mat"

    a2s="AnatToStandard.mat"
    s2a="StandardToAnat.mat"

    f2s="FuncToStandard.mat"
    s2f="StandardToFunc.mat"
    
    echo ""
    mkdir ${rdir}
    pushd ${rdir}

    echo ""
    echo "Reg1: Linearly Registering Func to ${afile}: ${f2a} and FuncInAnat.nii.gz"
    flirt -ref ${afile} -in ${efile} -out FuncInAnat -omat ${f2a} -cost corratio -dof 6 -interp trilinear

    echo ""
    echo "Reg2: inverse ${f2a} to have: ${a2f}"
    convert_xfm -inverse -omat ${a2f} ${f2a}


    echo ""
    echo "Reg3: Linearly Registering Func to Standard ${a2s} and AnatInStandard.nii.gz"
    flirt -ref ${StandardT1} -in ${afile} -out AnatInStandard -omat ${a2s} -cost corratio -searchcost corratio -dof 12 -interp trilinear

    echo ""
    echo "Reg4: inverse ${a2s} to have: ${s2a}"
    convert_xfm -inverse -omat ${s2a} ${a2s}

    echo ""
    echo "Reg5: concatinating ${a2s} and ${f2a} to have: ${f2s}"
    convert_xfm -omat ${f2s} -concat ${a2s} ${f2a}

    echo ""
    echo "Reg6: inverse ${f2s} to have: ${s2f}"
    convert_xfm -inverse -omat ${s2f} ${f2s}


    echo ""
    echo "Reg7: registering ${ex} to Standard to get: ExInStandard.nii.gz"
    flirt -ref ${StandardT1} -in ${efile} -out ExInStandard -applyxfm -init ${f2s} -interp trilinear

    echo ""
    popd

}


function smoothfunc {
    echo ""
    echo "***************************************************"
    echo "************  smoothing functional  ***************"
    echo "***************************************************"

    echo ""
    echo "FSm1: 3dAutomask: ${cr}_mask.nii.gz"
    fslmaths ${cr} -kernel gauss ${sigma} -fmean ${cr}_SM
    pr=${cr}
    if [ ${delete} -eq 1 ]; then echo "Deleting ${pr}"; rm ${pr}.nii.gz; fi
    cr="${cr}_SM"
}



function seg {
    echo ""
    echo "***************************************************"
    echo "*****************  Segmentation   *****************"
    echo "***************************************************"
    echo ""


    aname="${acr%%.nii*}"
    ex="${fdir}/${exname}"
    fm="${fdir}/${crfmask}"


    mkdir ${sdir}

    pushd ${sdir}

    echo ""
    echo "Seg1: Segmentation by FAST: ${acr}"
    echo "(Seg1 takes long...)"
    fast -t 1 -g -p -o SG_${aname} ${adir}/${acr}
    sg=SG_${acr}


    ccsf="CsfInFunc"


    echo ""
    echo "SegCSF2: registering CSF (in anat) to Functional: ${ccsf}"
    #flirt -in ${sg}_prob_0 -ref ${ex} -applyxfm -init ${rdir}/StandardToFunc.mat -out ${ccsf}
    flirt -in ${sg}_prob_0 -ref ${ex} -applyxfm -init ${rdir}/AnatToFunc.mat -out ${ccsf}
    ccsf=${ccsf}


    echo ""
    echo "SegCSF3: smoothing ${ccsf}: ${ccsf}_SM"
    fslmaths ${ccsf} -kernel gauss ${sigma} -fmean ${ccsf}_SM
    ccsf="${ccsf}_SM"

    echo ""
    echo "SegCSF4: registering csf to standard: SM_CsfInStandard"
    flirt -in ${ccsf} -ref ${StandardT1} -applyxfm -init ${rdir}/FuncToStandard.mat -out CsfInStandard_SM
    ccsf="CsfInStandard_SM"

    echo ""
    echo "SegCSF5: intersection of ${ccsf} and prior CSF is taken as: ${ccsf}_MS" 
    fslmaths ${ccsf} -mas ${csfpr} ${ccsf}_MS
    ccsf="${ccsf}_MS"

    echo ""
    echo "SegCSF6: ${ccsf} is registered to functional space"
    flirt -in ${ccsf} -ref ${ex} -applyxfm -init ${rdir}/StandardToFunc.mat -out Csf_SM_MS
    ccsf="Csf_SM_MS"

    echo ""
    echo "SegCSF7: binary csf mask created: ${ccsf}_mask"
    fslmaths ${ccsf} -thr 0.4 -bin ${ccsf}_bin
    ccsf="${ccsf}_bin"

    echo ""
    echo "SegCSF8: CSF (${ccsf}) is masked by functional mask (${fm}): csf_mask"
    fslmaths ${ccsf} -mas ${fm} csf_mask




    echo ""
    echo "SegWM1 Now WH"


    cwm="WmInFunc"

    echo ""
    echo "SegWM2: registering wm (in anat) to Functional: ${cwm}"
    flirt -in ${sg}_prob_2 -ref ${ex} -applyxfm -init ${rdir}/AnatToFunc.mat -out ${cwm}
    cwm=${cwm}


    echo ""
    echo "SegWM3: smoothing ${cwm}: ${cwm}_SM"
    fslmaths ${cwm} -kernel gauss ${sigma} -fmean ${cwm}_SM
    cwm="${cwm}_SM"

    echo ""
    echo "SegWM4: registering wm to standard: ${cwm}_InStandard"
    flirt -in ${cwm} -ref ${StandardT1} -applyxfm -init ${rdir}/FuncToStandard.mat -out ${cwm}_InStandard
    cwm="${cwm}_InStandard"

    echo ""
    echo "SegWM5: intersection of ${cwm} and prior wm is taken as: ${cwm}_MS" 
    fslmaths ${cwm} -mas ${wmpr} ${cwm}_MS
    cwm="${cwm}_MS"

    echo ""
    echo "SegWM6: ${cwm} is registered to functional space"
    flirt -in ${cwm} -ref ${ex} -applyxfm -init ${rdir}/StandardToFunc.mat -out wm_SM_MS
    cwm="wm_SM_MS"

    echo ""
    echo "SegWM7: binary wm mask created: ${cwm}_mask"
    fslmaths ${cwm} -thr 0.4 -bin ${cwm}_bin
    cwm="${cwm}_bin"

    echo ""
    echo "SegWM8: wm (${cwm}) is masked by functional mask (${fm}): wm_mask"
    fslmaths ${cwm} -mas ${fm} wm_mask
    
    echo ""
    popd

    
}


function nui {
    echo ""
    echo "***************************************************"
    echo "**************  Nuisance Regression   *************"
    echo "***************************************************"
    echo ""

    if [ -f ${ndir} ]; 
     then mv ../*nui2mm ../oldnui2mm
    fi
    
    mkdir ${ndir}
    pushd ${ndir}


    echo ""
    echo "Nui1: extracting csf signal by using ${sdir}/csf_mask.nii.gz as a mask: csf.1D"
    3dmaskave -mask ${sdir}/csf_mask.nii.gz -quiet ${fdir}/${cr}.nii.gz > csf.1D

    echo ""
    echo "Nui2: extracting wm signal by using ${sdir}/wm_mask.nii.gz as a mask: wm.1D"
    3dmaskave -mask ${sdir}/wm_mask.nii.gz -quiet ${fdir}/${cr}.nii.gz > wm.1D


    echo ""
    echo "Nui3: generating mat files by replacing five variables in ${NuiFSF}: nuisance.fsf"
    sed -e s:nuisance_dir:${PWD}:g <${NuiFSF} >temp1
    sed -e s:nuisance_model_outputdir:"${PWD}/residuals.feat":g < temp1 >temp2
    sed -e s:nuisance_model_TR:"${TR}":g <temp2 > temp3
    sed -e s:nuisance_model_numTRs:"${VolN}":g < temp3 > temp4
    sed -e s:nuisance_model_input_data:"../func/${cr}":g <temp4 >nuisance.fsf 

    rm temp?


    echo ""
    echo "Nui4: separating 6 motion parameters in ${par} to 6 separate files"
    awk '{print $1}' ${par} > mc1.1D
    awk '{print $2}' ${par} > mc2.1D
    awk '{print $3}' ${par} > mc3.1D
    awk '{print $4}' ${par} > mc4.1D
    awk '{print $5}' ${par} > mc5.1D
    awk '{print $6}' ${par} > mc6.1D




    echo ""
    echo "Nui5: running feat"
    feat_model nuisance


    echo ""
    echo "Nui6: estimating the minimum value"
    minVal=$(3dBrickStat -min -mask ${fdir}/${crfmask}.nii.gz ${fdir}/${cr}.nii.gz)


    echo ""
    echo "Nui7: film_gls to get residuals"
    #film_gls -rn stats -noest -sa -ms 5 ${fdir}/${cr}.nii.gz nuisance.mat ${minVal}
    # new in ver 20 for FSL5/6
    film_gls --rn stats --noest --sa --ms 5 --in==${fdir}/${cr}.nii.gz --pd==nuisance.mat --thr==${minVal}


    echo ""
    echo "Nui8: de-meaning: stats/res4d_mean.nii.gz"
    3dTstat -mean -prefix stats/res4d_mean.nii.gz stats/res4d.nii.gz
    #3dcalc -a ${nuisance_dir}/stats/res4d.nii.gz -b ${nuisance_dir}/stats/res4d_mean.nii.gz -expr '(a-b)+100' -prefix ${func_dir}/${rest}_res.nii.gz


    echo ""
    echo "Nui9: normalizing"
    3dcalc -a stats/res4d.nii.gz -b stats/res4d_mean.nii.gz -expr '(a-b)+100' -prefix ${fdir}/${cr}_Nui.nii.gz

    pr=${cr}
    if [ ${delete} -eq 1 ]; then echo "Deleting ${pr}"; rm ${pr}.nii.gz; fi
    cr="${cr}_Nui"

    # echo ""
    # echo "Nui10: registering to Standard"
    # flirt -ref ${StandardT1} -in ${fdir}/${cr}_Nui.nii.gz -out ${fdir}/${cr}_NuiInStandard -applyxfm -init ${rdir}/FuncToStandard.mat -interp trilinear
    echo ""
    echo "Nuisance Regression Completed"
    echo ""
    popd
    echo ""

    # ${NuiScr}  SM_func_FL_FD_RPI_DSP_MCF_SS.nii.gz func_FL_FD_RPI_DSP_MCF_mask.nii.gz  ${NuiFSF} 2 146 /usr/local/opt/fsl/data/standard/MNI152_T1_2mm_brain.nii.gz mcf/func_FL_FD_RPI_DSP_MCF.par

    
    
}

function censor {
    echo ""
    echo "***************************************************"
    echo "**************  SENSORING/SCRUBBING  **************"
    echo "***************************************************"


    mkdir CENSORED CENSORED/CensoredOut
    fslsplit ${cr}.nii.gz CENSORED/${cr} -t


    relFile=$(ls mcf/${fname}*_rel.rms)
    echo ""
    echo "CENS1 ${relFile} is taken"


    echo ""
    echo "CENS2: censoring out"

    ct=0 #temporary counter for volumes
    for line in $(cat ${relFile});
        do
            if [ ${ct} -lt 1000 ]; then vl=$(echo -en "0${ct}");fi
            if [ ${ct} -lt 100 ]; then vl=$(echo -en "00${ct}");fi
            if [ ${ct} -lt 10 ]; then vl=$(echo -en "000${ct}");fi
            mot="${line}";
            # ${mot} is the motion for the particular volume
            if (( $(echo "${mot} > ${MotThresh}" |bc -l) ));
            then
                            echo ""
                echo "${mot} motion at the volume ${vl}"
                mv CENSORED/${cr}${vl}.nii.gz CENSORED/CensoredOut
            fi
            ct=$(echo "${ct}+1" | bc)
        done

    lst=$(ls CENSORED/${cr}*)

    echo ""
    echo "CENS3: ${cr}_CS"
    fslmerge -t ${cr}_CS ${lst}
    pr=${cr}
    if [ ${delete} -eq 1 ]; then echo "Deleting ${pr}"; rm ${pr}.nii.gz; fi
    cr="${cr}_CS"



    echo ""
    echo "CENSSORING COMPLETED"
    echo ""

}


function tostandard {
    echo ""
    echo "***************************************************"
    echo "*****  Registering Final Product to Standard  *****"
    echo "***************************************************"

    echo ""
    echo "STD: registering ${cr} to Standard"
    flirt -ref ${StandardT1} -in ${fdir}/${cr}.nii.gz -out ${fdir}/${cr}_InStandard -applyxfm -init ${rdir}/FuncToStandard.mat -interp trilinear
    echo ""
 
    echo "Registered to Standard."
    echo "All Complete"
    echo ""


}

#################################################################
#############execusion
#################################################################

pushd ${fdir}

if [ ${delete} -eq 1 ]; then echo " "; echo "Interemediate functional files will be deleted."; echo " "; fi



aprep #T1 preperation
#acr="${acr}_RPI"


ssT1 #skull stripping T1
#acr="${acr}_brain"


funcprep 
echo ""
echo "${cr} was created"
echo ""

mccr

skstfunc

exfunc

reg

smoothfunc

seg

nui

censor

tostandard

popd #out from the sbjdir


# cd /data/OpenData/COBRE/coinsCobre/TCCRS/SbjDir


# chmod a+x  ~/db/AcademicW/Scripts/TIR/*.sh
#
#
# rsync -avpu ~/db/AcademicW/Scripts/TIR/TIR_Nui_01.sh dstar:/usr/local/bin/TIR/

# rsync -avpu ~/db/AcademicW/Scripts/TIR/TIR_10.sh dstar:/usr/local/bin/TIR/
# 
# rsync -av ~/db/AcademicW/Scripts/TIR/TIR_Reg_02.sh ~/mtpt/data/OpenData/COBRE/coinsCobre/TCCRS/SbjDir/
# rsync -av ~/db/AcademicW/Scripts/TIR/TIR_Seg_02.sh ~/mtpt/data/OpenData/COBRE/coinsCobre/TCCRS/SbjDir/

# cd /data/OpenData/COBRE/coinsCobre/TCCRS/SbjDir/test_*
# /usr/local/bin/TIR/TIR_11.sh func.nii.gz anat.nii.gz 2 4





