#!/bin/bash
#!-*-sh-*-
#!====================================================================
#! Abs: Collects the KLYS data and creates the KDLS daily report. 
#! 
#! Arg: None. The sectors for which to create the report are coded into 
#!      the array variable kdlsreport_sectors below.
#! 
#! Rem: The output is written to files in /u1/lcls/physics/amrf/klydata/kdls.
#!
#! Auth: Greg White, 8-Mar-2011, SLAC
#! 
#! --------------------------------------------------------------------
#! Mod: 
#!       14-Mar-2011, Mike Zelazny, Add ENVS.bash
#!       26-Mar-2021: Dave Steele, Sophia Chadsey, Add data collection for LCLSII SSA
#! ====================================================================
#
source /usr/local/lcls/tools/script/ENVS.bash
#
declare -a kdlsreport_sectors
declare -a klyss
declare -a alldupcds

#! The list of sectors for which to create the report, at present
#! only 1 sector long.
kdlsreport_sectors=( "LI10" "LI20" "LI21" "LI22" "LI23" "LI24" "LI25" "LI26" "LI27" "LI28" "LI29" "LI30" )
#kdlsreport_sectors=( "LI26" )

HSTA_ELECTROMAG=0x2000
DATED=`date +"%d-%b-%Y"`
#KDLSREPORTDIR=/home/acclegr/dskl/tools/script
KDLSREPORTDIR=/u1/lcls/physics/amrf/klydata/kdls
TFILE=`mktemp`

#! Construct filenames
#! FNAME is a file specifically formatted appropriately for
#! /u/kl/sec10rf/procmail/kdls/kdls.load to be able to load its
#! contents into Oracle.
#
if [ -e ${KDLSREPORTDIR} ]; then
  FNAME=${KDLSREPORTDIR}/kdlsdaily-${DATED}.dat
  LOGFILE=${KDLSREPORTDIR}/kdlsdaily-${DATED}.log
else
  FNAME=kdlsdaily-${DATED}.dat
  LOGFILE=kdlsdaily-${DATED}.log
fi

#! For each sector in the list defined above, write
# the file to be loaded into the DB, and a log file.
# 
for sec in ${kdlsreport_sectors[@]}
do
#/home/acclegr/dskl/tools/script/makekdls_sector ${sec} >> $TFILE 2>> $LOGFILE
makekdls_sector ${sec} >> $TFILE 2>> $LOGFILE
done

#! Cleanup
#
cp $TFILE $FNAME
chmod +r $FNAME
rm $TFILE
find /u1/lcls/physics/amrf/klydata/kdls/kdlsdaily-*.* -type f -mtime +30 -exec rm -f {} \;

#!--------------------------------------------------------------------
#
#! Collects data for LCLSII SSA.
#
declare -a ssa_cm
declare -a cmss
declare -a salldupcds

#! The list of cryomodules and SSA station for which to create the report, at present
#! only 1 CM long.
ssa_cm=("L2B:06" "L2B:07" "L2B:08" "L2B:09" "L2B:10" "L2B:11" "L2B:12" "L2B:13" "L2B:14" "L2B:15" "L3B:16" "L3B:17" "L3B:18" "L3B:19" "L3B:20" "L3B:21" "L3B:22" "L3B:23" "L3B:24" "L3B:25" "L3B:26" "L3B:27" "L3B:28" "L3B:29" "L3B:30")

SDATED=`date +"%d-%b-%Y"`

SSAREPORTDIR=/u1/lcls/physics/amrf/klydata/kdls
STFILE=ssatemp

#! Construct filenames
#! FNAME is a file specifically formatted appropriately for
#! /u/kl/sec10rf/procmail/kdls/kdls.load to be able to load its
#! contents into Oracle.
#!
if [ -e ${SSAREPORTDIR} ]; then
 SFNAME=${SSAREPORTDIR}/SSAdaily-${SDATED}.dat
 SLOGFILE=${SSAREPORTDIR}/SSAdaily-${SDATED}.log
else
  SFNAME=SSAdaily-${SDATED}.dat
  SLOGFILE=SSAdaily-${SDATED}.log
fi

#! For each sector in the list defined above, write
#! the file to be loaded into the DB, and a log file.
# 
for cm in ${ssa_cm[@]}
do
makeSSA_cm ${cm} >> $STFILE 2>> $SLOGFILE
done

#! Cleanup
#
cp $STFILE $SFNAME
chmod +r $SFNAME
rm $STFILE
find /u1/lcls/physics/amrf/klydata/kdls/SSAdaily-*.* -type f -mtime +30 -exec rm -f {} \;

#!--------------------------------------------------------------------
#
#! Collects data for LCLS psc and mcor magnets
#
declare -a sec
declare -a pscss
declare -a pscalldupcds

#makes list of magnet PVs to collect data from, using sqlplus
makepsc_devicelist
process_id=$!
wait $process_id

#! The list of sectors for which to create the report.
input_file='/u1/lcls/physics/amrf/klydata/kdls/PSC_Device_List.txt'
#input_file=./PSC_Device_List.txt
psc_sec=$(<$input_file)

SDATED=`date +"%d-%b-%Y"`

PSC_REPORTDIR=/u1/lcls/physics/amrf/klydata/kdls
#PSC_REPORTDIR=home/acclegr/dskl
STFILE=psctemp

#! Construct filenames
#! FNAME is a file specifically formatted appropriately for
#! /u/kl/sec10rf/procmail/kdls/kdls.load to be able to load its
#! contents into Oracle.
#!
if [ -e ${PSC_REPORTDIR} ]; then
 SFNAME=${PSC_REPORTDIR}/PSCdaily-${SDATED}.dat
 SLOGFILE=${PSC_REPORTDIR}/PSCdaily-${SDATED}.log
else
  SFNAME=PSCdaily-${SDATED}.dat
  SLOGFILE=PSCdaily-${SDATED}.log
fi

#! For each device in the list defined above, write
#! the file to be loaded into the DB, and a log file.
# 
for sec in ${psc_sec[@]}
do
makePSC_sector ${sec} >> $STFILE 2>> $SLOGFILE
done

#! Cleanup
#
cp $STFILE $SFNAME
chmod +r $SFNAME
rm $STFILE
find /u1/lcls/physics/amrf/klydata/kdls/PSCdaily-*.* -type f -mtime +30 -exec rm -f {} \;
exit 0
