#!/bin/sh
# PURPOSE:     	recreate VCAL pseudo-headers to build an association memory
# AUTHOR:       Reinhard Hanuschik / ESO
# VERSIONS:     1.0 -- December 2006
#		1.0.1- bug fixed with DFO_FILENAME (2009-06-15)
# PARAMETERS:   -d reference_date, -n non-std n_vcal_list (optional)
# OUTPUT:       pseudo-headers in $DFO_CAL_DIR/REFRESH_VCAL
# CONFIG:       none
# =========================================================================

TOOL_VERSION=1.0.1

# ==========================================================================
# 0. Initialize
# 0.1 Define help
# ==========================================================================

cat > $TMP_DIR/refreshVCAL_help <<EOT
USAGE:
	refreshVCAL -v | -h | -d <reference_date> [-n <non-std n_vcal_list>]
	reference_date: date to start backward scanning of ABs in \$DFO_LOG_DIR
	n_vcal_list:    optional number of days for scan (if different from config.createAB)
EOT

# ==========================================================================
# 0.2 get options
# ==========================================================================

REF_DATE=""
N_VCAL_LIST=0

while getopts d:n:hv OPTION
do
	case "$OPTION" in
	 v ) echo $TOOL_VERSION
             exit 0 ;;
         h ) cat $TMP_DIR/refreshVCAL_help
             exit 0 ;;
	 d ) REF_DATE=$OPTARG ;;
	 n ) N_VCAL_LIST=$OPTARG ;;
	 ? ) cat $TMP_DIR/refreshVCAL_help
             exit 0 ;;
	esac
done

if [ "Q$REF_DATE" = "Q" ]
then
	cd $DFO_LOG_DIR
	LAST_DATE=`ls -d 20* | tail -1`
	echo "You must enter a reference date (last available: $LAST_DATE). Exit."
	exit -1
fi

if [ $N_VCAL_LIST = 0 ]
then
	N_VCAL_LIST=`grep "^N_VCAL_LIST" $DFO_CONFIG_DIR/OCA/config.createAB | awk '{print $2}'`
fi

# ==========================================================================
# 0.3 prologue
# ==========================================================================

echo "We create a new directory \$DFO_CAL_DIR/REFRESH_VCAL with virtual calibs.
They are created from ABs in \$DFO_LOG_DIR, and from raw file headers in \$DFO_HDR_DIR.

Reference date: $REF_DATE 
N_VCAL_LIST:    $N_VCAL_LIST days

We start with the reference date and go backwards $N_VCAL_LIST days, as existing in
\$DFO_LOG_DIR. 

Dates without ABs are not counted. Dates with ABs but no headers are counted 
but give an error (you should then download the headers and start the tool again).

Hit return:"
read input 

# create dummy directory for new VCALs; always refresh content
rm -rf $DFO_CAL_DIR/REFRESH_VCAL
mkdir $DFO_CAL_DIR/REFRESH_VCAL

# ==========================================================================
# 1. Find all dates
# ==========================================================================

cd $DFO_LOG_DIR
rm -f $TMP_DIR/list_vcaldates
ls -d 20* | awk '{if ($1 <= refdate ) print }' refdate=$REF_DATE | tail -${N_VCAL_LIST} | sort -r > $TMP_DIR/list_vcaldates
cat $TMP_DIR/list_vcaldates

echo "OK to continue (Y/N)? (N)"
read input

if [ "Q$input" != "QY" ]
then
	echo "Think about it."
	exit
fi

# ==========================================================================
# 2.0 Create virtual headers
# 2.1 find all VIRTUAL calibrations used
# ==========================================================================

for DATE in `cat $TMP_DIR/list_vcaldates`
do
	echo " - $DATE ..."
	CHECK_AB=`ls $DFO_LOG_DIR/$DATE/*ab`
	if [ "Q$CHECK_AB" = "Q" ]
	then
		echo "*** Error: no AB found. Check out."
		exit 
	fi
	for AB in `ls $DFO_LOG_DIR/$DATE/*ab`
	do
		rm -f $TMP_DIR/virtual_hdr
		grep "VIRTUAL" $AB | awk '{print $3,$4,$5}' | sed "s/r.${DFO_FILE_NAME}/ r.${DFO_FILE_NAME}/" | sort -u -k2,2 > $TMP_DIR/virtual_hdr
		if [ -s $TMP_DIR/virtual_hdr ]
		then
			for E in `cat $TMP_DIR/virtual_hdr | awk '{print $2}'`
			do
				PRO_EXT=`echo $E | sed "s/_/ /" | awk '{print $2}'`
				DATE1=`grep     $E $TMP_DIR/virtual_hdr | awk '{print $1}' | sed "s/UCT}\//UCT}/" | sed "s/\// /" | sed "s/^.* 20/20/" | sed "s/\///"`

				PRO_CATG=`grep  $E $TMP_DIR/virtual_hdr | awk '{print $3}'`
				PARENT_AB=`grep $E $TMP_DIR/virtual_hdr | awk '{print $4}'`
				LINK_NAME=`grep "^RAWFILE"		$DFO_LOG_DIR/$DATE1/$PARENT_AB | awk '{print $2}' | head -1`
				LINK_NAME=`eval "echo $LINK_NAME"`
# read from AB
				PACK_DIR=`grep "^PACK_DIR"		$DFO_LOG_DIR/$DATE1/$PARENT_AB | awk '{print $2}'`
				RAW_TYPE=`grep "^RAW_TYPE"		$DFO_LOG_DIR/$DATE1/$PARENT_AB | awk '{print $2}'`
				MJD_OBSMODE=`grep "^MJD-OBS_MOD"	$DFO_LOG_DIR/$DATE1/$PARENT_AB | awk '{print $2}'`
				DO_CLASS=`grep "^RAWFILE"		$DFO_LOG_DIR/$DATE1/$PARENT_AB | awk '{print $3}' | head -1`

# hdr
				RAWHDR=`grep $E $TMP_DIR/virtual_hdr    | awk '{print $4}' | sed "s/_...\.ab/.hdr/" | sed "s/\.ab/.hdr/"`
				VIRT_HDR=$DFO_CAL_DIR/REFRESH_VCAL/$E.hdr

				if [ ! -d $DFO_HDR_DIR/$DATE1 ]
				then
					echo "*** ERROR: no \$DFO_HDR_DIR/$DATE1 found. Fill it and then re-try."
					exit -1
				fi
			
				if [ -s $ORIG_HDR ]
				then

# ==========================================================================
# 2.2. fill virtual hdr with pseudo FITS keys
# ==========================================================================

					cat $DFO_HDR_DIR/$DATE1/$RAWHDR | grep -v "^END" | grep -v "^DATE " > $VIRT_HDR
					cat >> $VIRT_HDR <<EOT
HIERARCH ESO MJD-OBS MOD= $MJD_OBSMODE
HIERARCH ESO RAW TYPE= '$RAW_TYPE'
CATG    = 'CALIB'
HIERARCH ESO AB NAME= '$PARENT_AB'
HIERARCH ESO DO CLASS= '$DO_CLASS'
PIPEFILE= '$E'
LINKNAME= '$LINK_NAME'
HIERARCH ESO PRO CATG= '$PRO_CATG'
DATE    = '$DATE1'
VIRTUAL = T
OLAS-ID = '$DFO_FILE_NAME'
HIERARCH ESO PRO EXT= '$PRO_EXT'
HIERARCH ESO PACK DIR= '$PACK_DIR'
EOT
					echo "END" >> $VIRT_HDR
				else
					echo "***ERROR: no raw header file $RAWHDR found in \$DFO_HDR_DIR/$DATE1 ."
					read input
				fi
			done
		fi
	done
done
	
# ==========================================================================
# 3. End
# ==========================================================================

echo "
... done. Find the new VCAL directory under \$DFO_CAL_DIR/REFRESH_VCAL.
No changes done to \$DFO_CAL_DIR/VCAL. 
You may want to check and then manually move the new directory to VCAL, in order to make it operational."

exit

