#! /bin/bash

# Copyright (C) 2016 Christopher M. Biwer
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

# sanity check command line
if [ $# -gt 1 ]; then
    echo "illegal number of command line options"
    exit
fi

# parse command line
# get midnight from yesterday in form of MM/DD/YYYY
if [ $# -eq 1 ]; then
    YESTERDAY=$1
else
    YESTERDAY=`date -u --date="yesterday" +"%x"`
fi

# convert midnight to GPS time and analyze a day of data
GPS_START_TIME=`lalapps_tconvert ${YESTERDAY}`
DURATION=86400
GPS_END_TIME=$((${GPS_START_TIME} + ${DURATION}))

# month subdir name in form of YYYYMM
MONTH=`lalapps_tconvert -f "%Y%m" ${GPS_START_TIME}`

# day subdir name in form of YYYYMMDD
DAY=`lalapps_tconvert -f "%Y%m%d" ${GPS_START_TIME}`

# name of workflow
WORKFLOW_NAME=daily_${DAY}

# change into run dir
RUN_DIR=/home/${USER}/projects/daily_cbc_offline/main/${MONTH}/${DAY}
mkdir -p ${RUN_DIR}
cd ${RUN_DIR}

# output options
OUTPUT_DIR=${RUN_DIR}/output
HTML_DIR=${HOME}/public_html/daily_cbc_offline/main/${MONTH}/${DAY}

# get executables
wget http://code.pycbc.phy.syr.edu/pycbc-software/v1.3.2/x86_64/composer_xe_2015.0.090/pycbc_make_coinc_search_workflow
wget http://code.pycbc.phy.syr.edu/pycbc-software/v1.3.2/x86_64/composer_xe_2015.0.090/pycbc_submit_dax
chmod +x pycbc_make_coinc_search_workflow pycbc_submit_dax

# get veto-definer file
VETO_DEF_NAME=H1L1-HOFT_C01_O1_CBC.xml
VETO_DEF_URL=file://${RUN_DIR}/${VETO_DEF_NAME}
curl https://code.pycbc.phy.syr.edu/detchar/veto-definitions/download/master/cbc/O1/${VETO_DEF_NAME} > ${VETO_DEF_NAME}

# run workflow generator
${RUN_DIR}/pycbc_make_coinc_search_workflow --workflow-name ${WORKFLOW_NAME} --output-dir ${OUTPUT_DIR} \
    --config-files \
      https://code.pycbc.phy.syr.edu/ligo-cbc/pycbc-config/download/4a82467e48b811866b7cee07dd37bd147119856e/O1/pipeline/analysis.ini \
      https://code.pycbc.phy.syr.edu/ligo-cbc/pycbc-config/download/a227ac6d4accdf5d9b4e51b93cc92543fe7e1ebc/O1/pipeline/plotting.ini \
      https://code.pycbc.phy.syr.edu/ligo-cbc/pycbc-config/download/541d4dc77d4d0fbc1b622ed0af4ef17f02fca685/ER8/pipeline/data_C01.ini \
      https://code.pycbc.phy.syr.edu/ligo-cbc/pycbc-config/download/a9d35a4b17fa9e62ff822ddffca9c2820b007d92/O1/pipeline/gps_times_O1_analysis_9.ini \
      https://code.pycbc.phy.syr.edu/ligo-cbc/pycbc-software/download/master/v1.3.2/x86_64/composer_xe_2015.0.090/executables.ini \
    --config-overrides \
      "results_page:output-path:${HTML_DIR}" \
      "workflow:start-time:${GPS_START_TIME}" \
      "workflow:end-time:${GPS_END_TIME}" \
      "workflow-coincidence:background-bins:bns:chirp:1.74 edge:SEOBNRv2Peak:220 bulk:total:150" \
      "workflow-segments:segments-veto-definer-url:${VETO_DEF_URL}" \
      "inspiral:cluster-window:4" \
      "inspiral:chisq-bins:\"0.4*get_freq('fSEOBNRv2Peak',params.mass1,params.mass2,params.spin1z,params.spin2z)**(2./3.)\"" \
      "results_page:analysis-subtitle:'Daily CBC Offline ${DAY}'"

#!FIXME: previous releases do not support ACCOUNTING_GROUP_USER so use the installed version
# plan and submit the workflow
LOCAL_DIR=${RUN_DIR}/condor
mkdir -p ${LOCAL_DIR}
cd ${OUTPUT_DIR}
pycbc_submit_dax --accounting-group ligo.dev.o1.detchar.singleifo.dailyahope \
    --accounting-group-user christopher.biwer \
    --dax ${WORKFLOW_NAME}.dax \
    --local-dir ${LOCAL_DIR} \
    --no-create-proxy

# name of the nagios monitor file
NAGIOS_GPS_TIME=`lalapps_tconvert`
NAGIOS_DIR=/home/${USER}/public_html/daily_cbc_offline/nagios
NAGIOS_FILE=${NAGIOS_DIR}/nagios-workflow-generator.json

# make sure directory exists
mkdir -p ${NAGIOS_DIR}

# update nagios monitor
pycbc_nagios_daily_monitor --update-transfer-files --output-file ${NAGIOS_FILE}  --gps-time ${NAGIOS_GPS_TIME}
