#!/bin/tcsh -fx
##########################################################################
#
# routine: bc_calnca.csh
#
# purpose: Tasks that preceed CALNICA
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 10/08/01 44598  MSwam     first version
# 11/01/01 44524  J.Baum    add exposure_times.pl 
# 09/10/12 70213  MSwam     use tcsh
# 03/20/12 70908  RDavis    replace check_cal_state.pl with .py
# 02/21/13 70908  Sherbert  replace exposure_times.pl w/.py
#
##########################################################################
set banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo $banner
\date
#
set go_absent = 102          #this exit status is in the FATAL range for XPOLL
set no_calibration = 7       #no calibration is required
set precal_error   = 3       #status mapped to OSF_ERROR
set all_is_well    = 1

#-------------------------------------------------------
# set dataset from triggering OSF
#-------------------------------------------------------
set expname = $OSF_DATASET

#-------------------------------------------------------
# resolve stretch for UNIX
#-------------------------------------------------------
set caldir = `osfile_stretch_file $OUTPATH`
set sisdir = `osfile_stretch_file $INPATH`

#-------------------------------------------------------
# Skip NDM (memory diagnostic) data (done by gc_ndm)
#-------------------------------------------------------
if ( $OSF_DATA_ID == 'ndm') then
    echo "PRECAL: calibration not required for NDM file $expname" | \
     tee -a ${sisdir}/${expname}.trx
    exit $no_calibration
endif

#-------------------------------------------------------
# verify that calibration should be attempted
#-------------------------------------------------------
check_cal_state.py $expname
set estat = $status
if ($estat == 2) then
   echo "WARNING: Calibration will NOT be attempted for $expname." | \
     tee -a ${sisdir}/${expname}.trx
   exit $no_calibration
else if ( $estat > 0 ) then
    exit $precal_error
endif

#-------------------------------------------------------
# move to working directory
#-------------------------------------------------------
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 the trailer file to .tra
#-------------------------------------------------------
\cp  $sisdir/$expname.trx $caldir/$expname.tra
if ($status != 0) then
   echo "ERROR: Failed to copy $sisdir/$expname.trx to cal path $caldir"
   echo $banner
   exit $precal_error
endif

#-------------------------------------------------------
# update qolink_sms times using raw science data
#-------------------------------------------------------
exposure_times.py ${expname} | tee -a $expname.tra
if ( $status != 0 ) then
  exit $precal_error
endif

#-------------------------------------------------------
# NOTE: NOT DOING THIS before PR 73784 discussed
# check if FGS OSF needs updating for this exposure
#-------------------------------------------------------

#-------------------------------------------------------
# check if calibration is required for this exposure
#-------------------------------------------------------
set calib = `kval ${expname}_spt.fits CALIBRAT`
if ( $calib == 'N' || $calib == 'F' ) then
    echo "PRECAL: calibration not required for $expname" | tee -a $expname.tra
    exit $no_calibration
endif

exit $all_is_well
