#!/bin/csh -x
##########################################################################
#
# routine: bc_calacs.csh 
#
# purpose: Script for ACS pre-calibration activities
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 10/08/01 44598  MSwam     first version
# 11/01/01 44524  J.Baum    add exposure_times.pl - move trailer copy
# 04/29/02 45661  Sherbert  copy member trailers, standardize with other
#                           bc_xxx.csh scripts
# 05/07/03 48363  Heller    mark OSF to error on qolink_sms failure. 
#
##########################################################################
set banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo $banner
/bin/date
#
set go_absent = 102          #causes XPOLL to error exit
set no_calibration = 2   #causes calibration to be skipped
set all_is_well = 0
set quit_this_dataset = 1 #status mapped to OSF_ERROR
#
# 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` 
#
#-------------------------------------------------------
# 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
#-------------------------------------------------------
set lastchar = `echo $expname | /bin/awk '{a=substr($1,length,1); print a}'`
if ( $lastchar =~ [0-9a-i] ) then
   echo "Creating list of existing member trailers to copy for ASN $expname"
   set members = `listasn ${expname}_asn.fits -s | /bin/grep -v F$ | /bin/awk '{print $1}' | /bin/dd conv=lcase`
   ## splitting long line above with continuation chars causing an error

   if ($#members == 0) then
      echo "WARNING: Failed to generate list of members for ${expname}: no trailers copied"
   else
      echo "Copying member trailers from SIS trx to CAL tra"
      foreach MEMBER_ROOT ( $members ) 
          /bin/cp  $sisdir/$MEMBER_ROOT.trx $caldir/$MEMBER_ROOT.tra
      end
   endif
else
   echo "Copying singleton trailer from SIS trx to CAL tra"
   /bin/cp  $sisdir/$expname.trx $caldir/$expname.tra
endif
#
#-------------------------------------------------------
# update qolink_sms times using raw data
#-------------------------------------------------------
#
if ( $lastchar =~ [0-9a-i] ) then
   # use asn table 
   exposure_times.pl ${expname}_asn.fits | tee -a $expname.tra
   set savestat = $status
else
   # use raw science 
   exposure_times.pl ${expname}_raw.fits | tee -a $expname.tra
   set savestat = $status
endif
if ( $savestat != 0 ) then
   exit $quit_this_dataset 
endif
#
#-------------------------------------------------------
# verify that calibration should be attempted
#-------------------------------------------------------
#
check_cal_state.pl $expname
if ($status == 1) then
   echo "WARNING: Calibration will NOT be attempted for $expname." | \
      tee -a $expname.tra
   exit $no_calibration
endif
if (-e {$expname}_spt.fits) then
   echo "Checking CALIBRAT value" |  tee -a $expname.tra
	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
endif

exit $all_is_well
