#!/bin/csh -x
##########################################################################
#
# routine: temp_from_bias.csh
#
# purpose: Runs the NICMOS task RUNTEMPFROMBIAS, which computes
#          the value for the TFBTEMP keyword, later used in CALNICA.
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 07/09/08 60367  MSwam     initial version
# 04/15/10 64437  RBelt     Set up temporary directory to hold uparm process info
#
##########################################################################
set banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo $banner
\date
#
set go_absent      = 102
set error_found    = 3
set all_is_well    = 1
#
# parse dataset,userid from OSF field
set expname = $OSF_DATASET
#
#-------------------------------------------------------
# resolve stretch for UNIX
#-------------------------------------------------------
set caldir = `osfile_stretch_file $OUTPATH`
#
#-------------------------------------------------------
# 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 "TEMPFROMBIAS------------ `date`------------------" | tee -a $expname.tra
#
#-------------------------------------------------------
# verify IRAF environment defined
#-------------------------------------------------------
if (! -e $STSDASDISK) then
  echo "TEMPFROMBIAS ERROR: STSDASDISK undefined. "| tee -a $expname.tra
  echo $banner
  exit $go_absent
endif

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

# -------------------------------------------------------
# see if this is a NICMOS TACQ dataset
#-------------------------------------------------------
if ( -e {$expname}_rwb.fits ) then
    ${STSDASDISK}/pkg/hst_calib/nicmos/runtempfrombias.py ${expname}_rwb.fits >> $expname.tra
    if ( $status != 0 and $status != 1) then
	echo "RUNTEMPFROMBIAS for ${expname}_rwb failed"
    endif
#
    ${STSDASDISK}/pkg/hst_calib/nicmos/runtempfrombias.py ${expname}_rwf.fits >> $expname.tra
    if ( $status != 0 and $status != 1) then
	echo "RUNTEMPFROMBIAS for ${expname}_rwf failed"
    endif
endif

#-------------------------------------------------------
${STSDASDISK}/pkg/hst_calib/nicmos/runtempfrombias.py ${expname}_raw.fits >> $expname.tra
set savestatus = $status
if ( $savestatus == 0) then
    echo "RUNTEMPFROMBIAS complete, TFBTEMP computed."
else if ( $savestatus == 1) then
    echo "RUNTEMPFROMBIAS complete, TFBTEMP could NOT be computed."
else if ( $savestatus == 2) then
    echo "RUNTEMPFROMBIAS failed with exception, but continue processing."
else
    echo "RUNTEMPFROMBIAS unexpected exit code: $savestatus"
    echo "TEMPFROMBIAS------------ `date`------------------" | tee -a $expname.tra
    rm -rf $temp_uparm
    exit $error_found
endif

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

#-------------------------------------------------------
# put termination banners in the trailer and log
#-------------------------------------------------------
echo "TEMPFROMBIAS------------ `date`------------------" | tee -a $expname.tra
exit $all_is_well
