#!/bin/bash -f
function usage {
echo "
   valz_devices prints a table of the PV names and extant PV 
   values, of the PVs matching the given PV name pattern, sorted by Z.

   Usage: 
         valz_devices [-h] [-b {FullMachine|CathodeTo135MeVSpect|...
                                CathodeTo52SL2|CathodeToGunSpect}] ...
                      [-s start-time] [-e end-time] [-x] ... 
                      PV-pattern 
  
   -h (optional) 
                 Prints help

   -b beamline (optional) 
                 If given, valz_devices will get only the devices specified 
                 by the PV-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
	
   -s (optional) Archive data start time. 
                 -s Instructs to get the archived history values of the devices
                 matching PV-device and PV-attribute, rather than the 
                 present values. The output will then be 3 columns, not 2.
                 -s takes an argument, the starting time from which the 
                 archive data will be retrieved. If -s is given, a 
                 valid start time must follow. See below.
   -e (optional) Archive data end time. 
                 -e Instructs to get the archived history values of the devices
                 matching PV-device and PV-attribute, rather than the 
                 present values. The output will then be 3 columns, not 2.
                 -e takes an argument, the ending time from which the 
                 archive data will be retrieved. If -e is given, a 
                 valid end time must follow it. See below. 
 
  -x (optional) Force use of scientific notation in output value formatting.
                6 digits of decimal precision (mantissa) are used.
 
   Valid times for -s and -e:

       Times must be in the syntax understood by the date command, ie one of
       syntaxes in the examples below (see man date):
 
      '10 minutes ago'
      '1 hour ago'
      '8 hours ago'
      '1 day ago'
      '1 week ago'
      '1 month ago'
      '1 year ago'
      'now'                    - System time 
      '01/13/2011 12:15:00'
      '13-Jan-2011 12:24:00'
      '4:30'                   - numerals are 24 hour clock
      '4:40pm'                 - 4:30 in the afernoon 


   If -b is given only the devices in the areas in the given model are 
   included. If -b is not given, the devices are sorted by quickzort.
   
   The device name argument can include wildcards, given as '%'. Presently,
   valz_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:

      vals_devices VGCC:% P                   - All Vacuum pressures, by 
                                                by nominal z.
      valz_devices -b FullMachine VGCC:% P    - Vacuum pressures of all
                                                such devices specifically in
                                                in the full machine model.
      valz_devices -b 52 VGCC:% P             - Vacumm pressures in the 
                                                52 line.
      
      valz_devices -b gun -s '5 minutes ago' -e '2 minutes ago' VGXX:% P
                                              - 3 minutes of archive history
                                                of all VGXX P values in the 
                                                gun beamline.
      valz_devices -x -s '5 minutes ago' VGXX:% P 
                                              - Force use of exp notation.

   Author: Greg White, 25-Jan-2011, SLAC. 

"
}

beamline=
starttime='1 hour ago'
endtime='now'
scidecimals=0
gethist=0
while getopts b:s:e:hx opt; do
   case "$opt" in
       h) usage; exit 0 ;;
       b) 
	   # beamline takes an argument - the name of the beamline
	   case "$2" in 
               FullMachine|full) beamline=FullMachine;;
	       CathodeTo135MeVSpect|135) beamline=CathodeTo135MeVSpect;;
	       CathodeTo52SL2|52) beamline=CathodeTo52SL2;;
	       CathodeToGunSpect|gun) beamline=CathodeToGunSpect;;
               *) echo "Beamline option b - unknown beamline given: '$2'."\                            " Check case and spelling"; exit 1;;
	   esac ;;
       s) starttime="${OPTARG}"; gethist=1;;
       e) endtime="${OPTARG}"; gethist=1;;
       x) scidecimals=1;;
       *) echo "Unknown Arguments, see valz_devices -h"; exit 1;;
   esac
done
shift $((OPTIND-1))

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

PVpattern_t=$1:$2
PVpattern=`(echo $PVpattern_t|tr '[[:lower:]]' '[[:upper:]]')`

format=
if [ $scidecimals -eq 1 ]; then
   if [ $gethist == 0 ]; then
      format="-e 6";
   else
      format="-x";
   fi
fi


if [ $gethist == 0 ]; then
   if [ -z $beamline ]; then
      aidalist "$PVpattern" | quickzort | xargs caget $format
   else
      aidalist "$PVpattern" | zort -b ${beamline} | xargs caget $format
   fi
else
   if [ -z $beamline ]; then
      aidalist "$PVpattern" | quickzort | xargs -Idevice \
         aidagethist -n $format -s "$starttime" -e "$endtime" device
   else
      aidalist "$PVpattern" | zort -b ${beamline} | xargs -Idevice \
         aidagethist -n $format -s "$starttime" -e "$endtime" device
   fi
fi

exit 0;
