#!/bin/bash -e

#######################################################################
#
# Proc script for particular kind of DTI data:
# + DTI: single phase encode (only diff prep)
# + run script under directory containing 'data_basic'; will create
#   'data_proc' in parallel to that, if it doesn't exist already
#
#######################################################################

# THIS ARRAY NEEDS TO BE POPULATED WITH ALL SUBJ IDS
all_subj=( )

here=${PWD}

# define two reference volumes for axializing, below
mni_axlz_ref=${here}/mni_icbm152_t2_relx_tal_nlin_sym_09a_ACPCE.nii
mni_axlz_wt=${here}/mni_icbm152_t2_relx_tal_nlin_sym_09a_ACPCE_wtell.nii

cd data_basic/

# get name of each subj dir in the basic tree
for subj in "${all_subj[@]}"
do

    echo "\n\n++ Processing subject:  ${subj} \n\n"

    # ---------- which subj, dir setup --------------
         
    # make subject output dir, if necessary
    \mkdir -p ../data_proc/${subj}/
 
    # ---------- convert single set of dwi dicoms -----------

    # all dwi dicoms just in a single dir
    echo "\n\n++ Process DWI dicoms \n\n"
    fat_proc_convert_dcm_dwis                        \
        -indir  ${subj}/preOp/dwi/                   \
        -prefix  ../data_proc/${subj}/dwi_00/DTI     \
        -do_movie AGIF

    # ---------- convert preOp t1 anat dicoms -----------

    echo "\n\n++ Process preOp T1w!\n\n"
    fat_proc_convert_dcm_anat                        \
        -indir ${subj}/preOp/t1w/                    \
        -prefix ../data_proc/${subj}/anat_00/t1w_pre

    # ---------- convert preOp t2 anat dicoms -----------

    echo "\n\n++ Process preOp T2w!\n\n"
    fat_proc_convert_dcm_anat                        \
        -indir ${subj}/preOp/t2w/                    \
        -prefix ../data_proc/${subj}/anat_00/t2w_pre

    # ---------- convert postOp t1 anat dicoms -----------

    echo "\n\n++ Process postOp T1w!\n\n"
    fat_proc_convert_dcm_anat                        \
        -indir ${subj}/postOp/t1w/                   \
        -prefix ../data_proc/${subj}/anat_00/t1w_post

    # ---------- convert preOp CT anat dicoms -----------

    echo "\n\n++ Process preOp CT!\n\n"
    fat_proc_convert_dcm_anat                        \
        -indir ${subj}/preOp/ct/                     \
        -prefix ../data_proc/${subj}/anat_00/ct_pre

    # ---------- convert postOp CT anat dicoms -----------

    echo "\n\n++ Process postOp CT!\n\n"
    fat_proc_convert_dcm_anat                        \
        -indir ${subj}/postOp/ct/                    \
        -prefix ../data_proc/${subj}/anat_00/ct_post

    # ---------- axialize t1w -----------

    # alignment programs are parallelized in OpenMP
    export OMP_NUM_THREADS=64

    # want to have all even slices at end, and with iso res 1x1x1 mm^3
    echo "\n\n++ Axialize T2w_pre!\n\n"
    fat_proc_axialize_anat                                   \
        -inset  ../data_proc/${subj}/anat_00/t2w_pre.nii.gz  \
        -prefix ../data_proc/${subj}/anat_01/t2w             \
        -mode_t2w                                            \
        -refset ${mni_axlz_ref}                              \
        -extra_al_wtmask ${mni_axlz_wt}                      \
        -out_match_ref

    # ---------- Pick bad volumes-----------
    
    # fat_proc_select_vols                                        \
    #     -in_dwi ../data_proc/${subj}/dwi_00/DTI.nii.gz          \
    #     -in_img ../data_proc/${subj}/dwi_00/DTI_sepscl.sag.png  \
    #     -prefix ../data_proc/${subj}/dwi_01/dwi_sel_DTI
    
    # ---------- Edit dset volumes -----------
    
    # set selstr = `cat ../data_proc/${subj}/dwi_01/dwi_sel_DTI_goods.txt`
    # fat_proc_filter_dwis                                         \
    #     -in_dwi ../data_proc/${subj}/dwi_00/DTI.nii.gz           \
    #     -in_col_matT ../data_proc/${subj}/dwi_00/DTI_matT.dat    \
    #     -select "${selstr}"                                      \
    #     -prefix ../data_proc/${subj}/dwi_02/DTI

    # uncompress the anatomical
    gunzip ../data_proc/${subj}/anat_01/t2w.nii.gz

    # for DIFFPREP command line, need row-vec and row-bval format
    1dDW_Grad_o_Mat++                                               \
        -in_col_matT      ../data_proc/${subj}/dwi_00/DTI_matT.dat  \
        -unit_mag_out                                               \
        -out_row_vec      ../data_proc/${subj}/dwi_01/DTI_rvec.dat  \
        -out_row_bval_sep ../data_proc/${subj}/dwi_01/DTI_bval.dat

    3dcopy                                                        \
        ../data_proc/${subj}/dwi_00/DTI.nii.gz                    \
        ../data_proc/${subj}/dwi_01/DTI.nii

    # ---------- run diffprep (from TORTOISE) -----------
    # NB: option names do sometimes change across versions

    DIFFPREP                                                      \
        --dwi         ../data_proc/${subj}/dwi_01/DTI.nii         \
        --bvecs       ../data_proc/${subj}/dwi_01/DTI_rvec.dat    \
        --bvals       ../data_proc/${subj}/dwi_01/DTI_bval.dat    \
        --structural  ../data_proc/${subj}/anat_01/t2w.nii        \
        --phase              vertical                             \
        --will_be_drbuddied  0                                    \
        --correction_mode quadratic                               \
        --gibbs_ringing_correction on                             \
        --denoising for_final                                     \
        --is_human_brain 1                                        \
        --do_QC 1                                                 \
        --high_b 0                                                \
        --res 1.5 1.5 1.5                                         \
        --keep_intermediate 1

    # ------- convert tort'ed DWIs to DTI stuff ------------

    # do autoflip check: not ideal to need this...
    @GradFlipTest \
        -in_dwi       ../data_proc/${subj}/dwi_01/DTI_DMC.nii            \
        -in_col_matT  ../data_proc/${subj}/dwi_01/DTI_DMC.bmtxt          \
        -prefix       ../data_proc/${subj}/dwi_01/GradFlipTest_rec.txt

    set my_flip = `cat ../data_proc/${subj}/dwi_02/GradFlipTest_rec.txt`

    # tensor estimates, with any flip auto chosen
    fat_proc_dwi_to_dt                                                   \
        -in_dwi       ../data_proc/${subj}/dwi_01/DTI_DMC.nii            \
        -in_col_matT  ../data_proc/${subj}/dwi_01/DTI_DMC.bmtxt          \
        -in_struc_res ../data_proc/${subj}/dwi_01/DTI_DMC_structural.nii \
        -in_ref_orig  ../data_proc/${subj}/anat_01/t2w.nii               \
        -prefix       ../data_proc/${subj}/dwi_02/dwi                    \
 	     -mask_from_struc                                                 \
        ${my_flip}

    # DEC map for investigating DT
    fat_proc_decmap                                                      \
        -in_fa       ../data_proc/${subj}/dwi_02/dt_FA.nii.gz            \
        -in_v1       ../data_proc/${subj}/dwi_02/dt_V1.nii.gz            \
        -mask        ../data_proc/${subj}/dwi_02/dwi_mask.nii.gz         \
        -prefix      ../data_proc/${subj}/dwi_02/DEC
done
