#!/bin/sh
#
# process.sh
#
# Perform Q transform based analysis of recent science mode data.
#
# usage:
#
#   process.sh [analysisInterval]
#
# The optional interval argument should be one of "minutes",
# "hour", or "day".  By default, an interval of "minutes" is
# assumed.

# Shourov K. Chatterji
# shourov@ligo.caltech.edu

# $Id: process.sh,v 1.3 2007/06/09 12:27:05 shourov Exp $

################################################################################
#                              Setup environment                               #
################################################################################

# change to directory containing this script
cd `dirname $0`

# path to Q transform installation
Q=../..

# setup environment
. ${Q}/bin/qsetup.sh

# setup condor environment
CONDOR_LOCATION=/ldcg/condor
export CONDOR_LOCATION
CONDOR_CONFIG=${CONDOR_LOCATION}/etc/condor_config
export CONDOR_CONFIG
PATH=${CONDOR_LOCATION}/bin:${CONDOR_LOCATION}/sbin:${PATH}
export PATH

# test for condor
condor_q 0 >/dev/null 2>&1
if [ $? -eq 0 ]; then
  run="condor_run"
else
  run=""
fi

################################################################################
#                            Command line arguments                            #
################################################################################

# usage information
if [ $# -ne 1 ]; then
  echo "usage: `basename $0` analysisInterval" 1>&2
  exit 1
fi

# analysis interval
analysisInterval=$1

################################################################################
#				  Run analysis                                 #
################################################################################

# current time
currentTime=`tconvert now`

# switch on analysis interval
case ${analysisInterval} in

  # handle minutes analysis interval
  minutes)

    # full minutes analysis latency
    analysisLatency=1200

    # analysis start time with latency
    startTime=`expr ${currentTime} - ${analysisLatency}`

    # analysis start time rounded down to nearest 10 minute boundary
    startMinute=`tconvert -f "%M" ${startTime} + 5 | \
                 awk ' { printf "%02d", $1 - $1 % 10 } '`
    startTimeString=`tconvert -f "%Y-%m-%d %H:${startMinute}:00" ${startTime} + 5`
    startTime=`tconvert "${startTimeString}"`

    # launch full minutes analysis
    ${run} process_minutes.sh ${startTime} triggers properties report event

    # analysis start time rounded down to nearest hour
    startTimeString=`tconvert -f "%Y-%m-%d %H:00:00" ${startTime} + 5`
    startTime=`tconvert "${startTimeString}"`

    # launch partial hour analysis
    ${run} process_hour.sh ${startTime} triggers properties report

    # end minutes analysis
    ;;

  # handle hour analysis interval
  hour)

    # full hour analysis latency
    analysisLatency=5100

    # analysis start time with latency
    startTime=`expr ${currentTime} - ${analysisLatency}`

    # analysis start time rounded down to nearest hour
    startTimeString=`tconvert -f "%Y-%m-%d %H:00:00" ${startTime} + 5`
    startTime=`tconvert "${startTimeString}"`

    # launch full hour analysis
    ${run} process_hour.sh ${startTime} triggers properties report event

    # analysis start time rounded down to nearest day
    startTimeString=`tconvert -f "%Y-%m-%d 00:00:00" ${startTime} + 5`
    startTime=`tconvert "${startTimeString}"`

    # launch partial day analysis
    ${run} process_day.sh ${startTime} triggers properties report

    # end hour analysis
    ;;

  # handle day analysis interval
  day)

    # full day analysis latency
    analysisLatency=89100

    # analysis start time with latency
    startTime=`expr ${currentTime} - ${analysisLatency}`

    # analysis start time rounded down to nearest day
    startTimeString=`tconvert -f "%Y-%m-%d 00:00:00" ${startTime} + 5`
    startTime=`tconvert "${startTimeString}"`

    # launch full day analysis
    ${run} process_day.sh ${startTime} triggers properties report event

    # end day analysis
    ;;

  # handle unknown analysis interval
  *)

    # report error
    echo "ERROR: Unknown analysis interval '${analysisInterval}'" 1>&2
    exit 1

    # end unknown analysis
    ;;

# end switch on analysis interval
esac

################################################################################
#				      End                                      #
################################################################################

# return
exit 0

