#!/bin/tcsh -fx
##########################################################################
#
# routine: csum_sti.csh
#
# purpose: Runs the STIS cumulative image tasks
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 09/21/10 61157  MSwam     first version
# 11/10/10 61157  MSwam     allow STIS to pick up pre-2000 files
# 11/30/10 61157  Sherbert  ASN!=mem/no new set/fix cci_rename_fits,osf_create 
# 12/08/10 61157  Sherbert  fix a var, add a test
#
##########################################################################
 set banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
 echo $banner
 \date
 set opustime = ( eval "date '+%Y%j%H%M%S'" )
 echo "`${opustime}`-I-INFO-*** executable is $0 *** "

 set go_absent = 102          #this exit status is in the FATAL range for XPOLL
 set no_csum_files_found = 2  #this status indicates an exposure had no CSUMs
 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 stretches for UNIX
 #-------------------------------------------------------
 set csum_dir = `osfile_stretch_file $STI_SUM_DIR`
 if (! -e $csum_dir) then
     set msg = "ERROR-$csum_dir directory does not exist"
     echo "`${opustime}`-E-$msg"
     echo $banner
     exit $go_absent
 endif

 set cci_dir = `osfile_stretch_file $OUTPATH`
 if (! -e $cci_dir) then
     set msg = "ERROR-$cci_dir directory does not exist"
     echo "`${opustime}`-E-$msg"
     echo $banner
     exit $go_absent
 endif

 set cci_archive_dir = `osfile_stretch_file $CCI_ARCHIVE_DIR`
 if (! -e $cci_archive_dir) then
     set msg = "ERROR-$cci_archive_dir directory does not exist"
     echo "`${opustime}`-E-$msg"
     echo $banner
     exit $go_absent
 endif

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

 # form the member/product list if an asn, or 
 # just the exposure name if a singleton
 #
 if ( -e ${cal_dir}/${p_dset}_asn.fits ) then
    set asn_present = 1
    ## grab each ROOTNAME in the asn file (member AND product), 
    ## (this task won't find any csum file for the product, but that's OK)
    set memASNList = ()
    # Do not need PRODucts because there are no csum files for products
    set MEMBERS = (`listasn ${cal_dir}/${p_dset}_asn.fits -s | \awk '{print $1}' | egrep -v '0$' `) 
    foreach ROOTNAME ( ${MEMBERS} )
        ## We need 9th character to identify missing members
        set lastchar = `echo "$ROOTNAME" | cut -c 9 `
        ## We need lowercased rootnames
        set r = `echo "$ROOTNAME" | \tr '[A-Z]' '[a-z]' `
        ## We need to avoid expansion of asterisks in broken ASN rootnames 
        if ( "$lastchar" !~ [*] ) then
            set memASNList = ( ${memASNList} ${r} )
        endif
    end
 else
    set asn_present = 0
    set memASNList = ${p_dset}
 endif

 # determine if the current CCI set should be CLOSED by checking each exposure
 # for a key parameter change or if the "week" has expired.  Once the set is
 # closed, no need to check any more of the member list.
 #
 set a_csum_found = 0
 foreach MEM ( ${memASNList} ) 
   #
   # check existing csum file
   # (for STIS there could be either a csum_n OR a csum_f)
   #
   set count = `\ls -1 $cal_dir/${MEM}_csum*.fits | wc -l`
   if (${count} == 1) then
     # LINK the file to the CSUM area, since that is where the tools want
     # to find it.
     # (this could have been done back in calsti.csh, but it would mean
     #  adding ASN member knowledge there, which isn't needed otherwise,
     #  so it seems better to do it here, where we need that knowledge anyway.)
     #
     set CSUM_FILENAME = $cal_dir/${MEM}_csum*.fits
     set CSUM_ROOT = $CSUM_FILENAME:t
     ln $CSUM_FILENAME $csum_dir/${CSUM_ROOT}
     set a_csum_found = 1
     cci_check_change -i stis ${CSUM_ROOT}
     set cci_check = $status
     #
     if ($cci_check == $CCI_NO_FILES) then
        # this is the INITIAL run, so create a set of empty CCI files
        cci_make_new -i STIS
        if ($status != 0) then
           echo "`${opustime}`-ERROR-cci_make_new FAILED.  Can't create CCI files"
           exit $quit_this_dataset
        endif
        # they are named "blank", so we rename to "current"
        cci_rename_fits -i STIS current
        if ($status != 0) then
           echo "`${opustime}`-ERROR-cci_rename_fits FAILED.  Files won't be named properly for csum processing."
           exit $quit_this_dataset
        endif
     #
     else if ($cci_check == $CCI_DIFFERENT) then
        #
        # rename current CCI set from CURRENT to dated names
        cci_rename_fits -i STIS
        #
        # make sure CCI files can be found
        set count = `\ls -1 o?1[01]_[12]????????????_cci.fits | wc -l`
        if (${count} != ${CCI_FILE_COUNT}) then
           echo "`${opustime}`-ERROR- Only found ${count} CCI files using o?1[01]_[12]????????????_cci.fits filemask"
           exit $quit_this_dataset
        endif
        #
        # create hard links in the archiving area (to avoid file copies)
        # (if the link is already there, this must be a retriggering)
        foreach cci_file (o?1[01]_[12]????????????_cci.fits)
           if (! -e ${cci_archive_dir}/${cci_file}) then
              ln ${cci_file} ${cci_archive_dir}/
              if ($status != 0) then
                 echo "`${opustime}`-ERROR-ln  FAILED to create link for CCI file ${cci_file} to ${cci_archive_dir}"
                 exit $quit_this_dataset
              endif
           endif
           #
           # create OSFs to trigger ingest, without '.fits' in the dataset
           set cci_root = ${cci_file:r}
           set osf_root = `echo $cci_root | sed -e "s/_cci//g"`
           osf_create -p ${PATH_BASENAME} -f ${osf_root} -t ${CCI_DATA_ID} ${CCI_ARCHIVE_TRIGGER}
           if ($status != 0) then
              # Tell user if osf_create failure is due to duplicate
              # (this msg takes the place of a server side message we cannot catch)
              set dsn = ()
              set dsn = `osf_test -p ${PATH_BASENAME} -f ${osf_root} -t ${CCI_DATA_ID} `
              if ( $#dsn > 0 ) then
                echo "`${opustime}`-DUPLICATE-ERROR: OSF for ${osf_root} in path ${PATH_BASENAME} already exists "
              endif
              echo "`${opustime}`-ERROR-osf_create FAILED for ${osf_root} in path ${PATH_BASENAME}"
              exit $quit_this_dataset
               
           endif
        end
        #
        # now remove the local CCI files and create a NEW empty set
        \rm o?1[01]_[12]????????????_cci.fits
        cci_make_new -i STIS
        if ($status != 0) then
           echo "`${opustime}`-ERROR-cci_make_new FAILED.  Can't create CCI files"
           exit $quit_this_dataset
        endif
        # they are named "blank", so we rename to "current"
        cci_rename_fits -i STIS current
        if ($status != 0) then
           echo "`${opustime}`-ERROR-cci_rename_fits FAILED.  Files won't be named properly for csum processing."
           exit $quit_this_dataset
        endif
     endif
   else
      echo "`${opustime}`-INFO-No CSUMs found for ${MEM}"
      continue # try next ASN member
   endif
 end # foreach

 # ALLOW for a case where no CSUM file is generated, but WARN
 if (${a_csum_found} == 0) then
    echo "`${opustime}`-INFO-  Found NO CSUM FILES for ${p_dset}"
    exit $no_csum_files_found
 endif

 echo "`${opustime}`-------------- CCIBEG -------------- $p_dset ------------------ " |& tee -a $cal_dir/$p_dset.tra

 # accumulate the csums into the CCI files
 foreach MEM ( ${memASNList} ) 
   # only check existing csum files
   set count = `\ls -1 $csum_dir/${MEM}_csum*.fits | wc -l`
   if (${count} == 1) then
     foreach CSUM_FILE ($csum_dir/${MEM}_csum*.fits)
       set CSUM_ROOT = $CSUM_FILE:t
       cci_stis_countmap $CSUM_ROOT |& tee -a $cal_dir/$p_dset.tra
       if ($status != 0) then
         echo "`${opustime}`-ERROR-cci_stis_countmap FAILED."
         exit $quit_this_dataset
       else
         # now that it worked, clean up the CSUM from all places
         echo "`${opustime}`-INFO-cci_stis_countmap added $CSUM_ROOT, now removing csum files to save space..."
         \rm $cal_dir/${CSUM_ROOT}
         \rm $CSUM_FILE
       endif
     end # foreach
   endif
 end

 #-------------------------------------------------------
 # put termination banners in the trailer and log
 #-------------------------------------------------------
 echo "`${opustime}`-------------- CCIEND -------------- $p_dset ------------------ " |& tee -a $cal_dir/$p_dset.tra
 echo $banner
 exit $all_is_well
