#!/bin/csh -x
##########################################################################
#
# routine: bc_calsti.csh
#
# purpose: Tasks that preceed CALSTIS
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 10/08/01 44598  MSwam     first version
# 11/01/01 44524  J.Baum    add exposure_times.pl 
# 12/04/02 47066  J.Baum    run *wav.fits file through exposure_times.pl 
#
##########################################################################
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 no_calib_spt = 9         #no calibration is required
set calib_error    = 3
set all_is_well    = 1
#
# parse dataset,userid from OSF field
set expname = $OSF_DATASET
#
# Skip SDM data
if ( $OSF_DATA_ID == 'sdm') then
    echo "PRECAL: calibration not required for SDN file $expname" | tee -a $expname.tra
    exit $no_calib_spt
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."
   exit $no_calibration
endif
#
#-------------------------------------------------------
# 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
#-------------------------------------------------------
\cp  $sisdir/$expname.trx $caldir/$expname.tra
if ($status != 0) then
   echo "ERROR: Failed to copy $sisdir/$expname.trx to $caldir"
   echo $banner
   exit $calib_error
endif
#
#-------------------------------------------------------
# get names of raw and spt files
#-------------------------------------------------------
set spt_file = "nofile"
if ( -e ${expname}_spt.fits ) then
   set spt_file = ${expname}_spt.fits
   set raw_file = ${expname}_raw.fits
else
  if ( -e ${expname}_wsp.fits ) then
   set spt_file = ${expname}_wsp.fits
   set raw_file = ${expname}_wav.fits
  endif
endif
if ( $spt_file == "nofile" ) then
   echo "ERROR: neither ${expname}_spt.fits nor ${expname}_wsp.fits exists"
   echo "PRECAL ERROR: no spt or wsp found. " | tee -a $expname.tra
   exit $no_calibration
endif
#
#-------------------------------------------------------
# update qolink_sms times using raw science data
#-------------------------------------------------------
exposure_times.pl $raw_file | tee -a $expname.tra
if ( $status != 0 ) then
  exit $calib_error
endif
#
#-------------------------------------------------------
# update qolink_sms times using wavecal file if
# wavcal file not already processed
#-------------------------------------------------------
set wav_file = ${expname}_wav.fits
if ( -e $wav_file ) then
  if ( $wav_file != $raw_file ) then 
    exposure_times.pl $wav_file | tee -a $expname.tra
    if ( $status != 0 ) then
      exit $calib_error
    endif
  endif
endif
#
#-------------------------------------------------------
# check if calibration is required for this exposure
#-------------------------------------------------------
set calib = `kval $spt_file CALIBRAT`
if ( $calib == 'N' || $calib == 'F' ) then
    echo "NOCAL: calibration not required for $expname" | tee -a $expname.tra
    exit $no_calibration
endif

exit $all_is_well
