#!/bin/bash

function usage {
echo "
   aidagethist prints the history of a given AIDA entity on stdout, from
   the start time given, to the end time given.

   Usage:
         aidagethist [-s start-time] [-e end-time] [-n] [-x] entity 

   Note that for EPICS PVs, the AIDA entity is exactly the 
   same the PV name (that is, a device-name:attribute-name) eg 
   QUAD:LI24:501:BDES. 

   -s starttime
      The default start time is 1 hour ago. See valid syntax below

   -e endtime The default end time is now.  
      If only the end time is given, and it's more than 1 hour ago, 
      then obviously no data will be returned.

   -n Print the name of the device (preceeded by #) prior to printing
      the data. This is so aidagethist can be called repeatedly
      to produce data for many devices and analysis.

   -x Print values in scientific notation. Normally aidagethist prints 
      6 decimal digits of precision (enough for single precision controls).
      Use -x when 6 decimal digits is not enough. 

   Examples:

      Get the last 1 hour of a PV's value: 
         aidagethist IOC:IN20:MC01:LCLSBEAMRATE 

      Get the last 8 hours of a PV's value:
         aidagethist -s '8 hours ago' IOC:IN20:MC01:LCLSBEAMRATE 

      Get 2 hours of a PV's value, from 3 hours ago to 1 hour ago:
         aidagethist -s '3 hours ago' \\
                 -e '1 hour ago' IOC:IN20:MC01:LCLSBEAMRATE 

      Get a PV's value between the given exact times (2 examples):
         aidagethist -s '01/13/2011 12:15:00' -e '01/13/2011 12:24:00' \\ 
             IOC:IN20:MC01:LCLSBEAMRATE 
         aidagethist -s '13-Jan-2011 12:15:00' -e '13-Jan-2011 12:24:00' \\
             IOC:IN20:MC01:LCLSBEAMRATE 

      Get archive of a combined vacuum prssure (requiring high precision),
      and print in scientific notation using -x: 
       aidagethist -x -s '5 minutes ago' VGXX:IN20:469:P

      For backward compatibility, the old syntax of aidagethist is supported.
         aidagethist '8 hours ago' IOC:IN20:MC01:LCLSBEAMRATE
         
 
   The AIDA instance must have the attribute 'HIST.lcls' (but this is not
   included in the argument). 
 
   Times must be in the syntax understood by the date command, ie one of
   syntaxes in the examples below (see man date):
 
      '1 hour ago'
      '8 hours ago'
      '1 day ago'
      '1 week ago'
      '1 month ago'
      '1 year ago'
      'now'
      '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 

   As is usual with EPICS Archiver acquisitions, if there was no specific 
   data point recorded in the time period given, the chronologically last data
   point immediately prior to the start time is returned.

   Error Conditions:
      - If the given PV name is not valid, no history data is printed. No
        other error message is emitted. 
      - If a valid PV name is given, but archive data is emitted, check that 
        the start and end times define a proper period. 
      Check cmlogviewer for related messages from AIDA and the archiver. 

   Author: Greg White, 25-Jan-2011, SLAC. 
"
}
#-----------------------------------------------------------
# Mod: 26-Jan-2011, Greg White
#      If exactly 2 args, set start time to $2 and remove shift. Fixes
#      bug in compatibility with aidaGetHist.
#===========================================================

#
# Set up oracle
if [ -f /usr/local/lcls/epics/setup/oracleSetup.bash ]; then
    source /usr/local/lcls/epics/setup/oracleSetup.bash
fi

# Set Defaults
start='1 hour ago'
end='now'
scidecimals=0;
printname=0;

#! For backward compatibility, check for the old syntax where 
#! $2 = the starttime and $1 was the device name.
if [ $# == 2 ]; then
  start="$2";
fi
 
# Process options and argments
while getopts hnxs:e: opt; do
   case $opt in
       h) usage; exit 0 ;;
       s) start="${OPTARG}";;
       e) end="${OPTARG}";;
       x) scidecimals=1;;
       n) printname=1;;
       *) echo "Unknown Arguments, see aidagethist -h"; exit 1;;
   esac
done
shift $((OPTIND-1))
instance=$1

#! Start and end time in syntax understood by AIDA
starttime=`date --date="${start}" +"%m/%d/%Y %T"`
endtime=`date --date="${end}" +"%m/%d/%Y %T"`

if [ $printname == 1 ]; then
   echo $instance;
fi

# fast original, but only one line produced by awk get_hist.awk
if [ $scidecimals == 0 ]; then
    aidaget ${instance}//HIST.lcls -DSTARTTIME="${starttime}" -DENDTIME="${endtime}" | awk '{if ( NF == 8) printf("%s %s %14.6f\n",$2,$3,$1)}'
else
    aidaget ${instance}//HIST.lcls -DSTARTTIME="${starttime}" -DENDTIME="${endtime}" | awk '{if ( NF == 8) printf("%s %s %14.6e\n",$2,$3,$1)}'
fi
