#!/bin/bash



. ${PWD}/script/param.txt

anat_base=$(basename $anat)
anat_noext="$(remove_ext $anat_base)"
anat_N4=$root_dir'anat/'$anat_noext'_N4.nii.gz'
anat_mask=$root_dir'anat/'$anat_noext'_brain_mask.nii.gz'


for i in "${func[@]}"
do

func_base=$(basename $i)

func_noext="$(remove_ext $func_base)"
func_noext_full="$(remove_ext $i)"

mkdir -p func/$func_noext
cd func/$func_noext
#fslorient -copysform2qform filtered_func_data_clean.nii.gz 
#WarpTimeSeriesImageMultiTransform 4 filtered_func_data.nii.gz filtered_func_data_reg.nii.gz -R $template ${anat2temp} reg/func2anat0Warp.nii.gz reg/func2anat0GenericAffine.mat

#fslmeants -i filtered_func_data_reg.nii.gz -m $template_mask -o '/project/4180000.18/optoDR/export_PG/ts/'$func_noext --label=/groupshare/traaffneu/preclinimg/templates/my_templates/atlas/ABI_atlas_reduced_TIDO_200um.nii.gz
cd glm

fsl_glm -i ../filtered_func_data -d design.txt -c contrast.txt -o func_beta -m ../mask_dil --demean --out_cope=func_COPE --out_z=func_zstat --out_res=func_res --vxt=../mc/motion.1D

cd ..


WarpTimeSeriesImageMultiTransform 4 glm/func_COPE.nii.gz  ${func_noext}'_COPE_reg.nii.gz' -R $template ${anat2temp} reg/func2anat0Warp.nii.gz reg/func2anat0GenericAffine.mat
WarpTimeSeriesImageMultiTransform 4 glm/func_zstat.nii.gz  ${func_noext}'_zstat_reg.nii.gz' -R $template ${anat2temp} reg/func2anat0Warp.nii.gz reg/func2anat0GenericAffine.mat

#antsApplyTransforms -i glm/func_COPE.nii.gz -r ${template} -t ${anat2temp} -t reg/func2anat0Warp.nii.gz -t reg/func2anat0GenericAffine.mat -o ${func_noext}'_COPE_reg.nii.gz' 

#antsApplyTransforms -i glm/func_zstat.nii.gz -r ${template} -t ${anat2temp} -t reg/func2anat0Warp.nii.gz -t reg/func2anat0GenericAffine.mat -o ${func_noext}'_zstat_reg.nii.gz'




cd ../..
done


