#!/bin/tcsh -fx
##########################################################################
#
# routine: calncb.csh
#
# purpose: Runs the SDAS calibration executable for NIC-B
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 11/30/99 39546  Rose      initial version
# 10/08/01 44598  MSwam     move pre-calibration tasks to earlier stage
# 01/31/03 47461  MSwam     use IRAFARCH to find cal executable
# 10/13/04 52107  Sherbert  Add CALNICB information to trailer file
# 01/14/04 52725  Sherbert  Rm a few more files to enable retrigger
# 04/15/10 64437  RBelt     Set up temporary directory to hold uparm process info
# 08/25/10 64342  Sherbert  fix time stamp and first line
# 09/10/12 70213  MSwam     use tcsh
#
##########################################################################
 set banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
 echo $banner
 set opustime = ( eval "date '+%Y%j%H%M%S'" )
 echo "`${opustime}`-I-INFO-*** executable is $0 *** "
 echo $banner
 #
 set go_absent = 102          #this exit status is in the FATAL range for XPOLL
 set all_is_well = 1
 set calib_error = 3
 #
 # parse dataset,userid from OSF field
 set expname = $OSF_DATASET
 #
 #-------------------------------------------------------
 # resolve stretch for UNIX
 #-------------------------------------------------------
 set caldir = `osfile_stretch_file $OUTPATH`
 set sisdir = `osfile_stretch_file $INPATH`
 #
 #-------------------------------------------------------
 # move to working directory
 #-------------------------------------------------------
 if (! -e $caldir) then
    set msg = "ERROR-$caldir directory does not exist"
    echo "`${opustime}`-E-$msg"
    echo $banner
    exit $go_absent
 endif
 cd $caldir
 if ($status != 0) then
    set msg = "ERROR-Failed to set directory to $caldir"
    echo "`${opustime}`-E-$msg"
    echo $banner
    exit $go_absent
 endif

 #
 #-------------------------------------------------------
 # verify IRAF environment defined
 #-------------------------------------------------------
 if (! -e $STSDASDISK) then
    set msg = "ERROR-STSDASDISK undefined. Unable to run CALNICB."
    echo "`${opustime}`-E-$msg"
    echo $banner
    exit $go_absent
 endif

 #-------------------------------------------------------
 # remove any old-CALNCB products (CALNCB will fail if they exist)
 #-------------------------------------------------------
 set msg = "INFO-Remove any possible old CALNCB products so CALNCB will not fail. "
 echo "`${opustime}`-I-$msg"
 \rm ${expname}_mos.fits
 \rm ${expname}_asc.fits
 \rm ${expname}_spt.fits
 \rm ${expname}_saa.fits

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

 #-------------------------------------------------------
 # run CALNCB
 #-------------------------------------------------------
 echo "CALBEG ------------ `date`------------------" | tee -a $expname.tra
 ${STSDASDISK}/bin.${IRAFARCH}/x_calnicb.e ${expname} >> $expname.tra
 set savestat = $status
 echo "CALEND ------------ `date` -----------------" | tee -a $expname.tra
 #
 #-------------------------------------------------------
 # Remove temporary uparm directory
 #-------------------------------------------------------
 rm -rf $temp_uparm

 echo $banner
 if ( $savestat == 0 ) then
    exit $all_is_well
 else
    exit $calib_error
 endif
