#!/bin/bash

##
##
##  This is the Unix script to run the PET example for post-reconstruction kinetic fitting, which uses:
##  - The pre-reconstructed 3D images
##  - A dynamic model (Patlak)
##  Dataset is list-mode generated from analytical projection of Zubal digital phantom with attenuation and noise
##   Zubal, I.G., Harrell, C.R, Smith, E.O, Rattner, Z., Gindi, G. and Hoffer, P.B.,
##   Computerized three-dimensional segmented human anatomy, Medical Physics, 21(2):299-302, 1994.
##  Activity in voxels represent typical FDG time activity curves.
##  The dynamic protocol starts at 2100 seconds, to reconstruct 5 frames of 300s each
##
##  This script will run the example and generate:
##  - 2 parametric images: "example_pet_dmodel-rmotion_patlak_post-reco-kinetic-fitting_par*" corresponding to Patlak slope and intercept
##                         and estimated from the example_pet_dmodel-rmotion_reco3D_it2 frames
##
##

###########################
# Some checks
###########################

# Set the CASToR dynamic image processing tools
tool="castor-imageDynamicTools"
# Test the existency of the CASToR reconstruction program in the PATH
type ${recon} > /dev/null
if [ $? != 0 ]
then
  echo "***** In order to run the benchmark script, please add the CASToR binary folder into your PATH !"
  exit 1
fi

###########################
# Command-line options
###########################

# To get help about the command-line options, run the program without argument or with '-h',
# '-help' or '--help' options. Then specific help about the different parts of the project
# can be prompted using specific help options.

# General verbose level (the verbose level relative to each specific part of the CASToR project
# can be customize separately if desired using options '-vb-XXXX'; to get specific help about
# these options, run the main program with the option '-help-misc')
verbose="-vb 2"

# The data file header (here, a pet list-mode data file). You can have a look at this ascii
# header to see the different fields used by the reconstruction program.
input="-i example_pet_dmodel-rmotion_reco3D_it2.mhd"

# The output file base name. All files saved on the disk by the program will use this base name
# with an appended suffix and an extension. As an alternative, you can use the option '-dout'
# instead of '-fout'; this will create a folder of the provided name, and all files will be
# saved inside this folder. Note that the CASToR programs create a '.log' file which logs
# everything that is prompted on the screen.
output="-fout example_pet_dmodel-rmotion_patlak_post-reco-kinetic-fitting"

# Number of iterations (2) and subsets (28). Using this option, you can also define sequences
# of iterations with different number of subsets. For example '-it 2:28,1:14,10:1' will perform
# 2 iterations using 28 subsets, followed by 1 iteration using 14 subsets and followed by 10
# iterations using 1 subset.
iteration=""
#iteration="-it 50"

# Dynamic model to be applied inside reconstruction between the frames. 
# The model type must be entered followed by its parameters 
# (provided either with set of parameters, or a configuration file containing these parameters) 
# Here the Patlak model is used with the nested optimization method (see Patlak help for more info)
dmodel="-dynamic-model Patlak:dmodel_patlak_nested.cf"

# Parallel computation using the OpenMP library. If CASToR was not compiled using OpenMP, a warning will
# be displayed if this option is used, specifying that only one thread will be used. Here we specify '0'
# in order to let the computer choose the number of threads with respect to the available ressources.
# One can also manually specify the number of threads to be used. To get details about the computation
# settings, run the program with the '-help-comp' option.
thread="-th 0"


###########################
# Launch the reconstruction
###########################

# Launch the reconstruction
echo "=============================================================================================="
echo "Reconstruction is going on. Should take from one to several minutes depending on the hardware."
echo "=============================================================================================="
${tool} ${verbose} ${input} ${output} ${iteration} ${dmodel} ${thread}

# Finished
echo ""
exit 0

