#!/bin/csh -x
##########################################################################
#
# routine: calwf2.csh 
#
# purpose: Runs the SDAS calibration tasks for WF2.
#
# modification history:
#
#   date     opr     who      reason
# -------- --------  -------- --------------------------------------
# 07/27/99 39511     Heller   initial version
# 12/16/99 39511_02  Heller   Guess at STSDAS exe locations
# 05/31/00 41693_01  Heller   Add calend and calbeg banners
# 08/20/01 43913_02  MSwam    Use shorter variable names for Solaris
# 01/31/03 47461     MSwam    use IRAFARCH to find cal executable
# 08/21/03 48370     Sherbert Add missing /bin/ qualifier to ls, wc
# 01/03/08 58938     MSwam    Use calwp2 instead of caltwp2
# 04/15/10 64437     RBelt     Set up temporary directory to hold uparm process info
#
##########################################################################
set banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo $banner
\date
#
set go_absent = 102             # exit status is in the FATAL range for XPOLL
set bad_news = 1    # exit status is mapped to dataset error
set all_is_well = 0 # exit status for successful processing
#
# 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`
setenv ucal  $caldir
# 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
#
# verify IRAF environment defined
if (! -e $STSDASDISK) then
    echo "ERROR: STSDASDISK undefined. Unable to run CALWF2."
    echo $banner
    exit $go_absent
endif
# Clean up from any previous attempts
# 
set workdir = $caldir/
set cnt = `\ls -1 ${p_dset}.trl | wc -l`  # Don't overwrite trailer
if ($cnt == 0) then
  \cp  $sisdir/$p_dset.trx $caldir/$p_dset.trl
endif
#
#
#
#
# run CALWF2
#
echo "CALBEG ------------ `date`------------------" | tee -a ${p_dset}.trl
\rm ${p_dset}.c??       # Clean up previous calibration attempts
\rm ${p_dset}_c??.fits  # Clean up previous calibration attempts in fits-era

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

${STSDASDISK}/bin.${IRAFARCH}/x_calwp2.e calwp2 inname=${p_dset} outname=${p_dset} >>${p_dset}.trl
set save_status = $status
echo $banner

# Remove temporary uparm directory
rm -rf $temp_uparm

if ( $save_status == 0 ) then
  echo "Dataset successfully calibrated"
  echo "CALEND ------------ `date`------------------" | tee -a ${p_dset}.trl
  exit $all_is_well
else
  echo "ERROR: Calibration task failed"
  echo "CALEND ------------ `date`------------------" | tee -a ${p_dset}.trl
  exit $bad_news
endif
