#!/bin/csh -x
##########################################################################
#
# routine: calnca.csh
#
# purpose: Runs the SDAS calibration executable for NIC-A
#
# 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   fix calnca exit status check (0=success)
# 09/20/00 42386_01 Heller  calnca creating unwanted trailer file
# 01/20/01 43165  MSwam     check for bypass calibration
# 10/08/01 44598  MSwam     move tasks to pre-calibration stage
# 01/31/03 47461  MSwam     use IRAFARCH to find cal executable
# 11/17/07 55049  MSwam     remove unused sisdir
# 04/15/10 64437  RBelt     Set up temporary directory to hold uparm process info
#
##########################################################################
set banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo $banner
\date
#
set go_absent = 102          #this exit status is in the FATAL range for XPOLL
set calib_error    = 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 "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-CALNCA products (CALNCA will fail if they exist)
#-------------------------------------------------------
\rm ${expname}_cal.fits
\rm ${expname}_ima.fits

#------------------------------------------------------
# Set up temporary directory to hold uparm process info
#------------------------------------------------------
set temp_uparm = /var/tmp/calnca$$_$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}/bin.${IRAFARCH}/x_calnica.e ${expname}_rwb >> $expname.tra
    if ( $status != 0 ) then
	echo "calibration for ${expname}_rwb failed"
        rm -rf $temp_uparm
	exit $calib_error
    endif

    ${STSDASDISK}/bin.${IRAFARCH}/x_calnica.e ${expname}_rwf >> $expname.tra
    if ( $status != 0 ) then
	echo "calibration for ${expname}_rwf failed"
        rm -rf $temp_uparm
	exit $calib_error
    endif
endif

#-------------------------------------------------------
# run CALNICA on the raw file
#-------------------------------------------------------
${STSDASDISK}/bin.${IRAFARCH}/x_calnica.e ${expname}_raw >> $expname.tra
if ( $status != 0 ) then
    echo "calibration for ${expname}_raw failed"
    rm -rf $temp_uparm
    exit $calib_error
endif
#-------------------------------------------------------
# Remove temporary uparm directory
#-------------------------------------------------------
rm -rf $temp_uparm

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