#!/bin/tcsh -fx
################################################################################
#
# routine: drzwf2.csh
#
# purpose: Runs the post-calibration distortion correction task for WFPC-2.
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------- ----------------------------------------------------
# 01/03/08 58997  MSwam     first version
# 06/05/08 58998  MSwam     add trailer update
# 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
#
#########1#########2#########3#########4#########5#########6#########7#########8

 set banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
 echo $banner
 set opustime = ( eval "date '+%Y%j%H%M%S'" )
 echo "`${opustime}`-I-INFO-*** executable is $0 *** "
 echo $banner
 \date

 #
 set go_absent = 102          #this exit status is in the FATAL range for XPOLL
 set quit_this_dataset = 1    #this exit status is mapped and will allow
                             #  the process to try another dataset
 set all_is_well = 0
 #
 # set dataset from triggering OSF
 set p_dset = $OSF_DATASET
 #
 # resolve stretch for UNIX
 set caldir = `osfile_stretch_file $OUTPATH`

 #
 # move to working directory
 if (! -e $caldir) then
    echo "`${opustime}`-E-ERROR-$caldir directory does not exist"
    echo $banner
    exit $go_absent
 endif
 cd $caldir
 if ($status != 0) then
    echo "`${opustime}`-E-ERROR-Failed to set directory to $caldir"
    echo $banner
    exit $go_absent
 endif

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

 # Set up temporary directory to hold uparm process info
 set temp_uparm = /var/tmp/drzwf2$$_$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 task
 if ( -e ${p_dset}_c0f.fits ) then
       ${STSDASDISK}/pkg/hst_calib/wfpc/${binname} ${p_dset}_c0f.fits | tee -a ${p_dset}.trl
       set savestat = $status
 else
     echo "`${opustime}`-E-ERROR-${p_dset}_c0f.fits does NOT exist.  Cannot drizzle. "
     rm -rf $temp_uparm
     exit $quit_this_dataset
 endif
 # Remove temporary uparm directory
 rm -rf $temp_uparm

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