#!/bin/csh -fx
################################################################################
#
# routine: md_acs.csh
#
# purpose: Runs the post-calibration tasks for ACS
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------- ----------------------------------------------------
# 09/07/04 51801  Sherbert  1st ver: Break up MD from asn_product_status.pl (AC)
# 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
#
#########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

 #
 # determine drizzle type
 if ( $DRIZZLE_TYPE == "PYDRIZZLE" ) then
   set binname = calacsdriz.py
 else
   # default is MULTIDRIZZLE
   set binname = runcaldriz.py
 endif

 #
 # get the last character of the ipppssoot
 set lastchar = `echo $p_dset | awk '{a=substr($1,length,1); print a}'`

 # Set up temporary directory to hold uparm process info
 set temp_uparm = /var/tmp/md_acs$$_$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 DRIZZLE
 if ( $lastchar =~ [0-9a-i] ) then
   if ( -e ${p_dset}_asn.fits ) then
     ${STSDASDISK}/pkg/hst_calib/acs/${binname} ${p_dset}_asn
     set savestat = $status
   else
     echo "`${opustime}`-E-ERROR-${p_dset}_asn.fits does NOT exist.  Cannot drizzle. "
     rm -rf $temp_uparm
     exit $quit_this_dataset
   endif
 else
   if ( -e ${p_dset}_raw.fits ) then
       ${STSDASDISK}/pkg/hst_calib/acs/${binname} ${p_dset}_raw
       set savestat = $status
   else
     echo "`${opustime}`-E-ERROR-${p_dset}_raw.fits does NOT exist.  Cannot drizzle. "
     rm -rf $temp_uparm
     exit $quit_this_dataset
   endif
 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
