#!/bin/csh -x
##########################################################################
#
# routine: bc_calwf2.csh 
#
# purpose: Runs pre-calibration steps for WF2
#
# modification history:
#
#   date     opr     who       reason
# -------- --------  --------- --------------------------------------
# 10/08/01 44598     MSwam     first version
# 11/01/01 44524     J.Baum    add exposure_times.pl
# 01/16/02 44990     J.Baum    always use .trl (never .tra) 
# 04/03/02 45563     MSwam     add call to check_cal_status
# 02/14/03 46993     Heller    add OTFR exit status
# 08/21/03 48370     Sherbert  Add missing /bin/ qualifier to ls, wc
# 10/08/03 49496     MSwam     Replace OTFR_WORLD with BUILD_WF2_ASN
#
##########################################################################
set banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo $banner
\date
#
set go_absent = 102           # exit status is in the FATAL range for XPOLL
set no_calibration = 2        # exit status to bypass calibration
set no_calibration_otfr = 3   # exit status to bypass calibration in OTFR
set precal_error = 1          # exit status is mapped to dataset error
set all_is_well = 0           # exit status for successful processing
set all_is_well_otfr = 4      # exit status for successful processing in OTFR
#
#-------------------------------------------------------
# parse dataset,userid from OSF field
#-------------------------------------------------------
set p_dset = $OSF_DATASET 
#
#-------------------------------------------------------
# resolve stretch for UNIX
#-------------------------------------------------------
set caldir = `osfile_stretch_file $OUTPATH`
set sisdir = `osfile_stretch_file $INPATH`
#
#-------------------------------------------------------
# move to working directory
#-------------------------------------------------------
if (! -e $sisdir) then
   echo "ERROR: $sisdir directory does not exist"
   echo $banner
   exit $go_absent
endif
if (! -e $caldir) then
   echo "ERROR: $caldir directory does not exist"
   echo $banner
   exit $go_absent
endif
cd $caldir
if ($status != 0) then
   echo "ERROR: Failed to set directory to $caldir"
   echo $banner
   exit $go_absent
endif
#
#-------------------------------------------------------
# copy trailer file (don't overwrite if already there)
#-------------------------------------------------------
set cnt = `\ls -1 ${p_dset}.trl | \wc -l`
if ($cnt == 0) then
  \cp  $sisdir/$p_dset.trx $caldir/$p_dset.trl
endif
#
#-------------------------------------------------------
# update qolink_sms times using raw science data
#-------------------------------------------------------
exposure_times.pl ${p_dset}_d0f.fits | tee -a $p_dset.trl
if ( $status != 0 ) then
  exit $precal_error
endif
#
#-------------------------------------------------------
# check if calibration is required for this exposure
#-------------------------------------------------------
set calib = `kval ${p_dset}_shf.fits CALIBRAT`
if ( $calib == 'N' || $calib == 'F' ) then
    echo "NOCAL: calibration not required for $p_dset " | tee -a $p_dset.trl
    if ( $BUILD_WF2_ASN =~ [Tt]* ) exit $no_calibration_otfr
    exit $no_calibration
endif
check_cal_state.pl $p_dset
if ($status == 1) then
   echo "NOCAL: Calibration will be bypassed for $p_dset." | tee -a $p_dset.trl
    if ( $BUILD_WF2_ASN =~ [Tt]* ) exit $no_calibration_otfr
   exit $no_calibration
endif

if ( $BUILD_WF2_ASN  =~ [Tt]* ) then
    exit $all_is_well_otfr
endif
exit $all_is_well
