#!/bin/bash -f
function usage {
echo "
   Plots the PV values of all the PVs matching the given device name and 
   device attribute patterns, by S position.
  
   Usage: plotz [-hlt] [-b beamline] ... 
          <device-name-pattern> <device-attribute-pattern>

   Options:

   -h (optional) Prints help

   -l (optional) Plots a log10 plot. 

   -t (optional) Print a table of the device names and values, as well as plot.

   -b (optional) Sets the modelling beamline. If given, valz will use the
                 latest Design model of that beamline, to match the 
                 PVs given the two arguments PV-device-name-regex and 
                 PV-attribute-name-regex.

                 Presently 4 beamlines are recognized (case sensitive):
                    FullMachine or full - This is the default beamline.
	            CathodeTo135MeVSpect or 135
	            CathodeTo52SL2 or 52
	            CathodeToGunSpect or gun
 
   Both the device arguments may be given as a regular
   expression. Specifically, the regular expressions must be given in the
   POSIX syntax understood by awk (see man page for awk). Make sure to quote
   regular expressions, so they're not processed by the shell before being
   passed to plotz. 
   
   Examples:

          plotz XCOR BDES      - all XCORs and their BDES
          plotz 'QUAD:LI2[4-9]' EACT - EACT of QUADs in pattern, note quotes. 
          plotz '(XCOR|YCOR|QUAD):(IN20|LI2[1-5])' EACT
                               - EACTs from IN20 to LI25.
          plotz '((.cor)|quad):.*' eact - EACT of all correctors and quads.
          plotz BPMS X1H       - very poor man's orbit plot.
          plotz 'bpms:li2[1-9]' x1h  - linac bpms
          plotz -b 135 QUAD EACT - Plot only the QUAD EACTs in the 
                                    CathodeTo135MeVSpect beamline.
          plotz -t QUAD EACT    - Table and plot all QUADs last calced energy
"
}

# Process given options
beamline=FullMachine
tabulate=0
while getopts hltb: opt
do
   case "$opt" in
       h) usage; exit 0 ;;
       l) # log plot wanted. Construct the relevant gnuplot line.
          export yscale="set log y;";;          
       b) case "$OPTARG" in 
               FullMachine|full) beamline=FullMachine;;
	       CathodeTo135MeVSpect|135) beamline=CathodeTo135MeVSpect;;
	       CathodeTo52SL2|52) beamline=CathodeTo52SL2;;
	       CathodeToGunSpect|gun) beamline=CathodeToGunSpect;;
               *) echo "Beamline option b - unknown beamline given: '$OPTARG'."
                  echo "Check case and spelling"; exit 1;;
	   esac ;;
       t) tabulate=1;;
       *) echo "Unknown Arguments, see plotz -h"; exit 1;;
   esac
done
shift $((OPTIND-1))

# Check for insufficient arguments
if [ $# -lt 2 ]; then
   echo "Too few arguments, see plotz -h for help."
   exit 1;
fi

# Exported since they're used in gnuplot
export devPattern="$1"
export attrPattern="$2"
export TFILE=`mktemp`
GNUPLOT_DIR=/usr/local/lcls/tools/script

echo Acquiring z data in ${beamline} matching pattern ${devPattern} ${attrPattern} ...
if [ $tabulate -eq 1 ]; then
    valz -b ${beamline} ${devPattern} ${attrPattern} | tee $TFILE
else
    valz -b ${beamline} ${devPattern} ${attrPattern} > $TFILE
fi

if [ -s $TFILE ]; then
egu_field=`tail -1 $TFILE | cut -d" " -f1`.EGU
export units=`caget $egu_field | awk '{print $2}'`
export timestamp=`date`

echo Plotting ...
# Start gnuplot, sizing the default window size (640x450) up 25%
gnuplot -geometry 800x562 -xrm 'gnuplot*line1Color:blue' $GNUPLOT_DIR/plotz.gp
else
echo "No data found in $beamline for $devPattern $attrPattern"
fi
 
rm -f $TFILE



