#!/usr/bin/ksh
#
# Processing History Extraction script, By Don Jennings, 2/2/94
#
# This script reads an ASCA processing history file specified by the
# first input argument ($1), extracts various information from it and
# writes it in the form of a "|" seperated line to the output file
# specified by the second intput argument ($2). Please note that the
# output line is appended to the output file; the original contents of
# the output file is not disturbed. 
# 
# The "various information" extracted is given by the values of the
# following processing history keyword values:
#
# OBJECT, SEQUENCE, RA, DEC, ROLL, STARTUTDATE, STARTUTTIME, ENDUTDATE,
# ENDUTTIME, TOTALTIME, USPINAME, JPPINAME, PROCDATE, GAINHISTVER,
# MKFILTERVER, FTOOLS, PROCVER, PROCLEVEL
#
#############################################################################
#
# extract the keyword values from the desired keywords in the input
# processing history file
#
lv=$(   grep PROCLEVEL=   $1 | awk ' BEGIN{FS = "="} {print $2}')
obj=$(  grep OBJECT=      $1 | awk ' BEGIN{FS = "="} {print $2}')
seq=$(  grep SEQUENCE=    $1 | awk ' BEGIN{FS = "="} {print $2}') 
ra=$(   grep RA=          $1 | awk ' BEGIN{FS = "="} {print substr($2,1,10)}')
dec=$(  grep DEC=         $1 | awk ' BEGIN{FS = "="} {print substr($2,1,10)}')
roll=$( grep ROLL=        $1 | awk ' BEGIN{FS = "="} {print substr($2,1,10)}')
sd=$(   grep STARTUTDATE= $1 | awk ' BEGIN{FS = "="} {print $2}')
st=$(   grep STARTUTTIME= $1 | awk ' BEGIN{FS = "="} {print $2}')
ed=$(   grep ENDUTDATE=   $1 | awk ' BEGIN{FS = "="} {print $2}')
et=$(   grep ENDUTTIME=   $1 | awk ' BEGIN{FS = "="} {print $2}')
tt=$(   grep TOTALTIME=   $1 | awk ' BEGIN{FS = "="} {print $2}')
uspi=$( grep USPINAME=    $1 | awk ' BEGIN{FS = "="} {print $2}')
jppi=$( grep JPPINAME=    $1 | awk ' BEGIN{FS = "="} {print $2}')
pdate=$(grep PROCDATE=    $1 | awk ' BEGIN{FS = "="} {print $2}')
ghf=$(  grep GAINHISTVER= $1 | awk ' BEGIN{FS = "="} {print $2}')
mkf=$(  grep MKFILTERVER= $1 | awk ' BEGIN{FS = "="} {print $2}')
ftool=$(grep FTOOLS=      $1 | awk ' BEGIN{FS = "="} {print $2}')
pver=$( grep PROCVER=     $1 | awk ' BEGIN{FS = "="} {print $2}')
#
# test the critical keyword values to make sure that they are non-empty; if
# one is found to be empty the print out an error message and exit
#
if [ "$lv" = "" ] || [ "$obj" = "" ] || [ "$seq" = "" ] || [ "$ra" = "" ] || [ "$dec" = "" ] || [ "$roll" = "" ] || [ "$pdate" = "" ] 
then 
  print "EXTRACT: missing infomation in file $1"
  print "EXTRACT aborts!" 
  exit 1
fi
#
# remove the '' marks from the following string keyword values
#
obj=$(  echo $obj  | awk ' {print substr($0,2,length($0)-2)}')
uspi=$( echo $uspi | awk ' {print substr($0,2,length($0)-2)}')
jppi=$( echo $jppi | awk ' {print substr($0,2,length($0)-2)}')
sd=$(   echo $sd   | awk ' {print substr($0,2,length($0)-2)}')
st=$(   echo $st   | awk ' {print substr($0,2,length($0)-2)}')
ed=$(   echo $ed   | awk ' {print substr($0,2,length($0)-2)}')
et=$(   echo $et   | awk ' {print substr($0,2,length($0)-2)}')
if [ "$uspi" = "" ]
then
  uspi="NONE"
fi
if [ "$jppi" = "" ]
then
  jppi="NONE"
fi
#
# build an accecptable file name based upon the $obj, $uspi and $jppi values.
# change any "/" characters in the object name into "-", any "#" characters
# in the object name into "_", any " " characters into "_" and any "+"
# characters into "_plus_" (the "/" "#" " " "+" characters reak havoc on html
# and unix file name syntax)
#
obj_fname=$( echo $obj | sed 's/\//-/g' | sed 's/ /_/g' | sed 's/#/_/g' | sed 's/+/_plus_/g')
uspi_fname=$( echo $uspi | sed 's/\//-/g' | sed 's/ /_/g' | sed 's/#/_/g' | sed 's/+/_plus_/g')
jppi_fname=$( echo $jppi | sed 's/\//-/g' | sed 's/ /_/g' | sed 's/#/_/g' | sed 's/+/_plus_/g')
#
# remove the 'GHF', 'MKF' and 'ftools' from the values of the GAINHISTVER,
# MKFILTERVER and FTOOLS keyword values, respectively. 
#
ghf=$(echo $ghf |awk 'index($0,"GHF") != 0 {print substr($0,index($0,"GHF")+3)}')
mkf=$(   echo $mkf   | awk ' index($0,"MKF") != 0 {print substr($0,index($0,"MKF")+3)}')
ftool=$( echo $ftool | awk ' index($0,"ftools.") != 0 {print substr($0,index($0,"ftools.")+7)}')
#
# extract the second digit of the SEQUENCE keyword value to determine the
# processing type
#
type=$(  echo $seq   | awk '{ print substr($0,2,1) }')
#
# if the second digit of SEQUENCE is '0' then this is a type 5 , else it is
# a type 0
#
if [ "$type" = "0" ]
then
  type="5"
else
  type="0"
fi
#
# convert the RA and DEC values into "old fashioned" units
#
oldra=$(/usr/local/bin/deg2hhmmss $ra)
olddec=$(/usr/local/bin/deg2degmmss $dec)
#
# write all the extracted keyword values to a single variable seperated by
# "|" marks (note that STARTUTDATE and STARTUTTIME, ENDUTDATE and ENDUTTIME
# are seperated by a blank space instead of a "|", and that SEQUENCE and
# PROCLEVEL are seperated by a ".").
# 
all=$obj'|'$seq'.'$lv'|'$ra'|'$dec'|'$roll'|'$sd' '$st'|'$ed' '$et'|'$tt'|'$uspi'|'$jppi'|'$type'|'$pdate'|'$ghf'|'$mkf'|'$ftool'|'$pver'|'$oldra'|'$olddec'|'$obj_fname'|'$uspi_fname'|'$jppi_fname
  
print $all  > > $2

#
# exit cleanly
#
exit 0
###############################################################################