generate_filename (version 1.10)

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

##########################################################################
#
# SYNTAX: generate_filename type inst index mode bitrate source
#
# DESCRIPTION: Generates a file name from information about that file.
#
# VERSION: 1.10
#
# HISTORY: 0.0 -> 1.0 8/19/96 
# HISTORY: quick analysis output files now
# HISTORY: have _sqa or _gqa as they were renamed in rev1
# HISTORY: 8/23/96 added scraps file type which is the tar-ed together
# HISTORY: small leftover raw files. intended to be kept as a trend
# HISTORY: product.
# HISTORY:
# HISTORY: 1.0 -> 1.1 9/10/96 
# HISTORY: changed sky and totsky to accept an additional energy attribute 
# HISTORY: in position 6
# HISTORY: deleted ".evt" from expo filename
# HISTORY: changed image type to corimg
# HISTORY: 
# HISTORY: 1.1 -> 1.2 9/13/96
# HISTORY: changed .log to .html
# HISTORY:
# HISTORY: 1.2 -> 1.3 9/26/96
# HISTORY: added sourcepic and smooth file types
# HISTORY: 
# HISTORY: 1.3 -> 1.4 10/8/96
# HISTORY: Added sourcecat, blankback, sourcefree.
# HISTORY: Modified spectrum and rmf
# HISTORY: 
# HISTORY: 1.4 -> 1.5 11/15/96
# HISTORY: added some quotes to the any instrument case of the rmf
# HISTORY: file type. This preserved wildcards and fixed the problem where
# HISTORY: the sis rmfs weren't being listed in the trend cat
# HISTORY: 
# HISTORY: 1.5 -> 1.6 11/25/96
# HISTORY: added brightearth, darkearth, calsource, and lightcurve
# HISTORY: 12/4/96 Changed sourcepic to be a .ps file instead of a .ppm file
# HISTORY: 
# HISTORY: 1.6 -> 1.7 12/19/96
# HISTORY: added sourceregion, specplot,lcplot, srcevent
# HISTORY: 
# HISTORY: 1.7 -> 1.8 2/4/97 
# HISTORY: added $index to file names for totexpo, totsky, corimg, smooth
# HISTORY: changed format for gif, sourcepic, sourceregion, backblank
# HISTORY: index is now forced to be a three digit number and padded with
# HISTORY: leading zeros if it isn't
# HISTORY: 2/6/97 Modified rmf so that source is in the source slot, 
# HISTORY: not the index slot.
# HISTORY: Changed the extension on srcevents from ".xtr" to ".ulc"
# HISTORY: 2/7/97 added sourceinfo
# HISTORY: 2/18/97 changed rmf to accomodate different numbers of
# HISTORY: PHA channels by reading NAXIS2 from the corresponding 
# HISTORY: spectrum file.
# HISTORY: 
# HISTORY: 1.8 -> 1.9 2/22/97
# HISTORY: Changed calsource to accomodate merged cal source spectra
# HISTORY: by taking bitrate out of name
# HISTORY: Added calsourceplot
# HISTORY: 2/25/97 modified rmf to accomodate calsource rmfs
# HISTORY: 
# HISTORY: 1.9 -> 1.10 3/24/97
# HISTORY: added orbit file as read from job.par
# HISTORY: added discrim and chaned sourceregion to accomodate 
# HISTORY: files for each of the four instruments.
#
##########################################################################

type=$1
inst=$2
index=$3
mode=$4
bitrate=$5

seq=$($UTIL/read_parfile $JOBPAR sequence )
ver=$($UTIL/read_parfile $JOBPAR seqprocnum )


#################################
# parse info into neutral format
#################################

if [ "$seq" == "any" ]; then
     seq='[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
fi


if [ "$inst" == "any" ]; then
     inst="[sg][0123]"
fi


case "$index" in
any)
     index="*"
     ;;
*\**|*\[*\]*)
     index="$index"
     ;;
*)
     index=$(echo $index | nawk '{printf ("%03d", $0)}')
     ;;
esac



if [ "$mode" == "any" ]; then
     case "$inst" in
         s[01]) mode="[01][123]";;
         g[23]) mode="7[01]";;
         *)  mode="[017][0123]"
     esac
fi


if [ "$bitrate" == "any" ]; then
     bitrate="[hml]"
fi

#############################
# general filename base
#############################
base="ad${seq}${inst}${index}${mode}${bitrate}"


###################################################
# determine the appropriate file name for the type
####################################################
case "$type" in

orbit)
     #########################################
     # orbit file
     #########################################
     thing=$($UTIL/read_parfile $JOBPAR orbit)
     echo ${thing##*/}
     ;;

scraps)
     #################################################
     # the tar-ed together small leftover raw files
     #################################################
     echo "ad${seq}_${ver}_scraps.tar" ;;

jobpar)
     ##############################
     # "saved" version of job.par
     ###############################
     echo "ad${seq}_${ver}_$JOBPAR" ;;

parfile)
     ##############################
     # "saved" version of process.par
     ###############################
     echo "ad${seq}_${ver}_$PARFILE" ;;

sciencetimes)
     ##############################
     # science times trend products
     ##############################
     echo "ad${seq}_science_times.$inst" ;;

ratefile)
     ################
     # SIS rate file
     ##################
     echo ad${seq}${inst}.rate;;

ascamode)
     #####################
     # ascamode trend file
     ######################
     echo ad${seq}_ascamode.txt ;;

ascalog)
     #####################
     # ascamode trend file
     ######################
     echo ad${seq}_ascalog.txt ;;

joblog)
     #####################
     # job log
     #####################
     echo ad${seq}_${ver}_joblog.html ;;

errlog)
     ###################
     # error log
     ###################
     echo ad${seq}_${ver}_errors.html ;;

milestones)
     ###################
     # job log index
     ###################
     echo ad${seq}_${ver}_index.html ;;

header)
     ###################
     # HTML header page
     ###################
     echo ad${seq}_${ver}_hdr_page.html;;

eventinfo)
     #####################################
     # HTML event file information page
     ####################################
     echo ad${seq}_${ver}_event_info.html;;

fileinfo)
     ####################################
     # HTML list of all files
     ###############################
     echo ad${seq}_${ver}_file_info.html;;

sourceinfo)
     ####################################
     # HTML list of all files
     ###############################
     echo ad${seq}_${ver}_source_info.html;;


procinfo)
     ###############################
     # HTML list of all files
     ###############################
     echo ad${seq}_${ver}_proc_info.txt;;

tapecat)
     #####################################
     # FITS format distribution catalog
     ####################################
     echo ad${seq}_tape.cat;;

trendcat)
     #####################################
     # FITS format distribution catalog
     ####################################
     echo ad${seq}_trend.cat;;

unfiltered) 
     #########################
     # unfiltered event file
     #########################
     echo "$base".unf ;;

event) 
     #########################
     # filtered event file
     #########################
     echo "$base".evt ;;

srcevent)
     #############################################
     # event file extracted for a given source
     #############################################
     source=$6
     if [ "$source" = "any" ]; then
          source="*"
     fi

     echo "${base}_${source}.ulc"
     ;;

brightearth) 
     #########################
     # bright earth event file
     #########################
     echo "$base".brt ;;

darkearth) 
     #########################
     # dark earth event file
     #########################
     echo "$base".drk ;;

calsource) 
     ####################################
     # GIS calibration source spectrum
     ####################################
     echo "ad${seq}${inst}${index}${mode}".cal ;;

calsourceplot) 
     ####################################
     # GIS calibration source spectrum
     ####################################
     echo "ad${seq}${inst}${index}${mode}"_cal.ps ;;


sourcefree) 
     #####################################
     # PI spectrum of source-free regions
     #####################################
     echo "ad${seq}${inst}${index}${mode}".free ;;

detimg) 
     #########################
     # detector image
     #########################
     echo "$base".detimg ;;

expo) 
     #########################
     # exposure map
     #########################
     echo "$base".expo ;;

sky) 
     #########################
     # sky map from ascaexpo
     #########################
     nrg=$6

     case "$nrg" in
     all) echo "$base".img ;;
     any) echo "$base"*.img ;;
     *) echo "${base}_${nrg}.img" ;;
     esac;;


dfe)
     ########################
     # dark frame error file
     ########################
     echo ad${seq}_${inst}.dfe ;;

quickout)
     #####################################
     # sqaplot/gqaplot ascii output file
     #####################################
     dum="${inst%[0123]}"
     dum="${dum%\[0123\]}"
     dum="${dum%\[01\]}"
     dum="${dum%\[23\]}"

     echo ${base}_${dum}qa.out;;

quickps)
     #####################################
     # sqaplot/gqaplot ascii output file
     #####################################
     dum="${inst%[0123]}"
     dum="${dum%\[0123\]}"
     dum="${dum%\[01\]}"
     dum="${dum%\[23\]}"

     echo ${base}_${dum}qa.ps;;

     

telemetry)
     #################################################
     # telemetry corresponding to a given event file
     #################################################
     event=$($UTIL/generate_filename unfiltered $inst $index $mode $bitrate)

     if [ ! -r "$event" ]; then
          event=$($UTIL/generate_filename event $inst $index $mode $bitrate)
          
          if [ ! -r "$event" ]; then
               exception $0 1 \
               "Can\'t read event file corresponding requested telemetry file"
               event="none"
          fi
     fi

     if [ "$event" != "none" ]; then
          $UTIL/read_fits_keyword $event[0] TLM_FILE
     fi;;

totsky)
     #########################################
     # sky image summed over all event files
     #########################################
     nrg=$6
     if [ "$nrg" = "any" -o -z "$nrg" ]; then
          nrg="*"
     fi

     case "$inst" in
          g*)      echo ad${seq}gis${index}${mode}_${nrg}.totsky;;
          s*)      echo ad${seq}sis${index}${mode}_${nrg}.totsky;;
          \[gs\]*) echo ad${seq}[sg]is${index}${mode}_${nrg}.totsky;;
          \[sg\]*) echo ad${seq}[sg]is${index}${mode}_${nrg}.totsky;;

          *) $UTIL/exception $0 1 \
                  "Unknown instrument type for totsky file: $inst";;
     esac;;

totexpo)
     #########################################
     # sky image summed over all event files
     #########################################
     case "$inst" in
          g*) echo ad${seq}gis${index}${mode}.totexpo;;
          s*) echo ad${seq}sis${index}${mode}.totexpo;;
          \[gs\]*) echo ad${seq}[sg]is${index}${mode}.totexpo;;
          \[sg\]*) echo ad${seq}[sg]is${index}${mode}.totexpo;;

          *) $UTIL/exception $0 1 \
                  "Unknown instrument type for totexpo file: $inst";;
     esac;;

corimg)
     #################################################################
     # sky image summed over all event files and exposure corrected
     # assumes for all energies
     #################################################################
     case "$inst" in
          g*) echo ad${seq}gis${index}${mode}.corimg;;
          s*) echo ad${seq}sis${index}${mode}.corimg;;
          \[gs\]*) echo ad${seq}[sg]is${index}${mode}.corimg;;
          \[sg\]*) echo ad${seq}[sg]is${index}${mode}.corimg;;

          *) $UTIL/exception $0 1 \
                  "Unknown instrument type for image file: $inst";;
     esac;;

gif)
     #################################
     # a combined image in GIF format
     #################################
     case ${inst} in
     \[sg\]*) inst="[sg]is";;
     \[gs\]*) inst="[sg]is";;
     esac
     
     echo ad${seq}${inst}${index}${mode}.gif;;

smooth)
     ############################################
     # smoothed and exposure corrected sky image           
     ############################################
     nrg=$6
     if [ "$nrg" = "any" -o -z "$nrg" ]; then
          nrg="*"
     fi

     case "$inst" in
          g*)      echo ad${seq}gis${index}${mode}_${nrg}.smooth;;
          s*)      echo ad${seq}sis${index}${mode}_${nrg}.smooth;;
          \[gs\]*) echo ad${seq}[sg]is${index}${mode}_${nrg}.smooth;;
          \[sg\]*) echo ad${seq}[sg]is${index}${mode}_${nrg}.smooth;;

          *) $UTIL/exception $0 1 \
                  "Unknown instrument type for smooth file: $inst";;
     esac;;

mask) 
     #########################
     # detector map
     #########################
     echo "$base".evt.imap ;;


sourcepic)
     ###########################################
     # an image showing the sources
     ###########################################
     case ${inst} in
     \[sg\]*) inst="[sg]is";;
     \[gs\]*) inst="[sg]is";;
     esac
     
     echo ad${seq}${inst}${index}${mode}_source.ps;;

sourcecat)
     ###########################################
     # an image showing the sources
     ###########################################
     case ${inst} in
     \[sg\]*) inst="[sg]is";;
     \[gs\]*) inst="[sg]is";;
     esac
     
     echo ad${seq}${inst}${index}${mode}.src;;

sourceregion)
     ###########################################
     # an image showing the sources
     ###########################################
     source=$6
     if [ "$source" = "any" ]; then
          source="[0-9]*"
     fi

     
     echo ad${seq}${inst}${index}${mode}_${source}.reg
     ;;



spectrum) 
     #########################
     # PI spectrum file
     #########################
     source=$6
     if [ "$source" = "any" ]; then
          source="[0-9]*"
     fi

     echo "ad${seq}${inst}${index}${mode}_$source".pi ;;

specplot) 
     #########################
     # PI spectrum file
     #########################
     source=$6
     if [ "$source" = "any" ]; then
          source="*"
     fi

     echo "ad${seq}${inst}${index}${mode}_${source}_pi.ps"
     ;;

discrim) 
     ######################################
     # SIS area discriminator mask region
     ######################################
     echo "ad${seq}${inst}${index}${mode}.discrim"
     ;;



backblank)
     ####################################################
     # background spectrum from a blanksky observation
     ####################################################
     source=$6
     if [ "$source" = "any" ]; then
          source="*"
     fi
     
     echo ad${seq}${inst}${index}${mode}_$source.blank;;

lightcurve) 
     #########################
     # PI spectrum file
     #########################
     source=$6
     if [ "$source" = "any" ]; then
          source="*"
     fi

     echo "ad${seq}${inst}${index}${mode}_$source".lc ;;

lcplot) 
     #########################
     # PI spectrum file
     #########################
     source=$6
     if [ "$source" = "any" ]; then
          source="*"
     fi

     echo "ad${seq}${inst}${index}${mode}_${source}_lc.ps" 
     ;;





rate)
     #########################
     # SIS rate file
     #########################
     case "$inst" in
          s0) echo ad"$seq"S0.rate;;
          s1) echo ad"$seq"S1.rate;;
          g[23]) echo "none";;
          *) $UTIL/exception $0 1 \
                  "Unknown instrument for rate file name generation"
     esac;;
          

rmf)
     ###########################
     # Response Matrix File
     ###########################
     case "$inst" in
     s[01]) 
          source=$6
          if [ "$source" = "any" ]; then
               source="*"
          fi

          echo "ad${seq}${inst}${index}${mode}_${source}.rmf"
          ;;   
     g[23])
          #########
          # GIS
          #########

          ##########################################################
          #get number of PHA bins from corresponding spectrum file
          ##########################################################
          if [ -z "$6" -o "$6" = "any" ]; then
               ###################################################
               # no source mentioned, assume we are talking about
               # a calibration source spectrum
               ####################################################
               spec=$($UTIL/generate_filename calsource \
                                              "$inst" "$index" "$mode" )

          else
               ###################################
               # assume this is a source spectrum
               ###################################
               spec=$($UTIL/generate_filename spectrum \
                                    "$inst" "$index" "$mode" "$bitrate" "$6" )
          fi


          nchannels=$($UTIL/read_fits_keyword $spec[1] NAXIS2 )
          if [ -z "$nchannels" ]; then
               $UTIL/exception $0 1 \
                  "Can't determine number of PHA channels from $spectrum"
               exit 0
          fi


          $UTIL/read_parfile $PARFILE ${inst}_${nchannels}rmf;;

     \[sg\]*|\[gs\]*) 
          for dum in s0 s1 g2 g3; do
               $UTIL/generate_filename rmf $dum "$index" "$mode" "$bitrate" "$6"
          done
          ;;
          
     *)
          #####################
          # unknown instrument
          #####################
          $UTIL/exception $0 1 "Unknown instrument for rmf file generation";;
     esac;;

arf) 
     #########################
     # PI spectrum file
     #########################
     source=$6
     if [ "$source" = "any" ]; then
          source="*"
     fi

     echo "ad${seq}${inst}${index}${mode}_$source".arf ;;



regionmap)
     #######################################################
     # a 1's and 0's FITS image of an saoimage region file
     # these are used as input detector maps by ascaexpo
     # see extractregion)
     ########################################################
     base=$($UTIL/generate_filename extractregion $inst $index $mode $bitrate )
     if [ "$base" == "none" ]; then
          echo "none"
     else
          echo $base.map
     fi;;

teldef)
     ############################
     # telescope definition file
     ############################
     case "$inst" in
     s[01])
          ##########
          # SIS 
          ##########
          echo "${inst}_teldef_ascalin.fits";;
     g[23])
          ##########
          # GIS 
          ##########
          event=$($UTIL/generate_filename unfiltered \
                                          $inst $index $mode $bitrate)

          if [ ! -r "$event" ]; then
               event=$($UTIL/generate_filename event \
                                               $inst $index $mode $bitrate)
          
               if [ ! -r "$event" ]; then
                    exception $0 1 \
                    "Can\'t read event file corresponding requested teldef file"
                    event="none"
               fi
          fi

          if [ "$event" != "none" ]; then
               ##################
               # read POS_DET
               #################
               $FTOOLS/fkeypar fitsfile=${event}[0] keyword="POS_DET"
               $UTIL/exit_test $? $0 1 \
                    "while reading POS_DET from $event to parfile"

               pos_det=$(pget fkeypar.par value)
               $UTIL/exit_test $? $0 1 "while reading pos_det from par file"

               #################
               # read ANO_TUNE
               #################
               $FTOOLS/fkeypar fitsfile=${event}[0] keyword="ANO_TUNE"
               $UTIL/exit_test $? $0 1 \
                    "while reading ANO_TUNE from $event to parfile"

               ano_tune=$(pget fkeypar.par value)
               $UTIL/exit_test $? $0 1 "while reading ano_tune from par file"

          fi

          ####################
          # debugging output
          ####################
          if [ -n "$DEBUG" ]; then
               echo $0: pos_det=$pos_det
               echo $0: ano_tune=$ano_tune
          fi

          ##########################################################
          # choose the correct file based on pos_det and ano_tune
          ##########################################################
          case "$pos_det" in
          *POW*) 
               case "$ano_tune" in
               *ON*) 
                    $FTOOLS/pget ./$PARFILE ${inst}onpow
                    $UTIL/exit_test $? $0 1 
                         "while reading ${inst}onpow from par file";;
               *OFF*)
                    $FTOOLS/pget ./$PARFILE ${inst}offpow
                    $UTIL/exit_test $? $0 1 \
                         "while reading ${inst}onpow from par file";;
               hound)
                    exception $0 1 \
                    "unknown ANO_TUNE keyword: ${ano_tune} from: ${event}";;

               esac;;

          *FLF*)
               case "$ano_tune" in
               *ON*) 
                    $FTOOLS/pget ./$PARFILE ${inst}onflf
                    $UTIL/exit_test $? $0 1 \
                         "while reading ${inst}onflf from par file";;
               *OFF*)
                    $FTOOLS/pget ./$PARFILE ${inst}offflf
                    $UTIL/exit_test $? $0 1 \
                        "while reading ${inst}onflf from par file";;
               *)
                    exception $0 1 \
                    "unknown ANO_TUNE keyword: ${ano_tune} from: ${event}";;
               esac;;

          *)
               ###########################
               # unknown POS_DET keyword
               ##########################
               exception $0 1 "Unknown POS_DET keyword from: $event";;

          esac;;


     *) 
          #####################
          # unknown instrument
          #####################
          $UTIL/exception $0 1 \
                      "Unknown instrument ${inst} for teldef file generation" ;;
     esac;;

extractregion)
     #######################################################
     # basic saoimage format region file for extracting events
     # this is used to remove the calibration source and noisy 
     # outer ring when filtering GIS data
     #######################################################
     case "$inst" in
     s[01])
          ################
          # SIS
          #################
          echo "none";;
     g[23])
          ################
          # GIS
          ################
          unfiltered=$($UTIL/generate_filename unfiltered \
                       $inst $index $mode $bitrate)

          if [ ! -r $unfiltered ]; then
               unfiltered=$($UTIL/generate_filename event \
                             $inst $index $mode $bitrate)
          fi

          ext=1
          if [ -r $unfiltered ]; then
               rawxbins=$($UTIL/read_fits_keyword ${unfiltered}[${ext}] \
                          RAWXBINS )

               rawybins=$($UTIL/read_fits_keyword ${unfiltered}[${ext}] \
                          RAWYBINS )

               if [ $rawxbins -ne $rawybins ]; then
                    ######################
                    # inconsistent bins
                    ######################
                    exception $0 1     "RAWXBINS=$rawxbins, "\
                                   "but RAWYBINS=$rawybins "\
                                   "in ${unfiltered}[${ext}]"
               else
                    ##########################################
                    # get appropriate filename from par file
                    ##########################################
                    $UTIL/read_parfile ./$PARFILE ${inst}region${rawxbins}
               fi
          else
               ##################################
               # can't read unfiltered event file
               ###################################
               $UTIL/exception $0 1 "Can't read RAW[X/Y]BINS from ${unfiltered}"
               
            
          fi;;
     *)
          #####################
          # unknown instrument
          #####################
          $UTIL/exception $0 1 "Unknown instrument: $inst" for extractregion ;;
     esac;;
     
* ) 
     ########################
     # unknown file type
     ########################
     $UTIL/exception $0 1 "can't generate file name for type: $type" ;;

esac