#!/bin/bash

#afni_rt
#This schell script will call realtime AFNI and Dimon.

if [ -z $1 ] || [ -z $2 ]; then
	echo "Usage: afni_rt TR(sec) NR(integer) Paradigm(1D file)"
	exit
fi

previousDir=`pwd`

#### Set variables ####
UserName="Xin"
WorkDir=/home/$UserName/rt_afni/
Name="realtime"
Basis="basis+orig"

if [ ! -d $WorkDir ]; then
	mkdir $WorkDir
fi
cd $WorkDir

if [ ! -f $Basis.BRIK ]; then
	echo "Can't find $Basis.BRIK"
	exit
fi

#### Remove previous existing realtime images ####
if ls -l $WorkDir/$Name*.* >/dev/null 2>&1; then rm $WorkDir/$Name*.*;  fi

afni -rt \
     -yesplugouts \
     -DAFNI_DONT_LOGFILE=YES \
     -DAFNI_REALTIME_Registration=3D:_realtime \
     -DAFNI_REALTIME_External_Dataset=$Basis \
     -DAFNI_REALTIME_Graph=Realtime \
     -DAFNI_REALTIME_MP_HOST_PORT=localhost:53214 \
     -DAFNI_REALTIME_SEND_VER=YES \
     -DAFNI_REALTIME_SHOW_TIMES=YES \
     -DAFNI_REALTIME_Mask_Vals=ROI_means \
     -DAFNI_REALTIME_Function=FIM \
     -DAFNI_REALTIME_YR=0.5 \
     -DAFNI_REALTIME_NR=$2 \
     -DAFNI_FIM_IDEAL=$3 &

Dimon -tr $1 -nt $2 \
      -rt -quit \
      -infile_pattern $Name*.BRIK \
      -file_type AFNI -sp alt+z \


#### Quit AFNI ####

cd $previousDir
