#!/bin/sh
# PURPOSE:      trendPlotter: the generic trending plot generator for QC
# AUTHOR:       Reinhard Hanuschik / ESO
# VERSIONS:     1.0 -- 2005-08-10
#		1.1 -- support for HEALTH CHECK plots added (2006-01-04)
#		1.1.1- browser calls generalized (2006-03-08)
#		1.1.2- uses qcdate instead of mjd2date; config key WIDTH added (2006-05-15)
#		1.2 -- HC_COND added, CERTIF added, report groups added (2006-06-21)
#		1.2.1- minor modifications (2006-07-24)
#		1.2.2- optional <report>.msg and <report>.inf added; option to specify AVAIL_YN plot name (2006-07-25)
#		1.3 -- full suport for HISTORY plots (incl. navbar, dynamic configuration) (2006-11-27)
#		1.3.1- bug fixed with statistics (2007-01-31)
#		1.3.2- added options for thresholds; check mode disabled; bug fixed with PLOT_NAME (2007-02-12)
#		1.3.3- not NULL check for xparam added (2007-04-02)
#		1.3.4- bug fixed with PLOT_NAME (2007-04-26)
#		1.3.5- bug fixed with median offset (2007-06-13); last version with CERTIF and -M/-V/-L!
#		1.4 -- CERTIF turned off; RANGE=FULL added; improved navigation; DAILY/NONDAILY marking; OPSLOG table (2007-06-27)
#		1.4.1- dircheck for FULL on web4; check for other processes improved; aliens marking changed (2007-07-10)
#		1.4.2- bug fixes for NOPS and date lines (2007-07-12)
#		1.4.3- transfer to web server generalized (ssh to $WEB_SERVER) (2007-07-13)
#		1.4.4- bug fixed for TMODE=FULL; handling enabled for non-QC trending plots (2007-08-07)
#		1.4.5- string conversion to handle very small number right in SQL (line 2088) (MNe, 2007-10-12) 
#		1.4.6- introducing QC_SERVER as optional config key (default: opus8) (2007-10-16)
#		1.5 -- OPSLOG table improved, update time difference displayed, ftp watcher implemented (2007-11-30)
#		1.5.1- bug fix in line 2699 to have same plot format in overview and closeup plots (2008-01-10, BW)
#		1.5.2- set/format changed for precise median calculation; bug fix for opslog output to html (2008-03-03, BW)
#		1.5.3- improved self-check (2008-04-24, BWo)
#		2.0 -- upgraded to include scores (QUICK_LOOK) (2008-10-16)
#		2.0.1- ssh/scp with BatchMode=yes (no pausing if public key not found); SCORE_ACTIVE evaluated (2008-11-03)
#		2.0.2- bug fixed with VAL thresholds; 'nowrap' attributes to stabilize line "Last update..." (2008-11-12)
#		2.0.3- HC HTML files stored in $DFO_TREND_DIR/reports/recent, for quick messaging (2009-01-16)
#		2.1 -- enabled for parallel execution; non-default $MID_WORK, requires scoreHC v1.3 (2009-02-17)
#		2.1.1- QC_SERVER obsolete, removed; INFO link --> HELP to align with calChecker et al. (2009-04-01)
#		2.1.2- REFRESH rate relaxed to 300 sec; MARK_NO_DATA configurable; DATE_RANGE=NO added (special feature) (2009-09-30)
#		2.1.3- SCORE_ACTIVE turned off, minor design changes (2009-10-10)
#		2.1.4- new key DATA_TRANSFER=DISK: for VIRCAM only (2009-10-21, BWo)
#		2.1.5- direct scp to w1/w2, qcWebcp not used anymore, requires change of QC_DIR in config file (2009-12-21)
#		2.2 -- new mode MONITOR; DETID displayed in opslog files; DFO_DATE and PSO_DATE follow DFO convention; .msg files included; safweb1 replaced by qcweb (2010-02-15)
#		2.3 -- added: MON_1; enabled for condor (option -u) (2010-03-22) 
#		2.4 -- added: non-standard configuration HISTO_FLAG (2010-04-21)
#		2.5 -- added: tooltips; requires scoreHC v1.6; availability plots turned off (2010-10-06)
#		2.6 -- added: links to daily raw, nightlog, and processing reports from calChecker; added over effect (2010-11-23)
#		2.7 -- new option FIXED; warning about 3sig/1sig; java tool for turning off browser_refresh (2010-12-20)
#		2.7.1- better handling of updates to date.dat (2011-01-18, BWo)
#		2.7.2- 3SIG/1SIG turned off; added to plot signature: "powered by QC" (2011-02-07, BWo)
#		2.7.3- comment section promoted; new: general comment; tooltipps modified (2011-07-06)
#		2.7.4- ${DFO_INSTRUMENT}_POSTIT: instrument news (2011-11-15)
#		3.0 -- switch to Python as plotting engine; requires pet.py; closeup plots: default ticks (2012-06-12) [BWo]
#		3.1 -- php comment editor; writes PIPELINE version into HC header; new configurable parameter SFORMAT; improved locking mechanism for date.dat; comes together with pet.py v1.1 (2012-10-08)
#		3.2 -- comment management (checkValidity); comment button on HC reports; FULL symbol on navbar (2012-12-04)
#		3.2.1- hostname marked (invisibly) (2013-02-06)
#		3.2.2- FULL reports not linked by horizontal navbar if TRANGE=FULL and $HTML_NAME_CURRENT (line 2984) (2013-10-10)
#		3.3 -- stargate1 replaced by $DFO_WEB_SERVER (2013-11-04)
#		3.3.1- minor change in line :1094 for $DATE2 (2014-10-13)
#		3.3.2- time-out added for scp/ssh commands (2014-10-24) [BWo]
#		3.4 -- OPSLOG support de-commissioned; comment management changed for non-standard plots (2014-10-31) [BWo]
#		3.4.1- small bug fixes (2015-01-13) [BWo]
#		3.4.2- AVAIL_YN removed (obsolete) (2015-02-12)
#		3.4.3- DISPLAY_DETID removed (obsolete) (2015-02-26) [BWo]
#		3.5 -- ConnectTimeout removed, DTS and ngas flags displayed (2015-03-23)
#		3.6 -- link to forced refresh of autoDaily; bug fixed with javascript openWin missing for certain non-std configurations; select-->select distinct (2016-06-30)
#		3.6.1- forced_refresh made visible (2015-07-08)
#		3.6.2- small bug fix for PIPE_VERSION; support for CONFIGTP_DIR terminated (2015-09-24)
#		3.6.3- small bug fix for REPORT_GROUP (2017-08-17)
#		3.7 -- support for KPI plots (labels and REF_VALUES); prepared for upcoming webDocuSys (2018-02-16)
#		4.0 -- usage of Highcharts for close-up plots (2019-03-26)
#		4.0.1- create PNGs for close-up only if INTERACTIVE=STATIC (2019-05-15)
#		4.0.2- force the 'refresh' button to go to HQ server (:3182 and :3742) (2019-08-21)
#		4.1 -- added link to 'quality events log' (2020-07-21)
#		4.1.1- fix issue with indicated data range in case of no data (2020-09-02)
#		4.1.2- fine-tuning the 'quality events' link (2020-09-24)
#
# PARAMETERS:	-r <report_name> (mandatory)
#		-t <type>:  HISTORY|HEALTH (optional; default: HEALTH)
#
# TOOLS:	qcdate; pet.py
#		interaction with scoreQC (QUICK-LOOK information), webNavBar (vnavbar creation), webCMS and webDocuSys
#		scoreHC
# OPTIONS:
#		-f scp the html/png files to web servers
#		-s <start>: YEAR-MONTH (YYYY-MM, for HISTORY; default: present>
#		-c	       special (non-standard config file, special purpose only!)
#		-u	       user (for condor)
#
#		-B	       deBug mode: do not delete temporary directory;
#					   echo call of pet.py; new with V3.0
#
#		-A	       create all HISTORY plots (only for HISTORY)
#		-N	       create all group and history navigation bars
#		-T 	       like -N, for all reports
#
# NOTES: 
#		-PSO_DATE and DFO_DATE defined from mjd_obs, include $DFO_OFFSET.
# 		- v3.7 connects trendPlotter to upcoming webCMS and webDocuSys. 
#		  A new link 'HC docu' becomes visible if INCLUDE_WEBDOCUSYS=YES in config.webCMS.
# 		  If NO, the link is whited out for testing without visibility. 
#		  Obviously this will evolve with the next version.
#		- consistency problem with KPI reference values: their value in config.tp and in version_KPI.txt 
#		  needs to be aligned (only one source!); no easy way found yet.
# =====================================================================================
TOOL_VERSION="4.1.2"
TOOL_NAME="trendPlotter"

DFO_WEB_URL="http://qcweb/tqs/config_tp" 		#corresponding URL
QCBIN_DIR="/home/qc/bin"                         	#on $DFO_WEB_SERVER

REFRESH=300      	# browser refresh intervall in sec
DELAY=15		# delay (in days), between TODAY and last applicable date for history plots
			# (to account for delivery and processing delays)
MAX_AGE=14		# maximum age for report news (outdated ones will fire email)

# self-check
OTHER_TIMEOUT=300	# wait time for other trendPlotter process if found
DELAY_NEXT=10		# wait time for self-check

# colors
HC_COLOR=#006A9D
NEWS_COL=#FFFF99  # light-yellow

TAB_BGCOLOR="#66CCFF"	# background color for result table
FTCOLOR="#666666"	# font color for history navbars
THIS_REPORT_COLOR="#FFFF99"	# light-yellow, color for marking selected item in group navbar
OTHER_REPORT_COLOR="#66CCFF"	# light-blue, color for marking other items in group navbar
IMG_SRC="http://www.eso.org/images"	#source for history symbols

DAILY_MARKER="or_ball.gif"	#marker for DAILY reports
BOUNDARY=`echo 12 $DFO_OFFSET | awk '{print $1+$2}'`

QC1DB_GET="javascript:openWin('http://archive.eso.org/bin/qc1_cgi?action=qc1_browse_get"
QC1DB_DOCU="javascript:openWin2('http://archive.eso.org/bin/qc1_cgi?action=qc1_browse_get"

# connection to webCMS and webDocuSys
if [ -s $DFO_CONFIG_DIR/webCMS/config.webCMS ]
then
	INCLUDE_WEBDOCUSYS=`grep "^INCLUDE_WEBDOCUSYS" $DFO_CONFIG_DIR/webCMS/config.webCMS | awk '{print $2}'`
fi

if [ Q$INCLUDE_WEBDOCUSYS != QYES ]
then
	INCLUDE_WEBDOCUSYS=NO
fi

# =====================================================================================
# 0. Initialization
# 0.1 get options and configuration
# 0.1.1 options
# =====================================================================================

# get own file name
TPNAME=`basename $0`

# set default values for options
SCP="no"
TRENDING_TYPE=HEALTH
HIST_MODE=SINGLE
CREATE_ALL=NO
CREATE_NAVBAR=NO
TOOL_CONFIG=config.trendPlotter
DEBUG=NO

# option c: non-standard tool config file (hidden option, for non-QC trending plots)
while getopts t:s:r:c:u:fhvANTB OPTION
do
        case "$OPTION" in
         v ) echo $TOOL_VERSION ; exit 0 ;;
         h ) cat $DFO_DOC_DIR/trendPlotter.h | more ; exit 0 ;;

	 A ) HIST_MODE="ALL" ;;
	 T ) CREATE_ALL="YES" ;;
	 N ) CREATE_NAVBAR="YES" ;;

         r ) REPORT_NAME=$OPTARG ;;
         t ) TRENDING_TYPE=$OPTARG ;;
	 s ) YMSTART=$OPTARG ;;

	 c ) TOOL_CONFIG=$OPTARG ;;
	 f ) SCP="yes" ;;
	 u ) export USER="$OPTARG"
	     export HOME="/home/$USER" 
	     source $HOME/.bashrc	#CONDOR has no environment variables
	     ;;
	 B ) DEBUG="YES" ;;
         ? ) exit -1 ;;
        esac
done

# =====================================================================================
# 0.1.2 check for DFO variables
# =====================================================================================

CHECK=`printenv | grep DFO`
if [ "Q$CHECK" = "Q" ]
then
        echo "*** ERROR: define DFO variables in ~/.dfosrc and re-start."
        exit
fi

# =====================================================================================
# 0.2 this and that
# 0.2.1 some directory checks
# =====================================================================================

CONFIG_DIR=$DFO_CONFIG_DIR/trendPlotter
REPORT_DIR=$DFO_TREND_DIR/reports

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

if [ "Q$DFO_BROWSER" = "Q" ]
then
	echo "***WARNING: No \$DFO_BROWSER defined in \$HOME/.dfosrc. We assume firefox."
	DFO_BROWSER=firefox
fi

if [ "Q$DFO_WEB_SERVER" = "Q" ]
then
        echo "*** WARNING: \$DFO_WEB_SERVER not defined, update your .dfosrc."
fi

if [ ! -s $DFO_BIN_DIR/qcdate ]
then
	echo "***ERROR: qcdate not available. Download utilPack and start again."
	exit -1
fi

# for technical reports only:
if [ $TOOL_CONFIG !=  "config.trendPlotter" ]
then
	if [ ! -s $DFO_CONFIG_DIR/trendPlotter/$TOOL_CONFIG ]
	then
		echo "***ERROR: $TOOL_CONFIG not found. Exit."
		exit -1
	fi
	DIRNAME=`dirname $TOOL_CONFIG`
	CONFIG_DIR=`eval "echo $CONFIG_DIR/$DIRNAME"`
fi

# =====================================================================================
# 0.2.2 CREATE_ALL=YES: create job file for complete re-creation of all HISTORY+HEALTH plots
# =====================================================================================

if [ $CREATE_ALL = YES ]
then
	echo "
This mode creates an executable job file \${DFO_TREND_DIR}/reports/createAll which can be used
to create all HEALTH and HISTORY plots for all reports. This may be useful, or required,
when the output format changes. 

Step 1: We now create the job file \${DFO_TREND_DIR}/reports/createAll which you then can execute. Hit return:"
	read input

	rm -f $REPORT_DIR/createAll
	for PARAM in `grep "START_DATE" ${CONFIG_DIR}/config.tp_* | sed "s/^.*config.tp_//" | sed "s/:/ /" | awk '{print $1}'`
	do
		echo "trendPlotter -r $PARAM -A; $TMP_DIR/list_history" >> $REPORT_DIR/createAll
	done
	echo "  ... done. The file contains all reports with START_DATE enabled in their config file.

Step 2: You may want to edit \${DFO_TREND_DIR}/reports/createAll to remove e.g. test reports. Then call it, 
and all trending reports of type HEALTH and HISTORY will be created. Hit return:"
	read input

	echo "Note: The complete re-creation of all trending plots may take a significant amount of time. 
All cronjobs calling 'trendPlotter' should be disabled for that. "
	chmod u+x $REPORT_DIR/createAll
	exit
fi

# interactive report selection
if [ "Q$REPORT_NAME" = "Q" ] 
then
	echo "Select from the available trending reports:"
	ls ${CONFIG_DIR}/config.tp_* | sed "s/^.*config.tp_//"
	echo ""
	echo "Enter report name:"
	read REPORT_NAME

	if [ "Q$REPORT_NAME" = "Q" ]
	then
		echo "***ERROR: you must define a REPORT_NAME. Exit."
		exit -1
	fi

	echo "Enter trending type (HEALTH/HISTORY):"
	read TRENDING_TYPE
	if [ "Q$TRENDING_TYPE" = "Q" ]
	then
		echo "***ERROR: you must define a TRENDING_TYPE. Exit"
		exit -1
	fi
	
	echo "Next time you can start this report directly by typing:"
	echo "trendPlotter -r $REPORT_NAME -t $TRENDING_TYPE"
	echo "Type trendPlotter -h to see the other options."
	echo ""
fi

# =====================================================================================
# 0.2.3 some self-documentation
# =====================================================================================

cat > $REPORT_DIR/AAREADME <<EOT
This directory is used by trendPlotter for 
- storing HTML and png output which is not transfered to the web 
  (since you e.g. want to debug or develop a report)
- hosting reserved subdirectories like statFiles
EOT

cat > $REPORT_DIR/statFiles/AAREADME <<EOT
This directory is used by trendPlotter for collecting statistics information.
These files are read by scoreQC in order to find thresholds managed by trendPlotter.
There are report statistics files (stat_text_<report>), and group statistics files (stat_grp_<name>).
EOT

# =====================================================================================
# 0.2.4 validation
# =====================================================================================

if [ ! -d ${CONFIG_DIR} ]
then
	echo "***ERROR: ${CONFIG_DIR} not found. Exit."
	exit -1
fi

CHECK_VALID=`eval "ls ${CONFIG_DIR}/config.tp_${REPORT_NAME}"`

if [ "Q$CHECK_VALID" = "Q" ]
then
	echo "***ERROR: $REPORT_NAME is not a valid report name. Exit."
	exit -1
fi

# TRENDING_TYPE
if [ "Q${TRENDING_TYPE}" = "Q" ]
then
	TRENDING_TYPE=HEALTH
fi

# YMSTART
if [ "$TRENDING_TYPE" != HISTORY ] && [ "Q$YMSTART" != "Q" ]
then
	echo "***WARNING: Option -s implies TRENDING_TYPE = HISTORY. We force TRENDING_TYPE=HISTORY."
	TRENDING_TYPE=HISTORY
fi

CONFIG=${CONFIG_DIR}/config.tp_${REPORT_NAME}	# main config file 
DELTA=${CONFIG_DIR}/delta.tp_${REPORT_NAME}	# optional delta config file (for config versioning, HISTORY)

CONFIG_TYPE=`grep "^CONFIG_TYPE"	$CONFIG | awk '{print $2}'`
if [ "Q$CONFIG_TYPE" = "QNON_STANDARD" ] && [ $TOOL_CONFIG = config.trendPlotter ]
then
	echo "***ERROR: This is a non-standard report, don't call it with standard tool configuration config.trendPlotter!"
	exit -1
fi 
 
# needs to be set for cronjob
if [ "Q$USER" = "Q" ]
then
	export USER=`grep "^USER" 	$DFO_CONFIG_DIR/config.autoDaily | awk '{print $2}'`
fi

if [ "${TRENDING_TYPE}" != "HISTORY" ] && [ "${TRENDING_TYPE}" != "HEALTH" ] 
then
	echo "***ERROR: Unknown trending type ${TRENDING_TYPE}."
	exit -1
fi

if [ "$HIST_MODE" = "ALL" ] 
then
	START_DATE=`grep "^START_DATE" $CONFIG | awk '{print $2}'`
	if [ "Q$START_DATE" = "Q" ]
	then
		echo
		echo "***WARNING: no \$START_DATE configured in $CONFIG, can't do HISTORY plots. Exit."
		exit -1
	fi
fi

# =====================================================================================
# 0.3 get configuration
# 0.3.1 tool configuration
# =====================================================================================

# for debugging: replace $$ by something less random 
# or use option -B
export JTMP_DIR=`echo $TMP_DIR/$$`     # TMP_DIR for this PID, removed afterwards
rm -rf $JTMP_DIR
mkdir  $JTMP_DIR

# tool executes in $JTMP_DIR
cd $JTMP_DIR

TOOL_CONFIG=$DFO_CONFIG_DIR/trendPlotter/$TOOL_CONFIG

BROWSER_DISPLAY=`grep "^BROWSER_DISPLAY" $TOOL_CONFIG | awk '{print $2}'`
CGI_URL=`grep "^CGI_URL"		$TOOL_CONFIG | awk '{print $2}'`
HELP_URL=`grep "^HELP_URL"		$TOOL_CONFIG | awk '{print $2}'`
QC_DIR=`grep "^QC_DIR"			$TOOL_CONFIG | awk '{print $2}'`
QC_URL=`grep "^QC_URL"                  $TOOL_CONFIG | awk '{print $2}'`
INS_NEWS=`grep "^INS_NEWS"		$TOOL_CONFIG | awk '{print $2}'`
if [ Q$INS_NEWS != QNO ]
then
	INS_NEWS=YES
fi

SCORE_TT_URL=`echo $QC_URL | sed "s/reports/scores/"`

STYLES="http://www.eso.org/observing/dfo/quality/ALL/common/qc_eso.style" # style sheet 	

# check QC_DIR to be updated properly; if not, enforce here and send mail!
CHECK_UPDATE=`echo $QC_DIR | grep "home\/web\/eso"`

if [ "Q$CHECK_UPDATE" != Q ]
then
        QC_DIR1=`echo $QC_DIR | sed "s|home/web/eso|home/qc/eso|"`
        cat > $TMP_DIR/trendP_mail <<EOT
Please update \$QC_DIR in your $TOOL_CONFIG!
From: $QC_DIR
To:   $QC_DIR1
EOT
        QC_DIR=$QC_DIR1

        mail -s "Please update \$QC_DIR in your $TOOL_CONFIG!" $OP_ADDRESS <$TMP_DIR/trendP_mail
fi

if [ "Q$CONFIG_TYPE" = "QNON_STANDARD" ]
then
	WIDTH=`grep "^WIDTH"		$TOOL_CONFIG | awk '{print $2}'`
else
	WIDTH=950		#hard-coded for standard reports with v2.0
fi

WIDTH1=782 		#width of rightmost (result) column, same as for gif plot

EMAIL=`grep "^EMAIL"			$TOOL_CONFIG | awk '{print $2}'`
if [ "Q$EMAIL" = "Q" ]
then
	echo "***WARNING: no EMAIL address configured in $TOOL_CONFIG. Can't add contact label."
fi

# technical reports: some hidden keys, not documented
# non-standard plot labels and headers
PLOT_LABEL=`grep "^PLOT_LABEL"	$TOOL_CONFIG | awk '{print $2}' | sed "s/_/ /g"`
HEADER=`grep "^HEADER"		$TOOL_CONFIG | awk '{print $2}'`
if [ "Q$HEADER" = "Q" ]
then
	HEADER="/observing/dfo/quality/ALL/common/header_HC.html"
fi

# DATE_RANGE might be switched off (no "(last 90 days)", no "QC data range")
DATE_RANGE=`grep	"^DATE_RANGE"	$TOOL_CONFIG | awk '{print $2}'`
if [ Q$DATE_RANGE != QNO ]
then	
	DATE_RANGE=YES
fi

# DATE_RNG_LABEL: can be specified (otherwise "QC_data_range")
DATE_RNG_LABEL=`grep 	"^DATE_RNG_LABEL" $TOOL_CONFIG | awk '{print $2}' | sed "s/_/ /g"`
if [ "Q$DATE_RNG_LABEL" = Q ]
then
	DATE_RNG_LABEL="Data range"
fi

# technical reports may have this switched off
PRODUCT_SECTION=`grep "^PRODUCT_SECTION" $TOOL_CONFIG | awk '{print $2}'`
if [ Q$PRODUCT_SECTION != QNO ]
then
	PRODUCT_SECTION=YES
fi

# technical reports may have this switched off
HISTORY_SECTION=`grep "^HISTORY_SECTION"	$TOOL_CONFIG | awk '{print $2}'`
if [ Q$HISTORY_SECTION != QNO ]
then
	HISTORY_SECTION=YES
fi

# technical reports could have no INFO_SECTION
INFO_SECTION=`grep "^INFO_SECTION"	$TOOL_CONFIG | awk '{print $2}'`
if [ Q$INFO_SECTION != QNO ]
then
	INFO_SECTION=YES
fi

# technical reports could have no scoring
SCORE_FLAG=`grep "^SCORE_FLAG"	$TOOL_CONFIG | awk '{print $2}'`
if [ Q$SCORE_FLAG != QNO ]
then
	SCORE_FLAG=YES
fi

# non-standard reports could have no history
HISTO_FLAG=`grep "^HISTO_FLAG"	$TOOL_CONFIG | awk '{print $2}'`
if [ Q$HISTO_FLAG != QNO ]
then
	HISTO_FLAG=YES
fi

# ... have optional EDIT button suppression
EDIT_FLAG=`grep "^EDIT_FLAG"	$TOOL_CONFIG | awk '{print $2}'`
if [ Q$EDIT_FLAG != QNO ]
then
	EDIT_FLAG=YES
fi

# ... control validity check
EDIT_PERMA=`grep "^EDIT_PERMA"	$TOOL_CONFIG | awk '{print $2}'`
if [ Q$EDIT_PERMA != QYES ]
then
	EDIT_PERMA=NO
fi

# special: option to configure a general non-standard vnavbar in $TOOL_CONFIG
# normally there is none
VNAVBAR=`grep "^VNAVBAR"	$TOOL_CONFIG | awk '{print $2}'`

if [ "Q$BROWSER_DISPLAY" = "Q" ]
then
	BROWSER_DISPLAY=NO
fi

# non-std reports never have INCLUDE_WEBDOCUSYS=YES
if [ "Q$CONFIG_TYPE" = "QNON_STANDARD" ]
then
	INCLUDE_WEBDOCUSYS=NO
fi

# connection to QUALITY_EVENTLOG
INCLUDE_QUALITY_EVENTLOG=`grep "^INCLUDE_QUALITY_EVENTLOG"	$TOOL_CONFIG | awk '{print $2}'`
if [ Q$INCLUDE_QUALITY_EVENTLOG != QYES ]
then
	INCLUDE_QUALITY_EVENTLOG=NO
fi
QUALITY_EVENTLOG_URL=`grep "^QUALITY_EVENTLOG_URL"		$TOOL_CONFIG | awk '{print $2}'`
QUALITY_EVENTLOG_URL="javascript:openWin2('${QUALITY_EVENTLOG_URL}?instrumentId=${QC1_INSTRUMENT}')"

# =====================================================================================
# 0.3.2 get version of operational pipeline (only for $INFO_SECTION = YES)
# (to be plotted in header of HTML output)
# the following is from processAB/dfoMonitor
# =====================================================================================

# could be in ESOREX_PLUGIN_DIR ...
if [ $INFO_SECTION = YES ]
then
	PIPE_VERSION=$ESOREX_PLUGIN_DIR
	if [ Q$PIPE_VERSION = Q ]
	then
	# ... or in esorex.rc ...
		PIPE_VERSION=`grep "^esorex.caller.recipe-dir" $HOME/.esorex/esorex.rc | sed "s/esorex.caller.recipe-dir=//"`
	fi
# ... or in ${PIPE_HOME}
	if [ Q$PIPE_VERSION = Q ]
	then
		PIPE_NAME=`grep "^PIPE_NAME"    $DFO_CONFIG_DIR/config.processAB | awk '{print $2}'`
		if [ -s ${PIPE_HOME}/$PIPE_NAME ]
		then
			PIPE_VERSION=`ls -latr ${PIPE_HOME}/$PIPE_NAME | awk '{print $11}'`
		else
			PIPE_VERSION="unknown"
		fi
	fi

# exclude detmon
	PIPE_VERSION=`echo $PIPE_VERSION | tr ":" "\012" | grep -v detmon | head -1`
# basename only
	PIPE_VERSION1=`basename $PIPE_VERSION`

# check for '-' in PIPE_VERSION
	CHECK_VERSION=`echo $PIPE_VERSION1 | grep -e -`

# if empty: PIPE_VERSION is a link which needs to be dereferenced
	if [ "Q$CHECK_VERSION" = "Q" ]
	then
		PIPE_VERSION=`ls -latr ${PIPE_HOME}/$PIPE_VERSION1 | awk '{print $11}'`
		PIPE_INSTALL=`date -r ${PIPE_HOME}/$PIPE_VERSION1 +%Y-%m-%d`
	else
# PIPE_VERSION is a directory
		PIPE_INSTALL=`date -r $PIPE_VERSION +%Y-%m-%d`
		PIPE_VERSION=$PIPE_VERSION1	
	fi
fi

# =====================================================================================
# 0.3.3 general report configuration
# =====================================================================================

RELATED_URL=`grep "^RELATED_URL"	$CONFIG | awk '{print $2}'`
REPORT_GROUP=`grep "^REPORT_GROUP"	$CONFIG | awk '{print $2}'`

if [ "Q$REPORT_GROUP" = "Q" ]
then
	REPORT_GROUP=NO
fi

# VNAVBAR: normally there is none at this point
# now: either there is a group-specific one ...
if [ "Q$VNAVBAR" = "Q" ]
then
	VNAVBAR=`grep "^VNAVBAR"	$CONFIG | awk '{print $2}'`

	if [ "Q$VNAVBAR" = "QYES" ]
	then
		VNAVBAR_URL=`echo $QC_URL | sed "s/reports/common/"`
		VNAVBAR=`echo $REPORT_GROUP | sed "s/.grp//"`

		if [ "Q$VNAVBAR" = "Q" ] || [ "Q$VNAVBAR" = QNO ]
		then
			echo "***ERROR: VNAVBAR in $CONFIG set to YES but no REPORT_GROUP defined. Take the standard one:"
			VNAVBAR="/observing/dfo/quality/ALL/common/navbar_HC_${DFO_INSTRUMENT}.html"
			echo "          $VNAVBAR"
		else
			VNAVBAR="${VNAVBAR_URL}/vnavbar_HC_${VNAVBAR}.html"
		fi
# ... or a generic one:
	else
		VNAVBAR="/observing/dfo/quality/ALL/common/navbar_HC_${DFO_INSTRUMENT}.html"
	fi
fi

# optional RELATED_URL with additional information for the plot (linked under "more ...")
if [ "Q$RELATED_URL" = "Q" ]
then
	RELATED_URL=NONE
	RELATED_URL1=NONE
else
	RELATED_URL1="<a href=$RELATED_URL>more</a>"	
fi

# optional FULL report name
FULL_NAME=`grep "^FULL_NAME"		$CONFIG | awk '{print $2}'`
if [ "Q$FULL_NAME" = "Q" ]
then
	FULL_NAME=NONE
fi

BROWSER_ACTIVE=`ps -wfC $DFO_BROWSER | grep -v CMD`
INFO_FILE="${REPORT_NAME}.inf"
NEWS_FILE="${REPORT_NAME}.msg"

# =====================================================================================
# 0.4 Report template format definition
# 0.4.1 plot definitions
# =====================================================================================
# For the time being, these definitions are also used by pet.py.
# Could be transferred later to genuine Python matplotlib values.
# =====================================================================================
# one line per plot defines the box size, position, labelling, date information
# P_INDEX:      index of plot
# XSIZE:        size in mm (MIDAS); '-' required
# YSIZE:        size in mm (MIDAS); '-' required
# XOFF:         offset from lower left corner, in mm
# YOFF:         offset from lower left corner, in mm
# XLAB:         X position of index label (relative to box)
# YLAB:         Y position of index label (relative to box)
# DATE:         YES means vertical date labels are plotted (one per month)
# DLABEL_SIZE:  relative size of date label
# =====================================================================================
#			P_INDEX	XSIZE	YSIZE	XOFF	YOFF	XLAB	YLAB	DATE	DLABEL_SIZE
#REPORT_TYPE	REPORT2	1	-70	-70	20	22	-6	77	YES	1.4
#REPORT_TYPE	REPORT2	2	-70	-70	105	22	-6	77	YES	1.4

#			P_INDEX	XSIZE	YSIZE	XOFF	YOFF	XLAB	YLAB	DATE	DLABEL_SIZE
#REPORT_TYPE	REPORT4	1	-70	-35	20	58	-6	40	YES	1.4
#REPORT_TYPE	REPORT4	2	-70	-35	110	58	-6	40	YES	1.4
#REPORT_TYPE	REPORT4	3	-70	-35	20	13	-6	40
#REPORT_TYPE	REPORT4	4	-70	-35	110	13	-6	40

#			P_INDEX	XSIZE	YSIZE	XOFF	YOFF	XLAB	YLAB	DATE	DLABEL_SIZE
#REPORT_TYPE	REPORT6	1	-40	-35	20	58	-6	40	YES	1.4
#REPORT_TYPE	REPORT6	2	-40	-35	79	58	-6	40	YES	1.4
#REPORT_TYPE	REPORT6	3	-40	-35	138	58	-6	40	YES	1.4
#REPORT_TYPE	REPORT6	4	-40	-35	20	13	-6	40
#REPORT_TYPE	REPORT6	5	-40	-35	79	13	-6	40
#REPORT_TYPE	REPORT6	6	-40	-35	138	13	-6	40

#			P_INDEX	XSIZE	YSIZE	XOFF	YOFF	XLAB	YLAB	DATE	DLABEL_SIZE
#REPORT_TYPE	REPORT8	1	-33	-35	13	58	-6	40	YES	1.4
#REPORT_TYPE	REPORT8	2	-33	-35	59	58	-6	40	
#REPORT_TYPE	REPORT8	3	-33	-35	105	58	-6	40	YES	1.4
#REPORT_TYPE	REPORT8	4	-33	-35	151	58	-6	40
#REPORT_TYPE	REPORT8	5	-33	-35	13	13	-6	40
#REPORT_TYPE	REPORT8	6	-33	-35	59	13	-6	40
#REPORT_TYPE	REPORT8	7	-33	-35	105	13	-6	40
#REPORT_TYPE	REPORT8	8	-33	-35	151	13	-6	40

#			P_INDEX	XSIZE	YSIZE	XOFF	YOFF	XLAB	YLAB	DATE	DLABEL_SIZE
#REPORT_TYPE	REPORT10 1	-30	-35	15	58	-4	40	YES	1.4
#REPORT_TYPE	REPORT10 2	-30	-35	49	58	-1	40	
#REPORT_TYPE	REPORT10 3	-30	-35	84	58	-4	40	YES	1.4
#REPORT_TYPE	REPORT10 4	-30	-35	118	58	-1	40	
#REPORT_TYPE	REPORT10 5	-30	-35	152	58	-4	40	YES	1.4
#REPORT_TYPE	REPORT10 6	-30	-35	15	13	-4	38	
#REPORT_TYPE	REPORT10 7	-30	-35	49	13	-1	38	
#REPORT_TYPE	REPORT10 8	-30	-35	84	13	-1	38
#REPORT_TYPE	REPORT10 9	-30	-35	118	13	-1	38
#REPORT_TYPE	REPORT10 10	-30	-35	152	13	-1	38

#REPORT_TYPE	REPORT1 1	-150	-70	20	20	-6	77	YES	1.4

# =====================================================================================
# 0.4.2 Configuration of quick-look HTML panel (new with v1.6+)
# List sequentially  all cells of the HTML quick-look table,
# by P_INDEX and row, starting with ROW1.
# P_INDEX:      index of plot; empty cells: EMPTY
# ROW_TAG:      ROW1, ROW2 etc.
# =====================================================================================
#			 P_INDEX ROW_TAG
#HTML_REPORT_TYPE    REPORT1 1  ROW1


#HTML_REPORT_TYPE    REPORT2 1  ROW1
#HTML_REPORT_TYPE    REPORT2 2  ROW1

#HTML_REPORT_TYPE    REPORT4 1  ROW1
#HTML_REPORT_TYPE    REPORT4 2  ROW1
#HTML_REPORT_TYPE    REPORT4 3  ROW2
#HTML_REPORT_TYPE    REPORT4 4  ROW2

#HTML_REPORT_TYPE    REPORT6 1  ROW1
#HTML_REPORT_TYPE    REPORT6 2  ROW1
#HTML_REPORT_TYPE    REPORT6 3  ROW1
#HTML_REPORT_TYPE    REPORT6 4  ROW2
#HTML_REPORT_TYPE    REPORT6 5  ROW2
#HTML_REPORT_TYPE    REPORT6 6  ROW2

#HTML_REPORT_TYPE    REPORT8 1  ROW1
#HTML_REPORT_TYPE    REPORT8 2  ROW1
#HTML_REPORT_TYPE    REPORT8 3  ROW1
#HTML_REPORT_TYPE    REPORT8 4  ROW1
#HTML_REPORT_TYPE    REPORT8 5  ROW2
#HTML_REPORT_TYPE    REPORT8 6  ROW2
#HTML_REPORT_TYPE    REPORT8 7  ROW2
#HTML_REPORT_TYPE    REPORT8 8  ROW2

#HTML_REPORT_TYPE    REPORT10 1 ROW1
#HTML_REPORT_TYPE    REPORT10 2 ROW1
#HTML_REPORT_TYPE    REPORT10 3 ROW1
#HTML_REPORT_TYPE    REPORT10 4 ROW1
#HTML_REPORT_TYPE    REPORT10 5 ROW1
#HTML_REPORT_TYPE    REPORT10 6  ROW2
#HTML_REPORT_TYPE    REPORT10 7  ROW2
#HTML_REPORT_TYPE    REPORT10 8  ROW2
#HTML_REPORT_TYPE    REPORT10 9  ROW2
#HTML_REPORT_TYPE    REPORT10 10 ROW2

# =====================================================================================
# 0.5 find report properties
# =====================================================================================

REPORT_TYPE=`grep "^REPORT_NAME"	$CONFIG | grep "[[:space:]]$REPORT_NAME[[:space:]]" | awk '{print $3}'`
TRANGE=`grep "^REPORT_NAME"		$CONFIG | grep "[[:space:]]$REPORT_NAME[[:space:]]" | awk '{print $4}'`
TRANGE1=`echo $TRANGE | grep "MON_" | sed "s/MON_.*/MONITOR/"`	#checks if RANGE is of type MON[itor]
if [ Q$TRANGE1 = Q ]
then
	TRANGE1=NORMAL
fi

if [ $TRANGE1 != NORMAL ] && [ $TRENDING_TYPE != "HEALTH" ]
then
	echo "*** ERROR: this is a MONITOR report. It can be called only as TRENDING_TYPE = HEALTH."
	exit -1
fi

DESCR_FILE=`grep "^REPORT_NAME"		$CONFIG | grep "[[:space:]]$REPORT_NAME[[:space:]]" | awk '{print $8}'`

# find other group members if ${REPORT_GROUP} is defined (will create a navbar)
rm -f list_report_params
if [ $REPORT_GROUP != "NO" ]
then
	if [ ! -s $CONFIG_DIR/$REPORT_GROUP ]
	then
		echo "***WARNING: No group file $CONFIG_DIR/$REPORT_GROUP configured. Will be ignored."
		REPORT_GROUP="NO"
	else
		grep "^GROUP_MEMBERS"	$CONFIG_DIR/$REPORT_GROUP > list_report_params
	fi

	if [ ! -s list_report_params ]
	then
		echo "***WARNING: No group parameters set. Will be ignored."
		REPORT_GROUP="NO"
	fi	
fi

if [ "Q$DESCR_FILE" != "Q" ] && [ "Q$DESCR_FILE" != "QNONE" ]
then
	DESCR_TAG="<a href=$CONFIG_DIR/$DESCR_FILE>$DESCR_FILE</a>"
else
	DESCR_TAG="$DESCR_FILE"
fi

if [ Q$REPORT_TYPE = Q ]
then
	echo "***ERROR: mismatch between config file name and report name. Check REPORT_NAME section. Exit."
	rm -rf $JTMP_DIR
	exit
fi
		
case $REPORT_TYPE in
	"REPORT2"|"REPORT4"|"REPORT6"|"REPORT8"|"REPORT10"|"CUSTOM" ) : ;;
 * ) 		echo "***ERROR: REPORT_TYPE $REPORT_TYPE not supported. Exit."
	rm -rf $JTMP_DIR
	exit ;;
esac

# define where to find format definition
# V3.0: still needed for HTML output
if [ "${REPORT_TYPE}" != "CUSTOM" ]
then
	FDF="$DFO_BIN_DIR/trendPlotter"
else
	FDF="$CONFIG_DIR/${REPORT_NAME}.fdf"
	if [ ! -s $FDF ]
	then
		echo "***ERROR: no user-provided format definition file $FDF found. Exit."
		rm -rf $JTMP_DIR
		exit -1
	fi
fi

# check TRANGE for supported values
if [ $TRANGE1 = NORMAL ]
then
	case $TRANGE in
 	60|90|180|365|FULL|KPI ) : ;;
 	* ) 	echo "***ERROR: Time range $TRANGE not supported, must be in 60|90|180|365|FULL|KPI. Exit."
		rm -rf $JTMP_DIR
		exit  -1 ;;
	esac
# MONITOR
else
	TRANGE=`echo $TRANGE | sed "s/MON_//"`
	if [ $TRANGE -gt 20 ]
	then
		echo "***ERROR: Time range $TRANGE too big for MONITOR report, maximum value is 20. Exit."
		rm -rf $JTMP_DIR
		exit  -1
	elif [ $TRANGE -lt 1 ]
	then
		echo "***ERROR: Time range $TRANGE too small for MONITOR report, minimum value is 1. Exit."
		rm -rf $JTMP_DIR
		exit  -1
	fi
fi

if [ $TRANGE = FULL ] || [ $TRANGE = KPI ]
then
	START_DATE=`grep "^START_DATE" $CONFIG | awk '{print $2}'`
	if [ "Q$START_DATE" = "Q" ]
	then
		echo
		echo "***WARNING: no \$START_DATE configured in $CONFIG, can't do plots for TRANGE=$TRANGE. Exit."
		rm -rf $JTMP_DIR
		exit -1
	fi
fi

# special settings for TRANGE=KPI
if [ $TRANGE = KPI ]
then
	SCORE_FLAG=NO
	PRODUCT_SECTION=NO
	EDIT_PERMA=YES
fi

# =====================================================================================
# 0.6 link to webDocuSys
#     If the structure of the QC1_docu_plots and QC1_docu_KPI tables is changed,
#     this must be reflected here.
# =====================================================================================
# a) normal HC plot
echo "HC_report
definition
QC1_docu_link
QC1_table
QC1_parameter
HC_plot_index
HC_plot_label
scored
score_thresholds
score_justification
flag
version
last_update
PROP_status
PROP_ticket" > $JTMP_DIR/tp_pquery_plots

# b) KPI plot
if [ $TRANGE = KPI ]
then
	echo "KPI_plot
QC1_table
QC1_parameter
KPI_plot_index
KPI_plot_label
flag
scored
ref_value
ref_units
ref_source
version
last_update
PROP_status
PROP_ticket" > $JTMP_DIR/tp_pquery_plots
fi

if [ $TRANGE = KPI ]
then
	FILTER="filter_instrument=${DFO_INSTRUMENT}&filter_KPI_plot=trend_report_${REPORT_NAME}.html"
	QUERY_URL_PLOTS="${QC1DB_DOCU}&table=QC1_docu_KPI"
else
	FILTER="filter_instrument=${DFO_INSTRUMENT}&filter_HC_report=trend_report_${REPORT_NAME}_HC.html"
	QUERY_URL_PLOTS="${QC1DB_DOCU}&table=QC1_docu_plots"
fi

PARAM_QUERY_PLOTS=`cat $JTMP_DIR/tp_pquery_plots | sed "s/^.*/\&field_&=&/" | tr "\012" " " | sed "s/ //g"`
QC1_DOCU_PLOTS="${QUERY_URL_PLOTS}${PARAM_QUERY_PLOTS}&from=2000-10-01&to=2060-12-31&output=html"

case $INCLUDE_WEBDOCUSYS in
 "YES" ) QC1_DOCU_LINK="| <a href=\"${QC1_DOCU_PLOTS}&$FILTER');\" title=\"documentation about this HC report (prototype and test!)\"><font color=$HC_COLOR>HC_docu...</font></a>" ;;
 "NO" )  QC1_DOCU_LINK="<font color=white>| HC_docu...</font>" ;;
esac

case $INCLUDE_QUALITY_EVENTLOG in
 "YES" ) QUALITY_EVENTLOG_LINK="| <a href=\"${QUALITY_EVENTLOG_URL}\" title=\"Entries in quality event log (ESO internal)\"><font color=$HC_COLOR>quality events...</font></a>" ;;
 "NO" )  QUALITY_EVENTLOG_LINK="<font color=white>|</font> <a href=\"${QUALITY_EVENTLOG_URL}\" title=\"Entries in quality event log (ESO internal)\"><font color=white>quality events...</font></a>" ;;
esac

# =====================================================================================
# 0.7 get plots per report
# =====================================================================================

grep "^PLOT_NAME" $CONFIG | awk '{print $2,$3}' > $JTMP_DIR/list_reportplots

if [ ! -s $JTMP_DIR/list_reportplots ]
then
	echo "***ERROR: no plots defined for this report."
	rm -rf $JTMP_DIR
	exit
fi

# =====================================================================================
# 0.8 Date management
# procedure singleAccess (from scoreHC), BWo 2012-09-21
# $1: file to locked (maximum for 10 sec)
# =====================================================================================

singleAccess(){
# if found locked wait until unlocked
WAIT=10
while [ -f ${1}.lock ] && [ $WAIT -gt 0 ]
do
	if [ $WAIT = 10 ]
	then
		echo "    $1 found locked, waiting for $WAIT more secs ." | tr -d "\012"
	else
		echo "." | tr -d "\012"
	fi
	sleep 1
	WAIT=`echo $WAIT | awk '{print $1-1}'`
done

echo

# WAIT = 0: timeout (otherwise we may end up in endless loop)
# now lock this file to protect against overwrites by another trendPlotter
touch ${1}.lock
}

# =====================================================================================
# 0.8.1 MONITOR: we create date_$REPORT_NAME.dat on the fly
# =====================================================================================

if [ $TRANGE1 = MONITOR ]
then
	TODAY=`date -u +%Y-%m-%d`
	TODAY_H=`date -u +%H`
	TODAY_M=`date -u +%m`
	OFF=`echo $DFO_OFFSET | awk '{print 12+$1}'`
	if [ $TODAY_H -lt $OFF ]
	then
		export TODAY=`qcdate $TODAY -1`
	fi

	rm -f $CONFIG_DIR/date_${REPORT_NAME}.dat
	TR1=`echo $TRANGE | awk '{print $1-1}'`

	if [ $TRANGE != 1 ]
	then
        	TODAY_MIN=`qcdate $TODAY -$TR1`
	        TODAY1=`qcdate $TODAY +1`
        	MJD_MIN=`qcdate $TODAY_MIN`
        	MJD1=`qcdate $TODAY1`

		MJD=$MJD_MIN
        	D=$TODAY_MIN

		while [ $MJD -le $MJD1 ]
		do
			MJD_OFFSET=`echo $MJD $DFO_OFFSET | awk '{printf"%10.3f\n", $1+0.5+$2/24}'`
               		echo $D $MJD_OFFSET >> $CONFIG_DIR/date_${REPORT_NAME}.dat
               		D=`qcdate $D +1`
               		MJD=`echo $MJD | awk '{print $1+1}'`
        	done
		cat $CONFIG_DIR/date_${REPORT_NAME}.dat | awk '{print $1,$2,substr($1,6,2)}' > date1.dat
	else
		echo "start_date -0.125
22:00 -0.083
00:00 0.000
02:00 0.083
04:00 0.167
06:00 0.250
08:00 0.333
10:00 0.417
12:00 0.500
14:00 0.583
16:00 0.667
18:00 0.750
20:00 0.833
next_date 0.875
22:00 0.917
00:00 1.000" > $JTMP_DIR/date_${REPORT_NAME}.dat
	
		TODAY=`date -u +%Y-%m-%d`
		MJD_TODAY=`qcdate $TODAY`
		MJD_TIMESTAMP=`echo $MJD_TODAY $TODAY_H $TODAY_M | awk '{printf"%10.5f\n", $1+$2/24+$3/24/60}'`

# special window for $TRANGE=1: NOW -12/+3 hr (moving window with high time resolution during night)
# note that for fractional date lines, we work with limited accuracy
		MJD_MIN=`echo $MJD_TIMESTAMP | awk '{print $1-12/24}'`
		MJD_MAX=`echo $MJD_TIMESTAMP | awk '{print $1+3/24}'`

		cat $JTMP_DIR/date_${REPORT_NAME}.dat | awk '{printf"%5s %10.3f\n", $1,$2+mjd_today}' mjd_today=$MJD_TODAY > $CONFIG_DIR/date_${REPORT_NAME}.dat
		cat $CONFIG_DIR/date_${REPORT_NAME}.dat | awk '{print $1,$2}' > date1.dat
	fi
	
	cp date1.dat $REPORT_DIR/date_${REPORT_NAME}.dat
fi

# =====================================================================================
# 0.8.2 not MONITOR: we use date.dat and check if a new date needs to be added 
# =====================================================================================

if [ $TRANGE1 = NORMAL ]
then
	rm -f date_check
	TODAY=`date -u +%Y-%m-%d`
	THIS_MONTH="`date -u +%Y-%m`-01"
	echo "${THIS_MONTH}" > date_check
	MONTHplus1=`qcdate ${THIS_MONTH} +32 | cut -c1-7`
	echo "${MONTHplus1}-01" >> date_check
	MONTHplus2=`qcdate ${MONTHplus1}-01 +32 | cut -c1-7`
	echo "${MONTHplus2}-01" >> date_check
	
# needs protection in parallel mode:
# if found locked by another trendPlotter, wait until unlocked
	singleAccess $CONFIG_DIR/date.dat

# BWo 2011-01-18, according to suggestion from WHu
# check if new entry is needed in date.dat
	NEW_DATE=0
	for D1 in `cat date_check`
	do
		MONTH_INCLUDED=`grep "^$D1" $CONFIG_DIR/date.dat`
		if [ "Q$MONTH_INCLUDED" = "Q" ]
		then
			NEW_DATE=1
			break
		fi
	done

# update date.dat if new date
	if [ $NEW_DATE -eq 1 ]
	then
		cp $CONFIG_DIR/date.dat date.dat.new
		for D1 in `cat date_check`
		do
			MONTH_INCLUDED=`grep "^$D1" date.dat.new`
			if [ "Q$MONTH_INCLUDED" = "Q" ]
			then
				MJD_NEW=`qcdate $D1 | awk '{print $1+0.5}'`
				echo "$D1 $MJD_NEW" >> date.dat.new
				echo "[`date +%Y-%m-%d" "%H:%M:%S`]     trendPlotter: $D1 added to $CONFIG_DIR/date.dat" > tp_mail
			fi
		done
		cat date.dat.new | sort -u > $CONFIG_DIR/date.dat
	fi

	cat $CONFIG_DIR/date.dat | awk '{print $1,$2,substr($1,6,2)}' > date1.dat
	rm -f $CONFIG_DIR/date.dat.lock
	
	cp date1.dat $REPORT_DIR/
fi

# =====================================================================================
# 0.8.3 HISTORY: find latest $YMSTART if not specified
# =====================================================================================
# MONTH_DELAY: subtract $DELAY days from $TODAY, since last QC data will never be from TODAY

if [ $TRENDING_TYPE = HISTORY ]
then
	if [ $TRANGE = KPI ] || [ $TRANGE = FULL ]
	then
		echo "***ERROR: this is a $TRANGE plot, cannot be called as type $TRENDING_TYPE. Exit."
		rm -rf $JTMP_DIR
		exit
	fi

	CHECK_YMSTART=`echo $YMSTART | wc -c`
	if [ $CHECK_YMSTART != 8 ] && [ $CHECK_YMSTART != 1 ]
	then
		echo "***ERROR: \$YMSTART must have the format YYYY-MM (was: $YMSTART) ."
		rm -rf $JTMP_DIR
		exit
	fi
fi

if [ "${TRENDING_TYPE}" = "HISTORY" ] && [ "Q$YMSTART" = "Q" ]
then
	YEAR_DELAY=`qcdate  $TODAY -$DELAY | cut -c1-4`
	MONTH_DELAY=`qcdate $TODAY -$DELAY | cut -c6-7`
else
	MONTH_DELAY=`echo $YMSTART | cut -c6,7`
fi

# map start month to numbering scheme, for standard $TRANGE values
echo "01 1" > raster

case $TRANGE in
  "60" ) cat >> raster <<EOT
03 2
05 3
07 4
09 5
11 6
EOT
	;;
 "90" ) cat >> raster <<EOT
04 2
07 3
10 4
EOT
	;;
 "180") echo "07 2" >> raster ;;
esac

if [ "Q$MONTH_DELAY" != "Q" ]
then
	YINDEX=0
	for R in `cat raster | awk '{print $1}'`
	do
		if [ ${MONTH_DELAY} -ge $R ]
		then
			MONTH_R=$R
			YINDEX=`echo $YINDEX | awk '{print $1+1}'`
		else
			break
		fi
	done
fi

if [ "${TRENDING_TYPE}" = "HISTORY" ] && [ "Q$YMSTART" = "Q" ]
then
	YMSTART=`echo "${YEAR_DELAY}-${MONTH_R}"`
fi

if [ "${TRENDING_TYPE}" != "HISTORY" ]
then
	YMSTART=HEALTH
fi

# check for START_DATE being compatible with TRANGE
START_MONTH=`echo $START_DATE | cut -c6,7`
CHECK_COMP=`grep "^$START_MONTH" raster`
if [ "Q$CHECK_COMP" = "Q" ] && [ $TRANGE != FULL ]
then
	echo "***WARNING: Configured START_DATE ($START_DATE) is incompatible with configured TRANGE ($TRANGE). 
Possible values for MONTH in START_DATE:"
	cat raster | awk '{print $1}'
	echo "
Fix and start again."
	rm -rf $JTMP_DIR
	exit
fi

TMODE=PARTIAL

if [ $TRANGE = FULL ] || [ $TRANGE = KPI ]
then
	TO_MJD=`qcdate $TODAY | awk '{print $1+1}'`
	FROM_MJD=`qcdate ${START_DATE}-01`
	TMODE=$TRANGE
	TRANGE=`echo $TO_MJD $FROM_MJD | awk '{print $1-$2}'`
fi

# =====================================================================================
# 0.8.4 date ranges
# =====================================================================================

if [ $TRENDING_TYPE = "HEALTH" ] 
then
	DATE2=`qcdate $TODAY +2`
	TO_MJD=`qcdate $TODAY | awk '{print $1+2}'`
	FROM_MJD=`echo $TO_MJD $TRANGE | awk '{print $1-$2-1}'`
	DATE1=`qcdate $FROM_MJD`

	if [ $TRANGE1 = MONITOR ]
	then
		TODAY=`date -u +%Y-%m-%d`
		MJD_TODAY=`qcdate $TODAY`
		TODAY_H=`date -u +%H`
		TODAY_M=`date -u +%M`
# NOW:
		MJD_TIMESTAMP=`echo $MJD_TODAY $TODAY_H $TODAY_M | awk '{printf"%10.5f\n", $1+$2/24+$3/24/60}'`

# special window for $TRANGE=1: NOW -12/+3 hr (moving window with high time resolution during night)
		if [ $TRANGE = 1 ]
		then
			MJD_MIN=`echo $MJD_TIMESTAMP | awk '{printf"%10.5f\n", $1-12/24}'`
			MJD=`echo     $MJD_TIMESTAMP | awk '{printf"%10.5f\n", $1+3/24}'`
			FROM_MJD=$MJD_MIN
			TO_MJD=$MJD
		else
			FROM_MJD=`echo $TO_MJD $TRANGE | awk '{print $1-$2-2}'`
		fi
		DATE1=`qcdate $FROM_MJD`
	fi

	case $TMODE in
	 "PARTIAL"  ) DATE_DESC="(last $TRANGE days)" ;;
	 "FULL" ) DATE_DESC="(FULL range, $TRANGE days)" ;;
	 "KPI"  ) DATE_DESC="(KPI; FULL range, $TRANGE days)" ;;
	esac

	if [ $TRANGE = 1 ]
	then
		TODAY_H=`date -u +%H`
		OFF=`echo $DFO_OFFSET | awk '{print 12+$1}'`
		if [ $TODAY_H -lt $OFF ]
		then
			TODAY=`qcdate $TODAY -1`
		fi
		DATE_DESC="(current date: $TODAY)"
		DATE1=$TODAY
	fi

	YEAR=$TRENDING_TYPE
	HTAG="_HC"
fi

if [ $TRENDING_TYPE = "HISTORY" ] 
then
	DATE1=`echo ${YMSTART}-01`
	FROM_MJD=`qcdate $DATE1`

	TO_MJD=`echo $FROM_MJD $TRANGE | awk '{print $1+$2+1}'`
	DATE2=`qcdate $TO_MJD`
	DATE_DESC="($TRANGE days period)"

	YEAR=`echo ${YMSTART} | cut -c1-4`
	HTAG=_${YEAR}_$YINDEX
fi

# special configuration
if [ $DATE_RANGE = NO ]
then
	DATE_DESC=""
fi

# for data download: full year
DATE_YEAR=`qcdate $TO_MJD -365`

# for data download: all
DATE_ALL="1998-08-01"
DATE_ALL2="2098-12-31"

# =====================================================================================
# 0.8.5 jtimeDiff: javascript to calculate elapsed time
# =====================================================================================

jtimeDiff(){
YEAR_TS=`echo $TIMESTAMP | cut -c1-4`
MONTH_TS=`echo $TIMESTAMP | cut -c6,7 | awk '{print $1-1}'`
DAY_TS=`echo $TIMESTAMP | cut -c9,10`
HOUR_TS=`echo $TIMESTAMP | cut -c12,13`
MIN_TS=`echo $TIMESTAMP | cut -c15,16`
SEC_TS=`echo $TIMESTAMP | cut -c18,19`

cat >> htmltable <<EOT
<SCRIPT language="JavaScript"><!--
var today = new Date();
var tzdiff = today.getTimezoneOffset();
var Now = today.getTime() / 1000 + tzdiff*60;
var docStarted = new Date($YEAR_TS, $MONTH_TS, $DAY_TS, $HOUR_TS, $MIN_TS, $SEC_TS);
var Started = docStarted.getTime() / 1000;
	
var dayDiff = Math.floor((Now-Started) / 86400 );
var hourDiff = Math.floor(((Now-Started) - dayDiff*86400)/3600);
var hourDiff2 = ((hourDiff < 10) ? "d 0" : "d ");
var minDiff = Math.floor(((Now-Started) - dayDiff*86400 - hourDiff*3600)/60);
var minDiff2 = ((minDiff < 10) ? "h:0" : "h:");
	
var Month = today.getUTCMonth()+1;
var Month2 = ((Month < 10) ? "-0" : "-");
var Days = today.getUTCDate();
var Days2 = ((Days < 10) ? "-0" : "-");
var Hours = today.getUTCHours();
var Hours2 = ((Hours < 10) ? "T0" : "T");
var Minutes = today.getUTCMinutes();
var Minutes2 = ((Minutes < 10) ? ":0" : ":");
var Seconds = today.getUTCSeconds();
var Seconds2 = ((Seconds < 10) ? ":0" : ":");
	
document.write("<b>(", dayDiff, hourDiff2, hourDiff, minDiff2, minDiff,"m ago)</b>");

//--></SCRIPT>
<font size=1><noscript>[enable javascript for displaying time difference!]</noscript></font>
EOT
}

# =====================================================================================
# 0.8.6 javascript to display plot result table as tool tips
# =====================================================================================

cat > $JTMP_DIR/tp_plotresults <<EOT
<!-- java script for plot results -->
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
PlotResults = null;

document.onmousemove = updatePlotResults;

function updatePlotResults(e) {
        x = (document.all) ? window.event.x + document.body.scrollLeft : e.pageX;
        y = (document.all) ? window.event.y + document.body.scrollTop  : e.pageY;
        if (PlotResults != null) {
                PlotResults.style.left = (x + 17) + "px";
                PlotResults.style.top  = (y - 50) + "px";
        }
}

function showPlotResults(id) {
        PlotResults = document.getElementById(id);
        PlotResults.style.display = "block"
}

function hidePlotResults() {
        PlotResults.style.display = "none";
}
//--></SCRIPT>
<font size=1><noscript>[enable javascript for displaying plot results as tooltips!]</noscript></font>
EOT

# =====================================================================================
# 0.8.7 javascript to control browser refresh (code from getStatusAB)
# =====================================================================================

cat > $JTMP_DIR/offlink <<EOT
<!-- java script for switching off/on the refresh link-->
<script type="text/javascript">
function OffLink () {
  if (document.getElementById("refresh_link").firstChild.nodeValue = "[page auto-refreshes after $REFRESH sec]") {
      document.getElementById("refresh_link").firstChild.nodeValue = "[auto-refresh turned off, click \'on\' to resume] ";
      document.getElementById("refresh_link").className = "off";
} else {
      document.getElementById("refresh_link").firstChild.nodeValue = "on";
      }
}
</script>
EOT

# =====================================================================================
# 0.9 procedure checkValidity
# $NEWS_FILE is downloaded by scoreHC; analyzed here to be not outdated (younger than $MAX_AGE)
# =====================================================================================

checkValidity(){
rm -f ${JTMP_DIR}/mail_message

if [ -s $CONFIG_DIR/$NEWS_FILE ] 
then
	MSG_DATE=`cat $CONFIG_DIR/$NEWS_FILE | tr "\012" " " | awk '{print $1}' | sed "s/<!--SEP-->//" | sed "s/<!--COMMENT-->//"`

	if [ Q$MSG_DATE != Q ] 
	then
		echo "
5.3 Analyze validity of $NEWS_FILE ..."
		CHECK1=`echo $MSG_DATE | wc -c`		
		CHECK2=`echo $MSG_DATE | cut -c1,2`
		CHECK3=`echo $MSG_DATE | sed "s/-/ /"g | wc -w`
		if [ ${CHECK1}${CHECK2}${CHECK3} != 11203 ]
		then
			echo "***ERROR: $MSG_DATE does not seem to be a valid date."
			echo "Please fix the date (format: 20YY-MM-DD) to avoid this message." > ${JTMP_DIR}/mail_message
			cat $CONFIG_DIR/$NEWS_FILE | sed "s/<!--SEP-->//g" | sed "s/<!--COMMENT-->//" >> ${JTMP_DIR}/mail_message
			mail -s "trendPlotter: comment for ${REPORT_NAME} has invalid date" $OP_ADDRESS < ${JTMP_DIR}/mail_message
		else
			MSG_TIME=`date +%s -d $MSG_DATE | awk '{print $1}'`
	                TODAY_TIME=`date +%s`
       	         	DIFF=`echo $MSG_TIME $TODAY_TIME | awk '{print ($2-$1)/86400}'`
       		        ALERT=`echo $DIFF $MAX_AGE | awk '{ if ($1 > $2) print "alert!" }'`

			if [ Q$ALERT != Q ]
			then
				echo "   ***WARNING: Report $REPORT_NAME has outdated report news ($DIFF days old)." |  tee -a ${JTMP_DIR}/mail_message
				#echo "Outdated comments will be deleted with the next tool version. Content:" >> ${JTMP_DIR}/mail_message
				echo "Content:" >> ${JTMP_DIR}/mail_message
				echo "" >> ${JTMP_DIR}/mail_message
				cat $CONFIG_DIR/$NEWS_FILE | sed "s/<!--SEP-->//g" | sed "s/<!--COMMENT-->//" >> ${JTMP_DIR}/mail_message
				mail -s "trendPlotter: comment for ${REPORT_NAME} is outdated" $OP_ADDRESS < ${JTMP_DIR}/mail_message
			else
				echo "   ... report $NEWS_FILE: report news $DIFF days old, ok"
			fi
		fi
	fi
fi
}

# =====================================================================================
# 0.10 procedure getYMSTART: calculate all possible YMSTART values for HISTORY plot
# 	$1: START_DATE
# =====================================================================================

getYMSTART(){
rm -f $JTMP_DIR/list_history
# FULL/KPI/MONITOR reports: nothing to do
if [ $TMODE = FULL ] || [ $TMODE = KPI ] || [ $TRANGE1 = MONITOR ]
then
	return
fi

# others: translate RANGE in month steps
case $TRANGE in
        60  ) DELTAT=2  ;;
        90  ) DELTAT=3  ;;
        180 ) DELTAT=6  ;;
        365 ) DELTAT=12 ;;
esac

# start YEAR and MONTH
YEAR1=`echo $START_DATE | cut -c1-4`
MONTH=`echo $START_DATE | cut -c6-7 | awk '{printf"%2i\n",$1}'`

# current YEAR and MONTH
YEAR_NOW=`qcdate  $TODAY -$DELAY | cut -c1-4`
MONTH_NOW=`qcdate $TODAY -$DELAY | cut -c6-7`

while [ $YEAR1 -le $YEAR_NOW ]
do
        while [ $MONTH -le 12 ]
        do
                if [ $YEAR1 -ge $YEAR_NOW ]
                then
                        if [ $MONTH -gt $MONTH_NOW ]
                        then
                                break 2
                        fi
                fi

                echo $YEAR1 $MONTH |\
		 awk '{if ( $2 < 10 ) { print $1"-0"$2 } else { print $1"-"$2 }}' |\
		 sed "s/^.*/trendPlotter -r $REPORT_NAME -t HISTORY -s &/" >> $JTMP_DIR/list_history
                MONTH=`echo $MONTH | awk '{ print $1+delta }'  delta=$DELTAT`
        done

        if [ $MONTH -gt 12 ]
        then
                YEAR1=`echo $YEAR1 | awk '{print $1+1}'`
                MONTH=1
        fi
done
if [ -s $JTMP_DIR/list_history ]
then
	chmod u+x $JTMP_DIR/list_history
fi
}

# =====================================================================================
# 0.11 procedure navbarGroup: group navigation bars (_HC, _QUICK)
# $1: HISTORY or HEALTH
# =====================================================================================

navbarGroup(){
cat > navbarGroup <<EOT
  <tr height="10" align="left" valign="top">
    <td bgcolor=#eeeeee>
      <table border="0" cellspacing="1" cellpadding="0"><tr><td bgcolor="#FFFFFF">

      <table border="0" cellspacing="1" cellpadding="1">
      <tr bgcolor=#CCCCCC>
        <td valign=top ROWSPAN=2><b><font color="#666666" size=2>same group:</font></b></td>
EOT

for M in `cat list_report_params | awk '{print $3}'`
do
	LABEL=`grep "[[:space:]]${M}[[:space:]]" list_report_params | awk '{print $2}'`
	RFREQ=`grep "[[:space:]]${M}[[:space:]]" list_report_params | awk '{print $4}'`
#to cope with multiple SPACEs (SPACE1, SPACE2 etc.):
	M1=`echo $M | sed "s/.$//"`

	if ([ "Q$RFREQ" != "QDAILY" ] && [ "Q$RFREQ" != "QNONDAILY" ]) && [ $M != "SPACE" ] && [ $M1 != "SPACE" ]
	then
		RFREQ=NONDAILY
		echo "***INFO: pls. update $CONFIG_DIR/$REPORT_GROUP to contain the config key REPORT_FREQ!"
	fi

	if [ $M = "SPACE" ] || [ $M1 = "SPACE" ]
	then
		RFREQ=NONDAILY
	fi

	case $RFREQ in
	 "DAILY" ) RFREQ_TAG="<img src=\"${IMG_SRC}/${DAILY_MARKER}\" width=\"9\" height=\"9\" border=\"0\" align=\"left\" title=\"daily\">" ;;
	 *       ) RFREQ_TAG="" ;;
	esac
	
	TITLE=`grep "[[:space:]]${M}[[:space:]]" list_report_params | sed "s/&&$//" | sed "s/^.*&&//"`

	if [ "Q$LABEL" = "Q" ]
	then
		LABEL=`grep "[[:space:]]${M}$" list_report_params | awk '{print $2}'`
		TITLE=""
		echo "***INFO: no TITLE configured in $REPORT_GROUP. You may want to add a TITLE as column 5!"
	fi

	if ( [ $M = "SPACE" ] || [ $M1 = "SPACE" ] ) && [ $LABEL = "MARKER" ]
	then
		echo "        <td ROWSPAN=2 bgcolor=#cccccc>&nbsp;</td>" >> navbarGroup

	elif [ $M = $REPORT_NAME ]
	then
		echo "        <td bgcolor=${THIS_REPORT_COLOR} nowrap>${RFREQ_TAG}<font size=2><a title=\"$TITLE\">$LABEL</a></font></td>" >> navbarGroup

	else
# in rare non-standard cases, a link in the group file does not go to a report, then no config.tp_$M exists
		if [ -s $CONFIG_DIR/config.tp_$M ]
		then
			RTYPE=`grep "^REPORT_NAME" $CONFIG_DIR/config.tp_$M | awk '{print $4}'`
		fi
		case $RTYPE in
		"FULL" ) TARGET=FULL ;;
		*      ) TARGET=HEALTH ;;
		esac

		case $1 in
		 "HEALTH"  ) echo "        <td bgcolor=${OTHER_REPORT_COLOR} nowrap>${RFREQ_TAG}<font size=2><a href=\"$QC_URL/$TARGET/trend_report_${M}_HC.html\" title=\"$TITLE\">$LABEL</a></font></td>" >> navbarGroup ;;
		 "HISTORY" ) echo "        <td bgcolor=${OTHER_REPORT_COLOR} nowrap>${RFREQ_TAG}<font size=2><a href=\"$QC_URL/$TARGET/trend_report_${M}.html\" title=\"$TITLE\">$LABEL</a></font></td>" >> navbarGroup ;;
		esac
	fi
done

echo "      </tr>" >> navbarGroup

if [ $1 = HEALTH ] && [ $SCORE_FLAG = YES ]
then
	cat >> navbarGroup <<EOT

      <tr bgcolor=#CCCCCC>
EOT

	for M in `cat list_report_params | awk '{print $3}'`
	do
		M1=`echo $M | sed "s/.$//"`
		if [ $M != "SPACE" ] && [ $M1 != "SPACE" ] 
		then
			echo "        <td><!--#include virtual=\"${VNAVBAR_URL}/score_${M}.html\" --></td>" >> navbarGroup
		fi
	done

	cat >> navbarGroup <<EOT
        </tr>
EOT
fi

cat >> navbarGroup <<EOT
      </table>

    </td></tr></table>

    </td>
  </tr>
EOT

chmod uo+rwx,o+rx,o-w navbarGroup
if [ $1 = HEALTH ] && [ $SCORE_FLAG = YES ]
then
	cat navbarGroup | sed "s/_HC.html/_QUICK.html/" > navbarGroup_QUICK
	chmod uo+rwx,o+rx,o-w navbarGroup_QUICK
fi
}

# =====================================================================================
# 0.12 procedure navbarHistory: horizontal navbar for HISTORY plots
# =====================================================================================

navbarHistory(){
START_DATE=`grep "^START_DATE" 	$CONFIG | awk '{print $2}'`

if [ $TMODE = PARTIAL ]
then
	getYMSTART $START_DATE
fi

# find previous DATE in list_history
if [ $YMSTART != HEALTH ] && [ $YMSTART != CURRENT ] && [ $YMSTART != FULL ]
then
	LINE_PREV=`cat $JTMP_DIR/list_history | grep -n $YMSTART | sed "s/:.*//" | awk '{print $1-1}'`
	LINE_NEXT=`cat $JTMP_DIR/list_history | grep -n $YMSTART | sed "s/:.*//" | awk '{print $1+1}'`
	MAX_NUM=`cat   $JTMP_DIR/list_history | wc -l`

	YEAR=`echo $YMSTART | cut -c1-4`
	YEARminus1=`echo $YMSTART | cut -c1-4 | awk '{print $1-1}'`
	YEARplus1=`echo $YMSTART  | cut -c1-4 | awk '{print $1+1}'`

# get links to previous and next plots
	if [ $LINE_PREV = 0 ]
	then
      		PREV_LINK="<img src=${IMG_SRC}/itemOff.gif width=13 height=13 border=0>"
	else
		PREV_DATE=`cat $JTMP_DIR/list_history | awk '{print "INDEX"NR,$7}' | grep "INDEX$LINE_PREV" | awk '{print $2}'`
		PREV_YEAR=`echo $PREV_DATE | cut -c1-4`	
		PREV_MONTH=`echo $PREV_DATE | cut -c6,7`
		PREV_INDEX=`grep "^$PREV_MONTH" raster | awk '{print $2}'`
		PREV_LINK="${QC_URL}/$PREV_YEAR/${REPORT_NAME}/trend_report_${REPORT_NAME}_${PREV_YEAR}_${PREV_INDEX}.html"
		PREV_LINK="      <a href=${PREV_LINK} title=\"previous\"><img src=${IMG_SRC}/arr1redleft.gif width=11 height=11 border=0></a>"
	fi

	if [ $LINE_NEXT -gt $MAX_NUM ]
	then
		NEXT_LINK="${QC_URL}/HEALTH/trend_report_${REPORT_NAME}.html"
	        NEXT_LINK="      <a href=${NEXT_LINK} title=\"next\"><img src=${IMG_SRC}/arr1red.gif width=11 height=11 border=0></a>"
	else
		NEXT_DATE=`cat $JTMP_DIR/list_history | awk '{print "INDEX"NR,$7}' | grep "INDEX$LINE_NEXT" | awk '{print $2}'`
		NEXT_YEAR=`echo $NEXT_DATE | cut -c1-4`	
		NEXT_MONTH=`echo $NEXT_DATE | cut -c6,7`
		NEXT_INDEX=`grep "^$NEXT_MONTH" raster | awk '{print $2}'`
		NEXT_LINK="${QC_URL}/$NEXT_YEAR/${REPORT_NAME}/trend_report_${REPORT_NAME}_${NEXT_YEAR}_${NEXT_INDEX}.html"
	        NEXT_LINK="      <a href=${NEXT_LINK} title=\"next\"><img src=${IMG_SRC}/arr1red.gif width=11 height=11 border=0></a>"

# to have last HISTORY plot
		LAST_YEAR="$NEXT_YEAR"
		LAST_INDEX="$NEXT_INDEX"
	fi

# CURRENT navbar
elif [ $YMSTART = "CURRENT" ]
then
	PREV_LINK="${QC_URL}/$LAST_YEAR/${REPORT_NAME}/trend_report_${REPORT_NAME}_${LAST_YEAR}_${LAST_INDEX}.html"
	PREV_LINK="      <a href=${PREV_LINK} title=\"previous\"><img src=${IMG_SRC}/arr1redleft.gif width=11 height=11 border=0></a>"
      	NEXT_LINK="<img src=${IMG_SRC}/itemOff.gif width=11 height=11 border=0>"

# HEALTH navbar
elif [ $YMSTART = "HEALTH" ] && [ -s $JTMP_DIR/list_history ]
then
	LAST_ENTRY=`cat $JTMP_DIR/list_history | awk '{print $7}' | sort -r | sed "2,$ d"`
	LAST_YEAR=`echo $LAST_ENTRY | cut -c1-4`
	LAST_MONTH=`echo $LAST_ENTRY | cut -c6,7`
	LAST_INDEX=`grep "$LAST_MONTH" raster | awk '{print $2}'`

	PREV_LINK="${QC_URL}/${LAST_YEAR}/${REPORT_NAME}/trend_report_${REPORT_NAME}_${LAST_YEAR}_${LAST_INDEX}.html"
   	NEXT_LINK="<img src=${IMG_SRC}/itemOff.gif width=11 height=11 border=0>"
fi

# fill HTML code 
cat > navbarHistory <<EOT
<script language="JavaScript">
<!--
function openWin(URL) {
  aWindow=window.open(URL,"a","toolbar=yes,width=1000,height=700,scrollbars=yes,menubar=yes");
  }
function openWin2(URL) {
  aWindow=window.open(URL,"b","toolbar=yes,width=1500,height=700,scrollbars=yes,menubar=yes");
}
//-->
</script>

<!-- history navbar -->
  <tr height="10">
EOT

# fill the histo navbar
if [ $YMSTART = HEALTH ] || [ $TMODE = FULL ] || [ $TMODE = KPI ] 
then
	if [ $SCORE_FLAG = NO ]
	then
		HC_COLOR1=#FFFFFF #make invisible, placeholder only
		HC_COLOR2=#FFFFFF
		RIGHT=itemOff.gif
		HC_TITLE=""
		HC_LINK=""
		HC_LINK2=""
	else
		HTML_NAME_QUICK="trend_report_${REPORT_NAME}_QUICK.html"
		HC_COLOR1=#333333
		HC_COLOR2=#000000
		RIGHT=right.gif
		HC_TITLE="title=\"scores and comments (quick-look version)\""
		HC_LINK="<a class=\"nav-subitem\" href=\"$HTML_NAME_QUICK\" ${HC_TITLE}>"
		HC_LINK2="</a>"
	fi

	cat >> navbarHistory <<EOT
    <td bgcolor=#ffffff>
<table border="0" cellspacing="0" cellpadding="0" width=100%>
  <tr bgcolor=#ffffff>
    <td width=75% nowrap><font size=1><br>
	<img src=${IMG_SRC}/$RIGHT width=10 height=11 border=0 hspace=1>${HC_LINK}<font color=${HC_COLOR1}>scores&comments$HC_LINK2 </font> 
        <font color=${HC_COLOR2}> | </font>
EOT

# FULL symbol
	if [ $FULL_NAME != NONE ]
	then
		cat >> navbarHistory <<EOT
 <a href="javascript:openWin('${QC_URL}/FULL/trend_report_${FULL_NAME}.html');" title="full record"><img src=${IMG_SRC}/analog/sq3.png width=10 height=10 border=0></a> 
 <a href="javascript:openWin('${QC_URL}/FULL/trend_report_${FULL_NAME}.html');" title="full record"><font color=$HC_COLOR>FULL</font></a>
 <font color=${HC_COLOR2}> | </font>
EOT
	fi

# no history offered for FULL or MONITOR plots 
	if [ $TMODE = FULL ] || [ $TMODE = KPI ]
	then
		cat > navbarHistory1 <<EOT
 <font color=$HC_COLOR>history: all data plotted here</font>
EOT
	elif [ $TRANGE1 != NORMAL ]
	then
		cat > navbarHistory1 <<EOT
 <font color=$HC_COLOR> history: none</font>
EOT
# HEALTH: just a teaser "history ..."
	else
		cat > navbarHistory1 <<EOT
 <a href="javascript:openWin('${PREV_LINK}');" title="open historical record"><font color=$HC_COLOR>history ...</font></a>
EOT
	fi

	if [ $HISTO_FLAG = NO ]
	then
		cat > navbarHistory1 <<EOT
 <font color=$HC_COLOR> history: none</font>
EOT
	fi

	cat navbarHistory1 >> navbarHistory

	if [ "$RELATED_URL" != "NONE" ] 
	then
		cat >> navbarHistory <<EOT
 | <a href=${RELATED_URL} target=_blank title="tutorial information about this report"><font color=$HC_COLOR>plot tutorial ...</font></a>
EOT
	else
		cat >> navbarHistory <<EOT
 | <font color=$HC_COLOR> plot tutorial: n/a</font>
EOT
	fi

	if [ "Q$EMAIL" != Q ]
	then
		cat >> navbarHistory <<EOT
 | <a href="mailto:${EMAIL}?subject=${DFO_INSTRUMENT} ${HTML_NAME}" title="mail about this report"><font color=$HC_COLOR>contact</font></a>
EOT
	fi

	cat >> navbarHistory <<EOT
 $QC1_DOCU_LINK $QUALITY_EVENTLOG_LINK
EOT

	RFREQ=`grep "[[:space:]]${REPORT_NAME}[[:space:]]" list_report_params | awk '{print $4}'`

	if ([ "Q$RFREQ" != "QDAILY" ] && [ "Q$RFREQ" != "QNONDAILY" ])
	then
		RFREQ=NONDAILY
	fi

	if [ "$RFREQ" = "DAILY" ] && [ "Q$PLOT_LABEL" = "Q" ]
	then
		cat >> navbarHistory <<EOT
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <img src="${IMG_SRC}/${DAILY_MARKER}" width="11" height="11" border="0" title="daily"><font size=1 color=#333333><i>&nbsp; daily/often; important to check &nbsp; &nbsp;</i> [<a href="javascript:openWin('${HELP_URL}#daily');" title="info about navigation">?</a></font>]
EOT
	elif [ "$RFREQ" = "DAILY" ] && [ "Q$PLOT_LABEL" != "Q" ]
	then
		cat >> navbarHistory <<EOT
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <img src="${IMG_SRC}/${DAILY_MARKER}" width="11" height="11" border="0" title="daily"><font size=1 color=#333333><i>&nbsp; daily &nbsp; &nbsp; </i>[<a href="javascript:openWin('${HELP_URL}#daily');" title="info about navigation">?</a>]
EOT
	else
		cat >> navbarHistory <<EOT
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <font size=1 color=#000000><a href="javascript:openWin('${HELP_URL}#navigation');" title="info about navigation">[?]</a></font>
EOT
	fi
	echo "    </td>" >> navbarHistory

# YMSTART: full details for current year plus/minus 1, only a teaser for earlier/later years
# (to avoid over-population of navbar)
else
	cat >> navbarHistory <<EOT
    <td bgcolor=#CCCCCC>

<table border="0" cellspacing="1" cellpadding="1" width=100%>
  <tr>
    <td>

<table border="0" cellspacing="1" cellpadding="1">
  <tr bgcolor=#CCCCCC>
    <td ROWSPAN=2 valign=bottom nowrap><a href="javascript:self.close();"><font size=2 color=#FFFFFF>close window</a></td>
    <td bgcolor=#CCCCCC><b><font size="2" color="$FTCOLOR">history:</font></b></td>
EOT

	for Y in `cat $JTMP_DIR/list_history | awk '{print $7}' | cut -c1-4 | sort -u`
	do
		case $Y in
		 $YEARminus1|$YEAR|$YEARplus1 ) COLSPAN=`grep "$Y" $JTMP_DIR/list_history | wc -l` ; SPACE="    <td bgcolor=#FFFFFF ROWSPAN=2>&nbsp;</td>" ;;
		 *                            ) COLSPAN="1" ; SPACE="";
		esac
# RANGE=365: no SPACE 
		if [ $COLSPAN = "1" ]
		then
			SPACE=""
		fi

		cat >> navbarHistory <<EOT
    <td bgcolor=#FFFFFF COLSPAN=$COLSPAN align=center><b><font size="2" color="$FTCOLOR">$Y</font></b></td>
$SPACE
EOT
	done

	if [ $COLSPAN = "1" ]
	then
		cat >> navbarHistory <<EOT
    <td bgcolor=#FFFFFF ROWSPAN=2>&nbsp;</td>
EOT
	fi

	cat >> navbarHistory <<EOT
    <td bgcolor=#FFFFFF><b><font size="2" color="$FTCOLOR">HEALTH</font></b></td>
EOT

	if [ $FULL_NAME != NONE ]
	then
		cat >> navbarHistory <<EOT
    <td bgcolor=#FFFFFF><b><font size="2" color="$FTCOLOR">FULL</font></b></td>
EOT
	fi

	cat >> navbarHistory <<EOT
  </tr>
  
  <tr>
    <td bgcolor=#CCCCCC align=right>
${PREV_LINK} &nbsp;
${NEXT_LINK} 
    </td>
EOT

	for YMS in `cat $JTMP_DIR/list_history | awk '{print $7}'`
	do
		Y=`echo $YMS | cut -c1-4`
		M=`echo $YMS | cut -c6,7`
		I=`grep "^$M" raster | awk '{print $2}'`

		case "$YMS" in
		 "$YMSTART" ) IMG=sq0.png ;;
		 *          ) IMG=sqg.png ;;
		esac

		case $Y in
		 $YEARminus1|$YEAR|$YEARplus1 ) 
			LINK="${QC_URL}/$Y/${REPORT_NAME}/trend_report_${REPORT_NAME}_${Y}_${I}.html"
			echo "    <td bgcolor=#FFFFFF align=right><a href=$LINK title=\"historical plot for $YMS\"><img src=${IMG_SRC}/analog/$IMG width=7 height=7 border=0></a></td>" >> navbarHistory ;;
		 * )
			if [ $M != "01" ] && [ $YMS != $START_DATE ]
			then
				continue
			else
				LINK="${QC_URL}/$Y/${REPORT_NAME}/trend_report_${REPORT_NAME}_${Y}_${I}.html"
				echo "    <td bgcolor=#FFFFFF align=right><a href=$LINK title=\"historical plots for $Y\">...</a></td>" >> navbarHistory 
			fi ;;
		esac
	done

	LINK="${QC_URL}/HEALTH/trend_report_${REPORT_NAME}.html"
	case $YMSTART in
	 "CURRENT" ) IMG=sq0.png ;;
	 *         ) IMG=sq1.png ;;
	esac

	cat >> navbarHistory <<EOT
    <td bgcolor=#FFFFFF align=center><a href=$LINK title="current (Health Check) plot"><img src=${IMG_SRC}/analog/$IMG width=10 height=10 border=0></a></td>
EOT

	if [ $FULL_NAME != NONE ]
	then
		LINK="${QC_URL}/FULL/trend_report_${FULL_NAME}.html"
		case $YMSTART in
	 	   "FULL" ) IMG=sq0.png ;;
	 	    *	  ) IMG=sq3.png ;;
		esac
		cat >> navbarHistory <<EOT
    <td bgcolor=#FFFFFF align=center><a href=$LINK title="full record"><img src=${IMG_SRC}/analog/$IMG width=10 height=10 border=0></a></td>
EOT
	fi
	
	cat >> navbarHistory <<EOT
      </tr>
    </table>
    </td>
EOT
fi

cat >> navbarHistory <<EOT
    <td align=right valign=bottom nowrap><font size=1 color=#333333><i>*Date on this monitor changes at ${BOUNDARY}:00 UT</font></td>
  </tr>
</table>

    </td>
  </tr>
EOT

if [ $YMSTART = HEALTH ] || [ $TMODE = FULL ]
then
	cat navbarHistory | sed "/_QUICK/s/scores and comments (quick-look version)/HC plot (full version)/" | sed "/_QUICK/s/>scores&comments/>HC plot/" | sed "/_QUICK/s/_QUICK/_HC/" > navbarHistory_QUICK
fi
}

# =====================================================================================
# 0.13 procedure checkError ($1: file name)
# =====================================================================================
checkError(){
CHECK_ERROR1=`grep "CT-LIBRARY error" $1`
CHECK_ERROR2=`grep "connect" $1`
CHECK_ERROR=`echo $CHECK_ERROR1 $CHECK_ERROR2`
if [ "Q$CHECK_ERROR" != "Q" ]
then
	echo ""
	echo "*** ERROR on connection to QC1 database. Can't proceed with trendPlotter, exit."
	rm -rf $JTMP_DIR
	exit
fi
}

# =====================================================================================
# 1. getting started
# =====================================================================================

PNG_NAME="trend_report_${REPORT_NAME}${HTAG}.png"
HTML_NAME="trend_report_${REPORT_NAME}${HTAG}.html"

case $TRENDING_TYPE in
 "HEALTH" ) NAVBAR_NAME="navbar_${REPORT_NAME}.html" ;;
 *        ) NAVBAR_NAME="navbar_${REPORT_NAME}${HTAG}.html" ;;
esac

GROUP_NAVBAR="group_${REPORT_NAME}.html"

# =====================================================================================
# 1.1 Cleanup
# statFiles contains the statistical result files used by scoreQC and scoreHC
# =====================================================================================

if [ ! -d $REPORT_DIR/statFiles ]
then
	mkdir $REPORT_DIR/statFiles
fi

rm -rf $REPORT_DIR/recent

# =====================================================================================
# 1.2 Prologue
# =====================================================================================

if [ $CREATE_NAVBAR = YES ]
then
	echo ""
	if [ $REPORT_GROUP != "NO" ]
	then
		echo "This mode creates/refreshes all HISTORY and group navigation bars for $REPORT_NAME."
	else
		echo "This mode creates/refreshes all HISTORY navigation bars for $REPORT_NAME."
	fi

	echo "  Without option -f: creates them locally 
  With option -f:    transfers them to web server"

	if [ $SCP = "NO" ]
	then
		echo " Hit return:"
        	read input
	fi
fi

if [ $HIST_MODE = ALL ]
then
	echo "
This mode creates an executable job file \$TMP_DIR/list_history which can be used
to create all HISTORY plots for $REPORT_NAME.

By default, all entries come with -f option (web transfer), and the file may be executed
right away. You may want to edit the execution file to exclude option -f (no web transfer)
which may be useful for testing.
"      
fi

echo "====================================================================
trendPlotter: 	version $TOOL_VERSION

Report name:	${REPORT_NAME}"

if [ $TMODE = FULL ] || [ $TMODE = KPI ]
then
	echo "Type of report:	${TRENDING_TYPE} ($TMODE)"
else
	echo "Type of report:	${TRENDING_TYPE} ($TRANGE1)"
fi

echo "Time range: 	$DATE1 ... $DATE2"

if [ $REPORT_GROUP != "NO" ]
then
	echo "Report group:	$REPORT_GROUP"
fi

if [ "Q$INFO" != "Q" ]
then
	echo $INFO
fi

echo ""
if [ $CREATE_NAVBAR = YES ]
then
	echo "Output:		HISTORY and group navigation bars" 
elif [ $HIST_MODE = ALL ]
then
 	echo "Output:		HISTORY mode execution file" 
else
 	echo "Output:		\$REPORT_DIR/$HTML_NAME" 
fi

if [ "$HIST_MODE" = "NO" ]
then
	if [ $SCP = "yes" ]
	then
		echo "Product files are transfered to web server."
	else
		echo "Product files are *not* transfered to web server."
	fi 
fi 

echo "===================================================================="
echo ""

# =====================================================================================
# 1.3 HIST_MODE = ALL: prepare history plot file and exit
# =====================================================================================

if [ "$HIST_MODE" = ALL ]
then
	START_DATE=`grep "^START_DATE" 	$CONFIG | awk '{print $2}'`
	getYMSTART $START_DATE

	echo "trendPlotter -r $REPORT_NAME -t HEALTH" >> $JTMP_DIR/list_history
	echo "trendPlotter -r $REPORT_NAME -N" >> $JTMP_DIR/list_history
	cat $JTMP_DIR/list_history | sed "s/^.*/& -f/" > $JTMP_DIR/list_history1
	mv $JTMP_DIR/list_history1 $TMP_DIR/list_history
	chmod u+x $TMP_DIR/list_history
	echo "Find executable file to create all history plots in \$TMP_DIR/list_history ."
	rm -rf $JTMP_DIR
	exit
fi

# =====================================================================================
# 1.4 CREATE_NAVBAR=YES: create navbars and exit
# 1.4.1 group navbars
# =====================================================================================

if [ "$CREATE_NAVBAR" = YES ]
then

	echo "create group navigation bars ..."
	if [ $SCP = "yes" ]
	then
		ssh -o BatchMode=yes ${DFO_WEB_SERVER} "bin/qcDircheck $QC_DIR" 1>/dev/null
		ssh -o BatchMode=yes ${DFO_WEB_SERVER} "bin/qcDircheck ${QC_DIR}/HEALTH" 1>/dev/null
	fi

	if [ $REPORT_GROUP != "NO" ]
	then
		GROUP_NAVBAR="group_${REPORT_NAME}_HC.html"
		navbarGroup HEALTH
		mv navbarGroup $GROUP_NAVBAR	

		if [ $SCORE_FLAG = YES ]
		then
			GROUP_NAVBAR_QUICK="group_${REPORT_NAME}_QUICK.html"
			mv navbarGroup_QUICK $GROUP_NAVBAR_QUICK
		fi

		if [ $SCP = "yes" ]
		then
			scp -o BatchMode=yes ${JTMP_DIR}/$GROUP_NAVBAR ${DFO_WEB_SERVER}:${QC_DIR}/HEALTH	

			if [ $SCORE_FLAG = YES ]
			then
				scp -o BatchMode=yes ${JTMP_DIR}/$GROUP_NAVBAR_QUICK ${DFO_WEB_SERVER}:${QC_DIR}/HEALTH	
			fi
		else
			mv $GROUP_NAVBAR $REPORT_DIR/
			if [ $SCORE_FLAG = YES ]
			then
				mv navbarGroup_QUICK $REPORT_DIR/
			fi
		fi

		GROUP_NAVBAR="group_${REPORT_NAME}.html"
		navbarGroup HISTORY
		mv navbarGroup $GROUP_NAVBAR
		if [ $SCP = "yes" ]
		then
			scp -o BatchMode=yes ${JTMP_DIR}/$GROUP_NAVBAR ${DFO_WEB_SERVER}:${QC_DIR}/HEALTH 
			echo "... scp'ed to ${DFO_WEB_SERVER}."
		else
			mv $GROUP_NAVBAR $REPORT_DIR/
			echo "... created under \${DFO_TREND_DIR}/reports."
		fi
		echo ""
	fi

# =====================================================================================
# 1.4.2 Info files
# ====================================================================================

	if [ -s $CONFIG_DIR/${DESCR_FILE} ] && [ $SCP = "yes" ]
	then
		echo "transfer ${DESCR_FILE} ..."
		rm -f $DESCR_FILE
		cat $CONFIG_DIR/$DESCR_FILE | sed "s/<p>/<\/font>&<font size=\"2\">/" > $DESCR_FILE
		scp -o BatchMode=yes ${DESCR_FILE} ${DFO_WEB_SERVER}:${QC_DIR}/HEALTH 1>/dev/null
	fi

	if [ -s $CONFIG_DIR/${INFO_FILE} ] && [ $SCP = "yes" ]
	then
		echo "transfer ${INFO_FILE} ..."
		scp -o BatchMode=yes ${CONFIG_DIR}/${INFO_FILE} ${DFO_WEB_SERVER}:${QC_DIR}/HEALTH 1>/dev/null
	fi

	if [ -s $CONFIG_DIR/${DFO_INSTRUMENT}_POSTIT ] && [ $SCP = "yes" ]
	then
		echo "transfer ${DFO_INSTRUMENT}_POSTIT ..."
		scp -o BatchMode=yes ${CONFIG_DIR}/${DFO_INSTRUMENT}_POSTIT ${DFO_WEB_SERVER}:${QC_DIR} 1>/dev/null
	fi

# =====================================================================================
# 1.4.3 HEALTH and HISTORY navbars 
# =====================================================================================

# HEALTH navbar 
	CHECK_START=`grep "^START_DATE" $CONFIG`
	if [ "Q$CHECK_START" = "Q" ]
	then
		echo "***INFO: Report not configured for HISTORY information, no HISTORY navigation bars created."
		rm -rf $JTMP_DIR
		exit
	fi
	
	echo "create navigation bars ..."
	echo "  create HEALTH navigation bar ..."
	YMSTART=HEALTH
	NAVBAR_NAME="navbar_${REPORT_NAME}_HC.html"
	NAVBAR_NAME_QUICK="navbar_${REPORT_NAME}_QUICK.html"

	if [ $HISTORY_SECTION = YES ]
	then
		navbarHistory
		mv navbarHistory 	$NAVBAR_NAME
		mv navbarHistory_QUICK  $NAVBAR_NAME_QUICK

		if [ $SCP = "yes" ]
		then
			if [ $TMODE = FULL ] 
			then
				scp -o BatchMode=yes $JTMP_DIR/$NAVBAR_NAME $JTMP_DIR/$NAVBAR_NAME_QUICK ${DFO_WEB_SERVER}:${QC_DIR}/FULL
			elif [ $TMODE = KPI ]
			then
				scp -o BatchMode=yes $JTMP_DIR/$NAVBAR_NAME ${DFO_WEB_SERVER}:${QC_DIR}/FULL
				rm $JTMP_DIR/$NAVBAR_NAME_QUICK
			else
				scp -o BatchMode=yes $JTMP_DIR/$NAVBAR_NAME $JTMP_DIR/$NAVBAR_NAME_QUICK ${DFO_WEB_SERVER}:${QC_DIR}/HEALTH
			fi
		else
			mv $JTMP_DIR/$NAVBAR_NAME 	$REPORT_DIR
			mv $JTMP_DIR/$NAVBAR_NAME_QUICK $REPORT_DIR
		fi
	fi

# all HISTORY navbars 
	START_DATE=`grep "^START_DATE" 	$CONFIG | awk '{print $2}'`

	getYMSTART $START_DATE

	if [ -s $JTMP_DIR/list_history ]
	then
		for YMSTART in `cat $JTMP_DIR/list_history | awk '{print $7}'`
		do
			echo "  create HISTORY navigation bar for $YMSTART ..."
			YEAR=`echo $YMSTART | cut -c1-4`
			MONTH=`echo  $YMSTART | cut -c6,7`
			INDEX=`grep "^$MONTH" raster | awk '{print $2}'`
			NAVBAR_NAME="navbar_${REPORT_NAME}_${YEAR}_$INDEX.html"

			navbarHistory
			mv navbarHistory $NAVBAR_NAME

			if [ $SCP = "yes" ]
			then
				ssh -o BatchMode=yes ${DFO_WEB_SERVER} "bin/qcDircheck $QC_DIR/$YEAR" 1>/dev/null
				ssh -o BatchMode=yes ${DFO_WEB_SERVER} "bin/qcDircheck ${QC_DIR}/$YEAR/$REPORT_NAME" 1>/dev/null

				scp -o BatchMode=yes $JTMP_DIR/${NAVBAR_NAME} ${DFO_WEB_SERVER}:${QC_DIR}/$YEAR/$REPORT_NAME 
			else
				mv $NAVBAR_NAME $REPORT_DIR/
			fi
		done
	fi

# CURRENT navbar
	if [ $HISTORY_SECTION = YES ] && [ -s $JTMP_DIR/list_history ]
	then
		echo "  create CURRENT navigation bar ..."
		YMSTART=CURRENT
		NAVBAR_NAME="navbar_${REPORT_NAME}.html"
		navbarHistory
		mv navbarHistory $NAVBAR_NAME

		if [ $SCP = "yes" ]
		then
			scp -o BatchMode=yes $JTMP_DIR/$NAVBAR_NAME ${DFO_WEB_SERVER}:${QC_DIR}/HEALTH
		else
			mv $NAVBAR_NAME $REPORT_DIR/
		fi

# FULL navbar (only if TMODE = FULL)
		if [ $FULL_NAME != NONE ]
		then
			echo "create FULL navigation bar ..."
			YMSTART=FULL
			NAVBAR_NAME="navbar_${FULL_NAME}.html"
			navbarHistory
			mv navbarHistory $JTMP_DIR/$NAVBAR_NAME

			if [ $SCP = "yes" ]
			then
				scp -o BatchMode=yes $JTMP_DIR/$NAVBAR_NAME ${DFO_WEB_SERVER}:${QC_DIR}/FULL
			else
				mv $NAVBAR_NAME $REPORT_DIR/
			fi
		fi
	fi

# done
	if [ $SCP = "yes" ]
	then
		echo "... scp'ed to ${DFO_WEB_SERVER}."
	else
		echo "... created under \${DFO_TREND_DIR}/reports."
	fi
	rm -rf $JTMP_DIR
	exit
fi

# =====================================================================================
# 2.0 Loop over all plots per report
#     $P is the name of the plot, $P_INDEX its index 
# =====================================================================================

echo "1. Load data ..."

DFO_DATE=0
PSO_DATE=0
MIN_MJD=0
MAX_MJD=0

for P in `cat list_reportplots | awk '{print $2}'`
do
	P_INDEX=`grep "\<$P\>" list_reportplots | awk '{print $1}'`
	echo "   Plot $P_INDEX: $P"

	rm -f list_plots
	grep "^PLOT_NAME" $CONFIG | grep "[[:space:]]$P[[:space:]]" > list_plots

# dynamic configuration
	if [ $TRENDING_TYPE = HISTORY ] && [ -s $DELTA ] 
	then
		MATCH_YEAR=`echo $YMSTART | cut -c1-4 | sed "s/^.*/&_ALL/"`
		CHECK_DYNAMIC=`grep "^PLOT_NAME" $DELTA | grep "[[:space:]]$P[[:space:]]" | grep $YMSTART`
		CHECK_YEAR=`grep    "^PLOT_NAME" $DELTA | grep "[[:space:]]$P[[:space:]]" | grep $MATCH_YEAR`
		CHECK_ALL=`grep     "^PLOT_NAME" $DELTA | grep "[[:space:]]$P[[:space:]]" | grep HISTORY_ALL`
		if [ "Q$CHECK_DYNAMIC" != "Q" ]
		then
			echo $CHECK_DYNAMIC > list_plots
		elif [ "Q$CHECK_YEAR" != "Q" ]
		then
			echo $CHECK_YEAR    > list_plots
		elif [ "Q$CHECK_ALL" != "Q" ]
		then
			echo $CHECK_ALL     > list_plots
		fi
	fi

	if [ ! -s list_plots ]
	then
		echo "***ERROR: Plot $P listed but no parameters defined. Check!"
	fi

# =====================================================================================
# 2.1 Plot parameters, data source
# =====================================================================================

	XAXIS=`cat	list_plots | awk '{print $8}'`
	YAXIS=`cat	list_plots | awk '{print $9}'`

	if [ $TRANGE1 = MONITOR ]
	then
# export to have them available for pgi if needed
		export XMIN=`echo $MJD_MIN | awk '{print $1-0.05}'`
		export XMAX=`echo $MJD     | awk '{print $1+0.05}'`
		export XAXIS=`echo "$XMIN,$XMAX,$XAXIS"`
	fi

	XAXIS_PARAMS=`echo $XAXIS | sed "s/,/ /g" | wc -w | awk '{print $1}'`

# =====================================================================================
# 3.0 Parameter sets
#     $YP is the name of the parameter set 
# =====================================================================================

	rm -f list_parsets_$P

	grep "^PARSET_NAME" $CONFIG | grep "[[:space:]]$P[[:space:]]" > list_parsets_$P
	if [ ! -s list_parsets_$P ]
	then
		echo "***ERROR: No parameter set defined for plot $P!"
	fi

	FIRST_YP=`cat list_parsets_$P | awk '{print $3}' | head -1`

# loop over parameter sets
	for YP in `cat list_parsets_$P | awk '{print $3}'`
	do
		grep "[[:space:]]$YP[[:space:]]" list_parsets_$P > list_pp

# =====================================================================================
# 3.1 evaluate SOURCE; get XINDEX in case of LOCAL; colour code for SOURCE cell
# =====================================================================================

		SOURCE=`cat list_pp | awk '{print $4}'`
		TABLE=`cat  list_pp | awk '{print $5}'`
		XPARAM=`cat list_pp | awk '{print $6}'`
		SOURCE_TAG=`echo "<td><font size=1>$SOURCE</td>"`

# =====================================================================================
# 3.2 SOURCE = LOCAL: check for existence of LOCAL data source
# =====================================================================================

		if [ $SOURCE = "LOCAL" ]
		then
			CHECK_LOCAL=`grep "^LOCAL_DATASET" $CONFIG | grep "[[:space:]]$XPARAM[[:space:]]"`
			if [ "Q$CHECK_LOCAL" = "Q" ]
			then
				echo "***ERROR: data source $XPARAM undefined for parameter set $YP!"
				rm -rf $JTMP_DIR
				exit
			fi
			XINDEX=`grep "^LOCAL_DATASET" $CONFIG | grep "[[:space:]]$XPARAM[[:space:]]" | awk '{print $4}'`
			TINDEX=`grep "^LOCAL_DATASET" $CONFIG | grep "[[:space:]]$XPARAM[[:space:]]" | awk '{print $7}'`
			XPARAM=`grep "^LOCAL_DATASET" $CONFIG | grep "[[:space:]]$XPARAM[[:space:]]" | awk '{print $3}'`
		fi

# =====================================================================================
# 3.3 XPARAM = mjd_obs: have a range of FROM_MJD-5% ... TO_MJD+5% 
# =====================================================================================

# auto-expand $XAXIS by $XMIN and $XMAX
		if [ $XPARAM = "mjd_obs" ] && [ $XAXIS_PARAMS = 2 ] && [ "$YP" = "$FIRST_YP" ]
		then
			XMIN=`echo $FROM_MJD $TRANGE | awk '{print $1-$2*0.05}'`
			XMAX=`echo $TO_MJD   $TRANGE | awk '{print $1+$2*0.05}'`
			XAXIS=`echo $XMIN,$XMAX,$XAXIS`
		fi

		YPARAM=`cat 	 list_pp | awk '{print $7}'`
		YTYPE=R

		DOWNLOAD=`cat	 list_pp | awk '{print $8}'`
		if [ "Q$DOWNLOAD" = "Q" ]
		then
			DOWNLOAD=NO
		fi

		COND_NAME=`cat	 list_pp | awk '{print $9}'`
		if [ "Q$COND_NAME" = "Q" ]
		then
			COND_NAME=NONE
		fi

# =====================================================================================
# 3.4 evaluate SOURCE; get YINDEX in case of LOCAL
# =====================================================================================

		if [ $SOURCE = "LOCAL" ]
		then
			YINDEX=`grep "^LOCAL_DATASET" $CONFIG | grep "[[:space:]]$YPARAM[[:space:]]" | awk '{print $6}'`
			YPARAM=`grep "^LOCAL_DATASET" $CONFIG | grep "[[:space:]]$YPARAM[[:space:]]" | awk '{print $5}'`
		fi

# =====================================================================================
# 3.5 find condition if defined
# =====================================================================================
		if [ $COND_NAME != "NONE" ] 
		then
			CONDITION=`grep "^CONDITION"	$CONFIG | grep "[[:space:]]$COND_NAME[[:space:]]" | sed "s/&&$//" | sed "s/^.*&&//" | sed "s/^.*/AND &/"`
			if [ "Q$CONDITION" = "Q" ]
			then
				echo "***ERROR: condition $COND_NAME listed but not defined!"
			fi
			CGI_COND=`grep "^CGI_COND" $CONFIG | grep "[[:space:]]$COND_NAME[[:space:]]" | sed "s/&&$/\&/" | sed "s/^.*&&//"`
			HC_COND=`grep   "^HC_COND" $CONFIG | grep "[[:space:]]$COND_NAME[[:space:]]" | sed "s/&&$//"   | sed "s/^.*&&//"`
		else
			CONDITION=""
			CGI_COND="NONE"
			HC_COND="NONE"
		fi

		CHECK_CGI=`echo $CGI_COND | wc -w`

		if [ $CHECK_CGI -gt 1 ]
		then
			echo "***ERROR: CGI_COND appears to be configured multiple times."
			rm -rf $JTMP_DIR
			exit -1
		fi

		if [ "Q$CGI_COND" = "Q" ]
		then
			echo "***ERROR: CGI_COND required but not configured. Check!"
			rm -rf $JTMP_DIR
			exit -1
			CGI_COND=NONE
		fi

# =====================================================================================
# 4.0 get data
# 4.1 SOURCE = QC1DB: create SQL query
# We always add mjd_obs to the query, just in case it's not included
# =====================================================================================

		if [ $SOURCE = "QC1DB" ] 
		then

# BWo: added "$XPARAM is not NULL"
# MNe: str($YPARAM,20,16), to have SQL right in case of very small numbers (2007-10-12)		
			cat > query_$YP <<EOT
use qc1
go

SELECT DISTINCT
	$XPARAM ,
	str($YPARAM,20,16) ,
	mjd_obs
FROM 
	$TABLE
WHERE
	mjd_obs between $FROM_MJD and $TO_MJD 
AND
	$YPARAM is not NULL
AND
	$XPARAM is not NULL
AND
	visible = "Y"
-- begin of inserted CONDITION
$CONDITION	
-- end of inserted CONDITION
ORDER BY
	$XPARAM
GO
EOT
			chmod u+x query_$YP
			echo "    parameter set $YP: 	QC1 database connection"
			isql -Uqc -P`cat $QC1_PWD` -S${QC1_SERVER} -w999 -i query_$YP -o result_$YP
			checkError result_$YP

			echo "<tr bgcolor=#cccccc align=left valign=top><td><font size=2 color=#ff3333>$YP</td><td><font size=2>" >> queries
			cat query_$YP | sed "/use qc1/d" |\
			 sed "/^go/d" | sed "/^GO/d" |\
			 sed "s/--.*//" |\
			 sed "s/SELECT DISTINCT/<b>&<\/b>/" |\
			 sed "s/FROM/<b>&<\/b>/" |\
			 sed "s/WHERE/<b>&<\/b>/" |\
			 sed "s/AND/<b>&<\/b>/"g |\
			 sed "s/ORDER BY/<b>&<\/b>/" >> queries
			echo "</td></tr>" >> queries

			cat result_$YP | sed "1,2 d" | sed "$ d" | sed "$ d" | sort -u > input_$YP

			if [ $XPARAM = "mjd_obs" ] || [ $YPARAM = "mjd_obs" ]
			then
				CGI_1="<a href=${CGI_URL}?action=qc1_browse_get&table=$TABLE&field_civil_date=civil_date&field_$XPARAM=$XPARAM&field_$YPARAM=$YPARAM"
			elif [ $XPARAM != "mjd_obs" ] && [ $YPARAM != "mjd_obs" ]
			then
				CGI_1="<a href=${CGI_URL}?action=qc1_browse_get&table=$TABLE&field_mjd_obs=mjd_obs&field_civil_date=civil_date&field_$XPARAM=$XPARAM&field_$YPARAM=$YPARAM"
			fi

			if [ $DOWNLOAD = "YES" ]
			then
				if [ $CGI_COND = "NONE" ] 
				then
					CGI_COND1=""
				else
					CGI_COND1=$CGI_COND
				fi

				if [ $TMODE != FULL ] && [ $TMODE != KPI ]
				then
					cat > downloads_$YP <<EOT
  <td nowrap><font size=1>
    ${CGI_1}&${CGI_COND1}from=$DATE1&to=$DATE2&output=ascii target=_blank title="download data displayed here">this</a> |
    ${CGI_1}&${CGI_COND1}from=$DATE_YEAR&to=$DATE2&output=ascii target=_blank title="download data up to a year old">last_yr</a> |
    ${CGI_1}&${CGI_COND1}from=$DATE_ALL&to=${DATE_ALL2}&output=ascii target=_blank title="download all available data">all</a>
  </td>
EOT
				else
					cat > downloads_$YP <<EOT
  <td nowrap><font size=1>
    ${CGI_1}&${CGI_COND1}from=$DATE_ALL&to=${DATE_ALL2}&output=ascii target=_blank title="download all available data">all (full time range)</a>
EOT
				fi
			else
				cat > downloads_$YP <<EOT
   <td>&nbsp;</td>
EOT
			fi			
		fi

# =====================================================================================
# 4.2 SOURCE = LOCAL: find and extract local table 
# =====================================================================================

		if [ $SOURCE = "LOCAL" ] 
		then
			if [ ! -s $DFO_TREND_DIR/$TABLE ]
			then
				echo "***ERROR: $DFO_TREND_DIR/$TABLE not found. No plot generated for parameter set $YP."
				XAXIS=""
				continue
			else
				echo "    parameter set $YP:	local data set found"
			fi
# suppression of NULL entries only after all other selections have been made
			if [ "Q$HC_COND" != "QNONE" ] 
			then
				eval "cat $DFO_TREND_DIR/$TABLE | $HC_COND | grep -v \"^#\" | grep -v \"^!\" | awk '{print $XINDEX, $YINDEX, $TINDEX}'" | sort -u -k3,3 | awk '{if ( $3 > mjd1 ) {print $1,$2,$3}}' mjd1=$FROM_MJD | awk '{if ( $3 < mjd2 ) {print $1,$2,$3}}' mjd2=$TO_MJD  | grep -v NULL | sort -u > input_$YP
			else
				eval "cat $DFO_TREND_DIR/$TABLE | grep -v \"^#\" | grep -v \"^!\" | awk '{print $XINDEX, $YINDEX, $TINDEX}'" | sort -u -k3,3 | awk '{if ( $3 > mjd1 ) {print $1,$2,$3}}' mjd1=$FROM_MJD | awk '{if ( $3 < mjd2 ) {print $1,$2,$3}}' mjd2=$TO_MJD  | grep -v NULL | sort -u > input_$YP
			fi

			cat > downloads_$YP <<EOT
<td><font size=1>n/a</td>
EOT

		fi

		if [ -s input_$YP ]
		then
			LAST_ENTRY=`cat input_$YP | sort -u -k3,3 | tail -1 | awk '{print $3}'`
		else
			LAST_ENTRY=""
		fi

		if [ "Q$LAST_ENTRY" != "Q" ]
		then
			DFO_DATE=`echo $LAST_ENTRY $DFO_DATE | awk '{ if ( $1 > $2 ) {print $1} else {print $2}}'`
		fi

# export to make available for pgi
		export XMIN=`echo $XAXIS | sed "s/,/ /"g | awk '{print $1}'`
		export XMAX=`echo $XAXIS | sed "s/,/ /"g | awk '{print $2}'`
		export YMIN=`echo $YAXIS | sed "s/,/ /"g | awk '{print $1}'`
		export YMAX=`echo $YAXIS | sed "s/,/ /"g | awk '{print $2}'`

# save XMIN/XMAX for usage in pet.py only in case of $XPARAM = "mjd_obs"
		if [ $XPARAM = "mjd_obs" ]
		then
			MIN_MJD=$XMIN
			MAX_MJD=$XMAX
		fi

	done
done

# =====================================================================================
# 5. update LAST_DATE
# =====================================================================================

# transform mjd into civil_date, follow DFO convention (offset=9 hrs)
rm -f $JTMP_DIR/DFO_DATE $JTMP_DIR/PSO_DATE

if [ $DFO_DATE != 0 ]
then
	qcdate `echo $DFO_DATE $DFO_OFFSET | awk '{printf"%10f5\n", $1-$2/24-0.5}'` > $JTMP_DIR/DFO_DATE
	DFO_DATE=`cat $JTMP_DIR/DFO_DATE`
fi

# Error? DFO_DATE=0
if [ $DFO_DATE = ERROR ]
then
	DFO_DATE=0
fi

if [ $PSO_DATE != 0 ]
then
	qcdate `echo $PSO_DATE $DFO_OFFSET | awk '{printf"%10f5\n", $1-$2/24-0.5}'` > $JTMP_DIR/PSO_DATE
	PSO_DATE=`cat $JTMP_DIR/PSO_DATE`
else
	PSO_DATE=$DFO_DATE
fi

if [ $TRENDING_TYPE = "HEALTH" ] 
then
	# for pet.py
	DATE2_STRING="${DFO_DATE}*"
else
	# for pet.py
	DATE2_STRING="${DATE2}"
fi	

if [ $DFO_DATE = 0 ] && [ $TRENDING_TYPE = "HEALTH" ]
then
	RANGE_LABEL="${DATE_RNG_LABEL}: not applicable"
else
	RANGE_LABEL="${DATE_RNG_LABEL}: ${DATE1} ... ${DATE2_STRING}"
fi

# =====================================================================================
# 6. call Python procedure for main plot
# =====================================================================================

# for usage in monitor plots
export JTMP_DIR

if [ "Q$MJD_TIMESTAMP" = "Q" ]
then
	MJD_TIMESTAMP=0
fi

echo ""
if [ $TRENDING_TYPE = HEALTH ] && [ $SCORE_FLAG != NO ]
then
	echo "2. Create MAIN plot (call pet.py), fill \$DFO_TREND_DIR/reports/statFiles/stat_text_$REPORT_NAME ..."
else
	echo "2. Create MAIN plot (call pet.py) ..."
fi

rm -f ${JTMP_DIR}/error

echo "#Error report for report $REPORT_NAME" > ${JTMP_DIR}/error

if [ "Q$DEBUG" = "QYES" ]
then
	echo 
	echo "Now executing ..."
	echo "${DFO_BIN_DIR}/pet.py $REPORT_NAME --closeup=0 --trending_type=${TRENDING_TYPE} --date_descr=\"${DATE_DESC}\" --date_range=\"${RANGE_LABEL}\" --mjd_min=${MIN_MJD} --mjd_max=${MAX_MJD} --tp_filename=${TPNAME} --tool_config=${TOOL_CONFIG} --start=${YMSTART} --time_stamp=${MJD_TIMESTAMP}"
	echo
fi

${DFO_BIN_DIR}/pet.py $REPORT_NAME --closeup=0 --trending_type=${TRENDING_TYPE} --date_descr="${DATE_DESC}" --date_range="${RANGE_LABEL}" --mjd_min=${MIN_MJD} --mjd_max=${MAX_MJD} --tp_filename=${TPNAME} --tool_config=${TOOL_CONFIG} --start=${YMSTART} --time_stamp=${MJD_TIMESTAMP} 2>>${JTMP_DIR}/error

# for checking of fatal errors
EXITERR=0

# move html and png to final name
if [ -s trend_report.png ]
then
	mv trend_report.png ${PNG_NAME}
else
	echo "*** An error occured while executing pet.py. No png file created. "
	EXITERR=1
fi

# =====================================================================================
# 7.0 closeup plots
# =====================================================================================

echo ""
echo "3. Create CLOSEUP plots (call pet.py) ..."

# flag for scp of html output
ALL_STATIC=YES

for P in `cat list_reportplots | awk '{print $2}'`
do
	P_INDEX=`grep " ${P}$" list_reportplots | awk '{print $1}'`

	if [ "Q$DEBUG" = "QYES" ]
	then
		echo
		echo "Now executing ..."
		echo "${DFO_BIN_DIR}/pet.py $REPORT_NAME --closeup=${P_INDEX} --trending_type=${TRENDING_TYPE} --date_descr=\"${DATE_DESC}\" --date_range=\"${RANGE_LABEL}\" --mjd_min=${MIN_MJD} --mjd_max=${MAX_MJD} --tp_filename=${TPNAME} --tool_config=${TOOL_CONFIG} --start=${YMSTART} --time_stamp=${MJD_TIMESTAMP} "
		echo
	fi

	${DFO_BIN_DIR}/pet.py $REPORT_NAME --closeup=${P_INDEX} --trending_type=${TRENDING_TYPE} --date_descr="${DATE_DESC}" --date_range="${RANGE_LABEL}" --mjd_min=${MIN_MJD} --mjd_max=${MAX_MJD} --tp_filename=${TPNAME} --tool_config=${TOOL_CONFIG} --start=${YMSTART} --time_stamp=${MJD_TIMESTAMP} 2>>${JTMP_DIR}/error

# check if CLOSEUP plot is interactive
	INTER=`grep ^PLOT_NAME $CONFIG | grep "[[:space:]]${P}[[:space:]]" | awk '{print $14}'`
	if [ "Q$INTER" = "Q" ]
	then
		INTER=INTER
	fi

# close-up PNG created only if INTER=STATIC
	if [ $INTER = STATIC ]
	then
		if [ -s trend_report.png ]
		then
			mv trend_report.png trend_report_${REPORT_NAME}${HTAG}_${P_INDEX}.png
		else
			echo "*** An error occured while executing pet.py. No png file created. "
			EXITERR=1
		fi
	fi
# mv html to final name, only if INTER=INTER
	if [ $INTER = INTER ]
	then
		ALL_STATIC=NO
		if [ -s trend_report.html ]
		then
			mv trend_report.html trend_report_${REPORT_NAME}${HTAG}_${P_INDEX}.html
		else
			echo "*** An error occured while executing pet.py. No html file created. "
			EXITERR=1
		fi
	fi

done

# check for errors
CHKERR=`grep -v "#Error report" ${JTMP_DIR}/error | wc -l`
if [ $CHKERR -gt 0 ]
then
	mail -s "trendPlotter: an ERROR occured for ${REPORT_NAME} " $OP_ADDRESS < ${JTMP_DIR}/error
	echo "*** An email with error messages has been sent to $OP_ADDRESS "
fi

if [ $EXITERR -gt 0 ]
then
	echo "*** A fatal error has occured. Exit."
	exit -1
fi

# =====================================================================================
# 8.0 create HTML
# 8.1 header part
# =====================================================================================

echo ""
echo "4. Create HTML page ..."
TIMESTAMP=`date -u +%Y-%m-%d"T"%H:%M:%S`

# for HC plots, we duplicate most (but not all) HTML code to also have a page without vnavbar and HC header
HTML_NAME_CURRENT="trend_report_${REPORT_NAME}.html"

if [ $TRENDING_TYPE = "HEALTH" ] 
then
	LAST_DATE_STRING="last data from: $PSO_DATE"	
fi

case $TRENDING_TYPE in
 "HEALTH"  ) TTYPE="HEALTH CHECK" ;;
 "HISTORY" ) TTYPE="HISTORY" ;;
esac

if [ $TMODE = FULL ]
then
	TTYPE="FULL"
elif [ $TMODE = KPI ]
then
	TTYPE="KPI"
fi

if [ "Q$PLOT_LABEL" = "Q" ]
then
	PLOT_TITLE="${DFO_INSTRUMENT} trending system: $TTYPE report $REPORT_NAME"
	PLOT_TITLE1="${DFO_INSTRUMENT} trending system: $TTYPE report"
else
	PLOT_TITLE="$PLOT_LABEL $REPORT_NAME"
	PLOT_TITLE1="$PLOT_LABEL"
fi

# add javaScript openWin
# link the $STYLES stylesheet which is an edited version of /styles/eso.style
cat > $HTML_NAME <<EOT
<html>
<!-- created by ${TOOL_NAME} -v${TOOL_VERSION} -->
<head>
<META HTTP-EQUIV="Refresh" CONTENT="$REFRESH">
<META HTTP-EQUIV="Cache-Control" CONTENT="NO-CACHE">
<META HTTP-EQUIV="Expires" CONTENT="0">

<script language="JavaScript">
<!--
function openWin(URL) {
  aWindow=window.open(URL,"a","toolbar=yes,width=1000,height=700,scrollbars=yes,menubar=yes");
  }
//-->
</script>

  <title> ${PLOT_TITLE}</title>
  <link rel="StyleSheet" href="$STYLES" type="text/css">

EOT

# we add styles special for the trendPlotter links, including hover 
cat >> $HTML_NAME <<EOT

<!-- special trendPlotter colors for links, inspired by eso style sheets http://www.eso.org/css2007/eso2008.css -->
  <style type="text/css">
  a:link    {color: #000000;}
  a:visited {color: #000000;}
  a:hover   {color: #B22A00;}
  a:active  {color: #000000;}
  </style> 

<!-- style for switching off the refresh -->
<style type="text/css">
.off  { color:red; }
</style>

EOT

cat $JTMP_DIR/offlink        >> $HTML_NAME
cat $JTMP_DIR/tp_plotresults >> $HTML_NAME

cat >> $HTML_NAME <<EOT
</head>

<body bgcolor="#FFFFFF">

<!-- backbone table -->
<table width="$WIDTH" border="0" cellspacing="1" cellpadding="1" bgcolor=#FFFFFF>
EOT
cp $HTML_NAME $HTML_NAME_CURRENT 2>/dev/null

cat > htmltable <<EOT
<!-- caption part -->
  <tr>
   <td colspan=99>

<!--begin include header -->
<!--#include virtual="$HEADER" -->
<!--end include header -->

    </td>
  </tr>
<!--end caption -->

EOT

if [ $TRENDING_TYPE = "HEALTH" ]
then
	cat htmltable >> $HTML_NAME
fi

# KPI plots also get a header
if [ "$TTYPE" = KPI ]
then
	cat htmltable >> $HTML_NAME_CURRENT
fi

# =====================================================================================
# 8.2 main table 
# =====================================================================================

cat > htmltable <<EOT
  <tr align="left" valign="top" bgcolor="#999999">
EOT

cat htmltable >> $HTML_NAME
cat htmltable >> $HTML_NAME_CURRENT

# =====================================================================================
# 8.2.1 HEALTH: vnavbar (vertical navigation bar, created by webNavBar)
# =====================================================================================
if [ $TRENDING_TYPE = "HEALTH" ]
then
	cat >> $HTML_NAME <<EOT
<!-- column 1: vnavbar -->
    <td align="center" valign="top" bgcolor="${HC_COLOR}" width="10">

<!--begin include vnavbar -->
<!--#include virtual="$VNAVBAR" -->
<!--end include vnavbar -->

    </td>

<!-- column 2: proper distance -->
    <td width="10" bgcolor="#FFFFFF"><img src="/images/itemOff.gif" width="10" height="10" border="0"></td>
EOT
fi

# the <u>C</u> in ROW0 is just a placeholder to force the row to the same height as the quick links navbar
cat > htmltable <<EOT

<!-- column 3: hosting main table -->
    <td width="$WIDTH1" bgcolor="#FFFFFF">

<!-- main table --> 
<table width="100%" border="0" cellspacing="1" cellpadding="1" bgcolor=#FFFFFF>

<!-- ROW0: auto-refresh-->
  <tr align="left" valign="bottom" bgcolor=#FFFFFF>
    <td>
         <div align=right>
           &nbsp; &nbsp; &nbsp; &nbsp; <i><font size=1 color=#999999><a id="refresh_link">[page auto-refreshes after $REFRESH sec]</a> &nbsp;
            [<a href="javascript:OffLink()" onClick="window.stop()" title="click to provisionally stop browser refresh (will resume on reload)"><font color=#999999>stop</font></a>
           | <a href="javascript:location.reload()" title="click to resume browser refresh"><font color=#999999>on</font></a>]
           &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[press Ctrl+Shift+R to enforce refresh of scores, dates and news]</font></i>
         </div>
    </td>
  </tr>

<!-- ROW1: title row -->
  <tr align="left" bgcolor="${HC_COLOR}">
    <td width="$WIDTH1">
      <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr align="left" bgcolor="${HC_COLOR}">
	  <td align="left"><b><font color="#FFFFFF" size=+2>${PLOT_TITLE1} </font></b></td>
EOT
	if [ $INFO_SECTION = YES ]
	then
		cat >> htmltable <<EOT
          <td align="right" valign="bottom"><table border="0" cellpadding="0" cellspacing="2">
            <tr>
              <td bgcolor="${TAB_BGCOLOR}"><a href="javascript:openWin('http://www.eso.org/qc/ALL/HC_help.html#top');" title="plot help: navigation, symbols, statistics"><font size=2>HELP</font></a></td>
              <td bgcolor="${TAB_BGCOLOR}" nowrap><a href="javascript:openWin('http://www.eso.org/qc/HEALTH/');" title="Users Guide"><font size=2>USERS-GUIDE</font></a></td>
              <td bgcolor="${TAB_BGCOLOR}"><a href="javascript:openWin('http://www.eso.org/qc/ALL/qc1_info.html');" title="related information"><font size=2>MORE</font></a></td>
            </tr>
          </table></td>
        </tr>
      </table>
    </td>
EOT
	else
		cat >> htmltable <<EOT
          <td>&nbsp;</td>  
        </tr>
      </table>
    </td>
EOT
	fi

	cat >> htmltable <<EOT
  </tr>
<!-- ROW2: update row -->
  <tr align="left" bgcolor="${HC_COLOR}">
    <td>
EOT
cat htmltable >> $HTML_NAME
cat htmltable >> $HTML_NAME_CURRENT

# =====================================================================================
# 8.2.2. time difference: javascript, for HEALTH
# =====================================================================================

if [ $TRENDING_TYPE = "HEALTH" ]
then
	cat > htmltable <<EOT
      <table width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td align="left" nowrap width=10%><font color="#FFFFFF" size=2>Last update: $TIMESTAMP (UT)
EOT
# use javascript function to get time difference NOW-TIMESTAMP
	jtimeDiff
	cat >> htmltable <<EOT
          </td> 
          <td align="right" valign="middle" bgcolor="#006A9D" width=90%>

          <table width="100%" border="0" cellpadding="0" cellspacing="0">
            <tr>
              <td bgcolor="#006A9D" nowrap><font color="#FFFFFF" size="2">
             &nbsp; | now: 
EOT

# add SSI variable DATE_GMT
	cat >> htmltable <<EOT
        <!--#config timefmt="%Y-%m-%dT%H:%M:%S" -->
        <!--#echo var="DATE_GMT" -->
          (UT)</font></td>

          <td align="right" nowrap>
EOT

	if [ $INFO_SECTION = YES ]
	then
		cat >> htmltable <<EOT
            <font color=#006a9d size=1><a title="`hostname`">`hostname`&nbsp;</a></font><font color="#FFFFFF" size=1><a title="pipeline version used to calculate these data">QC pipeline: ${PIPE_VERSION} (installed ${PIPE_INSTALL})</a></font>
EOT
	else
		echo "&nbsp;" >> htmltable
	fi

	cat >> htmltable <<EOT
          </td>

        </tr>
      </table>
      </tr>
      </table>
EOT
	cat htmltable >> $HTML_NAME
	cat htmltable >> $HTML_NAME_CURRENT
else
	cat >> $HTML_NAME <<EOT
    <font color="#FFFFFF" size=2>Last update: $TIMESTAMP (UT)
EOT
fi

cat > htmltable <<EOT
    </td>
  </tr>
EOT
cat htmltable >> $HTML_NAME
cat htmltable >> $HTML_NAME_CURRENT

# =====================================================================================
# 8.2.3 Group navbar (controlled by REPORT_GROUP=YES/NO)
# =====================================================================================

if [ $REPORT_GROUP != "NO" ] && ( [ $TRENDING_TYPE = "HEALTH" ] || [ $TRENDING_TYPE = "HISTORY" ] )
then
	cat > htmltable <<EOT

<!-- ROW3: group navbar -->
EOT
	cat htmltable >> $HTML_NAME
	cat htmltable >> $HTML_NAME_CURRENT

# modified to suppress horizontal navigation for FULL reports (too difficult to implement) 
	if [ $TRENDING_TYPE = "HEALTH" ] && [ $TMODE != KPI ]
	then
		echo " <!--#include virtual=\"${QC_URL}/HEALTH/group_${REPORT_NAME}_HC.html\" -->" >> $HTML_NAME
		if [ $TMODE != FULL ] 
		then
			echo " <!--#include virtual=\"${QC_URL}/HEALTH/group_${REPORT_NAME}.html\" -->" >> $HTML_NAME_CURRENT
		fi
	elif [ $TRENDING_TYPE = "HISTORY" ]
	then
		echo " <!--#include virtual=\"${QC_URL}/HEALTH/group_${REPORT_NAME}.html\" -->" >> $HTML_NAME
		if [ $TMODE != FULL ]
		then
			echo " <!--#include virtual=\"${QC_URL}/HEALTH/group_${REPORT_NAME}.html\" -->" >> $HTML_NAME_CURRENT
		fi
	fi
	echo "<!--end include group navbar -->" >> $HTML_NAME
	echo "<!--end include group navbar -->" >> $HTML_NAME_CURRENT
fi

# =====================================================================================
# 8.2.4 News/comments (only for $HTML_NAME unless TTYPE=KPI)
# =====================================================================================

if [ $TRENDING_TYPE = HEALTH ]
then

# General news (HC_POSTIT)
	cat > htmltable <<EOT

<!-- ROW4: general and report comments -->
  <tr>
    <td bgcolor=#eeeeee>
    
    <table width="100%" border=0 cellpadding="1" cellspacing="1">
      <tr bgcolor=#eeeeee>
        <td align=right valign=top width=5% nowrap><b><font size=2 color=${HC_COLOR}>General news:<font></b></td>
        <td align=left  valign=top width=95% bgcolor=${NEWS_COL}><b><font size=2 color=${HC_COLOR}>
<!-- begin include HC_POSTIT -->
<!--#include virtual="/observing/dfo/quality/ALL/HC_POSTIT" -->
<!-- end include HC_POSTIT -->
        <font></b></td>
      </tr>
EOT

# INS_NEWS (exceptionally, they might be suppressed (controlled by INS_NEWS)
	if [ $INS_NEWS = YES ]
	then
		cat >> htmltable <<EOT

      <tr bgcolor=#eeeeee>
        <td align=right valign=top width=5% nowrap>
          <b><font size=2 color=${HC_COLOR}>${DFO_INSTRUMENT} news:<font></b>
        </td>
        <td align=left  valign=top width=95% bgcolor=${NEWS_COL}><b><font size=2 color=${HC_COLOR}>
EOT

		if [ -s $CONFIG_DIR/${DFO_INSTRUMENT}_POSTIT ]
		then
			cat >> htmltable <<EOT
<!-- begin include ${DFO_INSTRUMENT}_POSTIT -->
<!--#include virtual="${QC_URL}/${DFO_INSTRUMENT}_POSTIT" -->
<!-- end include ${DFO_INSTRUMENT}_POSTIT -->
EOT
		else
			cat >> htmltable <<EOT
&nbsp;
EOT
		fi

		cat >> htmltable <<EOT
        </font></b></td>
      </tr>
EOT
	fi

# Report news
	cat >> htmltable <<EOT
      <tr bgcolor=#eeeeee>
        <td align=right valign=top width=5% nowrap><b><font size=2 color=${HC_COLOR}>Report news:<font></b>
EOT

# exceptionally, the 'edit' button might be suppressed (key for WISQ plots)
	if [ $EDIT_FLAG != NO ]
	then
		cat >> htmltable <<EOT
          <a href="javascript:openWin('http://www.eso.org${QC_URL}/php/hcComments.php?reportSelect=${REPORT_NAME}&origin=report');" title="edit report news: exchange info with your closed-QC loop colleague"><img src="http://www.eso.org/~qc/dfos/images/edit2.png" border=0 height=20 width=42 align=top></a>
EOT
	fi

	cat >> htmltable <<EOT
        </td>
        <td align=left valign=top width=95% bgcolor=${NEWS_COL}><font size=2 color=${HC_COLOR}>

<!--begin include news file -->
<!--#include virtual="${QC_URL}/HEALTH/$NEWS_FILE" -->
<!--end include news file -->

        </font></td>
      </tr>
    </table>

    </td>
   </tr>
EOT

	cat htmltable >> $HTML_NAME

# for KPI plots, we add the news to $HTML_NAME_CURRENT
	if [ "$TTYPE" = KPI ]
	then
		cat htmltable >> $HTML_NAME_CURRENT
	fi
fi

# =====================================================================================
# 8.2.5 Links to daily raw, nightlog, and processing reports;  
#       (only for HTML_NAME = HC plot); also controlled by PRODUCT_SECTION=YES/NO
# =====================================================================================

# prevent score result from being cached
TODAY=`date +%Y-%m-%d`
PSEUDO_ID="`qcdate ${TODAY}`.$$"

if [ $TRENDING_TYPE = HEALTH ] && [ $PRODUCT_SECTION = YES ]
then
# include calChecker horizontal navbar
	cat >> $HTML_NAME <<EOT

<!-- ROW5: links to daily data report, nightlog, and processing reports (included from products.html, created by calChecker) -->
  <tr>
    <td bgcolor=#eeeeee>

    <table width="100%" border=0 cellpadding="0" cellspacing="1">
      <tr bgcolor=#eeeeee>
        <td align=left valign=top rowspan="1" bgcolor="#eeeeee">

<!--white table -->
        <table border="0" cellspacing="0" cellpadding="0" width=100%>
          <tr>
            <td bgcolor="#FFFFFF">

<!--begin include products.html -->
<!--#include virtual="${QC_URL}/HEALTH/products.html" -->
<!--end include products.html -->

            </td>
          </tr>
        </table>
<!--end white table-->
        </td>

EOT

# transfer and ngas watchers (created by dfoMonitor@autoDaily)
	cat >> $HTML_NAME <<EOT
        <td width="100" bgcolor=#eeeeee>&nbsp;</td>
        <td width="200" bgcolor=#eeeeee>

        <table><tr height=20><td>
<!-- transferWatcher and ngasWatcher -->
<!--#include virtual="${QC_URL}/transferWatcher.html" --> 
        </td><td width=10>&nbsp;</td><td>
<!--#include virtual="${QC_URL}/ngasWatcher.html" --> 
	</td>
	<td width=10><font size=2>[<a title="info about watcher flags" href="javascript:openWin('/qc/ALL/HC_help.html');">?</a>]</td>
	<td><a title="forced refresh: populate HC monitor with new QC1 parameters (before regular cronjob execution)" href="javascript:openWin('http://www.eso.org/qc/${DFO_INSTRUMENT}/reports/php/hcForcedRefresh.php?instrSelect=${DFO_INSTRUMENT}');"><img src="http://www.eso.org/~qc/dfos/images/forced_refresh.png" border=0 width=100 height=28></a></td>
        </tr></table>
        </td>
      </tr>
EOT

	cat >> $HTML_NAME <<EOT
    </table>
  </td>
</tr>
EOT
fi

# =====================================================================================
# 8.2.6 History navbar
# =====================================================================================

rm -f htmltable
START_DATE=`grep "^START_DATE" 	$CONFIG | awk '{print $2}'`
if [ "Q$START_DATE" != "Q" ] && [ $TRENDING_TYPE = HISTORY ]
then
	cat > htmltable <<EOT

<!--begin include history navbar -->
<!--#include virtual="${NAVBAR_NAME}" -->	
<!--end include history navbar -->
EOT
	cat htmltable >> $HTML_NAME
	cat htmltable >> $HTML_NAME_CURRENT
fi

# =====================================================================================
# 8.2.7 call scoreHC 
#        QUICK: quick-look version of result html, with scores replacing the plot
# =====================================================================================

# crosslink HC-QUICK version
if [ $TRENDING_TYPE = HEALTH ] && [ $HISTORY_SECTION = YES ]
then
	cat >> $HTML_NAME <<EOT

<!-- ROW6: history navbar -->
	<!--#include virtual="navbar_${REPORT_NAME}_HC.html" -->
EOT
	if [ "$TTYPE" = KPI ]
	then
		cat >> $HTML_NAME_CURRENT <<EOT

<!-- ROW6: history navbar -->
	<!--#include virtual="navbar_${REPORT_NAME}_HC.html" -->
EOT
	else
		cat >> $HTML_NAME_CURRENT <<EOT

<!-- ROW6: history navbar -->
	<!--#include virtual="navbar_${REPORT_NAME}.html" -->
EOT
	fi
fi

if [ $TRENDING_TYPE = HEALTH ] && [ $SCORE_FLAG != NO ]
then
	HTML_NAME_QUICK="trend_report_${REPORT_NAME}_QUICK.html"
	SCORE_NAME_QUICK="score_${REPORT_NAME}_QUICK.html"
	cat $HTML_NAME |\
 sed "s/vnavbar_HC/vnavbar_QUICK/" |\
 sed "/group_/s/_HC.html/_QUICK.html/" |\
 sed "/navbar_${REPORT_NAME}_HC/s/_HC.html/_QUICK.html/" |\
 sed "s/HEALTH CHECK report/SCORES (quick-look)/" > $HTML_NAME_QUICK

# call scoreHC (creation of score table)
	echo "4.1 Get scoring and create QUICK-LOOK html ..."
	scoreHC -r $REPORT_NAME -T $JTMP_DIR
	if [ -s sc_HC_table ]
	then
		echo "<!--#include virtual=\"$SCORE_NAME_QUICK\" -->" >> $HTML_NAME_QUICK
	fi
else
	echo "4.1 Scoring skipped for report $REPORT_NAME and type $TRENDING_TYPE ."
fi

# =====================================================================================
# 8.3 Image Map
# =====================================================================================

echo "4.2 Create main output ..."

cat > htmltable <<EOT

<!-- ROW7: main png and imap -->
  <tr align="center" valign="top">
    <td bgcolor="$TAB_BGCOLOR">
      <img src="${PNG_NAME}?${PSEUDO_ID}" border="0" title="click boxes for closeups" usemap="#Map">
      <map name="Map">
EOT
cat htmltable >> $HTML_NAME
cat htmltable >> $HTML_NAME_CURRENT

# =====================================================================================
# 8.3.1 create coordinates, calculated from:
#      MAPX1 = XOFF * XSCALE
#      MAPX2 = (XOFF+|XSIZE|) * XSCALE
#      MAPY1 = 542 - YOFF * YSCALE - |YSIZE| * YSCALE - YCONST
#      MAPY2 = 542 - YOFF * YSCALE - YCONST
#	XSCALE = 4.12, YSCALE = 4.07, YCONST=11 (empirical values), 542 =  png height
#	YCONST had to be introduced for Python-based plots
#      original values for MIDAS:
#	XSCALE = 4.12, YSCALE = 4.17, YCONST=0 (empirical values), 542 =  png height
# =====================================================================================

rm -f htmltable $JTMP_DIR/tp_jtitle

for P in `cat list_reportplots | awk '{print $2}'`
do
	P_INDEX=`grep " ${P}$" list_reportplots | awk '{print $1}'`

	XSCAL=4.12
	YSCAL=4.07
	YCONST=11.0
	XSIZE=`grep "^#REPORT_TYPE" $FDF | grep "[[:space:]]${REPORT_TYPE}[[:space:]]" | awk '{print $4,$3}' | grep "[[:space:]]${P_INDEX}$" | awk '{print $1}'`
	YSIZE=`grep "^#REPORT_TYPE" $FDF | grep "[[:space:]]${REPORT_TYPE}[[:space:]]" | awk '{print $5,$3}' | grep "[[:space:]]${P_INDEX}$" | awk '{print $1}'`
	XOFF=`grep "^#REPORT_TYPE" $FDF | grep "[[:space:]]${REPORT_TYPE}[[:space:]]" | awk '{print  $6,$3}' | grep "[[:space:]]${P_INDEX}$" | awk '{print $1}'`
	YOFF=`grep "^#REPORT_TYPE" $FDF | grep "[[:space:]]${REPORT_TYPE}[[:space:]]" | awk '{print  $7,$3}' | grep "[[:space:]]${P_INDEX}$" | awk '{print $1}'`

	MAPX1=`echo $XOFF $XSCAL | awk '{printf"%i\n", $1*$2}'`
	MAPX2=`echo $MAPX1 $XSIZE $XSCAL | awk '{printf"%i\n", $1-($2*$3)}'`

	MAPY1=`echo 542 $YOFF $YSIZE $YSCAL $YCONST | awk '{printf"%i\n", $1-$2*$4+($3*$4)-$5}'`
	MAPY2=`echo 542 $YOFF $YSCAL $YCONST | awk '{printf"%i\n", $1-$2*$3-$4}'`

# =====================================================================================
# 8.3.2 get statistics tool tips from stat_text (first QC1DB entry only)
# =====================================================================================

	rm -f stat_eval

# get QC1_DB name
	TABLE=`grep "^PARSET_NAME[[:space:]]*${P}" $CONFIG | egrep "QC1DB" | awk '{print $5}' | head  -1`
	if [ Q$TABLE = Q ]
	then
		TABLE="local data source"
	fi

	grep QC1DB stat_text | grep "^${P_INDEX}" | head -1 |\
 	 sed "s/MEDIAN/median/" |\
 	 sed "s/MEAN/mean/"     |\
 	 sed "s/FIXED/fixed/"   |\
	 sed "s/-999.0/no_data/"g > stat_eval

# if none found, try LOCAL
	if [ ! -s stat_eval ]
	then
		grep LOCAL stat_text | grep "^${P_INDEX}" | head -1 |\
	 	 sed "s/MEDIAN/median/" |\
	 	 sed "s/MEAN/mean/"     |\
 	 	 sed "s/FIXED/fixed/"   |\
		 sed "s/-999.0/no_data/"g > stat_eval
	fi

# create plot result table for java tooltips here
	TITLE3="no data points found"

	AVG1=`cat    stat_eval | awk '{print $4}'`
	THR1=`cat    stat_eval | awk '{print $6}' | sed "s/PER.*/PER/"`
	PER_VAL=`cat stat_eval | awk '{print $6}' | sed "s/PER//"`
	N1=`cat      stat_eval | awk '{print $9}'`
	THRESH_VAL=`cat stat_eval | awk '{print $7}'`

	if [ $TMODE = KPI ]
	then
		REF_VAL=`grep "^REF_VAL" $CONFIG | grep "[[:space:]]$P[[:space:]]" | egrep "FULL|>" | awk '{print $4}'`
		if [ Q$REF_VAL = Q ]
		then
			REF_VAL="<i>undefined</i>"
		fi
	fi

# find symbols, colors, and units (output from pet.py), needed for tool tips
	YP=`grep "[[:space:]]$P[[:space:]]" list_parsets_$P | egrep "[[:space:]]QC1DB[[:space:]]|[[:space:]]LOCAL[[:space:]]" | head -1 | awk '{print $3}'`
	YSYM_TRANS1=`awk '{print $3}' list_sym_$YP`
	YSYM_TRANS=`echo $YSYM_TRANS1 | sed "s/SPACE/ /g"`
	YCOL_TRANS=`awk '{print $4}' list_sym_$YP`
	UNITS1=`awk '{print $5}' list_sym_$YP`
	UNITS=`echo $UNITS1 | sed "s/SPACE/ /g"`

	MORE_THAN_ONE=`grep "[[:space:]]$P[[:space:]]" list_parsets_$P | egrep "[[:space:]]QC1DB[[:space:]]|[[:space:]]LOCAL[[:space:]]" | wc -l  | awk '{if ($1>1) {print "<br>(numbers below apply to this dataset)"} else {print ""}}'`

# class "OBcomm" must be defined in http://www.eso.org/observing/dfo/quality/ALL/common/qc_eso.style
	JTITLE0="<div class=\"OBcomm\" id=\"report${P_INDEX}\"><table style=\"border-collapse: collapse;\" border=1 bordercolor=#666666 cellspacing=1 cellpadding=1>"

# HEALTH: link scores, otherwise not
	if [ $TRENDING_TYPE = HEALTH ] && [ $TRANGE1 != MONITOR ] && [ $SCORE_FLAG = YES ]
	then
		JTITLE1=`cat stat_eval | awk '{print "<tr align=center valign=middle bgcolor=#FFFFCC><td colspan=3><font size=2><b>Plot "$1"</b></font><br><font size=1>scores: <!--#include virtual=\""incl_name"\"--></td></tr><tr bgcolor=#66CCFF><td align=right valign=top><font size=1><i>data source:</i></font></td><td align=left colspan=2><font size=1>"table"<br>"$3"</font></td></tr>"}' incl_name=${SCORE_TT_URL}/score_${REPORT_NAME}_${P_INDEX} table="$TABLE"`
	else
		JTITLE1=`cat stat_eval | awk '{print "<tr align=center valign=middle bgcolor=#FFFFCC><td colspan=3><font size=2><b>Plot "$1"</b></font></td></tr><tr bgcolor=#66CCFF><td align=right valign=top><font size=1><i>data source:</i></font></td><td align=left colspan=2><font size=1>"table"<br>"$3"</font></td></tr>"}' table="$TABLE"`
	fi
	
	JTITLE1b=`cat stat_eval | awk '{print "<tr bgcolor=#CCCCCC valign=top><td align=right><font size=1><i>dataset:"more"</i></font></td><td><font size=1>"$10"</font></td><td align=center><font size=1 color="ycol_trans">"ysym_trans"</font></td></tr>"}' ycol_trans="${YCOL_TRANS}" ysym_trans="${YSYM_TRANS}" more="${MORE_THAN_ONE}"`
	JTITLE4=`cat stat_eval | awk '{print "<tr bgcolor=#66CCFF><td align=right><font size=1><i>N_data plotted:</i></font></td><td valign=middle colspan=2><font size=1>"$9"</font></td></tr>"}'`
	JTITLE5="<tr><td nowrap colspan=3 align=center><font size=1><i>[click plot for closeup]</i></font></td></tr></table>"

	if [ $TMODE = KPI ]
	then
		JTITLE4b="<tr bgcolor=#66CCFF><td align=right><font size=1><i>(current) reference:</i></font></td><td valign=middle colspan=2><font size=1>${REF_VAL}</font></td></tr>"
	else
		JTITLE4b=""
	fi	

	case $AVG1 in
	 "mean"|"median"|"fixed" ) 
 		JTITLE2=`cat stat_eval | awk '{print "<tr bgcolor=#66CCFF><td align=right><font size=1><i>"$4":</i></font></td><td><font size=1>"$5"</font></td><td><font size=1>"units"</font></td></tr>"}' units="$UNITS" | sed "s/no_data/no data/" | sed "s/fixed/& average/"`
		case $THR1 in
	 	"VAL"  ) JTITLE3=`cat stat_eval | awk '{print "<tr bgcolor=#CCCCCC><td align=right><font size=1><i>"$6":</i></font></td><td><font size=1>"$7"..."$8"</font></td><td><font size=1>"units"</font></td></tr>"}' units="$UNITS" | sed "s/VAL/fixed thresholds/"`;;
	 	"PER"  ) JTITLE3=`cat stat_eval | awk '{print "<tr bgcolor=#CCCCCC><td align=right><font size=1><i>"$6"</i></font></td><td><font size=1>"$7"..."$8"</font></td><td><font size=1>"units"</font></td></tr>"}' units="$UNITS" | sed "s/PER/rel. thresholds: \&plusmn;/"`
		   if [ $THRESH_VAL = -999.0 ]
		   then
	 	  	JTITLE3=`cat  stat_eval | awk '{print "<tr bgcolor=#CCCCCC><td align=right><font size=1><i>"$6"</i></font></td><td><font size=1>no data</font></td><td><font size=1>"units"</font></td></tr>"}' units="$UNITS" | sed "s/PER/rel. thresholds: \&plusmn;/"`
		   fi ;;
	 	"OFF"  ) JTITLE3=`cat stat_eval | awk '{print "<tr bgcolor=#CCCCCC><td align=right><font size=1><i>"$6"</i></font></td><td><font size=1>"$7"..."$8"</font></td><td><font size=1>"units"</font></td></tr>"}' units="$UNITS" | sed "s/OFF/rel. thresholds: /"`
		   if [ $THRESH_VAL = -999.0 ]
		   then
	 	  	JTITLE3=`cat stat_eval | awk '{print "<tr bgcolor=#CCCCCC><td align=right><font size=1><i>"$6"</i></font></td><td><font size=1>no data</font></td><td><font size=1>"units"</font></td></tr>"}' units="$UNITS" | sed "s/OFF/rel. thresholds: /"`
		   fi ;;

	 	"none" ) JTITLE3=`cat stat_eval | awk '{print "<tr bgcolor=#CCCCCC><td align=right><font size=1><i>thresholds:</i></font></td><td><font size=1><i>none</i></font></td><td><font size=1>&nbsp;</font></td></tr>"}'`;;

	 	"*"    ) TITLE="$TITLE2" ;;
		esac
		;;
	 "none" ) 
 		JTITLE2="<tr bgcolor=#66CCFF><td align=right><font size=1><i>average:</i></font></td><td align=left colspan=2><font size=1><i>none</i></font></td>"
		case $THR1 in
	 	"VAL"  ) JTITLE3=`cat stat_eval | awk '{print "<tr bgcolor=#CCCCCC><td align=right><font size=1><i>"$6":</i></font></td><td><font size=1>"$7"..."$8"</font></td><td><font size=1>"units"</font></td></tr>"}' units="$UNITS" | sed "s/VAL/fixed thresholds/"`;;
	 	"none" ) JTITLE3=`cat stat_eval | awk '{print "<tr bgcolor=#CCCCCC><td align=right><font size=1><i>thresholds:</i></font></td><td colspan=2><font size=1><i>none</i></font></td></tr>"}'`;;
		esac
		;;
	esac

# fill result table for tooltips
	echo ""       >> $JTMP_DIR/tp_jtitle
	echo $JTITLE0 >> $JTMP_DIR/tp_jtitle
	echo $JTITLE1 | sed "s/QC1DB/(QC1 database)/" | sed "s/LOCAL//" >> $JTMP_DIR/tp_jtitle
	echo $JTITLE1b >> $JTMP_DIR/tp_jtitle
	echo $JTITLE2  >> $JTMP_DIR/tp_jtitle
	echo $JTITLE3  >> $JTMP_DIR/tp_jtitle
	echo $JTITLE4  >> $JTMP_DIR/tp_jtitle
	echo $JTITLE4b >> $JTMP_DIR/tp_jtitle
	echo $JTITLE5  >> $JTMP_DIR/tp_jtitle
 	echo "</div>"  >> $JTMP_DIR/tp_jtitle

	if [ "Q$N1" = "Q0" ] || [ "Q$N1" = "Q" ]
	then
		TITLE="$TITLE3"
	fi

	TITLE=`echo "onMouseOver=\"showPlotResults(;report${P_INDEX};)\" onMouseOut=\"hidePlotResults()\"" | tr ";" "'"`

# check if interactivity is disabled for this plot
	INTER=`grep ^PLOT_NAME $CONFIG | grep "[[:space:]]${P}[[:space:]]" | awk '{print $14}'`
	if [ "Q$INTER" = "Q" ]
	then
		INTER=INTER
	fi
	if [ "Q$INTER" != "QSTATIC" ] && [ "Q$INTER" != "QINTER" ]
	then
		echo
		echo "*** WARNING: ${P} configured as INTERACTIVE=${INTER}. Should be STATIC or INTER. Forced to INTER."
		echo
	fi
	if [ "Q$INTER" = "QSTATIC" ]
	then
# not interactive: link to PNG
		cat >> htmltable <<EOT
      <area shape="rect" coords="$MAPX1,$MAPY1,$MAPX2,$MAPY2" href="trend_report_${REPORT_NAME}${HTAG}_${P_INDEX}.png?${PSEUDO_ID}" $TITLE>
EOT
	else
# interactive: link to HTML containing Highcharts code
		cat >> htmltable <<EOT
      <area shape="rect" coords="$MAPX1,$MAPY1,$MAPX2,$MAPY2" href="trend_report_${REPORT_NAME}${HTAG}_${P_INDEX}.html?${PSEUDO_ID}" $TITLE>
EOT
	fi
done

cat >> htmltable <<EOT
      </map>
    </td>
  </tr>
EOT

# =====================================================================================
# 8.4 Add links for png; research; news
# =====================================================================================

cat >> htmltable <<EOT

<!-- ROW9: Links, result table -->
  <tr align="left" valign="top">
    <td bgcolor="$TAB_BGCOLOR">

      <table border=0 cellpadding="1" cellspacing="1" width="100%">
        <tr bgcolor="#999999">
          <td width="10%"><font size="2" color="#FFFFFF"><b>png:</b></font></td>
EOT
cat htmltable >> $HTML_NAME
cat htmltable >> $HTML_NAME_CURRENT

if [ $TRENDING_TYPE = "HISTORY" ] 
then
	cat >> $HTML_NAME <<EOT
          <td width="90%"><font size="2" color="#FFFFFF"><b>Information and research:</b></font></td>
EOT
else
	cat > htmltable <<EOT
          <td width="40%"><font size="2" color="#FFFFFF"><b>Information and research:</b></font></td>
EOT
	cat htmltable >> $HTML_NAME
	cat htmltable >> $HTML_NAME_CURRENT
fi

cat > htmltable <<EOT
        </tr>

        <tr valign=top align=left bgcolor=#FFFFFF>
          <td nowrap><font size="2"><b>
           - <a href=$PNG_NAME title="display png file">png</a> file 
          </td>
          <td><font size="2">
EOT

# link to QC1 plotter and browser
FIRST_TABLE=`grep "^PARSET_NAME" $CONFIG | grep QC1DB | awk '{print $5}' | head -1`
if [ "Q$FIRST_TABLE" != "Q" ]
then
	cat >> htmltable <<EOT
 - <b>advanced studies:</b> 
             <a href=${CGI_URL}?action=qc1_browse_table&table=${FIRST_TABLE}>QC1 browser</a> | 
             <a href=${CGI_URL}?action=qc1_plot_table&table=${FIRST_TABLE}>QC1 plotter</a><br>
EOT
fi

if [ -s $CONFIG_DIR/${INFO_FILE} ]
then
	cat >> htmltable <<EOT

<!--begin include info file -->
<!--#include virtual="${QC_URL}/HEALTH/$INFO_FILE" -->
<!--end include info file -->

EOT
fi

cat >> htmltable <<EOT
          </td>
        </tr>
      </table>
    </td>
  </tr>
EOT

cat htmltable >> $HTML_NAME
cat htmltable >> $HTML_NAME_CURRENT

# =====================================================================================
# 8.5 get statistics results from statistics file stat_text and write into HTML file
# =====================================================================================

cat > htmltable <<EOT
  <tr align="left" valign="top">
    <td bgcolor="${TAB_BGCOLOR}">
EOT

if [ -s stat_results ]
then
	OPEN="<td  nowrap ROWSPAN=2 valign=top><font size=2 color=#FFFFFF><b>"
	OPEN1="<td nowrap COLSPAN=3 valign=top><font size=2 color=#FFFFFF><b>"
	OPEN2="<td nowrap COLSPAN=2 valign=top><font size=2 color=#FFFFFF><b>"
	OPEN3="<td nowrap valign=top><font size=2 color=#FFFFFF><b>"

	cat >> htmltable <<EOT
      <table width="100%" border="0" cellspacing="1" cellpadding="1">
        <tr bgcolor="#999999">
          <td ROWSPAN=2 align=center valign=top width="1"><font size=2 color=#FFFFFF><b>Plot<br><a href="javascript:openWin('${HELP_URL}#result');">?</a></td>
          <td ROWSPAN=2 align=center valign=top width="1"><font size=2 color=#FFFFFF><b>Symb<br><a href="javascript:openWin('${HELP_URL}#symbols');">?</a></td>
          $OPEN  Source<br>*</td>
EOT

	cat >> htmltable <<EOT
          $OPEN1 Average    <a href="javascript:openWin('${HELP_URL}#stat');">?</a></td>
          $OPEN2 Thresholds <a href="javascript:openWin('${HELP_URL}#stat');">?</a></td>
          $OPEN  N_<br>data</td>
          $OPEN  QC1<br>parameter</td>
          $OPEN  Data<br>downloads</td>
          $OPEN  Remarks</td>
        </tr>

	<tr bgcolor="#999999">
	  $OPEN3 method</td>
          $OPEN3 value</td>
          $OPEN3 unit</td>
          $OPEN3 method</td>
          $OPEN3 value</td>
	</tr>
EOT

	cat stat_results | sed "s/-999.0*/\&nbsp;/"g >> htmltable
	cat >> htmltable <<EOT
        <tr>
          <td COLSPAN=13 bgcolor=#999999> </td>
        </tr>
	<tr>
          <td COLSPAN=13 bgcolor=#999999><font size=1><font color=#FFFFFF>
EOT
	cat >> htmltable <<EOT
            *Data sources: QC1DB: QC1 database; LOCAL: local data source 
EOT

	cat >> htmltable <<EOT
	  </td>
        </tr>
      </table>
EOT
	cat htmltable >> $HTML_NAME
	cat htmltable >> $HTML_NAME_CURRENT
fi

# fill tooltips
cat $JTMP_DIR/tp_jtitle >> $HTML_NAME
cat $JTMP_DIR/tp_jtitle >> $HTML_NAME_CURRENT

# =====================================================================================
# 8.6 add DESCR_FILE
# =====================================================================================

if [ "Q$DESCR_FILE" != "Q" ] && [ -s $CONFIG_DIR/$DESCR_FILE ]
then
	cat > htmltable <<EOT
      <font size="2"><b>This plot</b><p>

<!--begin include description  file -->
<!--#include virtual="${QC_URL}/HEALTH/$DESCR_FILE" -->
<!--end include description file -->

       &nbsp;
       <hr noshade>
EOT
	cat htmltable >> $HTML_NAME
	cat htmltable >> $HTML_NAME_CURRENT
fi

# =====================================================================================
# 8.7 add some generic caption info
# =====================================================================================

cat > htmltable <<EOT
<font size="2">
<b> General information </b><p>
 Click on any of the plots to see a <b>close-up version</b>.</p>
EOT

cat >> htmltable <<EOT
<p>The latest date is indicated on top of the plot.</p>
<p><font size=2>If configured,</p>
<ul>
<li>data points belonging to the latest date are specially marked<br>
<li>statistical averages are indicated by a solid line, and thresholds by broken lines<br>
<li>outliers are marked by a red asterisk. They are defined as data points outside the
threshold lines<br>
<li>"aliens" (= data points outside the plot Y limits) are marked by a red arrow (<font color=#ff3333 size=+2><b>&uarr;</b></font> or <font color=#ff3333 size=+2><b>&darr;</b></font>)
<li>you can download the data for each parameter set if the 'Data downloads' link shows up
</ul>
     </td>
    </tr>
  </table>

<!-- end main table -->
      </td>
    </tr>
  </table>

<!-- end backbone table -->
      </td>
    </tr>
  </table>
</body>
</html>
EOT
cat htmltable >> $HTML_NAME
cat htmltable >> $HTML_NAME_CURRENT
echo "   ... done."
echo ""

# =====================================================================================
# 8.8 Create group navbar for HEALTH and QUICK
# =====================================================================================

if [ $TRENDING_TYPE = "HEALTH" ] && [ $REPORT_GROUP != "NO" ]
then
	if [ $SCP = "yes" ]
	then
		echo "4.3 Refresh and scp group navbars and news file ..."
	else
		echo "4.3 Refresh group navbars ..."
	fi

	GROUP_NAVBAR="group_${REPORT_NAME}_HC.html"
	navbarGroup HEALTH
	mv navbarGroup $GROUP_NAVBAR

	if [ $SCORE_FLAG = YES ]
	then
		GROUP_NAVBAR_QUICK="group_${REPORT_NAME}_QUICK.html"
		mv navbarGroup_QUICK $GROUP_NAVBAR_QUICK
	fi

	if [ $SCP = "yes" ]
	then
		scp -o BatchMode=yes $JTMP_DIR/$GROUP_NAVBAR ${DFO_WEB_SERVER}:${QC_DIR}/HEALTH	

		if [ $SCORE_FLAG = YES ]
		then
			scp -o BatchMode=yes $JTMP_DIR/$GROUP_NAVBAR_QUICK ${DFO_WEB_SERVER}:${QC_DIR}/HEALTH   
		fi

		if [ -s $CONFIG_DIR/${DFO_INSTRUMENT}_POSTIT ]
		then
			scp -o BatchMode=yes ${CONFIG_DIR}/${DFO_INSTRUMENT}_POSTIT ${DFO_WEB_SERVER}:${QC_DIR}
		fi

		if [ -s $CONFIG_DIR/${DESCR_FILE} ]
		then
			cat $CONFIG_DIR/$DESCR_FILE | sed "s/<p>/<\/font>&<font size=\"2\">/" > $DESCR_FILE
			scp -o BatchMode=yes ${DESCR_FILE} ${DFO_WEB_SERVER}:${QC_DIR}/HEALTH
		fi
	else
		mv $GROUP_NAVBAR $REPORT_DIR/
		if [ $SCORE_FLAG = YES ]
		then
			mv $GROUP_NAVBAR_QUICK $REPORT_DIR/
		fi
	fi
fi

# save stat_text file (for scoreQC)
if ( [ $TRENDING_TYPE = HEALTH ] || [ $TRENDING_TYPE = FULL ] ) && [ $SCORE_FLAG != NO ]
then
	mv stat_text $REPORT_DIR/statFiles/stat_text_${REPORT_NAME}
fi

# SCP=NO: save all HTML info in $REPORT_DIR
if [ $SCP = no ]
then
	mv $JTMP_DIR/*.html ${REPORT_DIR}/
	mv $JTMP_DIR/*.png  ${REPORT_DIR}/
#else
	# change java script link from global to relative
	# relative links function on the Garching and Paranal servers but not on the local QC machine
	#sed -i -e "s|http://www.eso.org||" *.html #[disabled because of 'forced refresh' issue]
fi

# =====================================================================================
# 9. display html
# =====================================================================================

if [ $BROWSER_DISPLAY = YES ] && [ $SCP = no ]
then
	if [ "Q${BROWSER_ACTIVE}" != "Q" ]
	then
		echo "4. Display html ..."
		$DFO_GUI_DIR/refresh_browser ${REPORT_DIR}/${HTML_NAME}
	else
		echo "No active $DFO_BROWSER session found. Result file is ${REPORT_DIR}/${HTML_NAME} ."
	fi
fi

# =====================================================================================
# 10. scp to web server
# HEALTH:      $QC_DIR/HEALTH
# HEALTH,FULL: $QC_DIR/FULL
# HISTORY:     $QC_DIR/$YEAR
# =====================================================================================

echo "
5. scp output ..."
if [ $SCP = "yes" ]
then
	echo "5.1 scp HTML and png output to ${DFO_WEB_SERVER} ..."

	if [ $TMODE = FULL ] || [ $TMODE = KPI ]
	then
		YEAR=FULL
	fi

	case $TRENDING_TYPE in 
 	 "HEALTH" ) ssh -o BatchMode=yes ${DFO_WEB_SERVER} "bin/qcDircheck $QC_DIR/$YEAR"  		1>/dev/null ;;

 	 *        ) ssh -o BatchMode=yes ${DFO_WEB_SERVER} "bin/qcDircheck $QC_DIR/$YEAR"  1>/dev/null
		    ssh -o BatchMode=yes ${DFO_WEB_SERVER} "bin/qcDircheck $QC_DIR/$YEAR/$REPORT_NAME"  1>/dev/null
	    	    YEAR=$YEAR/$REPORT_NAME ;;
	esac

	if [ $TRENDING_TYPE = HEALTH ] && [ $SCORE_FLAG = YES ]
	then
		chmod uo+rwx,o+rx,o-w ${JTMP_DIR}/${HTML_NAME_QUICK}
		scp -o BatchMode=yes  ${JTMP_DIR}/${HTML_NAME_QUICK} ${DFO_WEB_SERVER}:${QC_DIR}/$YEAR   
	fi

	chmod uo+rwx,o+rx,o-w ${JTMP_DIR}/${HTML_NAME}
	scp -o BatchMode=yes  ${JTMP_DIR}/${HTML_NAME} ${JTMP_DIR}/trend_report_${REPORT_NAME}${HTAG}*.png ${DFO_WEB_SERVER}:${QC_DIR}/$YEAR 
	if [ $ALL_STATIC = NO ]
	then
		scp -o BatchMode=yes  trend_report_${REPORT_NAME}${HTAG}_*.html ${DFO_WEB_SERVER}:${QC_DIR}/$YEAR
	fi
	if [ $TRENDING_TYPE = HEALTH ] 
	then
		chmod uo+rwx,o+rx,o-w ${JTMP_DIR}/${HTML_NAME_CURRENT}
		scp -o BatchMode=yes  ${JTMP_DIR}/${HTML_NAME_CURRENT} ${DFO_WEB_SERVER}:${QC_DIR}/$YEAR 
	fi
else
	echo "5.1 $DFO_WEB_SERVER: skipped."
fi

echo "
  ... done."

if ( [ $TRENDING_TYPE = "HEALTH" ] || [ $TRENDING_TYPE = "FULL" ] ) && [ $EDIT_FLAG = YES ] && [ $EDIT_PERMA = NO ]
then
	checkValidity
fi

# =====================================================================================
# 11. exit message
# =====================================================================================

if [ $SCP = no ]
then
	echo "
====================================================================
Find 
- HTML output in 	\$DFO_TREND_DIR/reports/$HTML_NAME "

	if [ $TRENDING_TYPE = HEALTH ] && [ $SCORE_FLAG = YES ]
	then
		echo "
- HTML quick-look file in 	\$DFO_TREND_DIR/reports/$HTML_NAME_QUICK 
- statistics result file in	\$DFO_TREND_DIR/reports/statFiles/stat_text_${REPORT_NAME} (used by scoreQC)
- detailed scoring results in 	\$DFO_TREND_DIR/reports/sc_score_results
===================================================================="
	fi
else
	echo "
====================================================================
Find 
- result HC page under	http://www.eso.org${QC_URL}/HEALTH/$HTML_NAME "

	if [ $TRENDING_TYPE = HEALTH ] && [ $SCORE_FLAG = YES ]
	then
		echo "
- HTML quick-look file in 	\$DFO_TREND_DIR/reports/$HTML_NAME_QUICK 
- statistics result file in	\$DFO_TREND_DIR/reports/statFiles/stat_text_${REPORT_NAME} (used by scoreQC)
- detailed scoring results in 	\$DFO_TREND_DIR/reports/sc_score_results
===================================================================="
	fi
fi
echo ""

if [ "Q$DEBUG" = "QYES" ]
then
	echo "DEBUG: $JTMP_DIR"
else
	rm -rf $JTMP_DIR
fi

exit

