#!/bin/bash -f
function usage {
echo "
   Plots the PV values of all the PVs matching the given PV name pattern,
   by ordinal Z position.
  
   Usage: plotz_devices [-hlt] [-b beamline] PV-name-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 beamline   If given, plotz_devices will get only the devices specified 
                 by the PV-name-pattern argument, that are also in the given
                 modelling beamline. Specifically, it will return all and 
                 only the devices that are returned by aidalist for the 
                 device name pattern given, that are additionally 
                 in one of the areas of the beamline given. It will also sort
                 the devices specifically by the areas given in the 
                 beamline. 

                 Presently 4 beamlines are recognized:
                    FullMachine or full 
	            CathodeTo135MeVSpect or 135
	            CathodeTo52SL2 or 52
	            CathodeToGunSpect or gun

                 If -b IS NOT GIVEN, then quickzort is used to sort the 
                 devices into a quazi beamline order specified by quickzort
                 (see quickzort).

                 Not giving -b, should be roughly equivalent to -b FullMachine.
    
   The PV-name-pattern argument can include wildcards, given as '%'. Presently,
   plotz_devices does NOT process regular expressions. The reason is 
   performance; the list is generated by aidalist from an Oracle pattern
   query, and oracle regular expression selects are REALLY slow (~20 sec).
   
   Examples:
       plotz_devices -b full pics:% iact Plot MPS PICS currents in 
                                         full machine beamline.
       plotz_devices -l -b 52 VGXX:% P   Log plot all the vacuum guage 
                                         pressures in the 
                                         CathodeTo52SL2 beamline.
       plotz_devices FBCK:% YPOS         Plot YPOS setting of all FBCKs.

"
}

# Process options
beamline=
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_devices -h"; exit 1;;
   esac
done
shift $((OPTIND-1))

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

export devPattern="$1"
export attrPattern="$2"
export PVPattern="${devPattern}:${attrPattern}"
export TFILE=`mktemp`
GNUPLOT_DIR=/usr/local/lcls/tools/script

if [ -z $beamline ]; then
    echo Aquiring z data for all devices matching pattern  ${PVPattern} ...
    valz_devices ${devPattern} ${attrPattern} > $TFILE
else
    echo Acquiring z data in ${beamline} matching pattern ${PVPattern} ...
    valz_devices -b ${beamline} ${devPattern} ${attrPattern} > $TFILE
fi

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

if [ $tabulate -eq 1 ]; then
    cat $TFILE &
fi

echo Plotting ...
gnuplot -geometry 800x562-0+0 -xrm 'gnuplot*line1Color:blue' $GNUPLOT_DIR/plotz_devices.gp
else
echo "No data found for $PVPattern"
fi

rm -f $TFILE

exit 0;
