#!/bin/tcsh -fx
################################################################################
#
# routine: md_wf3.csh
#
# purpose: Runs the post-calibration tasks for WF3
# 
# notes: Pretty much a straight copy from md_acs.csh with obvious "acs/ACS" string
#        changes.   There is no calacsdriz.py script therefore I am certain 
#        PyDrizzle will ever be an option for WFC3 (and it probably should be 
#        removed from md_acs.csh).   Also, wf3 singletons should input flt, not raw.
# Doesn't look like we need this script after all.  DO NOT CHECK IN.
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------- ----------------------------------------------------
# 01/03/08 57556  Sherbert  1st ver: exit immediately until ready for this
# 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/07/11 69025  Sherbert  changing to astrodrizzle
# 08/29/12 72140  MSwam     add in-memory mode switch for ASTRODRIZZLE
#
#########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
 /bin/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
 # default is ASTRODRIZZLE after Sep 2011 (OPUS 2011_3 perhaps?)
 set drizzleDir = ${SSGROOT}/bin
 set drizzleExe = runastrodriz
 if ( $DRIZZLE_TYPE == "MULTIDRIZZLE" ) then
   # default is MULTIDRIZZLE until Sep 2011
   set drizzleDir = ${STSDASDISK}/pkg/hst_calib/wfc3
   set drizzleExe = runwf3driz.py 
   # There was no pydrizzle for WFC3 to best of my knowledge
 endif

 #
 # make sure the necessary software is available
 set drizzleScript = ${drizzleDir}/${drizzleExe}
 if ( ! -e ${drizzleScript} ) then
    echo "`${opustime}`-E-ERROR-Script does NOT exist: ${drizzleScript} "
    exit ${go_absent}
 endif

 # adjust ASTRODRIZZLE script name to append -i option for in-memory operation
 #   if the INTERMEDIATE_FILES resource parameter is set to "MEMORY"
 #   (only ASTRODRIZZLE supports this mode)
 #
 if ( $INTERMEDIATE_FILES == "MEMORY" ) then
   set drizzleScript = "${drizzleScript} -i "
 endif

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

 # Set up temporary directory to hold uparm process info
 set temp_uparm = /var/tmp/md_wf3$$_$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
     ${drizzleScript} ${p_dset}_asn
     set savestat = $status
   else
     echo "`${opustime}`-E-ERROR-File does NOT exist: ${p_dset}_asn.fits. Cannot drizzle. "
     rm -rf $temp_uparm
     exit $quit_this_dataset
   endif
 else
   # Although HB specifies _flt, WH says:
   # The python script that runs MultiDrizzle for ACS was written to accept the 
   # name of the raw file as input and figure out the correct input for 
   # MultiDrizzle for itself.  This will be how the WFC3 version works as well 
   # so that all the scripts in the pipeline can use the same input values.
   if ( -e ${p_dset}_raw.fits ) then
       ${drizzleScript} ${p_dset}_raw
       set savestat = $status
   else
     echo "`${opustime}`-E-ERROR-File does NOT exist: ${p_dset}_raw.fits. 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
