#!/bin/sh
#
#       $HOME/.lcmodel/execution-scripts/nice    (11 April 2013)
#
#     This script uses "nice" to prevent overloading your workstation with 
# simultaneous analyses.  A more effective way is to use the "make-batch"
# execution script, which, however, cannot skip over bad CSI voxels.
#----------------------------------------------------------------------------

# MAX_BAD = maximum number of bad CSI voxels (voxels that cause an LCModel 
#           abort) that can be skipped over.  You can change this value (with 
#           no white space between "=" and the number).
MAX_BAD=16
# --------------------------------------------------------------------------

#      LCMgui executes this script in background with 3 or 4 arguments.  
# They are $1--$4:
#
# $1: absolute path of temporary directory produced by LCMgui.
#
# $2: For the N'th run started from an LCMgui session, 
#     $2 = N*5 + <your default "nice" value> (but with $2 <= 19).
#     This provides a crude way to assign successive runs lower priorities, 
#     as illustrated below.
#
# $3: absolute path of the Output Script that will print out your results 
#     and save files.  This Output Script is automatically produced by LCMgui.
#
# $4: absolute path of the directory where your results will be saved, if you 
#     elected in the LCMgui "Save Files" menu to have files saved.
#     If you did not elect to have files saved, then $4 is missing.
#----------------------------------------------------------------------------

#             Outer loop for CSI (and also single-voxel)
#             ==========================================

rm  -f  $1/filcsi_sav_1
rm  -f  $1/filcsi_sav_2;  touch  $1/filcsi_sav_2

echo "    0    0    0
"  > $1/filcsi_sav_1

rm  -f  $1/work
sed  "/^ \$END/i\
 lcsi_sav_1 = 12,  filcsi_sav_1 = '$1/filcsi_sav_1',\
 lcsi_sav_2 = 13,  filcsi_sav_2 = '$1/filcsi_sav_2'"  $1/control > $1/work
mv  -f  $1/work  $1/control

J=0
while [ $J  -le  $MAX_BAD ]  # (Safer than infinite loop.)
do
    J=`expr $J + 1`

#  Execute LCModel
#  ---------------
#      The command below executes LCModel.  DO NOT change "<$1/control" below.
#
#      The first command below, "nice...", should work in nearly all 
# cases.  If not, try one of the other "nice" commands below it.
#
nice -$2  $HOME/.lcmodel/bin/lcmodel  <$1/control  2>$1/runtime-messages
#/usr/bin/nice -$2  $HOME/.lcmodel/bin/lcmodel  <$1/control  2>$1/runtime-messages
#/bin/nice -$2  $HOME/.lcmodel/bin/lcmodel  <$1/control  2>$1/runtime-messages
#nice +$2  $HOME/.lcmodel/bin/lcmodel  <$1/control  2>$1/runtime-messages
#nice $2  $HOME/.lcmodel/bin/lcmodel  <$1/control  2>$1/runtime-messages
#nice -$2  $HOME/.lcmodel/bin/lcmodel  <$1/control  2>$1/runtime-messages
#
# Display (relevant) error messages
sed '/^Skipping namelist/d'  $1/runtime-messages
# End of 1 LCModel analysis
# ---------------------------

    exec 3<&0 <$1/filcsi_sav_1
    read  dummy  nanalyses_done  dummy2
    exec 0<&3 3<&-
#   nanalyses_done = 1 means the abort was with the very first voxel analyzed, 
#                      indicating that you input bad Control Parameters.
#                  = 0 when LCModel has covered all voxels.
    if [ $nanalyses_done -le 1 ]; then break; fi
done
echo "
    *** LCModel finished ***"

#  End of outer loop for CSI
#  =========================

#----------------------------------------------------------------------------
#
#			Optional Postprocessing
#			=======================
# 
#     You could put in a "Postprocessor" here (e.g., for relaxation 
# corrections or the construction of other files for your own data base or 
# statistics).
#     You have the following files at your disposal:
# $1/met/RAW : LCModel RAW file of the (water-suppressed) data.
# $1/h2o/RAW : LCModel RAW file of the unsuppressed water reference (only if
#              you chose to do the eddy-current correction or if you have 
#              Picker data or GE P-files).
# $1/ps : LCModel PS file with the (PostScript) One-Page Output from LCModel.
# $1/control: LCModel CONTROL file
#     In addition, if you elected to produce or save the following:
# $1/table : LCModel TABLE file
# $1/coord : LCModel COORD file
# $1/print : LCModel PRINT file (the large ASCII dump of detailed output, not 
#            PostScript).
#     A natural directory to write the extra results from your Postprocessor 
# would be $4, but you must then be sure that you have have elected to save 
# something with the LCMgui "Save Files" menu, so that $4 is passed as an 
# argument.
#    If you only modify the above LCModel files with your Postprocessor, then 
# the Output Script (with path $3) executed below will automatically save 
# these modified files, if you elected to save them with the LCMgui "Save 
# Files" menu.
#-----------------------------------------------------------------------------
#
#			Output the Results
#			==================
#
#      The following "$3" outputs and saves what you specified in LCMgui.  
#  So you should leave this unchanged below.  You might want to add more 
#  output commands below.
#
$3
#-----------------------------------------------------------------------------
#
#			Signal Completion
#			=================
#
#      The following signals LCMgui that LCModel is done, and that the 
# temporary files can be deleted.  So, DO NOT change the following:
#
mkdir $1/done
