#!/bin/sh
# PURPOSE:	scoreQC: tool to assess quality of new QC parameters as compared to specs or trending
# AUTHOR:	Reinhard Hanuschik / ESO
# VERSIONS:	1.0 -- created: 2007-05-23
#		1.0.1- enabled for parallel execution (WH); links added in html output (2007-07-12)
#		1.0.2- DB_TYPE: check for legal values char or number (2007-10-12)
#		1.0.3- improved hyperlinks, local or external, to prepare for archiving (2007-12-06)
#		1.0.4- added link to QC1_plot interface (2008-01-18)
#		1.1 -- writes to qc1_score database; improved hyperlinks; extended configuration, reads statFiles from trendPlotter v2.x (2008-04-09)
#		1.1.1- suppresses messages from getStatusAB (2008-04-11)
#		1.1.2- RB section not evaluated anymore (2008-07-30)
#		1.1.3- Detector graphical summary in the case of no QC_PLOT configuration for given RAW_TYPE+QC_PAR [JDP]; basic output even without score configuration (2008-11-03)
#		1.2 -- detThresh extended to include $DET1; prepare for trendPlotter calls in $DFO_MON_DIR/HC_list_<date> (2008-12-15)
#		1.3 -- write CALIB or SCIENCE into TLOG, to provide specific TOTAL_SCOREs (2009-01-23)
#		1.3.1- bug fixed with MATCHKEY=^... in config.scoreQC (2009-02-18)
#		1.3.2- link added to HELP file (2009-03-30)
#		1.3.3- link added behind HC flags (2009-06-16)
#		1.4 -- dynamic plots with thresholds; QC1 parameter documentation added as tooltips; new key SHOW_ICON (2009-06-30)
#		1.4.1- bug fix line :1570 for MEF detector plots [BWo]; YMIN/YMAX bug for MEFs fixed (2009-07-15)
#               1.4.2- bug fix replacing "grep -e $M" with "grep -e [[:space]]$M[[:space]]" [MNe] (2009-12-23)
#		1.5 -- improved detector arrangement for MEF; fact sheet (2010-01-25)
#		1.5.1- option to specify $TMP_DIR for debugging (2010-09-14)
#		1.5.2- added some documentation for the score report (2010-10-07)
#		1.6 -- new coversheet support; unscored: display val[ue]; correct YMAX if VALUE higher than configured value (2011-05-31)
#		1.6.1- modified WEB_SERVER_URL, minor change in line 968 (2011-06-27)
#		1.6.2- supports config key ALL_DETECTORS (2011-12-12)
#		1.7  - explore button; investigate mode (2012-02-01)
#		1.7.1- display match key if INVESTIGATE=YES; fuzzy PROD_ROOT_NAME disabled; bug fixed with QC params containg 'filter' in their name (2012-06-28)
#		1.8  - support ATAB files; TPL_ID and TPL_NAME written into score report (2013-01-11)
#		1.8.1- bug fix for TPL_ID handling on QC cluster (2013-01-31)
#		1.8.2- optional config paramater UPDATE_YN, useful for phoenix (2013-05-13)
#		1.8.3- format=gif replaced by format=png (2013-10-01)
#		1.9 -- replace $WEB_SERVER by $DFO_WEB_SERVER (2013-11-04)
#		1.10-- determine trendPlotter calls from HC_PLOT configuration per RAW_TYPE (2014-11-18) [BWo]
#		2.0 -- new optional splitting of config files; marking for YELLOW_SCORE flag in getStatusAB (2015-04-13)
#		2.0.1- bug fix for case if called within recreateAB (2015-06-01)
#		2.0.2- no code change but new section 4.1A in config file for MULTI_CONFIG=YES and multiple RAW_TYPEs (2015-07-01)
#		2.1 -- optional CONFIG_PGI, to have flexibility if MULTI_CONFIG=NO (2015-09-21)
#		2.1.1- update HC_flag in table delay_time at the end (2015-10-28)
#		2.1.2- bug fix in update of dfo_delay_time (2015-10-29)
#		2.1.3- another bug fix related to delay_time (2015-10-30)
#		2.1.4- added OBS_TPLNO and OBS_NTPL to html report (2016-10-20)
#		2.1.5- decommission delay_time (2021-04-26)
#		2.1.6- do not write hc_pindex into TLOG as was done with v2.1.5 (2021-05-03) [BWo]
#		2.1.7- link to HC report not as pop-up window (2021-05-19)
#
# PARAMETERS:	-a <ab> 
# CONFIG:	config.scoreQC; if MULTI_CONFIG=YES, also the multiple config files as listed in config.scoreQC
# TOOLS CALLED:	qcdate
# OUTPUT:	tlog, html file per AB; entries in qc1_score (to be read by scoreHC)
# NOTES:	AB must reside in $DFO_AB_DIR (--> data not yet certified);
#		The number of isql queries is always 4, independent of number of detectors and qc_params.
#		The number of qc1Ingests is Ndet x Npar.
#		Make sure to not modify lines with SCORE_COL (sect 3.12) since this code is also used by the opshub tool.
#		Use of DFO table delay_times has been terminated with v2.1.5.
# ======================================================================
TOOL_VERSION="2.1.7"
TOOL_NAME="scoreQC"
HCTOOL_NAME="trendPlotter"

HELP_URL="http://www.eso.org/qc/ALL/score_help.html"
QC_DOCU_URL="http://www.eso.org/~qc/docu/QC1"
QC_TUTORIAL_URL="http://www.eso.org/observing/dfo/quality"

ARC_URL="http://archive.eso.org/bin/qc1_cgi"	# qc1_db interface
HC_URL="http://www.eso.org/qc"			# HC monitor

WEB_SERVER_URL="http://qcweb.hq.eso.org/~qc"		# QC web server
INSTR_DIR="/home/qc/public_html/${DFO_INSTRUMENT}"	# on QC web server

QC_DIR="/home/qc/qc/ALL/php" 			# on public web server

IMG_SRC="http://www.eso.org/images/analog"	# images for output
IMG_SRC1="http://www.eso.org/qc/ALL/img"	# same


# The following was prepared but not implemented because no convergence with A. Smette.
#QCD_PLOTS="QC1_docu_plots"			# for QC1_docu (webDocuSys)
#QCD_PARAMS="QC1_docu_params"			# for QC1_docu (webDocuSys)

# this is the list of fields to be displayed in the QCD_PLOTS links ('score'). 
# format: have #QCD_PLOTS as field #1 (qualifier), and field #2 as content. Special: must start with #
#QCD_PLOTS HC_report
#QCD_PLOTS HC_plot_index
#QCD_PLOTS flag
#QCD_PLOTS QC1_table 
#QCD_PLOTS QC1_parameter
#QCD_PLOTS definition
#QCD_PLOTS QC1_docu_link 	
#QCD_PLOTS score_thresholds
#QCD_PLOTS score_justification
#QCD_PLOTS version
#QCD_PLOTS last_update 	
#QCD_PLOTS PROP_ticket 	
#QCD_PLOTS PROP_status 

# this is the list of fields to be displayed in the QCD_PARAMS links ('docu').
# if to be modified, change here
# format: as above forQCD_PLOTS
#QCD_PARAMS QC1_table
#QCD_PARAMS QC1_parameter
#QCD_PARAMS FITS_keyword
#QCD_PARAMS definition
#QCD_PARAMS algorithm
#QCD_PARAMS template
#QCD_PARAMS conditions
#QCD_PARAMS pipeline_recipe
#QCD_PARAMS ETC_relevant
#QCD_PARAMS ETC_relation
#QCD_PARAMS user_manual
#QCD_PARAMS observing_software 

# end of not implemented

# =========================================================================
# 0. Initialize
# 0.1 read params
# =========================================================================

CHECK=`printenv | grep DFO`
if [ "Q$CHECK" = "Q" ]
then
        echo "*** ERROR: DFO variables not defined. Check ~/.dfosrc and restart."
        exit 1
fi

if [ $# = 0 ]
then
	cat $DFO_DOC_DIR/${TOOL_NAME}.h
	exit
fi

SPECIAL_TMP=NO
CREATE_PHP=NO
DISP_QCPARAM=NO
INVESTIGATE=NO

# -c is hidden, not on the command line!

while getopts a:c:T:DIPhv OPTION
do
	case "$OPTION" in
	 v ) echo $TOOL_VERSION
	     exit 0 ;;
	 h ) cat $DFO_DOC_DIR/${TOOL_NAME}.h
	     exit 0 ;;
	 T ) export JTMP_DIR=$OPTARG; SPECIAL_TMP=YES ;;

	 a ) export AB=$OPTARG;;
	 c ) SQC_CONFIG=$OPTARG ;;

	 D ) DISP_QCPARAM=YES;;
	 I ) INVESTIGATE=YES ;;
	 P ) CREATE_PHP=YES;;
	 * ) cat $DFO_DOC_DIR/${TOOL_NAME}.h; exit -1 ;;
	esac
done

# =========================================================================
# 0.2 initial checks
# =========================================================================

if [ $# -lt 2 ] && [ $CREATE_PHP = NO ]
then
	echo "***ERROR: You must provide two arguments. Hit return:"
	read input
	exit -1	
fi

if [ $CREATE_PHP = NO ]
then
	CHECK_AB=`echo $AB | grep .ab`
	if [ Q$CHECK_AB = Q ]
	then
		echo "***ERROR: $AB is not an AB. Exit."
		exit -1
	fi
fi

# make parallel (WH 2007-07-05)
if [ Q$JTMP_DIR = Q ]
then
	PID=$$
	export JTMP_DIR=${TMP_DIR}/${TOOL_NAME}_${PID}
fi

if [ ! -d $JTMP_DIR ]
then 
	mkdir $JTMP_DIR
fi

# prepare for multiple config
if [ Q$SQC_CONFIG = Q ]
then
	CONFIG1=config.scoreQC
else
	CONFIG1=$SQC_CONFIG
fi

if [ ! -s $DFO_CONFIG_DIR/$CONFIG1 ]
then
        echo "*** ERROR: No configuration file $DFO_CONFIG_DIR/$CONFIG1 found. Check and re-start."
        exit 2
fi
CONFIG=${DFO_CONFIG_DIR}/$CONFIG1

# check for multiple configuration
if [ $CONFIG1 = config.scoreQC ]
then
	MULTI_CONFIG=`grep "^MULTI_CONFIG"	$CONFIG | awk '{print $2}'`
	if [ Q$MULTI_CONFIG != QYES ]
	then
		MULTI_CONFIG=NO
	fi

	if [ $MULTI_CONFIG = YES ]
	then
		CONFIG_STRING=`grep "^CONFIG_STRING"	$CONFIG | sed "s/&&$//" | sed "s/^.*&&//"`
		if [ "Q$CONFIG_STRING" = Q ]
		then
			CONFIG_VALUE=`eval "cat $DFO_AB_DIR/$AB | grep RAW_TYPE | awk '{print \\$2}'"`
		else
			CONFIG_VALUE=`eval "cat $DFO_AB_DIR/$AB | grep RAW_TYPE | awk '{print \\$2}' | $CONFIG_STRING"`
		fi

# now find the proper config file
		if [ "Q$CONFIG_VALUE" = Q ]
		then
			echo "*** ERROR: no match found for CONFIG_STRING. Fix config error and try again."
			exit -1
		else
			CFG_FILE=`grep "^CONFIG_CASE"	$CONFIG | grep "[[:space:]]${CONFIG_VALUE}[[:space:]]" | awk '{print $3}'`

# none found --> other?
			if [ Q$CFG_FILE = Q ]
			then
				CONFIG_VALUE=other
				CFG_FILE=`grep "^CONFIG_CASE"	$CONFIG | grep "[[:space:]]${CONFIG_VALUE}[[:space:]]" | awk '{print $3}'`
			fi
# still none? Error
			if [ Q$CFG_FILE = Q ]
			then
				echo "*** ERROR: no config file configured for CONFIG_VALUE '$CONFIG_VALUE'. Fix config error and try again."
				exit -1
			else
				if [ ! -s $DFO_CONFIG_DIR/$CFG_FILE ]
				then
					echo "*** ERROR: config file '$CFG_FILE' not found. Fix config error and try again."
					exit -1
				fi
			fi

# config file found --> ok, call tool with that config file.
			echo "- Calling $TOOL_NAME -a $AB -c $CFG_FILE ..."
			scoreQC -a $AB -c $CFG_FILE	
			exit
		fi
# MULTI_CONFIG=NO: continue here
	fi
fi

# check for PGI (as alternative to multiple config files)
CONFIG_PGI=`grep "^CONFIG_PGI"	$CONFIG | awk '{print $2}'`

# check for existence
if [ Q$CONFIG_PGI != Q ]
then
	if [ ! -s $DFO_BIN_DIR/$CONFIG_PGI ]
	then
		echo "*** ERROR: $CONFIG_PGI configured as \$CONFIG_PGI in $CONFIG but not found in \$DFO_BIN_DIR. Check out!"
		CONFIG_PGI=""
	fi
fi

# =========================================================================
# 0.3 some configuration
# =========================================================================

MEF_NCOL=`grep "^MEF_NCOL"	$CONFIG | awk '{print $2}'`
MEF_NROW=`grep "^MEF_NROW"	$CONFIG | awk '{print $2}'`
MEF_NCOL1=`echo $MEF_NCOL | awk '{print $1+1}'`
MEF_PSEUDO=`grep "^MEF_PSEUDO"	$CONFIG | awk '{print $2}'`
if [ "Q$MEF_PSEUDO" != "QYES" ]
then
	MEF_PSEUDO=NO
fi

if [ $MEF_NCOL -gt 1 ] || [ $MEF_NROW -gt 1 ]
then
	MEF_MODE=YES
else
	MEF_MODE=NO
fi

ALL_DETECTORS=`grep "^ALL_DETECTOR"     $CONFIG | awk '{print $2}'`
if [ Q$ALL_DETECTORS != QNO ]
then
        ALL_DETECTORS=YES
fi

# SHOW_ICON: default = NO
SHOW_ICON=`grep "^SHOW_ICON" 		$CONFIG | awk '{print $2}'`
if [ Q$SHOW_ICON != QYES ]
then
	SHOW_ICON=NO
fi

SYMB_SIZE=`grep     "^SYMB_SIZE"	$CONFIG | awk '{print $2}'`
TABLE_SIZE=`grep    "^TABLE_SIZE" 	$CONFIG | awk '{print $2}'`
YELLOW_SCORE=`grep "^YELLOW_SCORE"	$CONFIG | awk '{print $2}'`

if [ Q$TABLE_SIZE = Q ]
then
	TABLE_SIZE=50
fi

# overwrite for design
TABLE_SIZE=100%

#default is 1
if [ "Q$YELLOW_SCORE" = "Q" ]
then
	YELLOW_SCORE=1
fi

LIGHT_RED=#F66       #scoring: NOK
YELLOW=#FF0          #scoring: 1 outlier
GREEN=#3F3           #scoring: OK
GREY=#CCC		#scoring: unknown
TITLE_COLOR=#006A9D	#eso-blue: font color
QDS_COLOR=#093		#light-green: QC docu system color

UPDATE_YN=`grep "^UPDATE_YN" $CONFIG | awk '{print $2}'`
if [ Q$UPDATE_YN = Q ]
then
	UPDATE_YN=YES
fi

# =========================================================================
# 0.4 procedures
# 0.4.1 cleanup: remove the PID tmp_dir
# =========================================================================

cleanup(){
if [ -d $JTMP_DIR ] && [ $SPECIAL_TMP = NO ]
then 
	rm -rf $JTMP_DIR
fi
}

# =========================================================================
# 0.4.2 makeDetLink: create table with detector links per QC1 parameter
# =========================================================================

makeDetLink(){
rm -f $JTMP_DIR/detlink
COUNT_DET=1
COUNT_ROW=1
QUICK=NO        #to have only once the CHIP_STRING condition met for JTMP_DIR/explore
while [ $COUNT_ROW -le $MEF_NROW ]
do
	COUNT_COL=1
	echo "  <tr>" >> $JTMP_DIR/detlink 
	while [ $COUNT_COL -le $MEF_NCOL ]
	do
		DET=`cat $JTMP_DIR/list_mefvalues | awk '{print "NR"NR,$1}' | grep "^NR${COUNT_DET} " | awk '{print $2}'`
		if [ $DET != EMPTY ]
		then
			DET_SCORE=`grep "^$QC[[:space:]]" $DFO_AB_DIR/$TLOG | grep "det=$DET[[:space:]]" | awk '{print $3}' | sed "s/score=//"`
			TITLE=`grep     "^$QC[[:space:]]" $DFO_AB_DIR/$TLOG | grep "det=$DET[[:space:]]" | awk '{print $4}'`
			DB_VAL=`grep    "^$QC[[:space:]]" $DFO_AB_DIR/$TLOG | grep "det=$DET[[:space:]]" | awk '{print $5}'`
			DB_NUM=`grep    "^$QC[[:space:]]" $DFO_AB_DIR/$TLOG | grep "det=$DET[[:space:]]" | awk '{print $6}'`
			TITLE1=$TITLE

			if [ "$DET_SCORE" = "unscored" ]
			then
				TITLE1="unscored"
			fi

# standard symbol size
			SYMB_SIZE1=$SYMB_SIZE

# AVG, RMS: slightly smaller symbols 
			if [ $DET = AVG ]
			then
				SYMB_SIZE1=`echo $SYMB_SIZE1 | awk '{print $1*0.8}'`
			elif [ $DET = RMS ]
			then
				SYMB_SIZE1=`echo $SYMB_SIZE1 | awk '{print $1*0.6}'`
			fi
	
			case $DET_SCORE in
	 	 	 0 ) PSYMB=sq2.png; SSYMB=ok.gif ; DET_SCORE="score=$DET_SCORE";;
		 	 unscored ) PSYMB=sqg.png; SSYMB=itemOff.gif ;;
	 	 	 * ) PSYMB=sq0.png; SSYMB=nok.gif ; DET_SCORE="score=$DET_SCORE";;
			esac

# mark_upper, mark_lower, mark_avg:
			if [ $TITLE1 != "unscored" ]
			then
				MARK_UPPER=`echo $TITLE | sed "s/^.*thresh2=//" | sed "s/^.*/\&mark_upper=&/"`
				MARK_LOWER=`echo $TITLE | sed "s/,val=.*//" | sed "s/thresh1=//" | sed "s/^.*/\&mark_lower=&/"`
				MARK_AVG=`echo  $DB_VAL | sed "s/db_avg=//" | sed "s/^.*/\&mark_avg=&/"`
			else
				MARK_UPPER=""
				MARK_LOWER=""
				MARK_AVG=""
			fi

			if [ $MEF = YES ] 
			then
				TITLE2="results for ${DET_ID} $DET:"
				if [ $DET = AVG ]
				then
					TITLE2="results for chip-averaged value $DET:"
				elif [ $DET = RMS ]
				then
					TITLE2="results for chip-averaged scatter $DET:"
				fi
					
				cat >> $JTMP_DIR/detlink <<EOT
    <td align="left">
      <a href="${ARC_URL}?action=qc1_plot_plot&table=${QC1_TABLE}&xaxis=mjd_obs&yaxis=$QC&from_stat=same+as+plot&to_stat=same+as+plot&from=${FROM}&to=${TO}&civil_dates=on&format=png${MARK_AVG}${MARK_LOWER}${MARK_UPPER}&mark_mjd=${MJD_OBS}&ymin=${YMIN}&ymax=${YMAX}&outliers=on&filter_${DET_ID}=${DET}${DB_SETUP}&plot=Plot" title="$TITLE2 $DET_SCORE $TITLE | $DB_VAL | $DB_NUM"><img src="${IMG_SRC}/$PSYMB" width="$SYMB_SIZE1" height="$SYMB_SIZE1" border="0"></a>
    </td>
EOT
			fi	
		fi
# EMPTY
		if [ $DET = EMPTY ]
		then
			cat >> $JTMP_DIR/detlink <<EOT
    <td align="center"><font size=1>&nbsp;</font></td>
EOT
		fi

# dynamic plot for all detectors
		if [ $MEF = YES ]
		then
			if [ $COUNT_ROW = 1 ] && [ $COUNT_COL = $MEF_NCOL ] && [ $ALL_DETECTORS = YES ]
			then
				SYMB_SIZE1=`echo $SYMB_SIZE1 | awk '{print $1*1.5}'`
				cat >> $JTMP_DIR/detlink <<EOT
    <td align="left"> 
      <a href="${ARC_URL}?action=qc1_plot_plot&table=${QC1_TABLE}&xaxis=mjd_obs&yaxis=$QC&from_stat=same+as+plot&to_stat=same+as+plot&from=${FROM}&to=${TO}&civil_dates=on&format=png${MARK_AVG}${MARK_LOWER}${MARK_UPPER}&mark_mjd=${MJD_OBS}&ymin=${YMIN}&ymax=${YMAX}&outliers=on&${DB_SETUP}&plot=Plot" title="dynamic plot for all detectors"><img src="${IMG_SRC}/sq3.png" width="$SYMB_SIZE1" height="$SYMB_SIZE1" border="0" hspace="5"></a>
    </td>
EOT
			elif [ $COUNT_COL = $MEF_NCOL ]
			then
				cat >> $JTMP_DIR/detlink <<EOT
    <td align="left"><font size=1>&nbsp;</font></td>
EOT
			fi

# MEF=NO
		else
			cat >> $JTMP_DIR/detlink <<EOT
    <td>
      <a href="${ARC_URL}?action=qc1_plot_plot&table=${QC1_TABLE}&xaxis=mjd_obs&yaxis=$QC&from_stat=same+as+plot&to_stat=same+as+plot&from=${FROM}&to=${TO}&civil_dates=on&format=png${MARK_AVG}${MARK_LOWER}${MARK_UPPER}&mark_mjd=${MJD_OBS}&ymin=${YMIN}&ymax=${YMAX}&outliers=on${DB_SETUP}&plot=Plot" title="results: $DET_SCORE $TITLE | $DB_VAL | $DB_NUM"><img src="${IMG_SRC}/$PSYMB" width="$SYMB_SIZE1" height="$SYMB_SIZE1" border="0"></a>
    </td>
EOT
		fi

# for $JTMP_DIR/explore interface: MARK_UPPER2 and MARK_LOWER2 needed, score thresholds of *first* detector
		if [ $COUNT_COL = 1 ] && [ $COUNT_ROW = 1 ]
		then
			MARK_UPPER2=`echo $MARK_UPPER | sed "s/^.*=//"`
			if [ Q$MARK_UPPER2 = Q ]
			then
				MARK_UPPER2=null
			fi

			MARK_LOWER2=`echo $MARK_LOWER | sed "s/^.*=//"`
			if [ Q$MARK_LOWER2 = Q ]
			then
				MARK_LOWER2=null
			fi
		fi

		#DB_SETUP2=`echo ${DB_SETUP} | sed "s/\&/ /"g | sed "s/=/ /g" | sed "s/filter_//g" | sed "s/^ //"`
# improved: QC1 keys can contain 'filter' in their name
		DB_SETUP2=`echo ${DB_SETUP} | sed "s/\&/ /"g | sed "s/=/ /g" | sed "s/filter_filter/filter_FGHIJ/g" | sed "s/filter_//g" | sed "s/FGHIJ/filter/g" | sed "s/^ //"`

		CHIP_STRING=""
		if [ $MEF = NO ] || ( [ $MEF = YES ] && [ $ALL_DETECTORS = NO ])
		then
			CHIP_STRING=1
			DET_ID="none"

# MEF=YES: QuickPlot interface only for $DET=AVG ...
		elif [ $MEF = YES ] && [ $DET = AVG ]
		then
# we need to check if the mefvalues include '_'; if so, we use '!' as separator, otherwise '_'
# currently there is only OMEGACAM using the '!'
			N_CHIPS=`cat $JTMP_DIR/list_mefvalues | grep -v EMPTY | wc -l`
			CHECK_SEP=`grep "_" $JTMP_DIR/list_mefvalues | grep -v EMPTY | head -1`
			if [ "Q$CHECK_SEP" != Q ]
			then
				CHIP_STRING=`cat $JTMP_DIR/list_mefvalues | egrep -v "^EMPTY|^AVG|^RMS" | tr "\012" "!" | sed "s/!$/\n/"`
				CHIP_STRING="${N_CHIPS}!${CHIP_STRING}!AVG!RMS"
			else
				CHIP_STRING=`cat $JTMP_DIR/list_mefvalues | egrep -v "^EMPTY|^AVG|^RMS" | tr "\012" "_" | sed "s/_$/\n/"`
				CHIP_STRING="${N_CHIPS}_${CHIP_STRING}_AVG_RMS"
			fi
			QUICK=YES

# MEF=YES: ... or (if no AVG): after last col and row

# special condition: if ALL_DETECTORS=NO, we turn off MEF here since then we have the special UVES situation:
# one arm is MEF, one arm is blue, the QC database needs DET_ID but the blue arm de facto is SEF

		elif [ $MEF = YES ] && [ $ALL_DETECTORS = YES ] && [ $COUNT_COL = $MEF_NCOL ] && [ $COUNT_ROW = $MEF_NROW ] && [ $QUICK = NO ]
		then
			N_CHIPS=`cat $JTMP_DIR/list_mefvalues | grep -v EMPTY | wc -l | awk '{print $1+2}'`
			CHECK_SEP=`grep "_" $JTMP_DIR/list_mefvalues | grep -v EMPTY | head -1`
			if [ "Q$CHECK_SEP" != Q ]
			then
				CHIP_STRING=`cat $JTMP_DIR/list_mefvalues | grep -v "^EMPTY" | tr "\012" "!" | sed "s/!$/\n/"`
				CHIP_STRING=`echo ${N_CHIPS}!${CHIP_STRING}!noavg!norms`
			else
				CHIP_STRING=`cat $JTMP_DIR/list_mefvalues | grep -v "^EMPTY" | tr "\012" "_" | sed "s/_$/\n/"`
				CHIP_STRING=`echo ${N_CHIPS}_${CHIP_STRING}_noavg_norms`
			fi
		fi

		if [ Q$CHIP_STRING != "Q" ]
		then	
			echo "$AB ${QC1_TABLE} $QC ${YMIN} ${YMAX} ${FROM} ${TO} ${MARK_LOWER2} ${MARK_UPPER2} ${MJD_OBS} $CHIP_STRING $DET_ID ${DB_SETUP2}" >> $JTMP_DIR/explore
		fi

		COUNT_COL=`echo $COUNT_COL | awk '{print $1+1}'`
		COUNT_DET=`echo $COUNT_DET | awk '{print $1+1}'`

# required if NCOLxNROW > NDET (e.g. 4x2 > 6=4+AVG+RMS: CRIRES)
		if [ $COUNT_DET -gt $N_DET_VALUES ]
		then
			echo "  </tr>" >> $JTMP_DIR/detlink
			break 
		fi
	done
	COUNT_ROW=`echo $COUNT_ROW | awk '{print $1+1}'`
done

# HC_MARK and link to HC_REPORT_LINK
S_TYPE=`grep    "^$QC[[:space:]]" $DFO_AB_DIR/$TLOG | awk '{print $7}' | sort -u | grep "score_type=stat"`

if [ "Q$S_TYPE" = "Qscore_type=stat" ] && [ Q${HC_REPORT_LINK} != Q ]
then
	HC_RANGE=`grep "^REPORT_NAME" $DFO_CONFIG_DIR/$HCTOOL_NAME/config.tp_${HC_REPORT_LINK} | awk '{print $4}'`
	case $HC_RANGE in
 	  "FULL" ) HC_PATH="${HC_URL}/${DFO_INSTRUMENT}/reports/FULL"   ;;
 	  *      ) HC_PATH="${HC_URL}/${DFO_INSTRUMENT}/reports/HEALTH" ;;
	esac
	HC_FLAG=`echo ${HC_MARK} | sed "s|HC|<a href=${HC_PATH}/trend_report_${HC_REPORT_LINK}_HC.html target=_blank title=\"score goes to HC report ${HC_REPORT_LINK}\"><font size=1 color=$TITLE_COLOR>HC</a>|"`

# just in case ${HC_REPORT_LINK} doesn't exist
elif [ "Q$S_TYPE" = "Qscore_type=stat" ]
then
	HC_FLAG=${HC_MARK}
else
	HC_FLAG=""
fi

cat $JTMP_DIR/sq_explore2 | sed "s|HC_MARK|${HC_FLAG}|" > $JTMP_DIR/html1
mv $JTMP_DIR/html1 $JTMP_DIR/sq_explore2
cat $JTMP_DIR/sq_explore1 | sed "s|HC_MARK|${HC_FLAG}|" > $JTMP_DIR/html1
mv $JTMP_DIR/html1 $JTMP_DIR/sq_explore1

# now fill in the detector links
cat $JTMP_DIR/detlink >> $JTMP_DIR/sq_explore1
cat $JTMP_DIR/detlink >> $JTMP_DIR/sq_explore2
cat $JTMP_DIR/detlink >> $JTMP_DIR/store_parstring
}

# =========================================================================
# 0.4.3 detThresh: find thresholds; 3 options:
#       - from configured thresholds (THRESH1 and THRESH2 set with numbers )
#       - from $HCTOOL_NAME configuration (THRESH1/2: HC)
#       - from plugin to have dynamic threshold determination (THRESH1=plugin name)
# =========================================================================

detThresh(){
HC_REPORT=`grep -e "[[:space:]]$M[[:space:]]" $JTMP_DIR/list_matchkeys1 | grep "DET=$DET1 " | sed "s/#.*//" | awk '{print $6}'`
HC_PINDEX=`grep -e "[[:space:]]$M[[:space:]]" $JTMP_DIR/list_matchkeys1 | grep "DET=$DET1 " | sed "s/#.*//" | awk '{print $7}'`
HC_FACTOR=`grep -e "[[:space:]]$M[[:space:]]" $JTMP_DIR/list_matchkeys1 | grep "DET=$DET1 " | sed "s/#.*//" | awk '{print $8}'`
THRESH1=`grep   -e "[[:space:]]$M[[:space:]]" $JTMP_DIR/list_matchkeys1 | grep "DET=$DET1 " | awk '{print $4}'`

# Quick update of HC plots:
# CALL_ENABLED is set to YES by createJob -F, in INCREMENTAL processing mode
# then, we write $HCTOOL_NAME calls (one per QC1 parameter) into execHC1
# which is later consolidated (sort -u) [createJob] into the execHC file and executed

if [ Q${HC_REPORT} != Q ] && [ Q$CALL_ENABLED = QYES ]
then
	echo "$HCTOOL_NAME -f -r $HC_REPORT" >> $DFO_JOB_DIR/execHC1_${MODE}_$DATE
fi

# determine type of scoring (S_TYPE): stat or spec
if [ $THRESH1 = HC ]
then
	S_TYPE=stat
else
	S_TYPE=spec
fi

# spec: specified thresholds (in $CONFIG)
if [ $S_TYPE = spec ]
then

# check first char: number     --> take as parameter
#                   not number --> take as plugin name and execute
	THRESH11=`echo $THRESH1 | cut -c1,1 | egrep "[0-9+]|\-"`
	if [ "Q$THRESH11" != "Q" ]  && [ "Q$THRESH1" != "Q-" ]
	then
		THRESH2=`grep -e " $M" $JTMP_DIR/list_matchkeys1 | grep "DET=$DET1 " | awk '{print $5}'`
# plugin
	else
		PGI_SCORE=$THRESH1
		if [ "Q$PGI_SCORE" = "Q" ] 
		then	
			echo "***ERROR: \$PGI_SCORE not configured. Check config file!"
			exit
		elif [ ! -s $DFO_BIN_DIR/$PGI_SCORE ]
		then
			echo "***ERROR: $DFO_BIN_DIR/$PGI_SCORE does not exist. Check config file!"
			exit
		else
# modified: BWo v1.2

			THRESH=`$DFO_BIN_DIR/$PGI_SCORE $SETUP $DET1`
	
			THRESH1=`echo $THRESH | awk '{print $1}'`
			THRESH2=`echo $THRESH | awk '{print $2}'`
		fi
	fi
fi

# stat: thresholds from statistics (S_TYPE=stat)
if [ $S_TYPE = stat ]
then
	if [ -s $DFO_TREND_DIR/reports/statFiles/stat_text_${HC_REPORT} ]
	then
		if [ "Q$HC_FACTOR" = Q ]
		then
			HC_FACTOR=1
		fi

		if [ "Q$HC_PINDEX" = "Q" ]
		then
			echo "  ${QC}: *** ERROR: \$THRESH1 = HC, but no \$HC_PINDEX configured (HC_REPORT = ${HC_REPORT}). Can't do scoring."
			exit -1
		fi
		THRESH1=`grep "^${HC_PINDEX} " $DFO_TREND_DIR/reports/statFiles/stat_text_${HC_REPORT} | grep " ${QC}$" | awk '{print $6,$7,$8}' | grep -v none | awk '{print $2*factor}' factor=$HC_FACTOR`
		THRESH2=`grep "^${HC_PINDEX} " $DFO_TREND_DIR/reports/statFiles/stat_text_${HC_REPORT} | grep " ${QC}$" | awk '{print $6,$7,$8}' | grep -v none | awk '{print $3*factor}' factor=$HC_FACTOR`
	else
		echo "*** ERROR: no stat_text_${HC_REPORT} found for $QC. Configuration problem?"
	fi
fi

HC_REPORT_LINK=`grep -e "[[:space:]]$M[[:space:]]" $JTMP_DIR/list_matchkeys1 | sed "s/#.*//" | awk '{print $6}' | sed "/^$/d"`

if [ Q$HC_REPORT_LINK != Q ]
then
	echo $HC_REPORT_LINK >> $JTMP_DIR/list_hc_reports
fi

if [ $INVESTIGATE = YES ]
then
# MATCH_LINE: complete line matching the configuration
	MATCH_LINE=`grep "[[:space:]]$M[[:space:]]" $JTMP_DIR/list_matchkeys1`
# MATCH_LINE2: edited match line, for HC, with report marked by '#' and hc_factor by 'hc_fact'
	MATCH_LINE2=`echo $MATCH_LINE | awk '{ if ($4 == "HC") {print $2,$3,$4,$5,$6,"#"$7,"hc_fact:"$8}}' | sed "s/hc_fact:$/hc_fact:1/"`
	HC_REPORT=`echo $MATCH_LINE | awk '{ if ($4 == "HC") {print $6}}'`
	MATCH_LINE_NUMBER2=""

	if [ Q$HC_REPORT != Q ]
	then
		HC_PLOT=`echo $MATCH_LINE | awk '{print $7}'`
		QC1_PAR=`echo $MATCH_LINE   | awk '{print $1}'`
		PLOT_NAME=`grep "^PLOT_NAME" $DFO_CONFIG_DIR/$HCTOOL_NAME/config.tp_$HC_REPORT | awk '{print $2,$3}' | grep "^${HC_PLOT}[[:space:]]" | awk '{print $2}'`
		PARSET_NAME=`grep "^PARSET_NAME" $DFO_CONFIG_DIR/$HCTOOL_NAME/config.tp_$HC_REPORT | grep -v OPSLOG | grep "[[:space:]]${PLOT_NAME}[[:space:]]" | grep "[[:space:]]$QC1_PAR[[:space:]]" | awk '{print $3}'`
# multi-param sets: could be more than one match
		MATCH_LINE_NUMBER2=`grep -n "^SYMBOLS" $DFO_CONFIG_DIR/$HCTOOL_NAME/config.tp_$HC_REPORT | grep "[[:space:]]$PARSET_NAME[[:space:]]" | sed "s/:SYMBOLS.*//" | tr "\012" " " | sed "s/ $//" | sed "s/ /or/"g`
	fi

# MATCH_LINE_edited: prepared to find the line in the config file
	MATCH_LINE_edited=`echo "QC_PARAM $RAW_TYPE $MATCH_LINE" | sed "s/ /[[:space:]]\*/"g`
	MATCH_LINE_NUMBER=`grep -n "^$MATCH_LINE_edited" $CONFIG | sed "s/:QC_PARAM.*//"`
	sed -i -e "/HC HC/s|${MATCH_LINE}|<a title=\"$CONFIG1\"><font color=blue>$MATCH_LINE_NUMBER</a><a title=\"config.tp_$HC_REPORT\"><font color=green>/$MATCH_LINE_NUMBER2</font></a>: </font><font color=red>$MATCH_LINE2 thresh:$THRESH1,$THRESH2</font>|" $TMP_DIR/sq_investigate
	sed -i -e "s|${MATCH_LINE}|<a title=\"$CONFIG1\"><font color=blue>$MATCH_LINE_NUMBER: </font></a><font color=red>&</font>|" $TMP_DIR/sq_investigate
	sed -i -e "s| HC HC | HC: config.tp_|" $TMP_DIR/sq_investigate
fi
}

# =========================================================================
# 0.5 getFactSheet: find documentation about qc1 parameter
#     implemented: find comment line in QC1 database
#     qc1_columns is a technical QC1_db table that stores the description of each 
#     configured column. It is not accessable via the QC1_db interfaces but 
#     to the isql query.
# =========================================================================
getFactSheet(){
rm -f $JTMP_DIR/sc_facts_query
for QC in `cat $JTMP_DIR/list_QCpars2`
do
	cat >> $JTMP_DIR/sc_facts_query <<EOT
SELECT
	"$QC", description	
FROM
	qc1..qc1_columns
WHERE
	tname = "${QC1_TABLE}"
AND
	name = "$QC"
GO
EOT
done

rm -f $JTMP_DIR/sc_facts

isql -Uqc -P`cat $QC1_PWD` -S${QC1_SERVER} -w999 -i $JTMP_DIR/sc_facts_query |\
 grep -v affected |\
 grep -v "\-\-\-" |\
 grep -v description |\
 sed "/^$/d" |\
 sed "s/   //g" |\
 sed "s/^ //" > $JTMP_DIR/sc_facts

DOCU_LINK=`grep "^QC_DOCU[[:space:]]" $CONFIG | grep "[[:space:]]${RAW_TYPE}[[:space:]]" | awk '{print $3}'`
if [ Q$DOCU_LINK != Q ]
then
	DOCU_LINK="<font size=+1>&rarr;</font> <a href=${QC_DOCU_URL}/${DFO_INSTRUMENT}/${DOCU_LINK} title=\"technical documentation about this QC report\" target=_blank>Documentation</a> about this QC report"
else
	DOCU_LINK="&nbsp;"
fi

DOCU_EXAMPLE=`grep "^QC_DOCU[[:space:]]" $CONFIG | grep "[[:space:]]${RAW_TYPE}[[:space:]]" | awk '{print $5}'`
if [ Q$DOCU_EXAMPLE != Q ]
then
	DOCU_EXAMPLE="<a href=${WEB_SERVER_URL}/${DFO_INSTRUMENT}/plots/${DOCU_EXAMPLE} target=_blank><img src=${WEB_SERVER_URL}/${DFO_INSTRUMENT}/plots/${DOCU_EXAMPLE} width=50 height=50 border=1 title=\"QC report example\" align=texttop></a>"
else
	DOCU_EXAMPLE="[none configured so far]"
fi

QC_TUTORIAL=`grep "^QC_DOCU[[:space:]]" $CONFIG | grep "[[:space:]]${RAW_TYPE}[[:space:]]" | awk '{print $4}'`
if [ Q$QC_TUTORIAL = Qnone ]
then
	QC_TUTORIAL="&nbsp;"
elif [ Q$QC_TUTORIAL != Q ]
then
	QC_TUTORIAL="<font size=+1>&rarr;</font> <a href=${QC_TUTORIAL_URL}/${DFO_INSTRUMENT}/qc/${QC_TUTORIAL} target=_blank title=\"QC tutorial in the QC web pages\">Tutorial</a> about HC plot(s) <font color=#666666><i>(there might be more related documentation pages around)</i></font>"
else
	QC_TUTORIAL="&nbsp;"
fi

INSTR_MANUAL=`grep "^INSTR_MANUAL[[:space:]]" $CONFIG | awk '{print $2}'`
if [ Q$INSTR_MANUAL != Q ]
then
	INSTR_MANUAL="<a href=$INSTR_MANUAL target=_blank title=\"$DFO_INSTRUMENT instrument manual\">Instrument manual</a> |"
else
	INSTR_MANUAL="[instrument manual not yet configured]"
fi

PIPE_MANUAL=`grep "^PIPE_MANUAL[[:space:]]" $CONFIG | awk '{print $2}'`
if [ Q$PIPE_MANUAL != Q ]
then
	PIPE_MANUAL="<a href=$PIPE_MANUAL target=_blank title=\"pipeline manual\">Pipeline manual</a> "
else
	PIPE_MANUAL="[pipeline manual not yet configured]"
fi

if [ -s $JTMP_DIR/list_HCplots ]
then
	FIRST_HC=`cat $JTMP_DIR/list_HCplots | awk '{print $2}' | head -1`
	for HC in `cat $JTMP_DIR/list_HCplots | awk '{print $2}'`
	do
		HC_TITEL=`grep " $HC" $JTMP_DIR/list_HCplots | awk '{print $1}'`
		if [ $HC != "$FIRST_HC" ]
		then
			HC_TAG=`echo "$HC_TAG  | <a href=\"${HC_URL}/${HC}\" title=\"Health Check plot\" target=_blank>${HC_TITEL}</a>"`
		else
			HC_TAG=`echo "<a href=\"${HC_URL}/${HC}\" title=\"Health Check plot\" target=_blank>${HC_TITEL}</a>"`
		fi
	done
else
	HC_TAG="<i>none</i>"
fi

cat > $JTMP_DIR/sc_fact_sheet <<EOT
<html>
<head>
<title> Fact sheet for ${DFO_INSTRUMENT} QC report $RAW_TYPE </title>
</head>
<body>
<table border=0 cellspacing="5" cellpadding="1" width=700>
<tr bgcolor="#CCCCCC">
<td align=left valign=top>
<b><font color=$QDS_COLOR size=+1>QC documentation system:<br> Fact sheet for ${DFO_INSTRUMENT} QC report "$RAW_TYPE"</font></b>
</td>
  </tr>
  <tr bgcolor="#CCCCCC"><td>
    <font size=2><font color=${QDS_COLOR}><b>QC report.</b></font> Example: $DOCU_EXAMPLE <font color=#666666><i>(there might be more, check out documentation about this QC report)</i> </font><br>
${DOCU_LINK} </font>
    </td></tr> 
  <tr bgcolor="#CCCCCC"><td>
    <font size=2><font color=${TITLE_COLOR}><b>HC plot(s).</b></font> Related ${DFO_INSTRUMENT} HC plot(s): 
$HC_TAG
<br>
$QC_TUTORIAL </font>
    </td></tr>
  <tr bgcolor="#CCCCCC"><td>
    <font size=2><font color=${TITLE_COLOR}><b>Scoring.</b></font> The $RAW_TYPE scoring is based on QC1 parameters from <br><font size=+1>&rarr;</font> QC1 database table <tt>${QC1_TABLE} </tt> 
 (<a href="${ARC_URL}?action=qc1_browse_table&table=${QC1_TABLE}" title="QC1 ${QC1_TABLE} browser" target=_blank>browser</a>
| <a href="${ARC_URL}?action=qc1_plot_table&table=${QC1_TABLE}" title="QC1 ${QC1_TABLE} plotter" target=_blank>plotter</a>).<br>
Configured thresholds for a given setup become visible if you hover the mouse over the score symbol in the score report. 
The following QC1 parameters are currently scored:</font>
    </td></tr> 
  <tr bgcolor="#CCCCCC"><td>
    <table bgcolor=#FFFFFF width=100%>
EOT
cat $JTMP_DIR/sc_facts | sed "s| |</td><td bgcolor=#CCCCCC><font size=2>|" | sed "s|^.*|<tr><td bgcolor=#CCCCCC><font size=2>&</td></tr>|" >> $JTMP_DIR/sc_fact_sheet

cat >> $JTMP_DIR/sc_fact_sheet <<EOT
    </table>
    </td>
  </tr>

  <tr bgcolor="#CCCCCC"><td>
     <font size=2>see also: <font size=+1>&rarr;</font> $INSTR_MANUAL $PIPE_MANUAL </font>
  </td></tr>

</table>
</body>
</html>
EOT

ssh -o BatchMode=yes $DFO_WEB_SERVER "bin/webDircheck ${INSTR_DIR}/factsheets"
scp -o BatchMode=yes $JTMP_DIR/sc_fact_sheet ${DFO_WEB_SERVER}:${INSTR_DIR}/factsheets/factsheet_${RAW_TYPE}.html 1>/dev/null
}

# =========================================================================
# 0.6 php script score_cgi.php
#     This script is created with option -S
#     To replace all '$' by '\$': sed "s/\$/\\$/g"
# =========================================================================
makePHP(){
cat > $TMP_DIR/score_cgi.php <<EOT
<?php
/*
This php script has been created by ${TOOL_NAME} v$TOOL_VERSION
It parses the input of the 'explore' form of the score report and transforms it into a plotter interface
to the QC1 database.
Note: for MEF files, the detector IDs either come separated by '_' or (like for OMEGACAM) by "!". The
php script recognizes these two cases which are needed for backwards compatibility and support for
detector IDs 'ESO_CCD_68' as used by OMEGACAM.
*/

// qcParams has all parameters in one array element
\$params = explode(" ",\$_POST['qcParam'][0]);

// exit if not found
if (!isset(\$_POST['qcParam'][0])) {
  die ('<img src=/observing/dfo/quality/ALL/img/php-warning.png height=30 width=30><font size=2>Error, no input data found.</font>');
}

\$abname = \$params[0];
\$table  = \$params[1];
\$qcPar  = \$params[2];
\$ymin   = \$params[3];
\$ymax   = \$params[4];
\$from   = \$params[5];
\$to     = \$params[6];
\$mark_lower = \$params[7];
\$mark_upper = \$params[8];
\$mark_mjd   = \$params[9];

// mef keys
\$allmef	    = \$params[10];

// \$allmef: all mef parameters in one string
// \$mefparams: array with all mef parameters
\$ocamcheck = substr_count(\$allmef,"!");
if (\$ocamcheck != 0)
 { 
\$mefparams = explode("!",\$allmef);
 }
else
 {
\$mefparams = explode("_",\$allmef);
 }

if (!isset(\$allmef)) {
  die ('<img src=/observing/dfo/quality/ALL/img/php-warning.png height=30 width=30><font size=2>Error, no MEF input data found.</font>');
}

\$nmef = \$mefparams[0]; // number of chip parameters incl. AVG and RMS, or n_chips+2 if no AVG and RMS
\$mef = \$params[11];

// setup keys: up to 5 ...
\$filter1 = \$params[12];
\$value1  = \$params[13];
\$filter2 = \$params[14];
\$value2  = \$params[15];
\$filter3 = \$params[16];
\$value3  = \$params[17];
\$filter4 = \$params[18];
\$value4  = \$params[19];
\$filter5 = \$params[20];
\$value5  = \$params[21];
\$filter6 = \$params[22];
\$value6  = \$params[23];
// add more filter/values as required

// assemble the interface
echo <<<EOT
<html>
<head>
<title>QuickPlot interface</title>
</head>

<body>
<table width=500 border="0" cellspacing="2" cellpadding="1">
  <tr align=left valign=top>
    <td colspan=2><font size="3"><b>QuickPlot interface</b></font><br><br></td>
  </tr>
  <tr>
    <td colspan=2><font size="2"><i>Explore here the behaviour of </i>\$qcPar<i> in the context of other data points, with choices about time range and plot range.
  Have the full interface capabilities <a href=${ARC_URL}?action=qc1_plot_table&table=\$table>here</a></i>.<p>
</font></td>
  </tr>
  <tr>
    <td width=150 nowrap align=left><font size="2">Selection for:</td>
    <td align=left><b><font size=2>\$abname</font></b></td>
  </tr>
  <tr align=left valign=top>
    <td nowrap align=left><font size="2">QC1 parameter:</td>
    <td><b><font size=2>\$qcPar</font></b></td>
  </tr>
</table>
</p>

<form action="${ARC_URL}" method="get">
<table border="0" cellpadding="1" cellspacing="1" style="font-size:x-small;">
  <tr>
    <td rowspan="99" align="left" valign="top" bgcolor="#FFFFFF"><font size="2"><b>Fixed plot parameters:</b></font></td>
    <td bgcolor=#eeeeee align=right>QC1 table: &nbsp;</td>
<td bgcolor=#eeeeee><a href=${ARC_URL}?action=qc1_plot_table&table=\$table>\$table</td></tr>
  </tr>
  <tr bgcolor=#eeeeee><td colspan=2><i>Plot items:</i></td></tr>
  <tr bgcolor=#eeeeee><td align=right>xaxis: &nbsp;</td><td>mjd_obs</td></tr>
  <tr bgcolor=#eeeeee><td align=right>yaxis: &nbsp;</td><td>\$qcPar</td></tr>
  <tr bgcolor=#eeeeee><td colspan=2><i>Setup keys (de-select to plot all values):</i></td></tr>
EOT;

// use \$nmef to let the script know how many to expect ...
// nmef=1: single detector
\$nmef2 = \$nmef-2;
\$nmef1 = \$nmef-1;
\$nmef_avg  = \$mefparams[\$nmef1]; // 3 to become \$nmef1!

// MEF row: only for MEF=YES; treatment depends on AVG=NO/YES
//+++++++++++ first MEF cell +++++++++++++++++++++++
if (\$nmef != "1") 
 {
	echo <<<EOT
    <tr bgcolor=#eeeeee><td valign=top>
  <img src="../img/mef.png" height=20 width=22 title="this is multi-detector data">&nbsp; MEF data<br>
EOT;
	if (\$nmef_avg == "AVG")
	{
		echo " <i>[AVG: average; RMS: rms]</i>";
        }

	echo "  </td>";

//+++++++++++ second MEF cell +++++++++++++++++++++++

	echo "  <td>";

	if (\$nmef_avg == "AVG")
 	{
		echo " <input type=checkbox name=\"filter_\$mef\" value=\"AVG\"><a title=\"averaged values across all detectors\">AVG</a>, OR select a \$mef:" ;
 	}
	else
 	{
		echo " Select a \$mef: &nbsp; &nbsp; &nbsp; &nbsp;" ;
	}

// options: any ...
	echo <<<EOT
<select name="filter_\$mef">
 <option value="" selected>any</option>
EOT;

// ... plus all detectors
	\$i = 1;
	while (\$i <= \$nmef2):
  		echo <<<EOT
 <option value="\$mefparams[\$i]">\$mefparams[\$i]</option>
EOT;
  	\$i++;
	endwhile;

// ... plus AVG and RMS if \$nmef_avg=AVG 
	if (\$nmef_avg == "AVG")
 	{
		echo <<<EOT
 <option value="AVG">AVG</option>
 <option value="RMS">RMS</option>

</select>
<br><i>[If you select a \$mef, de-select AVG. <br>
        Thresholds apply to first detector.]</i>
EOT;
	}
	else
	{
		echo <<<EOT

</select>
<br><i>[Thresholds apply to first detector.]</i>
    </td></tr>
EOT;
	}
}
//+++++++++++ end of MEF row +++++++++++++++++++++++

if (\$filter1 != "")
 {
echo <<<EOT
  <tr bgcolor=#eeeeee><td align=right>\$filter1: &nbsp;</td><td><input type=checkbox name="filter_\$filter1" value="\$value1" checked>\$value1</td></tr>
EOT;
}

if (\$filter2 != "")
 {
echo <<<EOT
  <tr bgcolor=#eeeeee><td align=right>\$filter2: &nbsp;</td><td><input type=checkbox name="filter_\$filter2" value="\$value2" checked>\$value2</td></tr>
EOT;
}

if (\$filter3 != "")
 {
echo <<<EOT
  <tr bgcolor=#eeeeee><td align=right>\$filter3: &nbsp;</td><td><input type=checkbox name="filter_\$filter3" value="\$value3" checked>\$value3</td></tr>
EOT;
}

if (\$filter4 != "")
 {
echo <<<EOT
  <tr bgcolor=#eeeeee><td align=right>\$filter4: &nbsp;</td><td><input type=checkbox name="filter_\$filter4" value="\$value4" checked>\$value4</td></tr>
EOT;
}

if (\$filter5 != "")
 {
echo <<<EOT
  <tr bgcolor=#eeeeee><td align=right>\$filter5: &nbsp;</td><td><input type=checkbox name="filter_\$filter5" value="\$value5" checked>\$value5</td></tr>
EOT;
}

if (\$filter6 != "")
 {
echo <<<EOT
  <tr bgcolor=#eeeeee><td align=right>\$filter6: &nbsp;</td><td><input type=checkbox name="filter_\$filter6" value="\$value6" checked>\$value6</td></tr>
EOT;
}

// add more such conditions for filter7 etc.

// mark_lower and mark_upper not null:
if (\$mark_lower != "null")
 {
	echo <<<EOT
  <tr bgcolor=#eeeeee><td colspan=2><i>Special (displayed in blue if selected):</i></td></tr>
EOT;
	if (\$nmef != "1")
 	 {
		echo <<<EOT
		<tr bgcolor=#eeeeee><td align=right>score thresholds &nbsp;<br> <i>(first detector)</i>: &nbsp;</td><td>
EOT;
	 }
	else
	{
		echo <<<EOT
  <tr bgcolor=#eeeeee><td align=right>score thresholds: &nbsp;</td><td>
EOT;
	}
	echo <<<EOT
    <input type=checkbox name="mark_lower" value="\$mark_lower" checked>
    <input type=hidden name="mark_upper" value="\$mark_upper">
    \$mark_lower, \$mark_upper</td></tr>
EOT;
}

// if (\$mark_lower = "null")

else
 {
echo <<<EOT
  <tr bgcolor=#eeeeee><td colspan=2><i>Special (displayed in blue if selected):</i></td></tr>
  <tr bgcolor=#eeeeee><td align=right><font color=#AAAAAA>score thresholds: &nbsp;</font></td><td>
    <input type=hidden name="mark_lower" value="\$mark_lower">
    <input type=hidden name="mark_upper" value="\$mark_upper">
    <font color=#AAAAAA>not set</font></td></tr>
EOT;
}

echo <<<EOT
  <tr bgcolor=#eeeeee><td align=right>marked mjd <i>(in blue)</i>: &nbsp;</td><td>\$mark_mjd</td></tr>
  <tr><td colspan=2>&nbsp;</td></tr>
</table>

          <input type=hidden name="action"     value="qc1_plot_plot">
          <input type=hidden name="table"      value="\$table">
          <input type=hidden name="xaxis"      value="mjd_obs">
          <input type=hidden name="yaxis"      value="\$qcPar">
          <input type=hidden name="format"     value="gif">
          <input type=hidden name="from_stat"  value="same as plot">
          <input type=hidden name="to_stat"    value="same as plot">
          <input type=hidden name="xmin"       value="auto">
          <input type=hidden name="outliers"   value="on">
          <input type=hidden name="xmax"       value="auto">
          <input type=hidden name="mark_mjd"   value="\$mark_mjd">

<font size=2>
<font size="2"><b>Dynamic plot parameters:</b></font><br>
Select time range, plot range and average options for parameter <b>\$qcPar</b>:<br>
[<i>Have the full interface capabilities <a href=${ARC_URL}?action=qc1_plot_table&table=\$table>here</a></i>]</font>

<table cellpadding="2" cellspacing="2" border="0" style="font-size:small;">
  <tr bgcolor="#99FF99">
    <td valign="top" align="right"><b>from:</b></td>
    <td colspan="2" bgcolor=#CCCCCC><input name="from" value="\$from" size=12></td>
    <td valign="top"><b>to:</b></td>
    <td bgcolor="#CCCCCC"><input name="to" value="\$to" size=12></td>
  </tr>

 <tr bgcolor="#99FF99">
    <td valign="top" align="right"><b>y_min:</b></td>
    <td colspan="2" bgcolor=#CCCCCC>
      auto <input type=checkbox name="ymin" value="auto"> OR value:
      <input name="ymin" value="\$ymin" size=6><br>
      <i><font size=1>(choose 'auto' to include ALL data points)</i></td>
    <td valign="top"><b>y_max:</b></td>
    <td bgcolor="#CCCCCC">
      auto <input type=checkbox name="ymax" value="auto"> OR value:   
      <input name="ymax" value="\$ymax" size=6><br>
      <i><font size=1>(choose 'auto' to include ALL data points)</i></td>
  </tr>

  <tr bgcolor="#99FF99">
    <td align="right"><b>average:</b></td>
    <td nowrap bgcolor="#CCCCCC"><input type="radio" name="avg" value="none">none</td>
    <td nowrap bgcolor="#CCCCCC"><input type="radio" name="avg" value="mean" checked>mean</td>
    <td nowrap bgcolor="#CCCCCC"><input type="radio" name="avg" value="median">median</td>
    <td nowrap bgcolor="#CCCCCC"><input type="radio" name="avg" value="linfit">least-square</td>
  </tr>

  <tr bgcolor="#99FF99">
    <td align="right"><b>special:</b></td>
    <td nowrap bgcolor="#CCCCCC" colspan=4><input type="checkbox" name="civil_dates" checked>civil dates</td>
  </tr>

  <tr><td>&nbsp;</td></tr>
  <tr>
    <td><input type="submit" value="Plot" name="plot"></td>
    <td><input type="submit" value="Get Data" name="dump_data"></td>
    <td><input type="reset" value="Reset" name="reset"></td>
    </td>
  </tr>
</table>
</form>
</html>
EOT;
exit;
EOT
}

if [ $CREATE_PHP = YES ]
then
	echo "This option creates the php script for the QuickPlot interface. It will not automatically overwrite the 
operational script. Nonetheless, it should be normally called only in coordination with the Group Head, 
except for emergencies.

Please confirm that you understand this restriction and still want to continue (yEs/N) (N):" 
	read UNDERSTAND_YN
	if [ "Q$UNDERSTAND_YN" != QyEs ]
	then
		cleanup
		exit
	fi

	makePHP
	scp -o BatchMode=yes $TMP_DIR/score_cgi.php ${DFO_WEB_SERVER}:${QC_DIR}/score_cgi.php_uploaded
	echo "You have created score_cgi.php and uploaded it to 
${DFO_WEB_SERVER}:${QC_DIR}/score_cgi.php_uploaded . The local version resides under
$TMP_DIR/score_cgi.php .
Login there and move the file to score_cgi.php to make it operational. Hit return:"
	read input
	cleanup
	exit
fi	

# =========================================================================
# 1. main part
# 1.1 get info from AB
# =========================================================================

if [ ! -s $DFO_AB_DIR/$AB ]
then
	echo "***ERROR: $AB not found in \$DFO_AB_DIR. Exit."
	cleanup
	exit -1
fi

ALOG=`echo $AB | sed "s/.ab/.alog/"`
TLOG=`echo $AB | sed "s/.ab/.tlog/"`
PLOG=`echo $AB | sed "s/.ab/.rblog/"`
HLOG=`echo $AB | sed "s/.ab/.html/"`
rm -f $DFO_AB_DIR/$TLOG $DFO_AB_DIR/$HLOG

MJD_OBS=`grep   "^MJD-OBS[[:space:]]"	$DFO_AB_DIR/$AB | awk '{print $2}'`
RAW_TYPE=`grep  "^RAW_TYPE"	 	$DFO_AB_DIR/$AB | awk '{print $2}'`
PROD_ROOT_NAME=`grep  "^PROD_ROOT_NAME"	$DFO_AB_DIR/$AB | awk '{print $2}'`
DATE=`grep 	"^DATE[[:space:]]"	$DFO_AB_DIR/$AB | awk '{print $2}'`
MODE=`grep 	"^DPR_CATG[[:space:]]"	$DFO_AB_DIR/$AB | awk '{print $2}'`
PROD_PATH=`grep "^PROD_PATH" 		$DFO_AB_DIR/$AB | awk '{print $2}'`
PROD_PATH=`eval "echo $PROD_PATH"`

HDR_NAME=`echo $PROD_ROOT_NAME | sed "s/r.//" | sed "s/_.*//"`

ARCFILE=`echo $AB | sed "s/_.*ab/.fits/" | sed "s/.ab/.fits/"`

# =========================================================================
# 1.2 get configuration per RAW_TYPE
# =========================================================================

# Call CONFIG_PGI before anything else
if [ Q$CONFIG_PGI != Q ]
then
	echo "- Calling \$DFO_BIN_DIR/$CONFIG_PGI ..."
	eval "$DFO_BIN_DIR/$CONFIG_PGI" | sed "s/^.*/  [$CONFIG_PGI]	&/"
fi

# QC1_TABLE must be configured and be unique per RAW_TYPE for scoring
QC1_TABLE=`grep "^QC1_TABLE"		$CONFIG | grep "[[:space:]]${RAW_TYPE}[[:space:]]" | awk '{print $3}'`
RANGE=`grep "^QC1_TABLE"		$CONFIG | grep "[[:space:]]${RAW_TYPE}[[:space:]]" | awk '{print $4}'`
TMODE=`grep "^QC1_TABLE"		$CONFIG | grep "[[:space:]]${RAW_TYPE}[[:space:]]" | awk '{print $5}'`
# BW: allow missing extensions
ALLOW_INCOMPLETE=`grep "^QC1_TABLE" 	$CONFIG | grep "[[:space:]]${RAW_TYPE}[[:space:]]" | awk '{print $6}'`

if [ "Q$QC1_TABLE" = "Q" ]
then
	echo "***WARNING: no QC1_TABLE configured in $CONFIG1 for ${RAW_TYPE}. No scoring possible."
	echo "   We create a basic HTML table and exit."
	BASIC_HTML=YES
else
	BASIC_HTML=NO
fi

if [ "Q$RANGE" = Q ] && [ $BASIC_HTML = NO ]
then
	echo "***INFO: No RANGE value configured. We use 90 as default."
	RANGE=90
fi

if [ "Q$TMODE" = "Q" ] && [ $BASIC_HTML = NO ]
then
	TMODE=CURRENT
fi

# =========================================================================
# 1.3 map key names into database column names
# =========================================================================

# list_setupkeys is required to translate key names into database column names
# isql_setup is used later in the WHERE clause of the isql query
rm -f $JTMP_DIR/list_setupkeys $JTMP_DIR/isql_setup $JTMP_DIR/list_rawmatchkeys
DB_SETUP=""

grep "^SETUP" 		$CONFIG 	| awk '{print $2,$3,$4}' > $JTMP_DIR/list_setupkeys
grep "^RAW_MATCH_KEY" 	$DFO_AB_DIR/$AB	| awk '{print $2}' 	 > $JTMP_DIR/list_rawmatchkeys

if [ -s $DFO_HDR_DIR/$DATE/${HDR_NAME}.hdr ]
then
        TPL_ID=`dfits $DFO_HDR_DIR/$DATE/${HDR_NAME}.hdr   | fitsort -d "TPL.ID" | awk '{print $2}'`
        TPL_NAME=`dfits $DFO_HDR_DIR/$DATE/${HDR_NAME}.hdr | fitsort -d "TPL.NAME" | sed "s/^.*${HDR_NAME}.hdr[[:space:]]//" | awk '{print $0}'`
        OBS_TPLNO=`dfits $DFO_HDR_DIR/$DATE/${HDR_NAME}.hdr | fitsort -d "OBS.TPLNO" | awk '{print $2}'`
        OBS_NTPL=`dfits  $DFO_HDR_DIR/$DATE/${HDR_NAME}.hdr | fitsort -d "OBS.NTPL" | awk '{print $2}'`
# for PHOENIX 
else
        TPL_ID="unknown"
        TPL_NAME="unknown"
	OBS_TPLNO="unknown"
	OBS_NTPL="unknown"
fi

if [ $BASIC_HTML = NO ]
then
	echo "- Read scoring configuration ..."
fi

for M in `cat $JTMP_DIR/list_rawmatchkeys`
do
# matchkey name and value
	MK_NAME=`echo  $M | sed "s/=.*//"`
	MK_VALUE=`echo $M | sed "s/^.*=//"`

# qc1db name, type and value
	DB_NAME=`grep "^$MK_NAME " $JTMP_DIR/list_setupkeys | awk '{print $2}'`
	DB_TYPE=`grep "^$MK_NAME " $JTMP_DIR/list_setupkeys | awk '{print $3}'`
	DB_VALUE=`echo $M | sed "s/${MK_NAME}/filter_${DB_NAME}/"`

# optional modification (value change from MK to DB)
	DB_MODIF=`grep "^SETUP" $CONFIG | grep "[[:space:]]${MK_NAME}[[:space:]]" | sed "s/&&\$//" | sed "s/^.*&&//"`
	DB_MODIF=`echo $DB_MODIF | grep -v SETUP`

	if [ "Q$DB_MODIF" != "Q" ]
	then
		MK_VALUE=`eval "echo $MK_VALUE | ${DB_MODIF}"`
		DB_VALUE="filter_${DB_NAME}=${MK_VALUE}"
	fi

	if [ "Q$DB_NAME" != "Q" ]
	then
		DB_SETUP=`echo "${DB_SETUP}&${DB_VALUE}"`

		case $DB_TYPE in
	 	 "char"   ) DB_VALUE=`echo $DB_VALUE | sed "s/^.*=//" | sed "s/^.*/\"&\"/"` ;;
	 	 "number" ) DB_VALUE=`echo $DB_VALUE | sed "s/^.*=//"` ;;
		 *        ) echo "***WARNING: $DB_NAME: DB_TYPE = $DB_TYPE is illegal (must be char or number); assumed: number"
			    DB_VALUE=`echo $DB_VALUE | sed "s/^.*=//"` ;;
		esac
		cat >> $JTMP_DIR/isql_setup <<EOT
AND
	$DB_NAME = $DB_VALUE
EOT
	fi
done

# SETUP is merged from all raw matchkeys. It can be used to fine-tune scoring per setup.
SETUP=`cat $JTMP_DIR/list_rawmatchkeys | sed "s/^.*=//" | tr "\012" "_" | sed "s/_$//"`

# =========================================================================
# 1.4 get DET_ID names if MEF
# =========================================================================

# check if DET_ID defined; if so, set MEF=YES and get all DET_VALUES for the MEF loop
DET_ID=`grep "^DET_ID"	$CONFIG | awk '{print $2}'`
if [ "Q$DET_ID" != "Q" ]
then
	MEF=YES		#MEF=YES: all possible DET_VALUES must be configured and are used for scoring.
	rm -f $JTMP_DIR/list_mefvalues	
	grep "^DET_VALUES" $CONFIG | awk '{print $2}' > $JTMP_DIR/list_mefvalues
	if [ ! -s $JTMP_DIR/list_mefvalues ]
	then
		echo "***ERROR: configuration problem, no DET_VALUES section in $CONFIG1. Exit."
		cleanup
		exit -1
	fi
else
	MEF=NO		#MEF=NO: dummy value '1' used here.
	echo "1" > $JTMP_DIR/list_mefvalues
fi

N_DET_VALUES=`cat $JTMP_DIR/list_mefvalues | wc -l`
N_DET_VALUES1=`cat $JTMP_DIR/list_mefvalues | grep -v EMPTY | wc -l`

# initialize
SCORE=0
NUMBER=0

# =========================================================================
# 2. get all QC1 parameters for scoring
# 2.1 No scoring configured: basic table only and exit
# =========================================================================

if [ $BASIC_HTML = YES ]
then
	cat > $DFO_AB_DIR/$HLOG <<EOT
<html>
<head>
<title> Score report for $AB</title>
</head>
<body>

<table border=0 cellspacing="5" cellpadding="1" width=400>
  <tr bgcolor="#CCCCCC">
    <td COLSPAN=$N_COL>
<b><font color=$TITLE_COLOR size=+1>Basic score report</font></b>
<pre><b>$AB</b> 
RAW_TYPE:	$RAW_TYPE
TPL_ID:		$TPL_ID
TPL_NAME:	$TPL_NAME
OBS_TPLNO:	$OBS_TPLNO
OBS_NTPL:	$OBS_NTPL
setup:		$SETUP 

<a href=./$AB title="view AB">AB</a> | <a href=./$ALOG title="association log">ALOG</a> | <a href=$DFS_LOG/$PLOG title="pipeline log">PLOG</a> 
[back to <a href=$DFO_MON_DIR/status_${DATE}.html>AB monitor</a>]</pre>
    </td>
  </tr>
</table>
</html>
EOT

cat > $DFO_AB_DIR/$TLOG <<EOT
<pre>
AB_NAME		$AB
RAW_TYPE	$RAW_TYPE
DATE		$DATE
MODE		$MODE

TPL_ID		$TPL_ID
TPL_NAME:	$TPL_NAME
OBS_TPLNO:	$OBS_TPLNO
OBS_NTPL:	$OBS_NTPL
SETUP		$SETUP		#made from RAW_MATCH_KEY section of AB

TOTAL_SCORE	0
TOTAL_NUMBER	0
EOT

# update AB monitor
	if [ -s $DFO_MON_DIR/status_${DATE}.html ]
	then
		echo "- Incremental update of AB monitor ..."
		getStatusAB -a $AB 2>/dev/null
	fi
	cleanup
	exit 0
fi

# =========================================================================
# 2.2 Get all QC1 parameters for scoring
# =========================================================================

echo "- Get all QC1 parameters for scoring ..."

rm -f $JTMP_DIR/list_QCpars $JTMP_DIR/list_HCplots
grep "^QC_PARAM" $CONFIG | grep "[[:space:]]${RAW_TYPE}[[:space:]]" | awk '{print $3,$4,$5,$6,$7,$8,$9,$10}' | sed "s/#.*//" > $JTMP_DIR/list_QCpars
grep "^HC_PLOT"  $CONFIG | grep "[[:space:]]${RAW_TYPE}[[:space:]]" | awk '{print $3,$4}' > $JTMP_DIR/list_HCplots

if [ ! -s $JTMP_DIR/list_QCpars ]
then
	echo "***ERROR: No QC1 parameters configured for RAW_TYPE ${RAW_TYPE}. Check $CONFIG1."
	cleanup
	exit -1
fi

# =========================================================================
# 2.3 first query: check for multiple entries for AB; 
#     if found, give a warning and take last id
# 2.3.1 create query
# =========================================================================

case $MEF in
 "NO"  ) ID_STRING="id"          
	 MULTIPLICITY=1 ;;
 "YES" ) ID_STRING="id, $DET_ID" 
	 MULTIPLICITY=`cat $JTMP_DIR/list_mefvalues | grep -v EMPTY | wc -l` ;;
esac

cat > $JTMP_DIR/qcdb_query <<EOT
SELECT
	$ID_STRING
FROM
	qc1..$QC1_TABLE
WHERE
	pipefile like "${PROD_ROOT_NAME}%"
AND
	visible = "Y"
GO
EOT

# =========================================================================
# 2.3.2 execute query
# =========================================================================

isql -Uqc -P`cat $QC1_PWD` -S${QC1_SERVER} -w999 -i $JTMP_DIR/qcdb_query |\
  sed "1,2d" |\
  grep -v affected |\
  sed "/^$/d" |\
  sed "/^   $/d" \
  > $JTMP_DIR/qcdb_results

# =========================================================================
# 2.3.3 analyze results
# =========================================================================

CHECK_NOTFOUND=`grep "not found" $JTMP_DIR/qcdb_results`
if [ "Q$CHECK_NOTFOUND" != "Q" ]
then
	echo "***ERROR with isql query; results may be wrong or missing! Check \$JTMP_DIR/qcdb_results and \$JTMP_DIR/qcdb_query."
	echo "Error message:"
	echo $CHECK_NOTFOUND
fi

CHECK_DUPLICATE=`cat $JTMP_DIR/qcdb_results | wc -l`

if [ $CHECK_DUPLICATE -gt $MULTIPLICITY ]
then
	echo "***WARNING: $CHECK_DUPLICATE visible entries found for $AB. "; echo "***WARNING: Use qc1Hide to make entries unique. We take the last one."
elif [ $CHECK_DUPLICATE -lt 1 ]
then
	echo "***ERROR: no entry found in $QC1_TABLE for $AB. No scoring possible." 
	cleanup
	exit -1
fi

rm -f $JTMP_DIR/last_ids
sort -n $JTMP_DIR/qcdb_results | tail -$MULTIPLICITY > $JTMP_DIR/last_ids
LAST_ID=`cat $JTMP_DIR/last_ids | awk '{print $1}' | tr "\012" "," | sed "s/,$//"`

# =========================================================================
# 2.4 second query: QC1 data for the AB
# 2.4.1 create query: only visible entries; last entry (MEF: per detector)
# =========================================================================

cat > $JTMP_DIR/qcdb_query2 <<EOT
SELECT
	"mjd_obs" , convert(numeric(13,8),mjd_obs) , char(10),
EOT

case $MEF in
  "NO"  ) cat $JTMP_DIR/list_QCpars | awk '{print $1}' | sort -u | awk '{print "\""$1" \", "$1", \"DET_ID=1\", char(10),"}' | sed "$ s/(10),/(10)/" >> $JTMP_DIR/qcdb_query2 ;;
  "YES" ) cat $JTMP_DIR/list_QCpars | awk '{print $1}' | sort -u | awk '{print "\""$1" \", "$1", \"DET_ID=\", "detid", char(10),"}' detid=${DET_ID} | sed "$ s/(10),/(10)/" >> $JTMP_DIR/qcdb_query2 ;;
esac

cat >> $JTMP_DIR/qcdb_query2 <<EOT

FROM
	qc1..$QC1_TABLE
WHERE
	pipefile like "${PROD_ROOT_NAME}%"
AND
	visible = "Y"
AND
	id in
(
EOT
cat $JTMP_DIR/last_ids | awk '{print $1}' | sed "s/^.*/&,/" | sed "$ s/,//" >> $JTMP_DIR/qcdb_query2
echo ")"              >> $JTMP_DIR/qcdb_query2
echo "go"             >> $JTMP_DIR/qcdb_query2

# =========================================================================
# 2.4.2 execute query
# =========================================================================

rm -f $JTMP_DIR/qcdb_results

isql -Uqc -P`cat $QC1_PWD` -S${QC1_SERVER} -w2000 -i $JTMP_DIR/qcdb_query2 |\
  sed "1,2d" |\
  grep -v affected |\
  sed "/^$/d" |\
  sed "/^   $/d" |\
  sed "s/= /=/" |\
  awk '{print $1,$2,$3}' \
  > $JTMP_DIR/qcdb_results

# =========================================================================
# 2.5 third query, returning setup-specific values from the database
# 2.5.1 create query
# =========================================================================

TODAY=`date +%Y-%m-%d`
if [ $TMODE = CURRENT ]
then
	FROM=`qcdate $TODAY -$RANGE`
	TO=$TODAY
	mjd2=`qcdate $TODAY | awk '{print $1+1}'`
	mjd1=`echo $mjd2 $RANGE | awk '{print $1-$2}'`
elif [ $TMODE = HISTORY ]
then
	FROM1=`echo $MJD_OBS $RANGE | awk '{print $1-$2/2}'`
	FROM=`qcdate $FROM1`
	TO=`qcdate $FROM $RANGE`
	mjd1=$FROM1
	mjd2=`echo $mjd1 $RANGE | awk '{print $1+$2}'`
fi

echo "SELECT" > $JTMP_DIR/qcdb_query3 

case $MEF in
  "NO"  ) cat $JTMP_DIR/list_QCpars | awk '{print $1}' | sort -u | awk '{print "\""$1" \", "$1", \"DET_ID=1\", char(10),"}' | sed "$ s/(10),/(10)/" >> $JTMP_DIR/qcdb_query3 ;;
  "YES" ) cat $JTMP_DIR/list_QCpars | awk '{print $1}' | sort -u | awk '{print "\""$1" \", "$1", \"DET_ID=\", "detid", char(10),"}' detid=${DET_ID} | sed "$ s/(10),/(10)/" >> $JTMP_DIR/qcdb_query3 ;;
esac

cat >> $JTMP_DIR/qcdb_query3 <<EOT
FROM
	qc1..$QC1_TABLE
WHERE
EOT

if [ -s $JTMP_DIR/isql_setup ]
then
	cat $JTMP_DIR/isql_setup | sed "1,1 d" >> $JTMP_DIR/qcdb_query3
	echo "AND" >> $JTMP_DIR/qcdb_query3
fi

cat >> $JTMP_DIR/qcdb_query3 <<EOT
        visible = "Y"
AND
	mjd_obs between $mjd1 and $mjd2
go
EOT

# =========================================================================
# 2.5.2 submit query
# =========================================================================

isql -Uqc -P`cat $QC1_PWD` -S${QC1_SERVER} -w999 -i $JTMP_DIR/qcdb_query3 |\
  sed "1,2d" |\
  grep -v affected |\
  sed "/^$/d" |\
  sed "/^   $/d" |\
  sed "s/= /=/" |\
  awk '{print $1,$2,$3}' \
  > $JTMP_DIR/qcdb_results3

CHECK_ERROR=`grep "Invalid" $JTMP_DIR/qcdb_results3`
if [ "Q$CHECK_ERROR" != "Q" ]
then
	echo "***WARNING: isql error message in \$JTMP_DIR/qcdb_results3: $CHECK_ERROR. "
	echo "You may want to analyze the query in \$JTMP_DIR/qcdb_query3 ."
fi

# =========================================================================
# 3. Scoring
# 3.1 Start filling the tlog; this is used later to create the html output
# =========================================================================

echo "<pre>" >> $DFO_AB_DIR/$TLOG
echo "AB_NAME		$AB" 	  >> $DFO_AB_DIR/$TLOG
echo "RAW_TYPE	$RAW_TYPE"	  >> $DFO_AB_DIR/$TLOG
echo "DATE		$DATE"    >> $DFO_AB_DIR/$TLOG
echo "MODE		$MODE"    >> $DFO_AB_DIR/$TLOG
echo ""				  >> $DFO_AB_DIR/$TLOG

echo "TPL_ID		$TPL_ID		#read from header"		 >> $DFO_AB_DIR/$TLOG
echo "TPL_NAME	$TPL_NAME	#read from header"			 >> $DFO_AB_DIR/$TLOG
echo "OBS_TPLNO	$OBS_TPLNO	#read from header"			 >> $DFO_AB_DIR/$TLOG
echo "OBS_NTPL	$OBS_NTPL	#read from header"			 >> $DFO_AB_DIR/$TLOG
echo "SETUP		$SETUP		#made from RAW_MATCH_KEY section of AB"	 >> $DFO_AB_DIR/$TLOG
echo "QC1_TABLE	$QC1_TABLE		#table in QC1 database used for scoring" >> $DFO_AB_DIR/$TLOG
echo "time range	$FROM ... $TO	#time range used for db_avg and db_num"	 >> $DFO_AB_DIR/$TLOG

echo "" 	>> $DFO_AB_DIR/$TLOG

echo "SCORED_ID	$LAST_ID	
				#id(s) of last database entry(ies) used for scoring" 	 >> $DFO_AB_DIR/$TLOG

#echo "MULTIPLE	$CHECK_DUPLICATE		#number of visible entries; if > ${MULTIPLICITY}, you should hide the other entries" >> $DFO_AB_DIR/$TLOG

# =========================================================================
# 3.2 link(s) to HC plot(s)
# =========================================================================

echo "HTML_PLOT	<a href=\"./${HLOG}\">HTML</a>" >> $DFO_AB_DIR/$TLOG
echo "" 	           >> $DFO_AB_DIR/$TLOG
echo "#Parameter scoring:" >> $DFO_AB_DIR/$TLOG

# =========================================================================
# 3.3 date information for dynamic plots
# =========================================================================

CHECK_MJD=`grep "mjd_obs " $JTMP_DIR/qcdb_results | awk '{print $2}' | wc -l`
if [ "Q$CHECK_MJD" != "Q$N_DET_VALUES1" ] && [ $MEF_PSEUDO = NO ]
then
	echo "*** WARNING: problem with mjd_obs values. Expected: $N_DET_VALUES1, found: $CHECK_MJD ."
elif [ "Q$CHECK_MJD" != "Q$N_DET_VALUES1" ] && [ $MEF_PSEUDO = YES ]
then
	echo "*** INFO: MEF_PSEUDO=YES, only one detector checked out of $N_DET_VALUES1."
fi

if [ $MEF_PSEUDO = YES ]
then
	MJD_OBS=`grep "mjd_obs " $JTMP_DIR/qcdb_results | awk '{print $2}' | sort -u | tail -1`
else
	MJD_OBS=`grep "mjd_obs " $JTMP_DIR/qcdb_results | awk '{print $2}' | sort -u`
fi

# preserve the configured order for QC parameters
rm -f $JTMP_DIR/list_QCpars2
grep "^QC_PARAM" $CONFIG | grep "[[:space:]]${RAW_TYPE}[[:space:]]" | awk '{print $3}' > $JTMP_DIR/list_QCpars1
PREV="Q"
for QC in `cat $JTMP_DIR/list_QCpars1`
do
	if [ "$QC" != "$PREV" ]
	then
		echo $QC >> $JTMP_DIR/list_QCpars2
		PREV=$QC
	fi
done

# =========================================================================
# 3.4 start creating HTML file
# =========================================================================

N_PARAMS=`cat $JTMP_DIR/list_QCpars2 | wc -l`
N_COL=$N_PARAMS

# HLOG
case $MEF in
 "YES" ) PAR_SCORE_REP="Score report" ;;
  "NO" ) PAR_SCORE_REP="Score report" ;;
esac

#if [ Q$QCD_PLOTS != Q ]
#then
# 	QC1_DOCU_TABLINK="<a href=\"${ARC_URL}?action=qc1_browse_table&table=${QCD_PLOTS}\" title=\"HC score documentation\"><font color=${QDS_COLOR}><b>HC scores</b></font></a> | <a href=\"${ARC_URL}?action=qc1_browse_table&table=${QCD_PARAMS}\" title=\"QC1 parameter documentation\"><font color=${QDS_COLOR}><b>QC1 params</b></font></a> |"
#else
	QC1_DOCU_TABLINK=""
#fi

FS_LINK="<a href=\"${WEB_SERVER_URL}/${DFO_INSTRUMENT}/factsheets/factsheet_${RAW_TYPE}.html\" title=\"QC report fact sheet\" target=_blank><font color=${QDS_COLOR}><b>factsheet</b></font></a>"

cat > $DFO_AB_DIR/$HLOG <<EOT
<html>
<!-- created by $TOOL_NAME v.$TOOL_VERSION -->
<head>
<title> Score report for $AB</title>
<script type="text/javascript">
<!-- this script is used for switching display on/off ("explore >>") -->
function onoff () {
  if (document.getElementById)
    document.getElementById("more").style.display = "inline";
    document.getElementById("less").style.display = "none";
}
</script>

</head>

<body>

<form action="${QC_TUTORIAL_URL}/ALL/php/score_cgi.php" method="post">

<table border=0 cellspacing="1" cellpadding="1">
  <tr bgcolor="#CCCCCC">
    <td COLSPAN=$N_COL>
    <table width=100%>
      <tr><td align=left>
      <b><font color=$TITLE_COLOR size=+1>${PAR_SCORE_REP}</font></b></td>
      <td align=right><a href=${HELP_URL} title="help for this page" target=_blank><font size=2 color=${TITLE_COLOR}><b>HELP</b></font></a></td> 
      </tr></table>
<pre><b>$AB</b> 
<a title="data type">RAW_TYPE:	$RAW_TYPE</a>
<a title="template signature ID">TPL_ID:		$TPL_ID / TPL.ID </a>
<a title="template name">TPL_NAME:	$TPL_NAME / TPL.NAME</a>
<a title="template number within OB">OBS_TPLNO:	$OBS_TPLNO / OBS.TPLNO</a>
<a title="number of templates within OB">OBS_NTPL:	$OBS_NTPL / OBS.NTPL</a>

<a title="instrument setup key">setup:		$SETUP </a>
<a title="time range for trending (visible if you click the squares)">time range: 	$FROM ... $TO </a>

<a href=./$AB title="view AB">AB</a> | <a href=./$ALOG title="association log">ALOG</a> | <a href=$DFS_LOG/$PLOG title="pipeline log">PLOG</a> | <a href="${ARC_URL}?action=qc1_plot_table&table=${QC1_TABLE}" title="QC1_db ${QC1_TABLE}" target=_blank>QC1_plotter</a> | $QC1_DOCU_TABLINK $FS_LINK

[back to <a href=$DFO_MON_DIR/status_${DATE}.html>AB monitor</a>]</pre>
</td></tr>

<tr><td bgcolor=#CCCCCC COLSPAN=${N_COL}>
EOT
# links to QC1 docu tables (not enabled)
#<a href=./$AB title="view AB">AB</a> | <a href=./$ALOG title="association log">ALOG</a> | <a href=$DFS_LOG/$PLOG title="pipeline log">PLOG</a> | <a href="${ARC_URL}?action=qc1_plot_table&table=${QC1_TABLE}" title="QC1_db ${QC1_TABLE}" target=_blank>QC1_plotter</a> | $QC1_DOCU_TABLINK $FS_LINK


# some docu
if [ $MEF = YES ]
then
	echo "<b><font color=$TITLE_COLOR size=+1>1. Parameter score report</font></b><br>
     <font size=2>Scores are sorted per QC1 parameter.</font><br>
     <font size=1> <i>Hover your mouse over QC1 parameter name for short documentation.<br>
     Grey squares: unscored parameter.
     The large orange square links to a dynamic plot for all detectors.<br>
     Smaller squares link to AVG and RMS values (if configured).</i></font>" >> $DFO_AB_DIR/$HLOG
else
	echo "      <font size=1><i>Point your mouse on QC1 parameter name for short documentation.<br>
     Grey squares: unscored parameter.</i></font>" >> $DFO_AB_DIR/$HLOG
fi

if [ $INVESTIGATE = YES ]
then
	INV_COMMENT="<p><font size=1> <i>Find all score thresholds (as configured in $CONFIG1) underneath the QC parameter names. <br> The matched one is marked <font color=red>red</font>, its line number in the $TOOL_NAME  config file is marked <font color=blue>blue</font>, its line<br> number in the $HCTOOL_NAME config file <font color=green>green</font>. Dynamic scores (from the HC monitor) are marked 'HC:'<br> and display the name of the configuration file, the report number, the HC_factor and the thresholds."
	echo "<!-- INV_COMMENT -->" >> $DFO_AB_DIR/$HLOG
fi

cat >> $DFO_AB_DIR/$HLOG <<EOT
    </td>
  </tr>
EOT
  
# =========================================================================
# 3.5 create fact sheet
# =========================================================================

rm -f $JTMP_DIR/store_parstring
HC_MARK=" HC"

echo "- Create fact sheet ..."
getFactSheet

# =========================================================================
# 3.6 loop over QC1 params
# 3.6.1 start table structure
# =========================================================================

echo "- Start scoring ..."
rm -f $JTMP_DIR/explore $JTMP_DIR/sq_explore $JTMP_DIR/sq_explore1 $JTMP_DIR/sq_explore2 $TMP_DIR/sq_investigate

FIRST_QC=`cat $JTMP_DIR/list_QCpars2 | head -1`
for QC in `cat $JTMP_DIR/list_QCpars2`
do

# get description of QC parameter from sc_facts_query
	QC_DESCR=`grep "^$QC " $JTMP_DIR/sc_facts | sed "s/^${QC} //"`

# link to QC1_docu database (not implemented)
#	if [ Q$QCD_PLOTS != Q ]
#	then
#		QCD_PLOTS_QUERY=`grep  "^#QCD_PLOTS" $DFO_BIN_DIR/$TOOL_NAME | awk '{print $2}' | sed "s/^.*/field_&=&/" | tr "\012" "&" | sed "s/\&$//"`
#		QCD_PARAMS_QUERY=`grep "^#QCD_PARAMS" $DFO_BIN_DIR/$TOOL_NAME | awk '{print $2}' | sed "s/^.*/field_&=&/" | tr "\012" "&" | sed "s/\&$//"`
# from/to are dummies for "whole time range"; we need later expansion of #1 to %231 but this doesn't work with java code, so we cannot use openWin2
#		QCD_PLOTS_LINK="DOCUSTART<a href=${ARC_URL}?action=qc1_browse_get&table=${QCD_PLOTS}&${QCD_PLOTS_QUERY}&from=2000-10-01&to=2060-12-31&output=html&filter_QC1_parameter=$QC&filter_scored=Y&filter_HC_report=HCREPORT&filter_HC_plot_index=HCPLOTINDEX></b><font size=1 color=${TITLE_COLOR} DOCUTAG</font></a><b>DOCUEND"
#		QCD_PARAMS_LINK="PARAMSTART<a href=${ARC_URL}?action=qc1_browse_get&table=${QCD_PARAMS}&${QCD_PARAMS_QUERY}&from=2000-10-01&to=2060-12-31&output=html&filter_QC1_table=QC1TABLE&filter_QC1_parameter=$QC></b><font size=1 color=${QDS_COLOR} title=\"documentation about this QC parameter\">[?]</font></a><b>PARAMEND"
#	else
#		QCD_PLOTS_LINK=""
#		QCD_PARAMS_LINK=""
#	fi

# prepare for 'HC' marking if thresholds have been taken from HC results 
	cat > $JTMP_DIR/sq_explore <<EOT

        <td VALIGN="top" ALIGN="center">

<!-- dynamic plot for $QC -->
<table bgcolor="#CCCCCC" width="${TABLE_SIZE}" border="0" cellpadding="1" cellspacing="1">
  <tr>
    <td colspan="$MEF_NCOL1" nowrap>
      <b><font size=2><a title="${QC_DESCR}">${QC}</a> HC_MARK</font></b>
    </td>
  </tr>

EOT
	cat $JTMP_DIR/sq_explore >> $JTMP_DIR/sq_explore1
	cat $JTMP_DIR/sq_explore >> $JTMP_DIR/sq_explore2

	PARAM_SCORE=0

# =========================================================================
# 3.6.2 read info for dynamic QC1 plots from configuration
# =========================================================================

# CHECK_DYN checks for special configuration of YMIN and YMAX in dynamic plots
# not empty: we read YMIN/YMAX from the ^QC_PLOT configuration
#            if YMAX < VALUE, we calculate YMAX from $THRESH2
#     empty: we calculate YMIN/YMAX from $THRESH1/$THRESH2 (in 3.5.8)
	CHECK_DYN=`grep "^QC_PLOT" $CONFIG | grep "[[:space:]]${RAW_TYPE}[[:space:]]" | grep "[[:space:]]$QC[[:space:]]" | sed "s/#.*//"`

	if [ "Q$CHECK_DYN" != "Q" ]
	then
		YMIN=`echo $CHECK_DYN | awk '{print $4}' | sed "s/AUTO/auto/" | sed "s/NONE/auto/"`
		YMAX=`echo $CHECK_DYN | awk '{print $5}' | sed "s/AUTO/auto/" | sed "s/NONE/auto/"`
	else
		YMIN=auto
		YMAX=auto
	fi

# mark for later determination if 'auto'
	if [ $YMIN = auto ]
	then
		YMIN1=auto
	else
		YMIN1=config
	fi

	if [ $YMAX = auto ]
	then
		YMAX1=auto
	else
		YMAX1=config
	fi

# =========================================================================
# 3.6.3 loop over DET_ID values; MEF=NO: DET_ID=1 always
# =========================================================================

	if [ $INVESTIGATE = YES ]
	then
		echo "     <td bgcolor=#DDDDDD nowrap><font size=1>" >> $TMP_DIR/sq_investigate
	fi

	rm -f $JTMP_DIR/list_hc_reports

	FIRST_DET=`cat $JTMP_DIR/list_mefvalues | grep -v EMPTY | head -1`

# check for DET=ANY	
	CHECK_ANY=`grep "^$QC " $JTMP_DIR/list_QCpars | awk '{print $3}' | grep "DET=ANY"`

	for DET in `cat $JTMP_DIR/list_mefvalues | grep -v EMPTY`
	do
		VALUE=`grep "^${QC} " $JTMP_DIR/qcdb_results | grep "DET_ID=$DET$" | awk '{print $2}'`
		if [ "Q$VALUE" = "Q" ]
		then
			echo "*** ERROR: no qc1db value found for DET_ID=$DET and $QC. Problem with configuration or database?"
			VALUE=X
		fi

		SUM_VALUE=`grep "^${QC} " $JTMP_DIR/qcdb_results3 | grep "DET_ID=$DET$" | awk '{sum +=$2} END {print sum}'`
		NUM_VALUE=`grep "^${QC} " $JTMP_DIR/qcdb_results3 | grep "DET_ID=$DET$" | wc -l`
		if [ $NUM_VALUE -gt 0 ]
		then
			AVG_VALUE=`echo $SUM_VALUE $NUM_VALUE | awk '{print $1/$2}'`
		else
			AVG_VALUE="NAN"
		fi

		rm -f $JTMP_DIR/list_matchkeys
		
		if [ "Q$CHECK_ANY" != "Q" ]
		then
			DET1="ANY"
		else
			DET1="$DET"
		fi

		grep "^$QC " $JTMP_DIR/list_QCpars | grep "DET=$DET1" | sed "s/#.*//" > $JTMP_DIR/list_matchkeys
		cp $JTMP_DIR/list_matchkeys $JTMP_DIR/list_matchkeys1

		if [ $INVESTIGATE = YES ]
		then
			if  [ $DET != $FIRST_DET ]
			then
				echo "<br>" >> $TMP_DIR/sq_investigate
			fi
			cat $JTMP_DIR/list_matchkeys | sed "s|^.*|&<br>|" >> $TMP_DIR/sq_investigate

			echo "
INVESTIGATE: QC_param ${QC}, det $DET ..."
			echo "  AB setup for score match:	$SETUP"
		fi

# =========================================================================
# 3.6.4 Evaluate matchkey value	and find THRESH1, THRESH2
# =========================================================================

		THRESH1=""
		THRESH2=""

		for M in `cat $JTMP_DIR/list_matchkeys | awk '{print $2}'`
		do
			THRESH1=""
			THRESH2=""
			M1=`echo $M | cut -c1,1`

# =========================================================================
# 3.6.4.1 specific setup (not ANY, not "^")
# =========================================================================

			if [ $M != ANY ] && [ $M1 != "^" ]
			then
				SETUP_MATCH=`echo $SETUP | egrep -e "$M"`
				if [ "Q$SETUP_MATCH" != "Q" ]
				then
					if [ $INVESTIGATE = YES ]
					then
						echo "  found: specific setup match	$M"
					fi

					detThresh 
					grep -v "[[:space:]]$M[[:space:]]" $JTMP_DIR/list_matchkeys1 > $JTMP_DIR/list_matchkeys2
					mv $JTMP_DIR/list_matchkeys2 $JTMP_DIR/list_matchkeys1
					break
				fi

# =========================================================================
# 3.6.4.2 not_value: starting with "^" (special care needed to handle \^ properly)
# =========================================================================

			elif [ $M1 = "^" ]
			then
				M2=`echo $M | sed "s/\^//"`
				M3=$M
				M=`echo "\^$M2"`
				SETUP_MATCH=`echo $SETUP | grep -v $M2`
				if [ "Q$SETUP_MATCH" != "Q" ]
				then
					if [ $INVESTIGATE = YES ]
					then
						echo "  found: 'not_value' match:	not $M2"
					fi
					detThresh 
					M=$M3
					grep -v -e "[[:space:]]${M}[[:space:]]" $JTMP_DIR/list_matchkeys1 > $JTMP_DIR/list_matchkeys2
					mv $JTMP_DIR/list_matchkeys2 $JTMP_DIR/list_matchkeys1
					break
				fi

# =========================================================================
# 3.6.4.3 ANY match
# =========================================================================

			elif [ $M = "ANY" ]
			then
				if [ $INVESTIGATE = YES ]
				then
					echo "  found:	ANY match"
				fi
				THRESH1=`grep "[[:space:]]ANY[[:space:]]" $JTMP_DIR/list_matchkeys1 | grep "DET=$DET1 " | awk '{print $4}'`
				THRESH2=`grep "[[:space:]]ANY[[:space:]]" $JTMP_DIR/list_matchkeys1 | grep "DET=$DET1 " | awk '{print $5}'`
#new for S_TYPE=stat
				detThresh
				grep -v "[[:space:]]ANY[[:space:]]" $JTMP_DIR/list_matchkeys1 > $JTMP_DIR/list_matchkeys2
				mv $JTMP_DIR/list_matchkeys2 $JTMP_DIR/list_matchkeys1
				break
			fi

# =========================================================================
# 3.6.4.4 configuration didn't match: remove from list_matchkeys1
# =========================================================================

			grep -v "[[:space:]]$M[[:space:]]" $JTMP_DIR/list_matchkeys1 > $JTMP_DIR/list_matchkeys2
			mv $JTMP_DIR/list_matchkeys2 $JTMP_DIR/list_matchkeys1
		done
		
# =========================================================================
# 3.6.5 THRESH1/2 found: scoring
#	At this point, THRESH1, THRESH2 should be known; 
#	also, for score_type=stat: hc_report, hc_pindex
# =========================================================================

		QC1=`echo $QC | awk '{printf"%-10s\n",$1}'`
		if [ "Q$THRESH1" != "Q" ] && [ "Q$THRESH2" != "Q" ] && [ "Q$THRESH1" != "QHC" ] && [ "Q$THRESH2" != "QHC" ]
		then
			if [ $VALUE = X ]
			then
				if [ "Q$ALLOW_INCOMPLETE" = "QYES" ]
				then
# BW: allow missing extensions
					echo "$QC1      det=$DET        unscored" >> $DFO_AB_DIR/$TLOG
					continue
				else
# normal case: error
					SCORE=X
					RESULT=X
				fi
			else
				TEST1=`echo "$VALUE $THRESH1" | awk '{if ( $1 >= $2 ) {print "0"} else {print "1"} }'`
				TEST2=`echo "$VALUE $THRESH2" | awk '{if ( $1 <= $2 ) {print "0"} else {print "1"} }'`
				RESULT=`echo $TEST1 $TEST2             | awk '{print $1+$2}'`
				SCORE=`echo $SCORE $RESULT             | awk '{print $1+$2}'`
				PARAM_SCORE=`echo $PARAM_SCORE $RESULT | awk '{print $1+$2}'`
				NUMBER=`echo $NUMBER | awk '{print $1+1}'`
			fi

			if [ Q$HC_REPORT != Q ]
			then
				HC_TAG=" hc_report=$HC_REPORT"
			else
				HC_TAG=""
			fi
			echo "$QC1	det=$DET	score=$RESULT	thresh1=$THRESH1,val=$VALUE,thresh2=$THRESH2 db_avg=$AVG_VALUE db_num=$NUM_VALUE score_type=${S_TYPE}${HC_TAG}" >> $DFO_AB_DIR/$TLOG

			if [ $INVESTIGATE = YES ]
			then
				echo "  thresholds: $THRESH1 $THRESH2; val=$VALUE; score_type=${S_TYPE}; ${HC_TAG}"
			fi

# =========================================================================
# 3.6.6 ingest scores into qc1_score
# =========================================================================

			rm -f $JTMP_DIR/sc_problems
			qc1Ingest -table qc1_score -civil_date $DATE -mjd_obs $MJD_OBS -ab_name $AB -arcfile $ARCFILE -instrument $DFO_INSTRUMENT -qc1_table $QC1_TABLE -qc1_param $QC -det $DET -qc1_val $VALUE -qc1_score $RESULT -thresh1 $THRESH1 -thresh2 $THRESH2 -db_avg $AVG_VALUE -db_num $NUM_VALUE -score_type $S_TYPE 1>$JTMP_DIR/sc_problems
			NO_PROBLEMS=`grep "Successfully ingested" $JTMP_DIR/sc_problems`
			if [ "Q$NO_PROBLEMS" = "Q" ]
			then
				echo "***ERROR: issue with qc1Ingest:"
				cat $JTMP_DIR/sc_problems
			else
				if [ $MEF_MODE = YES ]
				then
					case $S_TYPE in
				 	"stat" ) echo "  $QC, det $DET:	scored against HC and ingested in qc1_score."    ;;
				 	"spec" ) echo "  $QC, det $DET:	scored against specs and ingested in qc1_score." ;;
					esac
				else
					case $S_TYPE in
				 	"stat" ) echo "  $QC:	scored against HC and ingested in qc1_score."    ;;
				 	"spec" ) echo "  $QC:	scored against specs and ingested in qc1_score." ;;
					esac
				fi
			fi
			
# =========================================================================
# 3.6.7 THRESH1/2 not found: no scoring 
# =========================================================================

		else
			echo "  $QC, det $DET:	unscored" 
			echo "$QC1	det=$DET	unscored val=$VALUE db_avg=$AVG_VALUE db_num=$NUM_VALUE" >> $DFO_AB_DIR/$TLOG
		fi

# =========================================================================
# 3.6.8 adapt YMIN and YMAX for AUTO
# =========================================================================

		if [ $YMIN1 = "auto" ]
		then
# DET loop for the first time
			if [ $YMIN = "auto" ] && [ Q$THRESH1 != Q ]
			then
				YMIN=`echo $THRESH1 $THRESH2 | awk '{print $1-($2-$1)/2}'`
# DET loop for the second etc. time
			elif [ $YMIN = "auto" ]
			then
				YMIN="auto"
			elif [ Q$THRESH1 != Q ]
			then
				YMIN_old=$YMIN
				YMIN_new=`echo $THRESH1 $THRESH2 | awk '{print $1-($2-$1)/2}'`
				YMIN=`echo $YMIN_new $YMIN_old | awk '{if ( $1 < $2 ) {print $1} else {print $2} }'`
			else
				YMIN="auto"
			fi
		fi

		if [ $YMAX1 = "auto" ]
		then
# DET loop for the first time
			if [ $YMAX = "auto" ] && [ Q$THRESH1 != Q ]
			then
				YMAX=`echo $THRESH1 $THRESH2 | awk '{print $2+($2-$1)/2}'`
# DET loop for the second etc. time
			elif [ $YMAX = "auto" ]
			then
				YMAX="auto"
			elif [ Q$THRESH1 != Q ]
			then
				YMAX_old=$YMAX
				YMAX_new=`echo $THRESH1 $THRESH2 | awk '{print $2+($2-$1)/2}'`
				YMAX=`echo $YMAX_new $YMAX_old | awk '{if ( $1 > $2 ) {print $1} else {print $2} }'`
			else
				YMAX="auto"
			fi
		fi

# =========================================================================
# 3.6.9 Apply 20% correction to YMAX if VALUE or $THRESH2 are higher; 
#       same for YMIN
# =========================================================================

		if [ $YMAX != "auto" ]
		then
# new YMAX, increased by 20%
			NEW_YMAX=`echo $YMAX | awk '{print $1+sqrt(($1*0.2)^2)}'`
# ... to be applied if $VALUE larger than $YMAX2
			YMAX2=`echo $YMAX | awk '{print $1-sqrt(($1*0.2)^2)}'`
			YMAX=`echo $VALUE $YMAX $YMAX2 $NEW_YMAX | awk '{if ( $1 > $3 ) {print $4} else {print $2}}'`
# .. also  to check: $THRESH2 against $YMAX2
			if [ Q$THRESH2 != Q ]
			then
				YMAX2=`echo $YMAX | awk '{print $1-sqrt(($1*0.2)^2)}'`
				YMAX=`echo $THRESH2 $YMAX $YMAX2 $NEW_YMAX | awk '{if ( $1 > $3 ) {print $4} else {print $2}}'`
			fi 
		fi

		if [ $YMIN != "auto" ]
		then
# new YMIN, decreased by 20%
			NEW_YMIN=`echo $YMIN | awk '{print $1-sqrt(($1*0.2)^2)}'`
# ... to be applied if $VALUE smaller than $YMIN2
			YMIN2=`echo $YMIN | awk '{print $1+sqrt(($1*0.2)^2)}'`
			YMIN=`echo $VALUE $YMIN $YMIN2 $NEW_YMIN | awk '{if ( $1 < $3 ) {print $4} else {print $2}}'`
# .. also  to check: $THRESH1 against $YMIN2
			if [ Q$THRESH1 != Q ]
			then
				YMIN2=`echo $YMIN | awk '{print $1+sqrt(($1*0.2)^2)}'`
				YMIN=`echo $THRESH1 $YMIN $YMIN2 $NEW_YMIN | awk '{if ( $1 < $3 ) {print $4} else {print $2}}'`
			fi
		fi
	done

	if [ $INVESTIGATE = YES ]
	then
		echo "     </font></td>" >> $TMP_DIR/sq_investigate
	fi

# =========================================================================
# 3.6.10 End of DET_ID loop: write PARAM_SCORE
# =========================================================================

	echo "$QC1	PARAM_SCORE=$PARAM_SCORE" >> $DFO_AB_DIR/$TLOG

# =========================================================================
# 3.6.11 dynamic plot(s) 
# =========================================================================

	if [ -s $JTMP_DIR/list_hc_reports ]
	then
		HC_REPORT_LINK=`cat $JTMP_DIR/list_hc_reports | head -1`
	fi

	makeDetLink
	echo "" >> $DFO_AB_DIR/$TLOG

# =========================================================================
# 3.6.12 end of HTML table
# =========================================================================

	qcParam=`grep " $QC " $JTMP_DIR/explore`

	if [ $DISP_QCPARAM = YES ]
	then
		echo "DEBUG: $QC $qcParam"
	fi

	if [ $QC = $FIRST_QC ]
	then
		CHECKED=checked
	else
		CHECKED=""
	fi

	cat >> $JTMP_DIR/sq_explore2 <<EOT

  <tr>
    <td align=left colspan=${MEF_NCOL}>
      <input type="radio" name="qcParam[]" value="$qcParam" $CHECKED>
    </td>
    <td colspan=99>
      <font color=#CCCCCC size=1>___</font>
    </td>
  </tr>
EOT
	
	cat > $JTMP_DIR/sq_explore <<EOT
</table>

    </td>
EOT
	cat $JTMP_DIR/sq_explore >> $JTMP_DIR/sq_explore1 
	cat $JTMP_DIR/sq_explore >> $JTMP_DIR/sq_explore2

# BWo, 2021-05-18: removed
#	SCORE_TYPE=`grep "^${QC}[[:space:]]" $DFO_AB_DIR/$TLOG | grep -v PARAM_SCORE | grep -v unscored | grep "score_type=" | sed "s/^.*score_type=//" | awk '{print $1}'`
#	SCORE_HC_REPORT=`grep "^${QC}[[:space:]]" $DFO_AB_DIR/$TLOG | grep -v PARAM_SCORE | grep -v unscored | grep "hc_report=" | sed "s/^.*hc_report=//" | awk '{print $1}'`
#	SCORE_HC_PINDEX=`grep "^${QC}[[:space:]]" $DFO_AB_DIR/$TLOG | grep -v PARAM_SCORE | grep -v unscored | grep "hc_pindex=" | sed "s/^.*hc_pindex=//" | awk '{print $1}'`
#	SCORE_QC1_TABLE=`grep "^QC1_TABLE[[:space:]]" $DFO_AB_DIR/$TLOG | awk '{print $2}'`

#	if [ Q$SCORE_HC_REPORT != Q ]
#	then
# note: #1 has to be converted into %231 (where %23 encodes #)
#		sed -i -e "s|DOCUTAG|title=\"score thresholds from HC configuration\">score|" $JTMP_DIR/sq_explore1
#		sed -i -e "s|HCREPORT|trend_report_${SCORE_HC_REPORT}_HC.html|" $JTMP_DIR/sq_explore1
#		sed -i -e "s|HCPLTINDX|#${SCORE_HC_PINDEX}|" $JTMP_DIR/sq_explore1
#		sed -i -e "s|HCPLOTINDEX|%23${SCORE_HC_PINDEX}|" $JTMP_DIR/sq_explore1
#		sed -i -e "s|DOCUSTART||" $JTMP_DIR/sq_explore1
#		sed -i -e "s|DOCUEND||" $JTMP_DIR/sq_explore1
#		sed -i -e "s|QC1TABLE|${SCORE_QC1_TABLE}|" $JTMP_DIR/sq_explore1
#		sed -i -e "s|PARAMSTART||" $JTMP_DIR/sq_explore1
#		sed -i -e "s|PARAMEND||" $JTMP_DIR/sq_explore1

#		sed -i -e "s|DOCUTAG|title=\"score thresholds from HC configuration\">score|" $JTMP_DIR/sq_explore2
#		sed -i -e "s|HCREPORT|trend_report_${SCORE_HC_REPORT}_HC.html|" $JTMP_DIR/sq_explore2
#		sed -i -e "s|HCPLTINDX|#${SCORE_HC_PINDEX}|" $JTMP_DIR/sq_explore2
#		sed -i -e "s|HCPLOTINDEX|%23${SCORE_HC_PINDEX}|" $JTMP_DIR/sq_explore2
#		sed -i -e "s|DOCUSTART||" $JTMP_DIR/sq_explore2
#		sed -i -e "s|DOCUEND||" $JTMP_DIR/sq_explore2
#		sed -i -e "s|QC1TABLE|${SCORE_QC1_TABLE}|" $JTMP_DIR/sq_explore2
#		sed -i -e "s|PARAMSTART||" $JTMP_DIR/sq_explore2
#		sed -i -e "s|PARAMEND||" $JTMP_DIR/sq_explore2
#	elif [ Q$SCORE_TYPE = Qspec ]
#	then
#		sed -i -e "s|filter_HC_report=.*HCPLOTINDEX|filter_QC1_table=${SCORE_QC1_TABLE}|" $JTMP_DIR/sq_explore1
#		sed -i -e "s|DOCUTAG|title=\"score thresholds from static score configuration\">spec|" $JTMP_DIR/sq_explore1
#		sed -i -e "s|${TITLE_COLOR} title=\"score thresholds from static|${LIGHT_RED} title=\"score thresholds from static|" $JTMP_DIR/sq_explore1
#		sed -i -e "s|DOCUSTART||" $JTMP_DIR/sq_explore1
#		sed -i -e "s|DOCUEND||" $JTMP_DIR/sq_explore1
#		sed -i -e "s|QC1TABLE|${SCORE_QC1_TABLE}|" $JTMP_DIR/sq_explore1
#		sed -i -e "s|PARAMSTART||" $JTMP_DIR/sq_explore1
#		sed -i -e "s|PARAMEND||" $JTMP_DIR/sq_explore1

#		sed -i -e "s|filter_HC_report=.*HCPLOTINDEX|filter_QC1_table=${SCORE_QC1_TABLE}|" $JTMP_DIR/sq_explore2
#		sed -i -e "s|DOCUTAG|title=\"score thresholds from static score configuration\">spec|" $JTMP_DIR/sq_explore2
#		sed -i -e "s|${TITLE_COLOR} title=\"score thresholds from static|${LIGHT_RED} title=\"score thresholds from static|" $JTMP_DIR/sq_explore2
#		sed -i -e "s|DOCUSTART||" $JTMP_DIR/sq_explore2
#		sed -i -e "s|DOCUEND||" $JTMP_DIR/sq_explore2
#		sed -i -e "s|QC1TABLE|${SCORE_QC1_TABLE}|" $JTMP_DIR/sq_explore2
#		sed -i -e "s|PARAMSTART||" $JTMP_DIR/sq_explore2
#		sed -i -e "s|PARAMEND||" $JTMP_DIR/sq_explore2
#	else
#		sed -i -e "s/DOCUSTART.*DOCUEND//" $JTMP_DIR/sq_explore1
#		sed -i -e "s/DOCUSTART.*DOCUEND//" $JTMP_DIR/sq_explore2
#		sed -i -e "s|PARAMSTART||" $JTMP_DIR/sq_explore1
#		sed -i -e "s|PARAMEND||" $JTMP_DIR/sq_explore1
#		sed -i -e "s|PARAMSTART||" $JTMP_DIR/sq_explore2
#		sed -i -e "s|PARAMEND||" $JTMP_DIR/sq_explore2
#	fi

#end of QC param loop
done

# =========================================================================
# 3.7 fill explore (more/less) part
# 3.7.1 start
# =========================================================================

cat >> $DFO_AB_DIR/$HLOG <<EOT

<!-- exploration part -->
<tr style="font-size:small;" bgcolor=#eeeeee>
  <td align=right nowrap colspan=99>
  
   <div id="more" style="display:none">

     <table border=0 cellspacing="1" cellpadding="0" width="100%">
       <tr>
EOT
cat $JTMP_DIR/sq_explore2 >> $DFO_AB_DIR/$HLOG

cat >> $DFO_AB_DIR/$HLOG <<EOT
      </tr>
 
      <tr style="font-size:small;">
        <td colspan=99 align="right">
         <font size="1"><i>Select a QC1 parameter for further research:</i></font>
         <input type="submit" value="Submit" name="submit"> 
         <input type="reset" value="Reset" name="reset"> << <a href="javascript:location.reload()" id="more">close</a>
        </td>
      </tr>
    </table>
<!-- end -->
</div>

  <div id="less">
     <table border=0 cellspacing="1" cellpadding="0" width=100%>
       <tr>
EOT

cat $JTMP_DIR/sq_explore1 >> $DFO_AB_DIR/$HLOG

cat >> $DFO_AB_DIR/$HLOG <<EOT
  </tr>
EOT

# =========================================================================
# 3.7.2 Intermezzo for INVESTIGATE:  create $TMP_DIR/score_investigate.html
# =========================================================================

if [ $INVESTIGATE = YES ]
then
	INVEST_PAGE=$TMP_DIR/score_investigate.html
	echo "
- Find score report for investigation under $INVEST_PAGE ."
	cat $DFO_AB_DIR/$HLOG | sed "s|<\!-- INV_COMMENT -->|${INV_COMMENT}|" > $INVEST_PAGE
	echo "   <tr valign=top>" >> $INVEST_PAGE
	cat $TMP_DIR/sq_investigate >> $INVEST_PAGE 
	cat >> $INVEST_PAGE <<EOT
    </tr>
  </table>

</form>
</body>
</html>

EOT
fi

# =========================================================================
# 3.7.3 continue explore part
# =========================================================================
	
cat >> $DFO_AB_DIR/$HLOG <<EOT
       <tr>
        <td colspan=99 align=right><font size=2> 
          <a href="javascript:onoff()" title="submit for interactive research">explore</a> >> </font>
        <td>
      </tr>
    </table>
<!-- end -->
</div>
  </td>
   </tr>
EOT

# =========================================================================
# 3.8 add the scoring per detector
# =========================================================================

echo "#Detector scoring:" >> $DFO_AB_DIR/$TLOG
for DET in `cat $JTMP_DIR/list_mefvalues | grep -v EMPTY`
do
	DET_SCORE=`grep "[[:space:]]det=$DET[[:space:]]" $DFO_AB_DIR/$TLOG | awk '{print $3}' | sed "s/score=//" | awk '{sum +=$1} END {print sum }'`
	DET1=`echo $DET | awk '{printf"%-10s\n",$1}'`
	echo "det=$DET1	DET_SCORE=$DET_SCORE" >> $DFO_AB_DIR/$TLOG
done

# =========================================================================
# 3.9 add HC plot(s)
# =========================================================================

echo "  <tr bgcolor=#CCCCCC>" >> $DFO_AB_DIR/$HLOG
if [ -s $JTMP_DIR/list_HCplots ]
then
# new with v1.10:
	if [ Q$CALL_ENABLED = QYES ]
	then
		for HC in `cat $JTMP_DIR/list_HCplots | awk '{print $2}'`
		do
			HC_REPORT=`echo $HC | sed "s/^.*trend_report_//" | sed "s/_HC.html//" | sed "s/_FULL.html/_FULL/"`
			echo "$HCTOOL_NAME -f -r $HC_REPORT" >> $DFO_JOB_DIR/execHC1_${MODE}_$DATE
		done
	fi

	echo "    <td COLSPAN=$N_COL><font size=2><b><a title=\"related HC plots, may contain data from this AB\">HC plot(s):</a></b>" >> $DFO_AB_DIR/$HLOG
# only links to first 5 HC plots
	NUM_HC=`cat $JTMP_DIR/list_HCplots | wc -l`
	FIRST_HC=`cat $JTMP_DIR/list_HCplots | awk '{print $2}' | head -1`
	if [ $NUM_HC -lt 6 ]
	then
		SIXTH_HC=""
	else
		SIXTH_HC=`cat $JTMP_DIR/list_HCplots | awk '{print $2}' | head -6  | tail -1`
	fi

	for HC in `cat $JTMP_DIR/list_HCplots | awk '{print $2}' | head -6`
	do
		HC_TITEL=`grep " $HC" $JTMP_DIR/list_HCplots | awk '{print $1}'`
		if [ $HC != "$FIRST_HC" ] && [ Q$HC != "Q$SIXTH_HC" ]
		then
			echo "   | <a href=\"${HC_URL}/${HC}\" title=\"Health Check plot\">${HC_TITEL}</a>" >> $DFO_AB_DIR/$HLOG
		elif [ Q$HC = "Q$SIXTH_HC" ]
		then
			echo "   | ..." >> $DFO_AB_DIR/$HLOG
		else
			echo "    <a href=\"${HC_URL}/${HC}\" title=\"Health Check plot\">${HC_TITEL}</a>" >> $DFO_AB_DIR/$HLOG
		fi
	done
else
	CHECK_HC=`grep "score goes to HC report" $DFO_AB_DIR/$HLOG`
	if [ "Q$CHECK_HC" = "Q" ]
	then
		echo "    <td COLSPAN=$N_COL><font size=2><b>HC plot(s):</b> non configured" >> $DFO_AB_DIR/$HLOG
	else
		echo "    <td COLSPAN=$N_COL><font size=2><b>HC plot(s):</b> non configured, but check HC links above" >> $DFO_AB_DIR/$HLOG
	fi
fi
echo "    </td>" >> $DFO_AB_DIR/$HLOG
echo "  </tr>" >> $DFO_AB_DIR/$HLOG

# =========================================================================
# 3.10 link to coversheet(s)
# =========================================================================
# implemented with this version: 
# - coversheet: ${AB_ROOT_NAME}_cs.html
# - SCORE_REPORT: optional section in coversheet, marked as <!--SCORE_REPORT-->
# - if coversheet && SCORE_REPORT exist: extract SCORE_REPORT part and include here --> user-controlled links
# - if coversheet exists but no SCORE_REPORT: link first QC report (for fast access) plus coversheet
# - if not: link (first) QC report

rm -f $JTMP_DIR/list_graphics $JTMP_DIR/list_graphics1
if [ -d ${PROD_PATH} ]
then
	CS_NAME=`echo ${AB} | sed "s/.ab//" | sed "s/^.*/&_cs.html/"`	# predicted name

# for backward compatibility only; if _cs.html exists, first one of this list is linked.
	ls ${PROD_PATH} | grep ${PROD_ROOT_NAME} | egrep "gif|png|jpg" > $JTMP_DIR/list_graphics

	if [ -s $JTMP_DIR/list_graphics ]
	then
		QC_REPORT=`cat $JTMP_DIR/list_graphics | head -1`
		cat >> $DFO_AB_DIR/$HLOG <<EOT
<tr bgcolor=#CCCCCC>	<!--QC_REPORT-->
<td COLSPAN=$N_COL><font size=2>
EOT

# check for _cs.html
		if [ -s ${PROD_PATH}/$CS_NAME ]
		then
			rm -f $JTMP_DIR/user_links
			grep "<\!\-\-SCORE_REPORT\-\->" ${PROD_PATH}/$CS_NAME > $JTMP_DIR/user_links
		
			rm -f $JTMP_DIR/list_graphics1
			cat $JTMP_DIR/list_graphics | head -1 > $JTMP_DIR/list_graphics1
			mv $JTMP_DIR/list_graphics1 $JTMP_DIR/list_graphics

# user_links exist: link those
			if [ -s $JTMP_DIR/user_links ]
			then
				cat $JTMP_DIR/user_links >> $DFO_AB_DIR/$HLOG
			else
# user_links don't exist: link 1st QC plot and coversheet
				cat >> $DFO_AB_DIR/$HLOG <<EOT
<b><a title="First QC report + coversheet to all of them, created by QC tools and used for certification">1st QC report & coversheet:</a> </b> <!--QC_REPORT-->
EOT
				if [ $SHOW_ICON = NO ]
				then
					cat >> $DFO_AB_DIR/$HLOG <<EOT
$QCSPACE <a href="${PROD_PATH}/$QC_REPORT" title="QC report $QC_REPORT">QC</a> <!--QC_REPORT-->
EOT
				else
					cat >> $DFO_AB_DIR/$HLOG <<EOT
<a href="${PROD_PATH}/$QC_REPORT"><img src="${PROD_PATH}/$QC_REPORT" width=30 height=30 border=1 title="QC report $QC_REPORT"></a>  <!--QC_REPORT-->
EOT
				fi

				cat >> $DFO_AB_DIR/$HLOG <<EOT
| <a href="${PROD_PATH}/$CS_NAME" title="QC coversheet $CS_NAME">COVER</a> <!--QC_REPORT-->
EOT
			fi

# coversheet doesn't exist: link QC reports only (no coversheet) (for b/w compatibility)
		else
			cat >> $DFO_AB_DIR/$HLOG <<EOT
<b><a title="QC report(s), created by QC tools and used for certification">QC report(s):</a> </b> <!--QC_REPORT-->
EOT
			QCLABEL=1
			for G in `cat $JTMP_DIR/list_graphics`
			do
				case $QCLABEL in
				 1 ) QCSPACE="" ;;
				 * ) QCSPACE=" | " ;;
				esac

				if [ $SHOW_ICON = NO ]
				then
					cat >> $DFO_AB_DIR/$HLOG <<EOT
$QCSPACE <a href="${PROD_PATH}/$G" title="QC report $G">report${QCLABEL}</a> <!--QC_REPORT-->
EOT
				else
					cat >> $DFO_AB_DIR/$HLOG <<EOT
<a href="${PROD_PATH}/$G"><img src="${PROD_PATH}/$G" width=30 height=30 border=1 title="QC report $G"></a>  <!--QC_REPORT-->
EOT
				fi
				QCLABEL=`echo $QCLABEL | awk '{print $1+1}'`
			done
		fi

		cat >> $DFO_AB_DIR/$HLOG <<EOT
</td>	<!--QC_REPORT-->
</tr>		<!--QC_REPORT-->
EOT
	fi
fi

# =========================================================================
# 3.11 link to score results (TLOG)
# =========================================================================

cat >> $DFO_AB_DIR/$HLOG <<EOT
<tr bgcolor=#CCCCCC>
<td COLSPAN=$N_COL><font size=2><b><a title="text file with score data">Score data:</a> </b><a href="./$TLOG" title="text file with score data">details ...</a></td>
</tr>
EOT

# =========================================================================
# 3.12 total scoring
# =========================================================================

BEST_SCORE=`echo 0/$NUMBER`

echo "" >> $DFO_AB_DIR/$TLOG
echo "#Total scoring:" >> $DFO_AB_DIR/$TLOG
echo "TOTAL_SCORE	$SCORE	#total score; 0: all within specs; >0: outlier(s)" >> $DFO_AB_DIR/$TLOG
echo "TOTAL_NUMBER	$NUMBER	#number of scored values = n_det x n_param"        >> $DFO_AB_DIR/$TLOG
echo "BEST_SCORE	$BEST_SCORE	#best possible score = 0/TOTAL_NUMBER"     >> $DFO_AB_DIR/$TLOG
echo "" >> $DFO_AB_DIR/$TLOG

case $SCORE in
"X" ) SCORE_COL=$GREY;      SCORE_IMG="<img src=${IMG_SRC1}/itemOff.gif width=14 height=15 border=0>" ;;
"0" ) SCORE_COL=$GREEN;     SCORE_IMG="<img src=${IMG_SRC1}/ok.gif   width=14 height=15 border=0>" ;;
*  ) if [ $SCORE -le $YELLOW_SCORE ]
then 
	SCORE_COL=$YELLOW;    SCORE_IMG="<img src=${IMG_SRC1}/open.gif width=12 height=15 border=0>" 
else
	SCORE_COL=$LIGHT_RED; SCORE_IMG="<img src=${IMG_SRC1}/nok.gif  width=14 height=15 border=0>" 
fi ;;
esac

if [ $NUMBER = 0 ]
then
	SCORE_COL=$GREY
	SCORE_IMG="<img src=${IMG_SRC1}/itemOff.gif width=14 height=15 border=0>"
fi

cat >> $DFO_AB_DIR/$HLOG <<EOT
<tr>
<td COLSPAN=$N_COL>
<table border=0 cellspacing="1" cellpadding="1" width="100%">
<tr>
  <td width=5% bgcolor=#CCCCCC>${SCORE_IMG}</td>
  <td width=95% bgcolor=${SCORE_COL}><font size=2><a title="total score result (result=${SCORE}; number of scored parameters=$NUMBER; best possible score: $BEST_SCORE)"><b>score result:</b> ${SCORE}/$NUMBER <b>best:</b> $BEST_SCORE</a></td>
</tr>
<tr>
	  <td COLSPAN=2 align=right bgcolor=#CCCCCC><font size=1 COLOR=#FFFFFF><b>powered by QC</b> [${TOOL_NAME} v${TOOL_VERSION}]</font></td>
	</tr>
</table>
    </td>
  </tr>
</table>
EOT

# =========================================================================
# 4. MEF: add detector part of HLOG
# 4.1 start table
# =========================================================================

if [ $MEF = YES ]
then
	cat >> $DFO_AB_DIR/$HLOG <<EOT
<p>
<table border=0 cellspacing="5" cellpadding="1">
  <tr BGCOLOR=#CCCCCC>
    <td COLSPAN=$MEF_NCOL>
      <b><font color=$TITLE_COLOR size=+1>2. Detector score report</font></b><br>
      <font size=2>The same scores, sorted per detector.</font>
    </td>
  </tr>

  <tr>
EOT

# =========================================================================
# 4.2 arrange score results by QC parameter, per detector
# =========================================================================

	COUNT_DET=1
	COUNT_COL=1
	COUNT_ROW=1

	while [ $COUNT_ROW -le $MEF_NROW ]
	do
		while [ $COUNT_COL -le $MEF_NCOL ]
		do
			DET=`cat $JTMP_DIR/list_mefvalues | awk '{print "NR"NR,$1}' | grep "^NR${COUNT_DET} " | awk '{print $2}'`
			if [ $DET != EMPTY ]
			then
				cat >> $DFO_AB_DIR/$HLOG <<EOT

<!-- detector report $DET -->
    <td>

<table bgcolor="#CCCCCC" width=100%> 
  <tr bgcolor="#CCCCCC">
    <td COLSPAN=${N_PARAMS}><b><font size=2><a title="all QC1 parameters for $DET_ID $DET">$DET</a></b></td>
  </tr>
  <tr bgcolor="#CCCCCC">
EOT

# all QC1 parameters for $DET
				for QC in `cat $JTMP_DIR/list_QCpars2`
				do
					echo "    <td>" >> $DFO_AB_DIR/$HLOG
# [BWo bug fix]
					grep "yaxis=${QC}&" $JTMP_DIR/store_parstring | grep "${DET_ID}=${DET}&" | sed "s/title=\"results for ${DET}:/title=\"results for ${QC}:/" >> $DFO_AB_DIR/$HLOG
					echo "    </td>" >> $DFO_AB_DIR/$HLOG
				done	

				cat >> $DFO_AB_DIR/$HLOG <<EOT
  </tr>
</table>
    </td>
EOT
			fi
# DET = EMPTY
			if [ $DET = EMPTY ]
			then
				cat >> $DFO_AB_DIR/$HLOG <<EOT
    <td><font color=#FFFFFF>EMPTY</td>
EOT
			fi

			COUNT_COL=`echo $COUNT_COL | awk '{print $1+1}'`
			COUNT_DET=`echo $COUNT_DET | awk '{print $1+1}'`

# required if NCOLxNROW > NDET (e.g. 4x2 > 6=4+AVG+RMS: CRIRES)
			if [ $COUNT_DET -gt $N_DET_VALUES ]
			then
				echo "  </tr>" >> $JTMP_DIR/detlink
				break 
			fi
		done

		COUNT_COL=1
		COUNT_ROW=`echo $COUNT_ROW | awk '{print $1+1}'`
		cat >> $DFO_AB_DIR/$HLOG <<EOT
  </tr>

<!-- detector score report, row $COUNT_ROW -->
  <tr>
EOT
	done
	cat  >> $DFO_AB_DIR/$HLOG <<EOT
  </tr>
EOT
fi

# =========================================================================
# 4.3 finish HLOG
# =========================================================================

cat >> $DFO_AB_DIR/$HLOG <<EOT
</table>
</form>
</body>
</html>
EOT

# =========================================================================
# 5. entries in AB and ATAB
# 5.1 Update AB
# =========================================================================

LINE_SOFSECT=`grep -n "SOF section starts" $DFO_AB_DIR/$AB | sed "s/:# =====.*//" | awk '{print $1-1}'`
cat $DFO_AB_DIR/$AB | awk '{if (NR < n ) {print $0}}' n=$LINE_SOFSECT > $JTMP_DIR/new_$AB
echo "AB_STATUS	- scored by '${TOOL_NAME}'		on `date` by `whoami` on `hostname`" >> $JTMP_DIR/new_$AB
cat $DFO_AB_DIR/$AB | awk '{if (NR >= n ) {print $0}}' n=$LINE_SOFSECT >> $JTMP_DIR/new_$AB
mv $JTMP_DIR/new_$AB $DFO_AB_DIR/$AB

# =========================================================================
# 5.2 Update ATAB
# =========================================================================

ATAB=`echo $AB | sed "s/.ab/.tab/"`
if [ -s $DFO_AB_DIR/$ATAB ]
then
        SCORE_TYPE=`grep "score_type=stat"      $DFO_AB_DIR/$TLOG | head -1 | sed "s/^.*score_type=stat/YES/" | awk '{print $1}'`
        if [ "Q$SCORE_TYPE" != QYES ]
        then
                SCORE_TYPE=NO
                HC_MARK="HC_NONE"
        else
                HC_MARK="HC_MARK"
        fi

        if [ $SCORE = 0 ]
        then
                SCORE_COL=GREEN
                SCORE_IMG=0
        elif [ $SCORE = X ]
        then
                SCORE_COL=GREY
                SCORE_IMG=X
        elif [ $SCORE -le $YELLOW_SCORE ]
        then
               	SCORE_COL=YELLOW
		SCORE_IMG=1
        else
                SCORE_COL=LIGHTRED
                SCORE_IMG=2
        fi

        if [ $NUMBER = 0 ]
        then
                SCORE_COL=GREY
                SCORE_IMG=X
        fi

        SCOREFLAG=SIMG${SCORE_IMG}YLW${YELLOW_SCORE}

        SCORE_TAG=SCOL${SCORE_COL}
        SCORELINK=SCLINK${HLOG}_SCORE${SCORE}/${NUMBER}SCEND

        cat $DFO_AB_DIR/$ATAB |\
 awk '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19,$20,$21,$22,$23,$24,$25,$26,scflag,hcmark,sctag,sclink,$31,$32,$33}' \
 scflag=$SCOREFLAG hcmark=$HC_MARK sctag=$SCORE_TAG sclink=$SCORELINK \
 > $TMP_DIR/ATAB_$$
        mv $TMP_DIR/ATAB_$$ $DFO_AB_DIR/$ATAB
fi

# =========================================================================
# 6. end
# =========================================================================

echo "  
  $AB scored: TOTAL_SCORE = $SCORE/$NUMBER (BEST_SCORE = $BEST_SCORE)"

# update AB monitor
if [ -s $DFO_MON_DIR/status_${DATE}.html ] && [ $UPDATE_YN = YES ]
then
	echo "- Incremental update of AB monitor ..."
	getStatusAB -a $AB 2>/dev/null
fi

# update HC_flag
# 1. marked as HC --> HC_flag=Y
if [ "Q$HC_FLAG" != Q ]
then
	HC_flag=Y
fi

# 2. not marked? Check $CONFIG: if RAW_TYPE entered with an HC plot --> HC_flag=Y
if [ Q$HC_flag != QY ]
then
	HC_TRIGGER=`grep "^HC_PLOT"  $CONFIG | grep "[[:space:]]${RAW_TYPE}[[:space:]]" | awk '{print $3}' | head -1`
	if [ Q$HC_TRIGGER != Q ]
	then
		HC_flag=Y
	fi
fi

cleanup

exit

