wrapup (version 1.4)

You can also look at:
#! /usr/bin/ksh

###########################################################################
#
# 
# SYNTAX: wrapup
#
# BRIEFLY: Do a final wrap-up of all files.
#
# DESCRIPTION: This routine does some final manipulation of the files
# DESCRIPTION: in order to prepare them for distribution.
# DESCRIPTION: The following things are done to all but the log files:
# DESCRIPTION: <ol>
# DESCRIPTION: <li> A set of FITS keywords describing the processing
# DESCRIPTION:      and observation are added to each file.
# DESCRIPTION: <li> The <tt>fverify</tt> FTOOL is run on all files
# DESCRIPTION: <li> Set world read permission for .html and .par files
# DESCRIPTION: </ol>
#
# VERSION: 1.4
#
# HISTORY: 0.0 -> 1.0 8/21/96 
# HISTORY: Now remove z.tmp temporary file
# HISTORY: 
# HISTORY: 1.0 -> 1.1 9/23/96
# HISTORY: Now set world read permission for .html amd .par files.
# HISTORY: 
# HISTORY: 1.1 -> 1.2 12/26/96
# HISTORY: Now record tapecat, trendcat, and header file names in the job.par
# HISTORY: 1/2/97 also record filenames of front and back cover images
# HISTORY: 
# HISTORY: 1.2 -> 1.3 2/7/97
# HISTORY: corrected a typo "usppiname" which caused the USPINAME keywords
# HISTORY: to be blank. Also supplied missing "$" for jppinum != 0 test
# HISTORY: 
# HISTORY: 1.3 -> 1.4 3/3/97
# HISTORY: now re-save the jobpar at the end of this routine. Previously
# HISTORY: the job par was saved in $SUBS/filelists. It is still saved there
# HISTORY: so that it can be included in the list, but it is re-saved here
# HISTORY: so that keywords added after $SUBS/filelists are also saved.
#
##############################################################################
#DEBUG=1

$UTIL/milestone "Doing final wrap up of all files"


#######################################################
# dump the filenames in the tape.cat to an ascii file
#######################################################
dump=dump.tmp
tapecat=$($UTIL/generate_filename tapecat )
if [ ! -a $tapecat ]; then
     $UTIL/exception 3 "No tapecat file: $tapecat"
fi

########################
# debugging output
########################
if [ -n "$DEBUG" ]; then
     echo ${0##*/}: tapecat=$tapecat
fi

rm -f $dump
$UTIL/setup_parfile $FTOOLS/fdump.par infile=$tapecat     \
                                      outfile=$dump       \
                                      rows=-              \
                                      columns="filename"  \
                                      prhead=no           \
                                      prdata=yes          \
                                      showcol=no          \
                                      showunit=no         \
                                      showscale=no        \
                                      showrow=no     

$FTOOLS/fdump >stdout.log 2>stderr.log
$UTIL/ftool_test fdump $? $0 2 stdout.log stderr.log

######################################################
# adding fits keywords
######################################################

$UTIL/log_entry "Adding FITS keywords to all distributed FITS files"

########################################################################
# assemble what you will need to know
# fmodhead.tmp contains the information about keywords
# which are the same for all fits files.
# later z.tmp will contain this info plus the info unique to each file
# such as the file name
#########################################################################
rm -f fmodhead.tmp

sequence=$($UTIL/read_parfile $JOBPAR sequence )
seqpnum=$( $UTIL/read_parfile $JOBPAR seqprocnum )
procver=$( $UTIL/read_parfile $PARFILE version )


echo "SEQNUM   $sequence / Sequential number from ODB"        >>fmodhead.tmp
echo "SEQPNUM  $seqpnum / Number of times sequence processed" >>fmodhead.tmp
echo "PROCVER  $procver / Processing configuration number"    >>fmodhead.tmp

uspinum=$($UTIL/read_parfile $JOBPAR uspinum )
if [ "$uspinum" != "0" ]; then

     uspiname=$($UTIL/read_parfile $JOBPAR uspiname )

     uspiadr1=$($UTIL/read_parfile $JOBPAR uspiaddr1 )
     uspiadr2=$($UTIL/read_parfile $JOBPAR uspiaddr2 )
     uspiadr3=$($UTIL/read_parfile $JOBPAR uspiaddr3 )
     uspiadr4=$($UTIL/read_parfile $JOBPAR uspiaddr4 )
     uspiadr5=$($UTIL/read_parfile $JOBPAR uspiaddr5 )

     uspieadr=$($UTIL/read_parfile $JOBPAR uspiemail )

     echo "USPINUM  $uspinum"    >> fmodhead.tmp
     echo "USPINAME '$uspiname'" >> fmodhead.tmp
     echo "USPIADR1 '$uspiadr1'" >> fmodhead.tmp
     echo "USPIADR2 '$uspiadr2'" >> fmodhead.tmp
     echo "USPIADR3 '$uspiadr3'" >> fmodhead.tmp
     echo "USPIADR4 '$uspiadr4'" >> fmodhead.tmp
     echo "USPIADR5 '$uspiadr5'" >> fmodhead.tmp
     echo "USPIEADR '$uspieadr'" >> fmodhead.tmp
fi

jppinum=$($UTIL/read_parfile $JOBPAR jppinum )
if [ "$jppinum" != "0" ]; then

     jppiadr1=$($UTIL/read_parfile $JOBPAR jppiaddr1 )
     jppiadr2=$($UTIL/read_parfile $JOBPAR jppiaddr2 )
     jppiadr3=$($UTIL/read_parfile $JOBPAR jppiaddr3 )
     jppiadr4=$($UTIL/read_parfile $JOBPAR jppiaddr4 )
     jppiadr5=$($UTIL/read_parfile $JOBPAR jppiaddr5 )

     jppieadr=$($UTIL/read_parfile $JOBPAR jppiemail )

     echo "JPPINUM  $jppinum"    >> fmodhead.tmp
     echo "JPPINAME '$jppiname'" >> fmodhead.tmp
     echo "JPPIADR1 '$jppiadr1'" >> fmodhead.tmp
     echo "JPPIADR2 '$jppiadr2'" >> fmodhead.tmp
     echo "JPPIADR3 '$jppiadr3'" >> fmodhead.tmp
     echo "JPPIADR4 '$jppiadr4'" >> fmodhead.tmp
     echo "JPPIADR5 '$jppiadr5'" >> fmodhead.tmp
     echo "JPPIEADR '$jppieadr'" >> fmodhead.tmp
fi

##################################
# loop through all the FITS files
##################################
for file in $(grep -v \.wrap $dump ); do

     ########################
     # debugging output
     ########################
     if [ -n "$DEBUG" ]; then
          echo ${0##*/}: file=$file
     fi

     if [ -a "$file" ]; then

          ################################
          # add info unique to this file
          ################################
          rm -f z.tmp
          cp fmodhead.tmp z.tmp
          echo "FNAME '$file'" >>z.tmp

          $UTIL/setup_parfile $FTOOLS/fmodhead.par tmpfil=z.tmp \
                                                   infile="${file}[0]"

          $FTOOLS/fmodhead >stdout.log 2>stderr.log
          $UTIL/ftool_test fmodhead $? $0 2 stdout.log stderr.log

          rm -f z.tmp
     fi

done

rm -f fmodhead.tmp

############################################
# run fverify on all fits files
###########################################
$UTIL/log_entry "Running fverify on all distributed files"

for file in $(grep -v \.wrap $dump ); do

     ########################
     # debugging output
     ########################
     if [ -n "$DEBUG" ]; then
          echo ${0##*/}: file=$file
     fi

     if [ -a "$file" ]; then
  
          $UTIL/setup_parfile $FTOOLS/fverify.par prhead=no   \
                                                  testdata=no \
                                                  infile=$file

          $FTOOLS/fverify >stdout.log 2>stderr.log
          if [ $? -ne 0 -o -s stderr.log ]; then
               #########################
               # there is an error
               #########################
               $UTIL/exception $0 2 "$file failed fverify"
               $UTIL/log_entry "stdout output from fverify:"
               $UTIL/file_to_log stdout.log
               $UTIL/log_entry "stderr output from fverify"
               $UTIL/file_to_log stderr.log
          fi

          rm -f stdout.log
          rm -f stderr.log
          rm -f fverify.par

     fi
done

##########################################
# calculate checksums for all fits files
##########################################
$UTIL/log_entry "Calculating checksums for all FITS files"
for file in $(grep -v \.wrap $dump ); do

     if [ -a "$file" ]; then
          
          $UTIL/setup_parfile $FTOOLS/fchecksum.par infile=$file \
                                                    update=yes   \
                                                    datasum=yes  

          $FTOOLS/fchecksum >stdout.log 2>stderr.log
          $UTIL/ftool_test fchecksum $? $0 2 stdout.log stderr.log

     fi
done


###############################################################
# clear out any files not in the tape cat or trend cat.
# don't delete job.par or process.par
###########################################################
$UTIL/log_entry "Doing inventory of all files"

###########################
# dump the trend catalog and add trend files to $dump
###########################
trendcat=$($UTIL/generate_filename trendcat )
if [ ! -a $trendcat ]; then
     $UTIL/exception 3 "No trendcat file: $trendcat"
fi

rm -f trend.tmp
$UTIL/setup_parfile $FTOOLS/fdump.par infile=$trendcat    \
                                      outfile=trend.tmp   \
                                      rows=-              \
                                      columns="filename"  \
                                      prhead=no           \
                                      prdata=yes          \
                                      showcol=no          \
                                      showunit=no         \
                                      showscale=no        \
                                      showrow=no     

$FTOOLS/fdump >stdout.log 2>stderr.log
$UTIL/ftool_test fdump $? $0 2 stdout.log stderr.log

cat trend.tmp >>$dump
rm -f trend.tmp

######################################################
# add a couple extra file which shouldn't be removed,
# but aren't included in the tape and trend catalogs
######################################################

echo "$JOBPAR" >> $dump
echo "$PARFILE" >>$dump
echo "$dump" >>$dump

########################################
# remove all excess files
########################################

for file in $( ls -a ); do
    
     if [ -z "$(grep $file $dump )" ]; then
          $UTIL/exception $0 1 "Removing leftover file $file"
         # rm -f $file
     fi
done

###################################
# check for missing files
###################################
for file in $( cat $dump ); do

     file=${file%.wrap}

     if [ ! -a "$file" ]; then
          $UTIL/exception $0 2 "$file is missing"
     fi
done

rm -f $dump

#############################################
# set html files and job.pars to world read 
#############################################
$UTIL/log_entry "setting world read permission for .html and .par files"
for type in jobpar parfile           \
            joblog errlog milestones \
            eventinfo fileinfo header; do

     file=$($UTIL/generate_filename $type)
     chmod o+r $file

done

####################################################
# record some important file names in the job.par
####################################################

tapecat=$( $UTIL/generate_filename tapecat  )
trendcat=$($UTIL/generate_filename trendcat )
header=$(  $UTIL/generate_filename header   )
cover1=$(  $UTIL/generate_filename sourcepic sis any any any )
cover2=$(  $UTIL/generate_filename sourcepic gis any any any )

cover1=$(ls $cover1 2>/dev/null |head -1 )
cover2=$(ls $cover2 2>/dev/null |head -1 )

if [ -z "$cover1" ]; then
     cover1=$cover2
fi

if [ -z "$cover2" ]; then
     cover2=$cover1
fi




$UTIL/add_parameter $JOBPAR tapecat   $tapecat s "Distribution catalog file"
$UTIL/add_parameter $JOBPAR trendcat $trendcat s "Trend product catalog file"
$UTIL/add_parameter $JOBPAR header   $header   s "HTML header file"

$UTIL/add_parameter $JOBPAR cover1   "$cover1"  s "CD front cover image"
$UTIL/add_parameter $JOBPAR cover2   "$cover2"  s "CD back cover image"



     
############################
# save the jobpar 
############################
jobpar=$( $UTIL/generate_filename jobpar )
cp $JOBPAR $jobpar



exit 0