#!/bin/sh
#
#       $HOME/.lcmodel/gelx/preprocessors/select-frames (15 November 2003)

#     Allows a restricted range of frames from the water-suppressed data in a 
# Probe raw P-file to be used, e.g., when some frames at the beginning or end 
# of an experiment are to be omitted because of patient motion

#     Prompts for the 1st and last frames of the range to be included.

#     You must figure out the frame numbers that you want to include.  A frame 
# consists of OPNEX (also called NEX) scans.  So, the number of frames is the 
# (total number of scans)/OPNEX.  OPNEX is listed in the corresponding GE .shf 
# file.  In the TITLE at the top of the One-Page Output, NS will equal the 
# (reduced) total number of scans in the selected frames.

#     If you want to use data up to last frame, then you can simply enter a 
# large number.  Examples: enter 
# 1 999  (to include all frames)
# 5 999  (to skip the first 4 frames)
# 1 11   (to skip frames 12 and later)
# 5 11   (to use only frames 5 through 11)
#
#                      Usage with LCMgui
#                      =================
#
#     NOTE: You must not run LCMgui in background; i.e., you must not start 
# it with an "&" at the end, but simply with a command like
# $HOME/.lcmodel/lcmgui

#     Normally you do not have to read anything further in this script.

#=============================================================================
#=============================================================================
#=============================================================================
#
#                   Start of This Script
#                   ====================

#
# Function for checking if a variable is non-numeric (adapted from B. Blinn)
#
IsNonNumeric() {
    expr "$1" + 1  >/dev/null  2>&1
    if [ $?  -ge  2 ]
    then
	return 0
    fi
    return 1
}

#
# Prompting function (adapted from B. Blinn)
#
Prompt() {
    if [ "`echo -n`"  =  "-n" ]
    then
	echo  "${@:-> }\c"  >/dev/tty
    else
	echo -n  "${@:-> }"  >/dev/tty
    fi
}

# Additional checks on error conditions should be added below.
# Get NFRAME_START & NFRAME_END
#
while :
do
    Prompt "
>>> After the colon at the end,
    enter the numbers of the first & last frames to be included,
    as two integers separated by a space, e.g.,
    2 5 (to include frames 2 through 5),
    and then press the Enter key on your keyboard: "
    exec  3</dev/tty
    read  NFRAME_START  NFRAME_END  REMAINDER  <&3

    if [ "$REMAINDER"  !=  "" ]
    then
	echo  "
*** Your entry ($NFRAME_START $NFRAME_END $REMAINDER) was not two integers.  Try again."  >/dev/tty
	continue
    fi

    if  IsNonNumeric  $NFRAME_START
    then
	echo  "
*** Your 1st entry ($NFRAME_START) was not an integer.  Try again."  >/dev/tty
	continue
    fi

    if  IsNonNumeric  $NFRAME_END
    then
	echo  "
*** Your 2nd entry ($NFRAME_END) was not an integer.  Try again."  >/dev/tty
	continue
    fi

   if [ $NFRAME_START -gt $NFRAME_END ]
   then
	echo  "
*** Your 1st entry ($NFRAME_START) cannot exceed your 2nd entry ($NFRAME_END).  Try again."  >/dev/tty
	continue
   fi

   break
done

if [ ! -r $2 ]
then
    echo "The following extraInfo file is not readable:
$2"
fi
SRCRAW=`awk '/^srcraw/{ print $2 }' $2`

if [ "$SRCRAW"  =  "" ]
then
    echo "Unable to find SRCRAW in the following extraInfo file:
$2"
fi

# TMP_DIR = absolute path  of parent directory (with "/" at end, e.g., 
#          /d1/d2/d3/), where temporary files output by LCMgui (and LCModel) 
#	   go.  ("met" is stripped off in second dirname command.)
#
TMP_DIR=`dirname $1`
TMP_DIR=`dirname $TMP_DIR`/

#
# Run bin2asc-frames
$HOME/.lcmodel/gelx/bin2raw  $SRCRAW  $TMP_DIR  met  $NFRAME_START  $NFRAME_END

#----------------------------------------------------------------------------
#
# You must always exit this script with "exit 0"
#
exit 0
