#!/bin/csh -x
##########################################################################
#
# routine: g2fwf2.csh 
#
# purpose: Script to convert input GEIS files to FITS for OTFR
#          (OnTheFlyCalibration) pipeline.
#
#       The IRAF/STSDAS task 'stwfits' is used to perform the conversion.
#
# modification history:
#
#   date    opr     who       reason
# -------- -----    --------  --------------------------------------
# 09/22/98 36848    MSwam     first version
# 12/04/98 35081    MSwam     new format for request filename
# 02/19/99 38284_02 MSwam     let stwfits handle ASCII tables
# 04/09/99 38834    MSwam     add banner print
# 06/09/09 38834_01 MSwam     fix banner print
# 01/31/01 43165    MSwam     mods for OTFR
# 04/05/01 43165_16 MSwam     solaris variables < 20 chars
# 11/27/01 44598_02 MSwam     check for long trl lines
# 04/09/02 45610    MSwam     adjust FITS file check
# 05/07/02 45707    Sherbert  fix long trailer check because the previous
#                             awk trick ignored nulls, added a cmd to rm
#                             nulls, added a check for the size of fits file 
#                             created by stwfits since it does not return a 
#                             useful status value.  0-size fails.  Also,
#                             printing messages to OTFR trailer.
# 02/03/03 47461    MSwam     add IRAFARCH to find bin
# 08/21/03 48370    Sherbert  Add missing /bin/ qualifier to date
# 08/21/03 48370    Sherbert  Add missing "-1" param to ls 
# 05/02/06 51357    Heller    Extra line breaks
# 03/01/11 66639    MSwam     SSB says try autoscale=no
##########################################################################
set banner=">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
echo $banner
\date
#
set go_absent = 102          #this exit status is in the FATAL range for XPOLL
set no_fits_files = 2 # indicates that there are no FITS files to return
set quit_this_dataset = 1    #this exit status is mapped and will allow
                             #  the process to try another dataset
set all_is_well = 0
#
# resolve stretches for UNIX
set caldir = `osfile_stretch_file $OUTPATH`
#
# parse dataset,userid from OSF field
set p_dset = `echo $OSF_DATASET | awk '{n=split($1,a,"_");print a[2]}'`
set p_dir = $OSF_DATASET
set otf_trl = ${OSF_DATASET}.trl
#
# move to working directory
if (! -e $caldir/$p_dir) then
   echo -n `\date +"%Y%j%H%M%S"`"-E-ERROR: $caldir/$p_dir directory " \
            | tee -a $otf_trl
   echo    "does not exist" \
            | tee -a $otf_trl
   exit $go_absent
endif
cd $caldir/$p_dir
if ($status != 0) then
   echo -n `\date +"%Y%j%H%M%S"`"-E-ERROR: Failed to set directory " \
            | tee -a $otf_trl
   echo    "to $caldir/$p_dir" \
            | tee -a $otf_trl
   exit $go_absent
endif
#
# verify IRAF environment defined
if (! -e $TABLESDISK) then
   echo -n `\date +"%Y%j%H%M%S"`"-E-ERROR: TABLESDISK undefined. " \
            | tee -a $otf_trl
   echo    "Unable to run STRFITS." \
            | tee -a $otf_trl
    exit $go_absent
endif
#
echo " "
echo -n `\date +"%Y%j%H%M%S"`"-I-INFO --- G2FBEG ---" \
   | tee -a $otf_trl
echo    `\date +"%d-%b-%Y %T"` "------------------------------------" \
   | tee -a $otf_trl

#
# rename any .tra files to .trl for DADS
foreach i (`\ls -1 *.tra`)
  \mv -f $i $i:r.trl
end

#
# set flag to make sure that at least one file attempts GEIS-FITS conversion
set loop = 0
#
# check files for possible conversion (should be ipppssoot.???)
foreach i (`\ls -1 ?????????.???`)
   set loop = 1
   # get the file extension
   set filename = $i:t
   set dataset = $filename:r
   set extension = $filename:e
   set extlen = `echo $extension | awk '{print length}'`
   set lastchr = `echo $extension | awk '{a=substr($1,length,1);print a}'`
   #
   # GEIS files are assumed to end in ".??d" or ".??h"
   #
   if ($lastchr == 'd' && $extlen == '3') then
      # for now assume a GEIS data file, could specifically confirm if needed
      echo -n `\date +"%Y%j%H%M%S"`"-I-skipping $filename, " \
      echo    "assuming its a GEIS data file..."
      continue
   endif
   if ($lastchr == 'h' && $extlen == '3') then
      echo -n `\date +"%Y%j%H%M%S"`"-I-assuming $filename is a " \
      echo    "GEIS header file, forming FITS filename..."
      # create new extension using 2chars from current ext and appending "f"
      set partext = `echo $extension | awk '{a=substr($1,1,2);print a}'`
      set fitsname = {$dataset}_{$partext}f.fits
   #
   #  not a known GEIS extension, let stwfits handle the file type
   #
   else
      set fitsname = {$dataset}_{$extension}.fits
   endif

   # check trailer for nulls, then lines > 132 chars, and split lines if found
   if ($extension == 'trl') then
      ## Check for nulls
      echo  `\date +"%Y%j%H%M%S"`"-I-Getting rid of nulls, if any." \
         | tee -a $otf_trl
      set tmp_trl = ${filename}_tmp_$$
      echo `\date +"%Y%j%H%M%S"`"-I-\tr -d '\000' < $filename > $tmp_trl" 
      tr -d '\000' < $filename > $tmp_trl
      if ($status == 0) then
         echo `\date +"%Y%j%H%M%S"`"-I-\mv $tmp_trl $filename" 
         \mv $tmp_trl $filename
         if ($status != 0) then
            echo -n `\date +"%Y%j%H%M%S"`"-E-ERROR: renaming denull-ed " \
               | tee -a $otf_trl
            echo    "file, $tmp_trl, back to $filename did not work" \
               | tee -a $otf_trl
            exit $quit_this_dataset
         endif
      else
         echo -n `\date +"%Y%j%H%M%S"`"-E-ERROR: deleting nulls from " \
            | tee -a $otf_trl
         echo    "trailer $filename did not work" \
            | tee -a $otf_trl
         ## Well, at least we tried...no need to fail observation
      endif

      ## Check for line lengths and deal with lines > 132 chars
      @ maxlen = `linesize_wrapper.pl $filename`
      if ($maxlen < 0) then
         echo "g2fxxx ERROR: linesize.pl subroutine failed to open $inputfile"
      else if ($maxlen > 132) then
         echo -n `\date +"%Y%j%H%M%S"`"-W-WARNING: Trailer lines > 132 " \
            | tee -a $otf_trl
         echo "chars, lines will now be split." \
            | tee -a $otf_trl
         fold -w 131 $filename >   $filename.$$
         if ($status != 0) then
            echo -n `\date +"%Y%j%H%M%S"`"-E-ERROR: trimming long " \
               | tee -a $otf_trl
            echo    "lines from trailer $filename did not work" \
               | tee -a $otf_trl
         endif
         \mv $filename.$$ $filename
      endif
   endif

   if (-e {$fitsname}) then
      # FITS file already exists, delete it since STWFITS won't overwrite
      echo -n `\date +"%Y%j%H%M%S"`"-I-deleting existing FITS file " \
         | tee -a $otf_trl
      echo    "{$fitsname} before stwfits..." \
         | tee -a $otf_trl
      \rm {$fitsname}
      if ($status != 0) then
         echo -n `\date +"%Y%j%H%M%S"`"-E-ERROR: could not delete " \
            | tee -a $otf_trl
         echo       "existing FITS file {$fitsname}" \
            | tee -a $otf_trl
      endif
   endif
#
${TABLESDISK}/bin.${IRAFARCH}/x_fitsio.e stwfits iraf_files=$filename fits_files=$fitsname newtape=no long_header=no short_header=yes log_file=none bitpix=0 extensions=no binary_tables=yes gftoxdim=yes ieee=yes autoscale=no bscale=1. bzero=0. scale=yes blocking_factor=1 format_file={$TABLESDISK}{$FORMAT_DIR}format.mip
   set savestat = $status                           ## we can always hope
   set zerosize = `find . -name $fitsname -size 0`
   if ( $zerosize != '' || $savestat != 0 ) then
      echo -n `\date +"%Y%j%H%M%S"`"-E-ERROR: Failed to convert " \
         | tee -a $otf_trl
      echo    "GEIS/ASCII file $i to FITS" \
         | tee -a $otf_trl
      exit $quit_this_dataset
   endif
   echo `\date +"%Y%j%H%M%S"`"-I-GEIS-to-FITS OK for $i"
end
#
echo -n `\date +"%Y%j%H%M%S"`"-I-INFO --- G2FEND ---" \
   | tee -a $otf_trl
echo    `\date +"%d-%b-%Y %T"` "------------------------------------" \
   | tee -a $otf_trl
#
if ($loop == 0) then
   # no datasets processed, WARN
   echo -n `\date +"%Y%j%H%M%S"`"-W-WARNING: No datasets processed " \
         | tee -a $otf_trl
   echo    "by STWFITS" \
         | tee -a $otf_trl
endif
#
# (the /bin/awk pipe is necessary after wc to left-justify the wc output)
set file_count = `\ls -1 ./*.fits | wc -l | awk '{print $1}'`
if ($file_count == '0') then
   echo -n `\date +"%Y%j%H%M%S"`"-E-ERROR: No FITS files available " \
      | tee -a $otf_trl
   echo    "to return to archive." \
      | tee -a $otf_trl
   exit $no_fits_files
endif
exit $all_is_well
