#!/bin/sh
#
# getsegments.sh
#
# Retrieves recent science mode segments for online analysis
#
# usage:  sciencemode.sh interferometer [startTime stopTime]
#
#   interferometer   interferometer identifiers
#   startTime        optional start time of search [GPS seconds]
#   stopTime         optional stop time of search [GPS seconds]
#
# The requested interferometer identifiers should be a concatenated
# list of two character identifiers (i.e. G1, H1, H2, L1, or V1).
#
# If no times are specified, all available science mode segments
# will be returned.
#
# If a start and stop time are specified, segments that extend beyond
# these times are truncated at the requested start and stop times.
#
# To avoid end of segment transients, three seconds are excluded from
# the ends of science mode segments.
#
# The resulting science mode segments are written to the standard
# output.

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

# $Id: getsegments.sh,v 1.3 2007/06/06 01:04:48 shourov Exp $

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

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

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

# determine current gps time
now=`tconvert now`

# parse command line arguments
if [ $# -eq 0 ]; then
  echo "usage: `basename $0` interferometer [startTime] [stopTime]" 1>&2
  exit 1
fi
interferometer=$1
if [ $# -gt 1 ]; then
  startTime=$2
else
  startTime=0
fi
if [ $# -gt 2 ]; then
  stopTime=$3
else
  stopTime=${now}
fi

# seconds to exclude at end of science mode segments
excludeSeconds=3

# extract requested science mode segments
awk ' $2 > '${startTime}' && $1 < '${stopTime}' \
    { print ('${startTime}' > $1) ? '${startTime}' : $1,
            ('${stopTime}' < $2) ? '${stopTime}' : $2 } ' \
    ../segments/${interferometer}.txt

