#!/bin/bash
#: Title      : Very simple simulation on the effect of PVE on radiowater PET
#: Date       : 2019-03-30
#: Author     : "Vesa Oikonen" <vesa.oikonen@utu.fi>
#: Options    : None


printf "\n=====================================================================\n"
printf "  Making or emptying result folder\n"
if [ ! -d sim1 ]; then
  mkdir sim1
else
  rm -f sim1/*
fi


printf "\n=====================================================================\n"
printf "  Compute BTAC at 0.5 s sampling times \n"
printf "=====================================================================\n"
fit2dat -c=0,480,0.5 blend_lvcav.fit sim1/blood.tac
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
tacren --silent sim1/blood.tac 1 blood_artery
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
tacstudy --force sim1/blood.tac sim1
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi

printf "\n=====================================================================\n"
printf "  Simulate GM and WM TTACs \n"
printf "=====================================================================\n"

b2t_h2o -nosub -fpt -voiname="GM_CBF=50" sim1/blood.tac 50 1.00 1.0 0 0 sim1/gm.tac
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
b2t_h2o -nosub -fpt -voiname="WM_CBF=20" sim1/blood.tac 20 1.00 1.0 0 0 sim1/wm.tac
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
tacstudy --force sim1/gm.tac sim1
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
tacstudy --force sim1/wm.tac sim1
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi

b2t_h2o -nosub -fpt -voiname="CBF=35" sim1/blood.tac 35 1.00 1.0 0 0 sim1/f35.tac
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi

taccalc sim1/gm.tac + sim1/wm.tac sim1/temp.tac
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
taccalc sim1/temp.tac x 0.5 sim1/mixed.tac
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
tacren sim1/mixed.tac 1 "Mixed_50-50"
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
tacstudy --force sim1/mixed.tac sim1
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
tacstudy --force sim1/f35.tac sim1
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi

tac2svg -mt="" -x2=300 -legend=y sim1/cbf_gm_wm_ttacs.svg -l sim1/gm.tac sim1/wm.tac
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
tac2svg -mt="" -x2=300 -legend=y sim1/cbf_pve_ttacs.svg -l sim1/mixed.tac sim1/f35.tac
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
tac2svg -mt="" -x2=300 -legend=y sim1/cbf_gm_wm_pve_ttacs.svg -l sim1/gm.tac sim1/f35.tac sim1/mixed.tac sim1/wm.tac sim1/blood.tac
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi

tacadd -ovr sim1/ttacs.tac sim1/gm.tac sim1/f35.tac sim1/mixed.tac sim1/wm.tac
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi


printf "\n=====================================================================\n"
printf "  Simulate frames \n"
printf "=====================================================================\n"

printf "3/17/2019 17:05:23 22 2 1 sim1 O-15\n" > sim1/frames.sif
printf "0 5\n" >> sim1/frames.sif
printf "5 10\n" >> sim1/frames.sif
printf "10 15\n" >> sim1/frames.sif
printf "15 20\n" >> sim1/frames.sif
printf "20 25\n" >> sim1/frames.sif
printf "25 30\n" >> sim1/frames.sif
printf "30 40\n" >> sim1/frames.sif
printf "40 50\n" >> sim1/frames.sif
printf "50 60\n" >> sim1/frames.sif
printf "60 80\n" >> sim1/frames.sif
printf "80 100\n" >> sim1/frames.sif
printf "100 120\n" >> sim1/frames.sif
printf "120 140\n" >> sim1/frames.sif
printf "140 160\n" >> sim1/frames.sif
printf "160 180\n" >> sim1/frames.sif
printf "180 210\n" >> sim1/frames.sif
printf "210 240\n" >> sim1/frames.sif
printf "240 270\n" >> sim1/frames.sif
printf "270 300\n" >> sim1/frames.sif
printf "300 330\n" >> sim1/frames.sif
printf "330 360\n" >> sim1/frames.sif
printf "360 420\n" >> sim1/frames.sif
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi

simframe sim1/ttacs.tac sim1/frames.sif sim1/ttacsf.tac
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi


printf "\n=====================================================================\n"
printf "  Calculate perfusion using CM fit \n"
printf "=====================================================================\n"

fit_h2o -fpt -Vb=0 -delay=0 -k2 -fit=sim1/cbf_sim1_fit_h2o.tac sim1/blood.tac sim1/ttacsf.tac 999 sim1/fit_h2o.par
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
tac2svg -mt="" -legend=y sim1/cbf_sim1_fit_h2o.svg -s sim1/ttacsf.tac -l sim1/cbf_sim1_fit_h2o.tac 
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi

printf "  Shorter fit time \n"

fit_h2o -fpt -Vb=0 -delay=0 -k2 -fit=sim1/cbf_sim1_fit_h2o_300.tac sim1/blood.tac sim1/ttacsf.tac 300 sim1/fit_h2o_300.par
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
tac2svg -mt="" -legend=y sim1/cbf_sim1_fit_h2o_300.svg -s sim1/ttacsf.tac -l sim1/cbf_sim1_fit_h2o_300.tac 
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi

fit_h2o -fpt -Vb=0 -delay=0 -k2 -fit=sim1/cbf_sim1_fit_h2o_180.tac sim1/blood.tac sim1/ttacsf.tac 180 sim1/fit_h2o_180.par
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
tac2svg -mt="" -legend=y sim1/cbf_sim1_fit_h2o_180.svg -s sim1/ttacsf.tac -l sim1/cbf_sim1_fit_h2o_180.tac 
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi

printf "  Weighting \n"

cp -f sim1/ttacsf.tac sim1/ttacsf_wf.tac
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
dftweigh -wf --silent sim1/ttacsf_wf.tac
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
fit_h2o -fpt -Vb=0 -delay=0 -k2 -svg=sim1/cbf_sim1_fit_h2o_wf.svg sim1/blood.tac sim1/ttacsf_wf.tac 999 sim1/fit_h2o_wf.par
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi


printf "\n=====================================================================\n"
printf "  Yokoi plot \n"
printf "=====================================================================\n"

yokoi -svg=sim1/cbf_sim1_yokoi.svg sim1/ttacsf.tac sim1/blood.tac 0.5 5 sim1/yokoi.par
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi


printf "\n=====================================================================\n"
printf "  Calculate perfusion ratio \n"
printf "=====================================================================\n"

cp -f sim1/ttacsf.tac sim1/ttacsf_nn.tac
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
tacren sim1/ttacsf_nn.tac 0 TAC#
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
perfrat sim1/ttacsf_nn.tac TAC1 sim1/perfrat.par
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi


printf "\n=====================================================================\n"
printf "  ARG method \n"
printf "=====================================================================\n"

dftinteg sim1/ttacsf.tac 0 420 sim1/integral.tac
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi

arlkup -nr=5000 sim1/blood.tac 1.00 60 0 420 sim1/arg1.lkup
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
taclkup sim1/integral.tac sim1/arg1.lkup sim1/arg1.tac
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi

arlkup -nr=5000 sim1/blood.tac 0.92 60 0 420 sim1/arg2.lkup
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi
taclkup sim1/integral.tac sim1/arg2.lkup sim1/arg2.tac
if [ $? -ne 0 ] ; then printf "Failed!\n" ; exit 1 ; fi




printf "\n===================================================================\n"
printf "   All done! \n"
printf "===================================================================\n"
exit 0

