#! /bin/sh
#
#  Fill in the channel list for a specified frame type between two GPS times.
#
#  p1 frame type
#  p2 start gps
#  p3 end gps
#
# --- Set up the environment
prefix=/usr
exec_prefix=/usr
export PATH=".:/usr/bin:/usr/bin:/bin"
export LD_LIBRARY_PATH=/usr/lib
#
# --> Set default environment names
myhome=$HOME
nds2pwd=$myhome/channel-tracker
cache_root="channel_history"
framecache_url=http://ldas-cit.ligo-la.caltech.edu/ldas_outgoing/diskcacheAPI

if [ -f $HOME/.nds2rc ]
then
    . $HOME/.nds2rc
    [ -z $NDS2_CHANNEL_TRACKER ] || nds2pwd="$NDS2_CHANNEL_TRACKER"
    [ -z $NDS2_FRAME_CACHE_URL ] || framecache_url="$NDS2_FRAME_CACHE_URL"
    [ -z $NDS2_CACHE_ROOT_DIR ]  || cache_root="$NDS2_CACHE_ROOT_DIR"
fi
#
# --- Specify the top level directory for caching of channel information
cd $nds2pwd
cache_pwd=`cd $cache_root; pwd`
if test ! -d ${cache_pwd}
then
    exit 1
fi
#------------------------------------------------------------------------
# Loop over all frame types
#------------------------------------------------------------------------
ftype=$1
cache=$cache_pwd/$ftype
if [ ! -d $cache ]; then
    echo "$ftype not a defined frame type"
    exit 2
fi
#
#  gps times
[ -z $2 ] || startgps="--start-gps $2"
[ -z $3 ] || endgps="--end-gps $3"

#----------------------------------------------------------------
# Gather information about channels
#----------------------------------------------------------------
nds_chan_search \
    --loc-file nds2-source-list.txt --cache-dir $cache \
    --frame-type $ftype ${startgps} ${endgps} \
    >> build-$ftype-list.log

#-------------------------  Now build the index and channel list.
nds_channel_merge \
    --rebuild --list-directory $cache \
    --outfile ${ftype}-ChanList.txt $ftype \
    >> build-$ftype-list.log
#
