#!/bin/tcsh -fx
##########################################################################
#
# routine: csum_cos.csh
#
# purpose: Runs the COS cumulative image tasks
#
# modification history:
#
#   date    opr     who     reason
# -------- -----  --------  --------------------------------------
# 09/21/10 61157  MSwam     first version
# 10/14/10 61157  Mary      a few tweaks before delivery
# 12/01/10 61157  Sherbert  fix a rename line
# 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 no CSUM files were found
 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 $COS_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 $COS_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 = ()
    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
 set new_CCI_set = 0
 foreach MEM ( ${memASNList} ) 
   if (${new_CCI_set} == 1) then
      break  # period will be closed, no need to keep looping
   endif
   #
   # only check existing csum files (can be more than one, might be none...)
   set csum_count = `\ls -1 $csum_dir/${MEM}_*csum*.fits | wc -l`
   if (${csum_count} == 0) then
      echo "`${opustime}`-INFO-No CSUMs found for ${MEM} "
      continue  # try next ASN member
   endif

   foreach CSUM_FILE ( $csum_dir/${MEM}_*csum*.fits )
     set a_csum_found = 1
     cci_check_change -i cos $CSUM_FILE:t
     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 COS   
        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 COS 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
        set new_CCI_set = 1
     #
     else if ($cci_check == $CCI_DIFFERENT) then
        #
        # rename current CCI set from CURRENT to dated names
#
# (what if this is a retrigger, and they already have the dated names?)
        cci_rename_fits -i COS
        #
        # make sure CCI files can be found
        set count = `\ls -1 l??0[0-7]_2????????????_cci.fits | wc -l`
        if (${count} != ${CCI_FILE_COUNT}) then
           echo "`${opustime}`-ERROR- Only found ${count} CCI files using l??0[0-7]_2????????????_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 (l??0[0-7]_2????????????_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
           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 l??0[0-7]_2????????????_cci.fits
        cci_make_new -i COS   
        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 COS 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
        set new_CCI_set = 1
     endif
   end # foreach CSUM_FILE
 end # foreach MEM

 # no CSUM files is a possibility for at least one ACQ mode, 
 #   so warn and skip out if that is the case
 #
 if (${a_csum_found} == 0) then
    echo "`${opustime}`-WARN-  Found NO CSUM FILES for ${p_dset}"
    exit $no_csum_files_found
 endif

 # accumulate the csums into the CCI files
 foreach MEM ( ${memASNList} ) 
   # only check existing csum files (can be more than one, might be none...)
   set csum_count = `\ls -1 $csum_dir/${MEM}_*csum*.fits | wc -l`
   if (${csum_count} == 0) then
      echo "`${opustime}`-INFO-No CSUMs found for ${MEM} "
      continue  # try next ASN member
   endif

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

   foreach CSUM_FILE ( $csum_dir/${MEM}_*csum*.fits )
      cci_cos_countmap $CSUM_FILE:t   |& tee -a $cal_dir/$p_dset.tra
      if ($status != 0) then
         echo "`${opustime}`-ERROR-cci_cos_countmap FAILED for ${CSUM_FILE}."
         exit $quit_this_dataset
      else
         # csum added OK, delete the file to save space
         echo "`${opustime}`-INFO-cci_cos_countmap added ${CSUM_FILE}, now removing csum files to save space..."
         \rm $CSUM_FILE
      endif
   end # foreach CSUM_FILE
 end   # foreach MEM

 #-------------------------------------------------------
 # 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
