#!/bin/tcsh -fx
##########################################################################
#
# routine: CALSTI.csh
#
# purpose: Runs the SDAS calibration executable for CALSTIS
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 11/30/99 39546  Rose      initial version
# 01/05/00 37287  Heller    Always create tra file
# 01/17/00 39546_05 MSwam   if SPT exists, don't use WSP
# 01/20/01 43165  MSwam     Check for bypass calibration
# 01/31/03 47461  MSwam     use IRAFARCH to find cal executable
# 04/15/10 64437  RBelt     Set up temporary directory to hold uparm process info
# 09/21/10 61157  MSwam     Add call to mkstiscsum.py
# 09/10/12 70213  MSwam     use tcsh
#
##########################################################################
set banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo $banner
\date
#
set opustime = ( eval "date '+%Y%j%H%M%S'" )
echo "`${opustime}`-I-INFO-*** executable is $0 *** "
#
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 no_csum = 11             #skip CSUM/CCI step
set calib_error    = 3
set all_is_well    = 0
#
# parse dataset,userid from OSF field
set expname = $OSF_DATASET

 # set values from resource/path settings
 set OUTPATH=`get_res_item_OWL $RESOURCE_NAME $PATH_NAME OUTPATH`
 if ($status != 0) then
    echo "FAILED to find OUTPATH in $RESOURCE_NAME/$PATH_NAME"
    exit $go_absent
 endif

 set USE_CSUM=`get_res_item_OWL $RESOURCE_NAME $PATH_NAME USE_CSUM`
 if ($status != 0) then
    echo "FAILED to find USE_CSUM in $RESOURCE_NAME/$PATH_NAME"
    exit $go_absent
 endif

 set STI_SUM_DIR=`get_res_item_OWL $RESOURCE_NAME $PATH_NAME STI_SUM_DIR`
 if ($status != 0) then
    echo "FAILED to find STI_SUM_DIR in $RESOURCE_NAME/$PATH_NAME"
    exit $go_absent
 endif


# Decide if CSUM generation should occur, according to resource file setting
# (default = yes)
set csum_step = 1
if ( ${USE_CSUM} =~ [nNfF][oOaA]* ) then
    ## Turn off csum if path file has No or false
    set csum_step = 0
endif
set msg = "USE_CSUM is ${USE_CSUM}, so csum_step = ${csum_step} "
echo "`${opustime}`-D-DIAG-$msg"

#
#-------------------------------------------------------
# resolve stretch for UNIX
#-------------------------------------------------------
set caldir = `osfile_stretch_file $OUTPATH`
set sumdir = `osfile_stretch_file $STI_SUM_DIR`
#
#-------------------------------------------------------
# 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
echo "CALBEG ------------ `date`------------------" | tee -a $expname.tra
#
#-------------------------------------------------------
# verify IRAF environment defined
#-------------------------------------------------------
if (! -e $STSDASDISK) then
    echo "CALEND ERROR: STSDASDISK undefined. " | tee -a $expname.tra
    echo $banner
    exit $go_absent
endif
#-------------------------------------------------------
# remove any old-CALSTI products (CALSTI will fail if they exist)
#-------------------------------------------------------
\rm ${expname}_[cfxs][frl12x]?.fits

if ( -e ${expname}_spt.fits ) then
   set raw_file = ${expname}_raw.fits
else
  if ( -e ${expname}_wsp.fits ) then
     set raw_file = ${expname}_wav.fits
  endif
endif

#------------------------------------------------------
# Set up temporary directory to hold uparm process info
#------------------------------------------------------
set temp_uparm = /var/tmp/calsti$$_$OSF_DATASET/
mkdir $temp_uparm
if ( $status != 0 ) then
    echo "Could not create temporary uparm directory"
    exit $go_absent
endif
setenv uparm $temp_uparm

#-------------------------------------------------------
# run CALSTIS on the raw file
#-------------------------------------------------------
${STSDASDISK}/bin.${IRAFARCH}/cs0.e -tv $raw_file >> $expname.tra
if ( $status != 0 ) then
    echo "calibration for $raw_file failed"
    echo "CALERR ------------ `date`------------------" | tee -a $expname.tra
    rm -rf $temp_uparm
    exit $calib_error
endif

#-------------------------------------------------------
# Remove temporary uparm directory
#-------------------------------------------------------
rm -rf $temp_uparm

#----------------------------------------------------------------------
# if asked, run the STIS csum (cumulative image input file) generator,
#  which for any FUV,NUV exposures will write csums in the current directory
# and EXIT this script normally (the pre-archive case, will trigger CSUM/CCI)
#----------------------------------------------------------------------
if (${csum_step} == 1) then
  mkstiscsum.py $expname
  if ( $status != 0 ) then
    echo "Problem creating STIS csum file for $expname"
    echo "CALERR ------------ `date`------------------" | tee -a $expname.tra
    exit $calib_error
  endif
  echo "CALEND ------------ `date`------------------" | tee -a $expname.tra
  exit $all_is_well
else
  #-------------------------------------------------------
  # put termination banners in the trailer and log
  # and exit WITHOUT triggering CSUM/CCI processing (the OTFR case)
  #-------------------------------------------------------
  echo "CALEND ------------ `date`------------------" | tee -a $expname.tra
  exit $no_csum
endif
