---- bin/fsl_sub-dist    2016-01-19 14:40:58.000000000 +0000
-+++ bin/fsl_sub 2016-01-19 16:55:24.000000000 +0000
+--- bedpostx.bak	2018-06-06 15:00:57.000000000 +0100
++++ bedpostx	2018-06-06 15:55:45.000000000 +0100
+@@ -10,7 +10,8 @@
+ #   Imaging of the Brain), Department of Clinical Neurology, Oxford
+ #   University, Oxford, UK
+ #   
+-#   
++#   Modified for use on UCL Legion OS upgrade - January 2015 by BAA  
++#     
+ #   LICENCE
+ #   
+ #   FMRIB Software Library, Release 5.0 (c) 2012, The University of
+@@ -65,6 +66,8 @@
+ #   Innovation@innovation.ox.ac.uk quoting reference DE/9564.
+ export LC_ALL=C
+ 
++# Added support for user specified runtime for Legion jobs.
++
+ Usage() {
+     echo ""
+     echo "Usage: bedpostx <subject directory> [options]"
+@@ -78,6 +81,7 @@
+     echo "-b (burnin period, default 1000)"
+     echo "-j (number of jumps, default 1250)"
+     echo "-s (sample every, default 25)"
++    echo "-T (wallclock time for each job in minutes, default 15)"
+     echo "-model (Deconvolution model. 1: with sticks, 2: with sticks with a range of diffusivities (default), 3: with zeppelins)"
+     echo "-g (consider gradient nonlinearities, default off)"
+     #echo "-c do not use CUDA capable hardware/queue (if found)"
+@@ -169,6 +173,7 @@
+ model=2
+ gflag=0
+ nocuda=0
++wallclock=15	# Added for Legion
+ other=""
+ 
+ shift
+@@ -180,6 +185,7 @@
+       -b) burnin=$2;shift;;
+       -j) njumps=$2;shift;;
+       -s) sampleevery=$2;shift;;
++      -T) wallclock=$2; shift;;
+       -model) model=$2;shift;;
+       -g) gflag=1;;
+       -c) nocuda=1;;
+@@ -190,19 +196,22 @@
+ opts="--nf=$nfibres --fudge=$fudge --bi=$burnin --nj=$njumps --se=$sampleevery --model=$model"
+ defopts="--cnonlinear"
+ opts="$opts $defopts $other"
+-if [ -n "$SGE_ROOT" ]; then
+-	# Look for an SGE queue
+-	if [ -n "$FSLGECUDAQ" -a "$nocuda" -eq 0 ]; then
+-		# Test that the queue exists
+-		qconf -sq $FSLGECUDAQ 2>&1 >/dev/null
+-		if [ $? -eq 0 ]; then
+-			# CUDA queue exists
+-			if [ -x $0_gpu ]; then
+-				exec $0_gpu $myopts
+-			fi
+-		fi
+-	fi
+-fi
++
++# Commented out on legion - no user queues
++#
++#if [ -n "$SGE_ROOT" ]; then
++#	# Look for an SGE queue
++#	if [ -n "$FSLGECUDAQ" -a "$nocuda" -eq 0 ]; then
++#		# Test that the queue exists
++#		qconf -sq $FSLGECUDAQ 2>&1 >/dev/null
++#		if [ $? -eq 0 ]; then
++#			# CUDA queue exists
++#			if [ -x $0_gpu ]; then
++#				exec $0_gpu $myopts
++#			fi
++#		fi
++#	fi
++#fi
+ 
+ echo subjectdir is $subjdir
+ 
+@@ -262,9 +271,9 @@
+ mkdir -p ${subjdir}.bedpostX/logs/monitor
+ mkdir -p ${subjdir}.bedpostX/xfms
+ 
+-mailto=`whoami`@fmrib.ox.ac.uk
++mailto=`whoami`@ucl.ac.uk	# updated for UCL
+ echo Queuing preprocessing stages
+-preprocid=`${FSLDIR}/bin/fsl_sub -T 60 -m as -N bpx_preproc -l ${subjdir}.bedpostX/logs ${FSLDIR}/bin/bedpostx_preproc.sh ${subjdir} ${gflag}`
++preprocid=`${FSLDIR}/bin/fsl_sub -T ${wallclock} -m as -N bpx_preproc -l ${subjdir}.bedpostX/logs ${FSLDIR}/bin/bedpostx_preproc.sh ${subjdir} ${gflag}`
+ 
+ echo Queuing parallel processing stage
+ nslices=`${FSLDIR}/bin/fslval ${subjdir}/data dim3`
+@@ -293,10 +302,10 @@
+     fi
+     slice=$(($slice + 1))
+ done
+-bedpostid=`${FSLDIR}/bin/fsl_sub -j $preprocid -l ${subjdir}.bedpostX/logs -M $mailto -N bedpostx -t ${subjdir}.bedpostX/commands.txt`
++bedpostid=`${FSLDIR}/bin/fsl_sub -j $preprocid -T ${wallclock} -l ${subjdir}.bedpostX/logs -M $mailto -N bedpostx -t ${subjdir}.bedpostX/commands.txt`
+ 
+ echo Queuing post processing stage
+-mergeid=`${FSLDIR}/bin/fsl_sub -j $bedpostid -T 60 -m as -N bpx_postproc -l ${subjdir}.bedpostX/logs ${FSLDIR}/bin/bedpostx_postproc.sh $subjdir`
++mergeid=`${FSLDIR}/bin/fsl_sub -j $bedpostid -T ${wallclock} -m as -N bpx_postproc -l ${subjdir}.bedpostX/logs ${FSLDIR}/bin/bedpostx_postproc.sh $subjdir`
+ 
+ echo $mergeid > ${subjdir}.bedpostX/logs/postproc_ID
+ 
+--- fsl_sub.bak	2018-06-06 15:00:57.000000000 +0100
++++ fsl_sub	2018-06-06 15:48:41.000000000 +0100
 @@ -11,6 +11,8 @@
  #   Imaging of the Brain), Department of Clinical Neurology, Oxford
  #   University, Oxford, UK
- #
+ #   
 +#   Modified for use on UCL Legion OS upgrade - January 2015 by BAA  
 +#   
- #
+ #   
  #   LICENCE
- #
+ #   
 @@ -125,12 +127,27 @@
      else
-        queue=verylong.q
+ 	queue=verylong.q
      fi
 -    queueCmd=" -q $queue " 
 +
 +    # No user queues on Legion
 +    queueCmd="" 
-
+ 
      #echo "Estimated time was $1 mins: queue name is $queue"
  }
-
+ 
  ###########################################################################
 +# 
 +#   Convert FSL runtimne in minutes to h_rt=hrs:mins:secs for Legion

+                        smp for Legion.
                         <threads> specifies the number of threads to run
   -v                    Verbose mode.

-Queues:
-
-There are several batch queues configured on the cluster, each with defined CPU

       queue=$2
+      echo "Queue must not be specified on Legion"
+      exit 127
       queueCmd=" -q $queue " 
       $QCONF -sq $queue >/dev/null 2>&1
       if [ $? -eq 1 ]; then
@@ -328,7 +332,7 @@
       JobName=$2;
       shift;;
     -R)
-      RAM="-l mem_free=${2}M"
+      RAM="-l mem=${2}M"
       shift;;
     -m)
       MailOpts=$2;
@@ -355,6 +359,7 @@
     -s)
       pe_string=$2;
       peName=`echo $pe_string | cut -d',' -f 1`
+      peName='smp'	# must be smp on legion
       peThreads=`echo $pe_string | cut -d',' -f 2`
       shift;;
   esac
@@ -451,7 +456,7 @@
 
 	if [ "x$tasks" = "x" ] ; then
 	    if [ $scriptmode -ne 1 ] ; then
-		sge_command="qsub -V -cwd -shell n -b y -r y $queueCmd $pe_options -M $mailto -N $JobName -m $MailOpts $LogOpts $sge_arch $RAM $sge_hold"
+		sge_command="qsub -V -cwd -shell n -b y -r y $queueCmd -l h_rt=$h_rt $pe_options -M $mailto -N $JobName -m $MailOpts $LogOpts $sge_arch $RAM $sge_hold"
 	    else
 		sge_command="qsub $LogOpts $sge_arch $sge_hold"
 	    fi
@@ -461,7 +466,7 @@
 	    fi
 	    exec $sge_command $@ | awk '{print $3}'
 	else
-	    sge_command="qsub -V -cwd $queueCmd $pe_options -M $mailto -N $JobName -m $MailOpts $LogOpts $sge_arch $RAM $sge_hold $sge_tasks"
+	    sge_command="qsub -V -cwd $queueCmd $pe_options -M $mailto -N $JobName -l h_rt=$h_rt -m $MailOpts $LogOpts $sge_arch $RAM $sge_hold $sge_tasks"
 	    if [ $verbose -eq 1 ] ; then 
 		echo sge_command: $sge_command >&2
 		echo control file: $taskfile >&2
