#! /bin/tcsh -f

# t_cats 4.0

# Creates observation catalogues and checks LVDL, CCD mode, bit rate, and
# GIS bit assignments. Also checks that SIS0 data have chip 1 on, and SIS1 data
# have chip 3 on.
#
# Uses code from do_cats scripts by K.Nandra/D.Turcan

# Adaptation by Paul O'Neill

# Calls script: do_cats.xco

# Date in final form: 28 December 2004

########################################################################################
# Welcome 
########################################################################################

echo ""
echo "gnt_infrm (t_cats) ++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "gnt_infrm (t_cats)  t_cats 4.0  STARTED "`date`
echo "gnt_infrm (t_cats) ++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo ""

########################################################################################
# Get parameters and set up directory variables
########################################################################################

#### get parameters ####

set obsid = $1        # sequence to process
set SEQ = $2          # root directory of this observing sequence
set XCO = $3          # directory of xco scripts
set checklvdl = $4    # if this = "check" then the lvdl setting is checked when determing 
                      # best data mode.

if ( $#checklvdl == 0 ) set checklvdl = "none"    # default

#### set variables ####

set AUX = $SEQ/aux            # aux directory for this sequence
set SCR = $SEQ/screened       # screened data
set UNSCR = $SEQ/unscreened   # unscreened data
set WORK = $SEQ/work          # work directory

########################################################################################
########################################################################################
########################################################################################

# Certain sequences need some minor changes to be made manually  the files before they
# can be processed. This section has some initial changes, such as setting the source RA and DEC
# in the attitude file to the correct nominal source position.

# Any previously hidden files are first unhidden

# Some files for certain sequences are also hidden at this point. The files that are known
# to not have the nominal chip in the CCD readout list are hidden. Files also that have
# a different readout order to the bulk of the other data in the sequence are hidden also.
# All files that have been hidden are documented as a warning in the log file.

########################################################################################
# Unhide any "hidden." files
########################################################################################

# This will rename any existing hidden. files back to their original names. Files with
# perm.hidden will not change.

echo "gnt_infrm (t_cats)"
echo "gnt_infrm (t_cats) looking for hidden. files in $UNSCR"

cd $UNSCR

set files = `ls hidden.*`

if ( $#files == "0" ) then
    echo "gnt_infrm (t_cats) no hidden files in $UNSCR"
else
    echo "gnt_infrm (t_cats) number of hidden files in ${UNSCR}: $#files"
    foreach file ( $files )
	set newname = `echo $file | sed s/"hidden."//g`
	echo "gnt_infrm (t_cats) unhiding file $file"
	mv $UNSCR/$file $UNSCR/$newname
    end
endif

echo "gnt_infrm (t_cats)"

echo "gnt_infrm (t_cats)"
echo "gnt_infrm (t_cats) looking for hidden. files in $SCR"

cd $SCR

set files = `ls hidden.*`

if ( $#files == "0" ) then
    echo "gnt_infrm (t_cats) no hidden files in $SCR"
else
    echo "gnt_infrm (t_cats) number of hidden files in ${SCR}: $#files"
    foreach file ( $files )
	set newname = `echo $file | sed s/"hidden."//g`
	echo "gnt_infrm (t_cats) unhiding file $file"
	mv $SCR/$file $SCR/$newname
    end
endif

echo "gnt_infrm (t_cats)"

########################################################################################
# Determine attitude file name(s)
########################################################################################

# There may be more than one attitude file

cd $AUX

set attfile = `ls fa*`

if ( "$#attfile" == "0" ) then     # no attitude files
    echo "gnt_fatal (t_cats) attitude file not found - exiting"
    exit 1
else
    echo "gnt_infrm (t_cats) number of attitude files in ${AUX}: $#attfile"
    foreach file ( $attfile )
	echo "gnt_infrm (t_cats) attitude file: $file"
    end
endif

########################################################################################
# Fix attitude files in specific sequences 
########################################################################################

cd $AUX

if ( "$obsid" == 70003010 ) then
    echo "gnt_infrm (t_cats) **** SEQUENCE $obsid - changing RA and DEC in attitude file $attfile[1] ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - changing RA and DEC in attitude file $attfile[1] ****"
    echo "RA 299.8681 / changed by Tartarus from 299.8750" >! $WORK/modplate.tmp
    echo "DEC 40.7339 / changed by Tartarus from 40.8167" >> $WORK/modplate.tmp
    fmodhead infile=$attfile[1] tmpfil=$WORK/modplate.tmp
endif

if ( "$obsid" == 70013000 ) then
    echo "gnt_infrm (t_cats) **** SEQUENCE $obsid - changing RA and DEC in first attitude file $attfile[1] ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - changing RA and DEC in first attitude file $attfile[1] ****"
    echo "RA 155.8774 / changed by Tartarus from 150.9080" >! $WORK/modplate.tmp
    echo "DEC 19.8650 / changed by Tartarus from 19.8710" >> $WORK/modplate.tmp
    fmodhead infile=$attfile[1] tmpfil=$WORK/modplate.tmp
endif

if ( ("$obsid" == 76010000) &&  (-e fa980423_2241.2250) ) then
    echo "gnt_infrm (t_cats) **** SEQUENCE $obsid - hiding attitude file fa980423_2241.2250 -> perm.hidden.fa980423_2241.2250 ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - hiding attitude file fa980423_2241.2250 -> perm.hidden.fa980423_2241.2250 ****"
    mv fa980423_2241.2250 perm.hidden.fa980423_2241.2250
endif

########################################################################################
# Hide files with known bad CCDLST
########################################################################################

# Some files from some sequences are known to have S0CCDLST/S1CCDLST that is different to the 
# CCDLST for the files having the most exposure time. These files are renamed to hidden.
# so that they won't be used. If a sequence is found to have a changing CCDLST and
# the script does not already hide the appropriate files, then a fatal error is 
# later reported. Files in which the nominal CCD is not used are hidden in the same way.

if ( "$obsid" == "70000010" ) then
    mv $UNSCR/ad70000010s000501h.unf $UNSCR/hidden.ad70000010s000501h.unf
    mv $UNSCR/ad70000010s000502h.unf $UNSCR/hidden.ad70000010s000502h.unf
    mv $UNSCR/ad70000010s000512h.unf $UNSCR/hidden.ad70000010s000512h.unf
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad70000010s000501h.unf was hidden by t_cats (S0CCDLST CHANGES) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad70000010s000502h.unf was hidden by t_cats (S0CCDLST CHANGES) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad70000010s000512h.unf was hidden by t_cats (S0CCDLST CHANGES) ****"
endif

if ( "$obsid" == "70012000" ) then
    mv $UNSCR/ad70012000s100301h.unf $UNSCR/hidden.ad70012000s100301h.unf
    mv $UNSCR/ad70012000s100302h.unf $UNSCR/hidden.ad70012000s100302h.unf
    mv $UNSCR/ad70012000s100312h.unf $UNSCR/hidden.ad70012000s100312h.unf
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad70012000s100301h.unf was hidden by t_cats (S1CCDLST CHANGES) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad70012000s100302h.unf was hidden by t_cats (S1CCDLST CHANGES) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad70012000s100312h.unf was hidden by t_cats (S1CCDLST CHANGES) ****"
endif

if ( "$obsid" == "71011000" ) then
    mv $UNSCR/ad71011000s000502h.unf $UNSCR/hidden.ad71011000s000502h.unf
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad71011000s000502h.unf was hidden by t_cats (S0CCDLST CHANGES) ****"
endif

if ( "$obsid" == "71023000" ) then
    mv $UNSCR/ad71023000s000401h.unf $UNSCR/hidden.ad71023000s000401h.unf
    mv $UNSCR/ad71023000s000402h.unf $UNSCR/hidden.ad71023000s000402h.unf
    mv $UNSCR/ad71023000s000412h.unf $UNSCR/hidden.ad71023000s000412h.unf
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad71023000s000401h.unf was hidden by t_cats (S0CCDLST CHANGES) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad71023000s000402h.unf was hidden by t_cats (S0CCDLST CHANGES) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad71023000s000412h.unf was hidden by t_cats (S0CCDLST CHANGES) ****"
endif

if ( "$obsid" == "71042000" ) then
    mv $UNSCR/ad71042000s000101h.unf $UNSCR/hidden.ad71042000s000101h.unf
    mv $UNSCR/ad71042000s000102h.unf $UNSCR/hidden.ad71042000s000102h.unf
    mv $UNSCR/ad71042000s000112h.unf $UNSCR/hidden.ad71042000s000112h.unf
    mv $UNSCR/ad71042000s100101h.unf $UNSCR/hidden.ad71042000s100101h.unf
    mv $UNSCR/ad71042000s100102h.unf $UNSCR/hidden.ad71042000s100102h.unf
    mv $UNSCR/ad71042000s100112h.unf $UNSCR/hidden.ad71042000s100112h.unf
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad71042000s000101h.unf was hidden by t_cats (S0 AND S1 MODE MUST MATCH) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad71042000s000102h.unf was hidden by t_cats (S0 AND S1 MODE MUST MATCH) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad71042000s000112h.unf was hidden by t_cats (S0 AND S1 MODE MUST MATCH) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad71042000s100101h.unf was hidden by t_cats (S0 AND S1 MODE MUST MATCH) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad71042000s100102h.unf was hidden by t_cats (S0 AND S1 MODE MUST MATCH) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad71042000s100112h.unf was hidden by t_cats (S0 AND S1 MODE MUST MATCH) ****"
    mv $SCR/ad71042000s000101h.evt $SCR/hidden.ad71042000s000101h.evt     # must hide screened so correct mode will be chosen
    mv $SCR/ad71042000s000102h.evt $SCR/hidden.ad71042000s000102h.evt
    mv $SCR/ad71042000s000112h.evt $SCR/hidden.ad71042000s000112h.evt
    mv $SCR/ad71042000s100101h.evt $SCR/hidden.ad71042000s100101h.evt
    mv $SCR/ad71042000s100102h.evt $SCR/hidden.ad71042000s100102h.evt
    mv $SCR/ad71042000s100112h.evt $SCR/hidden.ad71042000s100112h.evt
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad71042000s000101h.evt was hidden by t_cats (S0 AND S1 MODE MUST MATCH) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad71042000s000102h.evt was hidden by t_cats (S0 AND S1 MODE MUST MATCH) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad71042000s000112h.evt was hidden by t_cats (S0 AND S1 MODE MUST MATCH) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad71042000s100101h.evt was hidden by t_cats (S0 AND S1 MODE MUST MATCH) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad71042000s100102h.evt was hidden by t_cats (S0 AND S1 MODE MUST MATCH) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad71042000s100112h.evt was hidden by t_cats (S0 AND S1 MODE MUST MATCH) ****"
endif

if ( "$obsid" == "73039000" ) then
    mv $UNSCR/ad73039000s000401m.unf $UNSCR/hidden.ad73039000s000401m.unf
    mv $UNSCR/ad73039000s000402m.unf $UNSCR/hidden.ad73039000s000402m.unf
    mv $UNSCR/ad73039000s000412m.unf $UNSCR/hidden.ad73039000s000412m.unf
    mv $UNSCR/ad73039000s000501l.unf $UNSCR/hidden.ad73039000s000501l.unf
    mv $UNSCR/ad73039000s000601h.unf $UNSCR/hidden.ad73039000s000601h.unf
    mv $UNSCR/ad73039000s000602h.unf $UNSCR/hidden.ad73039000s000602h.unf
    mv $UNSCR/ad73039000s000612h.unf $UNSCR/hidden.ad73039000s000612h.unf
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad73039000s000401m.unf was hidden by t_cats (S0CCDLST CHANGES - NO CHIP 1) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad73039000s000402m.unf was hidden by t_cats (S0CCDLST CHANGES - NO CHIP 1) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad73039000s000412m.unf was hidden by t_cats (S0CCDLST CHANGES - NO CHIP 1) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad73039000s000501l.unf was hidden by t_cats (S0CCDLST CHANGES - NO CHIP 1) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad73039000s000601h.unf was hidden by t_cats (S0CCDLST CHANGES - NO CHIP 1) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad73039000s000602h.unf was hidden by t_cats (S0CCDLST CHANGES - NO CHIP 1) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad73039000s000612h.unf was hidden by t_cats (S0CCDLST CHANGES - NO CHIP 1) ****"
endif

if ( "$obsid" == "73053000" ) then
    mv $UNSCR/ad73053000s000801h.unf $UNSCR/hidden.ad73053000s000801h.unf
    mv $UNSCR/ad73053000s000802h.unf $UNSCR/hidden.ad73053000s000802h.unf
    mv $UNSCR/ad73053000s000812h.unf $UNSCR/hidden.ad73053000s000812h.unf
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad73053000s000801h.unf was hidden by t_cats (S0CCDLST CHANGES) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad73053000s000802h.unf was hidden by t_cats (S0CCDLST CHANGES) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad73053000s000812h.unf was hidden by t_cats (S0CCDLST CHANGES) ****"
endif

if ( "$obsid" == "74080000" ) then
    mv $UNSCR/ad74080000s100601h.unf $UNSCR/hidden.ad74080000s100601h.unf
    mv $UNSCR/ad74080000s100602h.unf $UNSCR/hidden.ad74080000s100602h.unf
    mv $UNSCR/ad74080000s100612h.unf $UNSCR/hidden.ad74080000s100612h.unf
    mv $UNSCR/ad74080000s100701h.unf $UNSCR/hidden.ad74080000s100701h.unf
    mv $UNSCR/ad74080000s100702h.unf $UNSCR/hidden.ad74080000s100702h.unf
    mv $UNSCR/ad74080000s100712h.unf $UNSCR/hidden.ad74080000s100712h.unf
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad74080000s100601h.unf was hidden by t_cats (S1CCDLST CHANGES) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad74080000s100602h.unf was hidden by t_cats (S1CCDLST CHANGES) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad74080000s100612h.unf was hidden by t_cats (S1CCDLST CHANGES) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad74080000s100701h.unf was hidden by t_cats (S1CCDLST CHANGES) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad74080000s100702h.unf was hidden by t_cats (S1CCDLST CHANGES) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad74080000s100712h.unf was hidden by t_cats (S1CCDLST CHANGES) ****"
endif

if ( "$obsid" == "74084000" ) then
    mv $UNSCR/ad74084000s000301h.unf $UNSCR/hidden.ad74084000s000301h.unf
    mv $UNSCR/ad74084000s000302h.unf $UNSCR/hidden.ad74084000s000302h.unf
    mv $UNSCR/ad74084000s000312h.unf $UNSCR/hidden.ad74084000s000312h.unf
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad74084000s000301h.unf was hidden by t_cats (S0CCDLST CHANGES) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad74084000s000302h.unf was hidden by t_cats (S0CCDLST CHANGES) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid -  ad74084000s000312h.unf was hidden by t_cats (S0CCDLST CHANGES) ****"
endif

if ( "$obsid" == "75017000" ) then
    mv $UNSCR/ad75017000s100201h.unf $UNSCR/hidden.ad75017000s100201h.unf
    mv $UNSCR/ad75017000s100202h.unf $UNSCR/hidden.ad75017000s100202h.unf
    mv $UNSCR/ad75017000s100212h.unf $UNSCR/hidden.ad75017000s100212h.unf
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad75017000s100201h.unf was hidden by t_cats (S1CCDLST CHANGES - NO CHIP 3) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad75017000s100202h.unf was hidden by t_cats (S1CCDLST CHANGES - NO CHIP 3) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad75017000s100212h.unf was hidden by t_cats (S1CCDLST CHANGES - NO CHIP 3) ****"
endif

if ( "$obsid" == "75047020" ) then
    mv $UNSCR/ad75047020s000401h.unf $UNSCR/hidden.ad75047020s000401h.unf
    mv $UNSCR/ad75047020s100401h.unf $UNSCR/hidden.ad75047020s100401h.unf
    mv $UNSCR/ad75047020s100402h.unf $UNSCR/hidden.ad75047020s100402h.unf
    mv $UNSCR/ad75047020s100412h.unf $UNSCR/hidden.ad75047020s100412h.unf
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad75047020s000401h.unf was hidden by t_cats (NO CHIP 1) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad75047020s100401h.unf was hidden by t_cats (NO CHIP 3) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad75047020s100402h.unf was hidden by t_cats (NO CHIP 3) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad75047020s100412h.unf was hidden by t_cats (NO CHIP 3) ****"
endif

if ( "$obsid" == "76068000" ) then
    mv $UNSCR/ad76068000s000401h.unf $UNSCR/hidden.ad76068000s000401h.unf
    mv $UNSCR/ad76068000s000402h.unf $UNSCR/hidden.ad76068000s000402h.unf
    mv $UNSCR/ad76068000s000412h.unf $UNSCR/hidden.ad76068000s000412h.unf
    mv $UNSCR/ad76068000s100401h.unf $UNSCR/hidden.ad76068000s100401h.unf
    mv $UNSCR/ad76068000s100402h.unf $UNSCR/hidden.ad76068000s100402h.unf
    mv $UNSCR/ad76068000s100412h.unf $UNSCR/hidden.ad76068000s100412h.unf
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad76068000s000401h.unf was hidden by t_cats (S0CCDLST CHANGES - NO CHIP 1) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad76068000s000402h.unf was hidden by t_cats (S0CCDLST CHANGES - NO CHIP 1) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad76068000s000412h.unf was hidden by t_cats (S0CCDLST CHANGES - NO CHIP 1) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad76068000s100401h.unf was hidden by t_cats (S1CCDLST CHANGES - NO CHIP 3) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad76068000s100402h.unf was hidden by t_cats (S1CCDLST CHANGES - NO CHIP 3) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad76068000s100412h.unf was hidden by t_cats (S1CCDLST CHANGES - NO CHIP 3) ****"
endif

if ( "$obsid" == "77007000") then
    mv $UNSCR/ad77007000s000701h.unf $UNSCR/hidden.ad77007000s000701h.unf
    mv $UNSCR/ad77007000s000801h.unf $UNSCR/hidden.ad77007000s000801h.unf
    mv $UNSCR/ad77007000s000901h.unf $UNSCR/hidden.ad77007000s000901h.unf
    mv $UNSCR/ad77007000s100701h.unf $UNSCR/hidden.ad77007000s100701h.unf
    mv $UNSCR/ad77007000s100801h.unf $UNSCR/hidden.ad77007000s100801h.unf
    mv $UNSCR/ad77007000s100901h.unf $UNSCR/hidden.ad77007000s100901h.unf
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad77007000s000701h.unf was hidden by t_cats (S0CCDLST CHANGES - NO CHIP 1) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad77007000s000801h.unf was hidden by t_cats (S0CCDLST CHANGES - NO CHIP 1) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad77007000s000901h.unf was hidden by t_cats (S0CCDLST CHANGES - NO CHIP 1) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad77007000s100701h.unf was hidden by t_cats (S1CCDLST CHANGES - NO CHIP 3) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad77007000s100801h.unf was hidden by t_cats (S1CCDLST CHANGES - NO CHIP 3) ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - ad77007000s100901h.unf was hidden by t_cats (S1CCDLST CHANGES - NO CHIP 3) ****"
endif

if ( "$obsid" == "77045000" ) then
    mv $UNSCR/ad77045000s100501h.unf $UNSCR/hidden.ad77045000s100501h.unf
    echo "gnt_warng (t_cats) *** SEQUENCE $obsid - ad77045000s100501h.unf was hidden by t_cats (S1CCDLST CHANGES - NO CHIP 3) ****"
endif

########################################################################################
# Create a list of the effective event thresholds
########################################################################################

# The unscreened files are examined and the event threshold
# and lower discriminator settings are determined and stored in a text file. The start and stop
# times of each file are also recorded. 

# The GTIs of each file are collated and a FITS file is created that has the observing
# parameters for each GTI on the unscreened observation.

# If the "effective event threshold" changes during the GTI, then the
# file useband2 will be created so that the soft2
# band will be used in the web pages. The BRIGHT mode files are examined because there will always be
# BRIGHT files that cover the entire observation.

# Note that, regardless of the event threshold, soft, full, soft2 and full2 bands are all always used
# and the appropriate products are then selected for display on the web pages.

#### delete exisiting files ####

cd $WORK

if ( -e useband2 ) rm useband2

foreach inst ( sis0 sis1 )
    if ( -e thresh_${inst}_perfile.temp ) rm thresh_${inst}_perfile.temp
    if ( -e thresh_${inst}.temp ) rm thresh_${inst}.temp
    if ( -e thresh_${inst}.fits ) rm thresh_${inst}.fits    
    touch thresh_${inst}_perfile.temp
end

#### start loop through instruments ####

cd $UNSCR

foreach inst ( sis0 sis1 )

    echo "gnt_infrm (t_cats) recording the effective event thresholds and times"

 
#### set parameters and find the BRIGHT mode files ####

    if ( $inst == sis0 ) then
        set files = ( `ls ad${obsid}s0???02?.unf` )     # BRIGHT mode sis0 .unf files
	set change = "constant"                       # default is no change in effective event threshold
        set det = "S0"                                
        set detnum = "0"
        set chip = "1"                                # SIS0 uses chip 1
	set ofile = $WORK/thresh_sis0_perfile.temp
    else
        set files = ( `ls ad${obsid}s1???02?.unf` )     # BRIGHT mode sis1 .unf files
	set change = "constant"
        set det = "S1"
        set detnum = "1"
        set chip = "3"                                # SIS1 uses chip 3
	set ofile = $WORK/thresh_sis1_perfile.temp
    endif

    if ( $#files == "0" ) then         # if no BRIGHT files
	echo "gnt_fatal (t_cats) no BRIGHT $inst files in $UNSCR"
	exit 1
    endif

    echo "gnt_infrm (t_cats) number of BRIGHT $inst unscreened files: $#files"
    echo "gnt_infrm (t_cats)"
             
 
#### start loop through all files ####
 
    @ filenum = 0
             
    foreach file ( $files )
                                                                                                                  
	@ filenum ++              # next file for this inst
             

#### get the parameters in each file ####
 
        fkeypar fitsfile=${file}+0 keyword=${det}CCDMOD          # CCD mode
        set ccd = `pget fkeypar value`
             
        fkeypar fitsfile=${file}+0 keyword=${det}_EVTR${chip}    # event threshold
        set evtr = `pget fkeypar value`
 
        fkeypar fitsfile=${file}+0 keyword=${det}_LVENA          # level discrimination off/on
        set disc = `pget fkeypar value`
 
        fkeypar fitsfile=${file}+0 keyword=${det}_LVDL${chip}    # LVDL
        set lvdl = `pget fkeypar value`
 
        fkeypar fitsfile=${file}+0 keyword=EXPOSURE              # exposure time
        set exp = `pget fkeypar value`
 
        fkeypar fitsfile=${file}+2 keyword=TSTART                # start time from GTI extension
        set tstart = `pget fkeypar value`

        fkeypar fitsfile=${file}+2 keyword=TSTOP                 # stop time from GTI extension
        set tstop = `pget fkeypar value`

 
#### determine effective event threshold ###
 
        set diff = `echo $lvdl $evtr | awk '{print ($1>$2)}'`      # is the lvdl greater than the event threshold
 
        if ( ($diff == "1") && ($disc == "1") ) then               # lvdl is ON and is greater than evtr
            set efevtr = $lvdl
        else
            set efevtr = $evtr                                     # the effective event threshold is the actual event threshold
        endif
 
        if ( $filenum == "1" ) set refefevtr = $efevtr             # the starting effective event threshold
 
        if ( "$efevtr" != "$refefevtr" ) then
	    set change = "change"     # different to first file
	    echo "gnt_warng (t_cats) **** effective event threshold changes during observation ****"
 	    echo "The effective event threshold changed during the observation - use full2 (0.6-10 keV) and soft2 (0.6-2.0 keV) bands instead of full (0.5-10 keV) and soft (0.5-2.0 keV) bands." >! ${WORK}/useband2  # record this information by creating the file useband2
	endif

#### copy the GTIs to a new FITS file ####

# A FITS file is created that has the observing parameter values for each GTI in the unf file.

	ftabcopy infile=${file}+2 outfile="${WORK}/gtifile.fits" columns="START,STOP" rows="-" copyprime="no" clobber=yes

	fcalc infile="${WORK}/gtifile.fits+1" outfile="${WORK}/gtifile.fits" clname="FILENAME" expr="'${file}'" tform="A22" clobber=yes   # file name for these GTIs
	fcalc infile="${WORK}/gtifile.fits+1" outfile="${WORK}/gtifile.fits" clname="CCD" expr="${ccd}"  clobber=yes                      # CCD mode  
	fcalc infile="${WORK}/gtifile.fits+1" outfile="${WORK}/gtifile.fits" clname="EVTR" expr="${evtr}"  clobber=yes                    # event threshold
	fcalc infile="${WORK}/gtifile.fits+1" outfile="${WORK}/gtifile.fits" clname="DISC" expr="${disc}"  clobber=yes                    # disc on/off
	fcalc infile="${WORK}/gtifile.fits+1" outfile="${WORK}/gtifile.fits" clname="LVDL" expr="${lvdl}"  clobber=yes                    # lower discriminator
	fcalc infile="${WORK}/gtifile.fits+1" outfile="${WORK}/gtifile.fits" clname="EFEVTR" expr="${efevtr}"  clobber=yes                # effective event threshold

	if ( $filenum == "1" ) then                
	    cp ${WORK}/gtifile.fits ${WORK}/thresh_${inst}.fits    # create new file for this inst 
	else
	    fmerge infiles="${WORK}/thresh_${inst}.fits ${WORK}/gtifile.fits" outfile="${WORK}/thresh_${inst}.fits" columns="START,STOP,FILENAME,CCD,EVTR,DISC,LVDL,EFEVTR" copyprime=no clobber=yes                              # append to file already created     
	endif

 
#### write the parameters to file ####

# this file has the details per unf file 

        echo "gnt_infrm (t_cats) $obsid | $detnum | $filenum | $file | $tstart | $tstop | $ccd | $evtr | $disc | $lvdl | $exp | $efevtr | $change"
        echo "$obsid | $detnum | $filenum | $file | $tstart | $tstop | $ccd | $evtr | $disc | $lvdl | $exp | $efevtr | $change" >> $ofile           

 
#### close some statements and loops ####
 
    end   # end loop through all files

end       # end loop through instruments

#### sort the GTIs and dump ####

cd $WORK

foreach inst ( sis0 sis1 )
    fsort infile=thresh_${inst}.fits+1 columns="START" method="HEAP"
    fdump infile=thresh_${inst}.fits+1 columns="START,STOP,EFEVTR" rows="-" outfile="thresh_${inst}.temp" pagewidth=200 clobber=yes prhead=no showrow=no showcol=no showunit=no
end

########################################################################################
########################################################################################
########################################################################################

# this section of t_cats gets the object name, makes a catalog, determines the 
# dominant SIS data mode (BRIGHT or BRIGHT2), and gets the bitrates. A combined
# makefilter file is also created and the minimum acceptable bright Earth angle
# is determined. Some plots from the mkf file are created.

########################################################################################
# Check that some SIS0, SIS1, GIS2, GIS3 unscreened and screened files exist
########################################################################################

set havefiles = "GOOD"

foreach inst ( s0 s1 g2 g3 )
    set files = `ls -l $UNSCR/ad*${inst}*`
    echo "gnt_infrm (t_cats) number of unscreened $inst files: $#files"
    if ( $#files == "0" ) set havefiles = "ERROR"
end

foreach inst ( s0 s1 g2 g3 )
    set files = `ls -l $SCR/ad*${inst}*`
    echo "gnt_infrm (t_cats) number of screened $inst files: $#files"
    if ( $#files == "0" ) set havefiles = "ERROR"
end

if ( $havefiles == "ERROR" ) then
    echo "gnt_fatal (t_cats) missing unscreened or screened files"
    exit 1
endif

########################################################################################
# Get object name from attitude file
########################################################################################

echo "gnt_infrm (t_cats) Getting object name from attitude file"

cd $AUX
set attfile = `ls -1 fa* | head -1`   # first attitude file

cd $WORK

set object = `fkeyprint ${AUX}/${attfile}+1 OBJECT | grep "OBJECT  =" | sed s/\\[/"\("/g | sed s/\\]/"\)"/g`
set object = `echo "$object[3] $object[4]" | sed s/"'"//g | sed s/"\/"//g | sed s/" "//g`

echo "$object" >! $WORK/object.temp

echo "gnt_infrm (t_cats) Object name: $object"

########################################################################################
# Make observation catalogues
########################################################################################

# The hidden files won't be found so will not appear in the cat file.

#### confirm presence of data ####

foreach dir ( $AUX $SCR $UNSCR )
    set files = `ls -1 $dir`
    if ( $#files == 0 ) then
	echo "gnt_fatal (t_cats) directory $dir is empty"
	exit 1
    endif
end

#### make observation catalogues using xselect ####

cd $WORK
xselect @$XCO/do_cats.xco

########################################################################################
# Determine dominant SIS data mode using cat files
########################################################################################

echo " "
echo "gnt_infrm (t_cats) Determining dominant SIS datamode (BRIGHT or BRIGHT2)"
echo " "

fdump s0_bright.cat outfile=$WORK/bright.temp columns=DATAMODE rows=- \
    prhead=no showcol=no showrow=no page=no showunit=no clobber=yes

fdump s0_bright2.cat outfile=$WORK/bright2.temp columns=DATAMODE rows=- \
    prhead=no showcol=no showrow=no page=no showunit=no clobber=yes

set brfiles = `cat $WORK/bright.temp`
set b2files = `cat $WORK/bright2.temp`

echo "gnt_infrm (t_cats) Number of BRIGHT mode files : "$#brfiles
echo "gnt_infrm (t_cats) Number of BRIGHT2 mode files: "$#b2files

if ( $#brfiles > $#b2files ) then     # have more BRIGHT than BRIGHT2
    set datamode = "BRIGHT"
else
    set datamode = "BRIGHT2"
endif

echo $datamode >! $WORK/datamode.temp

########################################################################################
# Get the GIS and SIS bitrates
########################################################################################

cd $UNSCR

#### BITRATE SIS files ####

if ( -e $WORK/bitrate_sis.temp ) rm $WORK/bitrate_sis.temp
touch $WORK/bitrate_sis.temp

set sisfiles = `ls ad*s*`

foreach sisfile ( $sisfiles )
    set brate = `fkeyprint ${sisfile}+0 keynam=BIT_RATE | grep "BIT_RATE=" | sed s/"BIT_RATE= '"//g`
    echo "$sisfile $brate[1]" >> $WORK/bitrate_sis.temp
end

#### BITRATE GIS files ####

if ( -e $WORK/bitrate_gis.temp ) rm $WORK/bitrate_gis.temp
touch $WORK/bitrate_gis.temp

set gisfiles = `ls ad*g*`

foreach gisfile ( $gisfiles )
    set brate = `fkeyprint ${gisfile}+0 keynam=BIT_RATE | grep "BIT_RATE=" | sed s/"BIT_RATE= '"//g`
    echo "$gisfile $brate[1]" >> $WORK/bitrate_gis.temp
end

########################################################################################
# Merge and sort makefilter file(s) 
########################################################################################

cd $AUX

set mkffiles = `ls -1 *.mkf`

if ( $#mkffiles > 1 ) then          # more than one mkf file

    echo " "
    echo "gnt_infrm (t_cats) fmerging $#mkffiles mkf files into a single file"

    flcol $mkffiles[1] | grep -v "___" | awk '{print $1}' >! cols

    fmerge "$mkffiles" $mkffiles[1] "@cols" lastkey="TSTOP" clobber=yes

    foreach mkf ( $mkffiles )
	if ( $mkf != $mkffiles[1] ) rm -f $mkf
    end

    set mkf = $AUX/$mkffiles[1]

else if ( $#mkffiles == 1 ) then    # only have single mkf file

    set mkf = $AUX/$mkffiles[1]

else                                # don't have any mkf files

    echo "gnt_fatal (t_cats) makefilter file does not exist"
    exit 1

endif

echo "gnt_infrm (t_cats) Make filter file is: $mkf"
echo "gnt_infrm (t_cats) Sorting $mkf"

fsort $mkf columns=TIME method=heap

if ( -e cols ) rm -f cols

########################################################################################
# Determine minimum acceptable bright Earth angle
########################################################################################

# determine the minimum bright Earth angle in the data.  If this is less than 20 degress, 
# then the minimum acceptable angle is set to 20 deg.

echo "gnt_infrm (t_cats) Determining minimum acceptable BR_EARTH angle"
echo " "

#### make a X-Y text data file of BR_EARTH angles ####

cd $AUX

echo "plot" >! $AUX/comm.pco
echo "wenv ascii" >> $AUX/comm.pco
echo "quit" >> $AUX/comm.pco

if ( -e ascii.qdp ) rm ascii.qdp
 
if ( -e ascii.pco ) rm ascii.pco

fplot $mkf xparm="BR_EARTH" yparm="S0_EVNT1" rows="-" device="/null" \
    pltcmd="@comm.pco"

#### determine and store the BR_EARTH angle ####

echo "{ if ( "\$"1 > s ) s = "\$"1 }" >! $AUX/awkfile.tmp
echo "END { print s }" >> $AUX/awkfile.tmp

awk '{ if ($2 > 200 && $1 < 35) print $1,$2 }' $AUX/ascii.qdp >! $AUX/ascii.tmp

set max = `awk -f $AUX/awkfile.tmp < $AUX/ascii.tmp`
set brangle = `echo $max 5 | awk '{print int($1+$2)}'`

if ( "$brangle" < 20 ) set brangle = 20

echo $brangle >! $WORK/brangle.temp

########################################################################################
# Create mkf plots
########################################################################################

echo "wi 2" >! $AUX/comm2.pco
echo "r y 0.0 0.05" >> $AUX/comm2.pco
echo "wi 6" >> $AUX/comm2.pco
echo "r y -1 3" >> $AUX/comm2.pco
echo "wi 7" >> $AUX/comm2.pco
echo "r y -1 3" >> $AUX/comm2.pco
echo "h mkf1.tmp/vps" >> $AUX/comm2.pco
echo "quit" >> $AUX/comm2.pco

fplot $mkf xparm="TIME" yparm="ANG_DIST COR ELV BR_EARTH S0_MODE S1_MODE" rows="-" \
    device="/null" offset=yes pltcmd="@$AUX/comm2.pco"

fplot $mkf xparm="ELV" yparm="S0_EVNT1 S0_PIXL1 S1_EVNT3 S1_PIXL3" rows="-" \
    device="mkf2.tmp/vps" pltcmd="quit"

fplot $mkf xparm="BR_EARTH" yparm="S0_EVNT1 S0_PIXL1 S1_EVNT3 S1_PIXL3" rows="-" \
    device="mkf3.tmp/vps" pltcmd="quit"

cat mkf1.tmp mkf2.tmp mkf3.tmp > mkf.ps

########################################################################################
########################################################################################
########################################################################################

# This section of t_cats confirms that the GIS bit assignments are standard.
# GIS files must have either 1024 or 128 PHA bins, and  256 RAWXBINS, RAWYBINS
# GIS3 data with the bitfix problem have 128 PHA bins.  These data can be handled
# by the Tartarus scripts.

echo "gnt_infrm (t_cats) checking that GIS2 files have standard bit assigments"

########################################################################################
# Check GIS2 unscreened files
########################################################################################

cd $UNSCR

set gisfiles = `ls ad*g2*`     # check GIS2 files - must have 1024 PHA_BINS

foreach gisfile ( $gisfiles )

    set numbin = `fkeyprint ${gisfile}+0 keynam=PHA_BINS | grep "PHA_BINS="`
    if ( $numbin[2] != "1024" ) then
	echo "gnt_fatal (t_cats) NON-standard number of PHA_BINS $numbin[2]"
	exit 1
    endif

    set numbin = `fkeyprint ${gisfile}+0 keynam=RAWXBINS | grep "RAWXBINS="`
    if ( $numbin[2] != 256 ) then
	echo "gnt_fatal (t_cats) NON-standard number of RAWXBINS ($numbin[2])"
	exit 1
    endif

    set numbin = `fkeyprint ${gisfile}+0 keynam=RAWYBINS | grep "RAWYBINS="`
    if ( $numbin[2] != 256 ) then
	echo "gnt_fatal (t_cats) NON-standard number of RAWYBINS ($numbin[2])"
	exit 1
    endif

end

echo "gnt_infrm (t_cats) GIS2 bit assignments are okay."

########################################################################################
# Check GIS3 unscreened files
########################################################################################

cd $UNSCR

echo "gnt_infrm (t_cats)"
echo "gnt_infrm (t_cats) now checking GIS3 bit assignments. PHA_BINS must be either"
echo "gnt_infrm (t_cats) 1024 or 128 (BITFIX problem)"

set gisfiles = `ls ad*g3*`

foreach gisfile ( $gisfiles )

    set numbin = `fkeyprint ${gisfile}+0 keynam=PHA_BINS | grep "PHA_BINS="`
    if ( ($numbin[2] != "1024") && ($numbin[2] != "128") ) then
	echo "gnt_fatal (t_cats) NON-standard number of PHA_BINS $numbin[2]"
	exit 1
    endif

    if ( $numbin[2] == "128" ) then
    	echo "gnt_infrm (t_cats) $numbin[2] PHA_BINS in $gisfile"
    endif

    set numbin = `fkeyprint ${gisfile}+0 keynam=RAWXBINS | grep "RAWXBINS="`
    if ( $numbin[2] != 256 ) then
	echo "gnt_fatal (t_cats) NON-standard number of RAWXBINS ($numbin[2])"
	exit 1
    endif

    set numbin = `fkeyprint ${gisfile}+0 keynam=RAWYBINS | grep "RAWYBINS="`
    if ( $numbin[2] != 256 ) then
	echo "gnt_fatal (t_cats) NON-standard number of RAWYBINS ($numbin[2])"
	exit 1
    endif

end

echo "gnt_infrm (t_cats) GIS3 bit assignments are okay."

########################################################################################
########################################################################################
########################################################################################

# this section of t_cats determines which combined ccd and lvdl mode has the
# longest good time, as found from the evt files in the screened directory. The screened files
# that do not have both the best ccd mode AND best lvdl are renamed with the prefix
# hidden., so that they will not be found by ascascreen

#### THIS IS CARRIED OUT ONLY IF LVDL CHECKING IS REQUESTED ####

########################################################################################
# Determine if should check the lvdl
########################################################################################

if ( "$checklvdl" == "check" ) then

    echo "gnt_infrm (t_cats) **** SEQUENCE $obsid - LVDL is being checked ****"
    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - LVDL is begin checked ****"

########################################################################################
# Start the foreach inst loop for sis0 and sis1
########################################################################################

# check sis0 and sis1 files separately

foreach inst ( sis0 sis1 )

########################################################################################
# Determine modes and exposure for each file in screened directory
########################################################################################

# the screened files are used here since these will only contain GOOD time data.

#### use the screened BRIGHT mode files ####

    if ( $inst == sis0 ) set files = ( `ls $SCR/ad*s000*02*.evt` )     # BRIGHT mode sis0 .evt files
    if ( $inst == sis1 ) set files = ( `ls $SCR/ad*s100*02*.evt` )     # BRIGHT mode sis1 .evt files

    if ( $#files == "0" ) then         # if no BRIGHT files
	echo "gnt_fatal (t_cats) no BRIGHT $inst files in $SCR - exiting"
	exit 1
    else
	echo "gnt_infrm (t_cats) number of BRIGHT $inst screened files: $#files"
    endif

#### get the modes from each file ####

    set ccd0 = ()            # CCD mode (1,2 or 4)
    set evtr0 = ()           # event threshold
    set disc0 = ()           # level discrimination enable=1  or no discrimination=0
    set lvdl0 = ()           # lower level discriminator value (=event threshold if no level discrimination)
    set efevtr0 = ()         # effective event threshold
    set exp0 = ()            # exposure time
    set modes0 = ()          # has the ccd and lvdl info for each ccd/lvdl combination

    if ( -e $WORK/${inst}modes.temp ) rm $WORK/${inst}modes.temp
    touch $WORK/${inst}modes.temp

    foreach file ( $files )

# get CCD mode,event threshold,level discrimination on/off,lower level discriminator value  

	if ( $inst == "sis0" ) then
	    set det = "S0"      # SIS0 uses chip 1
	    set chip = "1"
	else
	    set det = "S1"   
	    set chip = "3"       # SIS1 uses chip 3
	endif

	fkeypar fitsfile=${file}+0 keyword=${det}CCDMOD
	set ccd = `pget fkeypar value`
	set ccd0 = ( $ccd0 $ccd )

	fkeypar fitsfile=${file}+0 keyword=${det}_EVTR${chip}    # event threshold
	set evtr = `pget fkeypar value`     
	set evtr0 = ( $evtr0 $evtr )

	fkeypar fitsfile=${file}+0 keyword=${det}_LVENA    # level discrimination off/on
	set disc = `pget fkeypar value`
	set disc0 = ( $disc0 $disc )

	fkeypar fitsfile=${file}+0 keyword=${det}_LVDL${chip}    # LVDL
	set lvdl = `pget fkeypar value` 
	set lvdl0 = ( $lvdl0 $lvdl )

# determine effective event threshold

	set diff = `echo $lvdl $evtr | awk '{print ($1>$2)}'`

	if ( ($diff == "1") && ($disc == "1") ) then    # lvdl is ON and is greater than evtr - use lvdl as effective event threshold
	    set efevtr = $lvdl
	else
	    set efevtr = $evtr   # the effective event threshold is the actual event threshold
	endif
	set efevtr0 = ( $efevtr0 $efevtr )

# exposure time

	fkeypar fitsfile=${file}+0 keyword=EXPOSURE
	set exp = `pget fkeypar value`
	set exp0 = ( $exp0 $exp )

# write info to file

	echo "$file $ccd $evtr $disc $lvdl $efevtr $exp" >> $WORK/${inst}modes.temp

# add this mode to the list of available modes, if it is not already in the list

	set havemode = "no"               # assume not in list
	foreach mode ( $modes0 )          # check the modes in the list to see if this mode combination (ccd-efevtr) is already listed
	    if ( "${ccd}-${efevtr}" == $mode ) set havemode = "yes"     # this mode has already been listed
	end
	if ( $havemode == "no" ) set modes0 = ( $modes0 "${ccd}-${efevtr}" )   # add this mode to the list

    end

#### check that the lists are the same length - must have one entry for each file ####

    if ( ($#ccd0 != $#files) || ($#evtr0 != $#files) || ($#disc0 != $#files) || ($#lvdl0 != $#files) || ($#efevtr0 != $#files) || ($#exp0 != $#files) ) then
	echo "gnt_fatal (t_cats) error getting modes from unscreened files"
	exit 1
    endif

#### display the modes ####

    echo "gnt_infrm (t_cats)"
    echo "gnt_infrm (t_cats) $inst file, ccd mode, evt thresh, disc on/off, LVDL, eff thresh, exposure"
    echo "gnt_infrm (t_cats)"

    @ index = 0
    foreach file ( $files )
	@ index ++
	echo "gnt_infrm (t_cats) $file $ccd0[$index] $evtr0[$index] $disc0[$index] $lvdl0[$index] $efevtr0[$index] $exp0[$index]"
    end

########################################################################################
# Determine the total exposure times for each of the unique modes
########################################################################################

#### check the list with the modes ####

    if ( $#modes0 == "0" ) then
	echo "gnt_fatal (t_cats) there are no $inst modes - exiting"
	exit 1
    endif

#### determine the cumulative exposure times ####

    set totexp = ()    # start list of total exposure for each mode

    foreach mode ( $modes0 )     # loop through each mode

	echo "gnt_infrm (t_cats) adding up times for mode $mode"
	@ exp = 0
	@ index = 0

	foreach file ( $files )
	    @ index ++
	    if ( "$ccd0[$index]-$efevtr0[$index]" == $mode ) then   # does this file have mode $mode
		set exp = `echo $exp $exp0[$index] | awk '{printf "%20.15E", $1 + $2}'`  # add to exposure time for $mode 
	    endif
	end

	set totexp = ( $totexp $exp )      # concat the exposure time to the list

    end

#### display the modes and total exposure times ####

    echo "gnt_infrm (t_cats)"
    echo "gnt_infrm (t_cats) $inst unique modes ccd-efevtr"
    echo "gnt_infrm (t_cats)"

    set totalexposure = "0.0"  # the total exposure in seconds
    @ index = 0
    foreach mode ( $modes0 )
	@ index ++
	echo "gnt_infrm (t_cats) $mode $totexp[$index]"
	set totalexposure = `echo $totalexposure $totexp[$index] | awk '{printf "%20.15E", $1 + $2}'`
    end

    echo "gnt_infrm (t_cats) the total exposure time for ALL modes is: $totalexposure"

#### determine the best mode to use (mode with longest exposure time) ####

    set bestexp = "0.0"
    set bestmode = "none"

    @ index = 0
    foreach mode ( $modes0 )
	@ index ++
	set diff = `echo $totexp[$index] $bestexp | awk '{print ($1 > $2)}'`
	if ( $diff == 1 ) then     # found a new longest exposure time
	    set bestexp = $totexp[$index]
	    set bestmode = $mode
	endif
    end

    set bestmode = ( `echo $bestmode | sed s/"-"/" "/g` )

    echo "gnt_infrm (t_cats)"
    echo "gnt_infrm (t_cats) best $inst mode and exposure time:   $bestmode[1]   $bestmode[2]   $bestexp"

    set totalexposure = `echo $bestexp $totalexposure | awk '{printf "%20.15E", $1/$2}'`

    echo "gnt_infrm (t_cats) fraction of total good exposure time in best $inst mode: $totalexposure"

    echo $bestmode[1] >! $WORK/bestccd${inst}.temp
    echo $bestmode[2] >! $WORK/bestefevtr${inst}.temp
    echo $totalexposure >! $WORK/bestfrac${inst}.temp

    echo $bestmode[1] >! $WORK/ccdmode.temp          # this file is used by some scripts

########################################################################################
# Hide unscreened files without the best mode
########################################################################################

# the unscreened files that do not have the same CCD clocking mode and effective event threshold value as those
# for which the longest exposure time was found, will be renamed to hidden.filename where 
# filename is their orignal name.  This will prevent ascascreen from seeing them, so that
# these data will not be included in further analysis.

    cd $UNSCR

#### check that the bestmode file were created ####

    foreach file ( $WORK/bestccd${inst}.temp $WORK/bestefevtr${inst}.temp )
	if ( ! -e $file ) then
	    echo "gnt_fatal (t_cats) file $file was not created"
	    exit 1
	endif
    end
    

#### list the unscreened files ####

    if ( $inst == sis0 ) set files = ( `ls ad*s0*.unf` )     # sis0 .unf files
    if ( $inst == sis1 ) set files = ( `ls ad*s1*.unf` )     # sis1 .unf files

    if ( $#files == "0" ) then         # if no files
	echo "gnt_fatal (t_cats) no $inst files in $UNSCR - exiting"
	exit 1
    else
	echo "gnt_infrm (t_cats) number of $inst unscreened files: $#files"
    endif

#### check the CCD mode and effective event threshold in each unscreened file and hide if required ####

    foreach file ( $files )

# get CCD mode,event threshold,level discrimination on/off,lower level discriminator value  

	if ( $inst == "sis0" ) then
	    set det = "S0"      # SIS0 uses chip 1
	    set chip = "1"
	else
	    set det = "S1"   
	    set chip = "3"       # SIS1 uses chip 3
	endif

	fkeypar fitsfile=${file}+0 keyword=${det}CCDMOD
	set ccd = `pget fkeypar value`

	fkeypar fitsfile=${file}+0 keyword=${det}_EVTR${chip}    # event threshold
	set evtr = `pget fkeypar value`     

	fkeypar fitsfile=${file}+0 keyword=${det}_LVENA    # level discrimination off/on
	set disc = `pget fkeypar value`

	fkeypar fitsfile=${file}+0 keyword=${det}_LVDL${chip}    # LVDL
	set lvdl = `pget fkeypar value` 

# determine effective event threshold

	set diff = `echo $lvdl $evtr | awk '{print ($1>$2)}'`

	if ( ($diff == "1") && ($disc == "1") ) then    # lvdl is ON and is greater than evtr - use lvdl as effective event threshold
	    set efevtr = $lvdl
	else
	    set efevtr = $evtr   # the effective event threshold is the actual event threshold
	endif

	if ( ($ccd != $bestmode[1]) || ($efevtr != $bestmode[2]) ) then
	    echo "gnt_infrm (t_cats)"
	    echo "gnt_infrm (t_cats) renaming $file to hidden.$file"
	    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - $file was hidden by t_cats - (NOT BEST MODE) ****"
	    echo "gnt_infrm (t_cats)"
	    mv $file hidden.$file
	endif

    end

    echo "gnt_infrm (t_cats)"

########################################################################################
# End foreach inst loop 
########################################################################################

end

########################################################################################
# End if ( "$checklvdl" == "check" ) statement
########################################################################################

endif

########################################################################################
########################################################################################
########################################################################################

# this section of t_cats determines which combined ccd mode has the
# longest good time, as found from the evt files in the screened directory. The screened files
# that do not have the best ccd mode are renamed with the prefix
# hidden., so that they will not be found by ascascreen

#### THIS IS ONLY CARRIED OUT IF THE LVDL CHECKING WAS NOT REQUESTED ####

########################################################################################
# Determine if should not check the lvdl
########################################################################################

if ( "$checklvdl" != "check" ) then

    echo "gnt_infrm (t_cats) **** SEQUENCE $obsid - LVDL was not checked, so data may be mixed ****"

########################################################################################
# Start the foreach inst loop for sis0 and sis1
########################################################################################

# check sis0 and sis1 files separately

foreach inst ( sis0 sis1 )

########################################################################################
# Determine modes and exposure for each file in screened directory
########################################################################################

# the screened files are used here since these will only contain GOOD time data.

#### use the screened BRIGHT mode files ####

    if ( $inst == sis0 ) set files = ( `ls $SCR/ad*s000*02*.evt` )     # BRIGHT mode sis0 .evt files
    if ( $inst == sis1 ) set files = ( `ls $SCR/ad*s100*02*.evt` )     # BRIGHT mode sis1 .evt files

    if ( $#files == "0" ) then         # if no BRIGHT files
	echo "gnt_fatal (t_cats) no BRIGHT $inst files in $SCR - exiting"
	exit 1
    else
	echo "gnt_infrm (t_cats) number of BRIGHT $inst screened files: $#files"
    endif

#### get the modes from each file ####

    set ccd0 = ()            # CCD mode (1,2 or 4)
    set exp0 = ()            # exposure time
    set modes0 = ()          # CCD modes in data

    foreach file ( $files )

# get CCD mode,event threshold,level discrimination on/off,lower level discriminator value  

	if ( $inst == "sis0" ) then
	    set det = "S0"      # SIS0 uses chip 1
	    set chip = "1"
	else
	    set det = "S1"   
	    set chip = "3"       # SIS1 uses chip 3
	endif

	fkeypar fitsfile=${file}+0 keyword=${det}CCDMOD
	set ccd = `pget fkeypar value`
	set ccd0 = ( $ccd0 $ccd )

# exposure time

	fkeypar fitsfile=${file}+0 keyword=EXPOSURE
	set exp = `pget fkeypar value`
	set exp0 = ( $exp0 $exp )

# add this mode to the list of available modes, if it is not already in the list

	set havemode = "no"               # assume not in list
	foreach mode ( $modes0 )          # check the modes in the list to see if this mode combination (ccd-efevtr) is already listed
	    if ( "${ccd}" == $mode ) set havemode = "yes"     # this mode has already been listed
	end
	if ( $havemode == "no" ) set modes0 = ( $modes0 "${ccd}" )   # add this mode to the list

    end

#### check that the lists are the same length - must have one entry for each file ####

    if ( ($#ccd0 != $#files) || ($#exp0 != $#files) ) then
	echo "gnt_fatal (t_cats) error getting modes from unscreened files"
	exit 1
    endif

#### display the modes ####

    echo "gnt_infrm (t_cats)"
    echo "gnt_infrm (t_cats) $inst file, ccd mode, exposure"
    echo "gnt_infrm (t_cats)"

    @ index = 0
    foreach file ( $files )
	@ index ++
	echo "gnt_infrm (t_cats) $file $ccd0[$index] $exp0[$index]"
    end

########################################################################################
# Determine the total exposure times for each CCD mode
########################################################################################

#### check the list with the modes ####

    if ( $#modes0 == "0" ) then
	echo "gnt_fatal (t_cats) there are no $inst modes - exiting"
	exit 1
    endif

#### determine the cumulative exposure times ####

    set totexp = ()    # start list of total exposure for each mode

    foreach mode ( $modes0 )     # loop through each mode

	echo "gnt_infrm (t_cats) adding up times for mode $mode"
	@ exp = 0
	@ index = 0

	foreach file ( $files )
	    @ index ++
	    if ( "$ccd0[$index]" == $mode ) then   # does this file have mode $mode
		set exp = `echo $exp $exp0[$index] | awk '{printf "%20.15E", $1 + $2}'`  # add to exposure time for $mode 
	    endif
	end

	set totexp = ( $totexp $exp )      # concat the exposure time to the list

    end

#### display the modes and total exposure times ####

    echo "gnt_infrm (t_cats)"
    echo "gnt_infrm (t_cats) $inst unique modes ccd"
    echo "gnt_infrm (t_cats)"

    set totalexposure = "0.0"  # the total exposure in seconds
    @ index = 0
    foreach mode ( $modes0 )
	@ index ++
	echo "gnt_infrm (t_cats) $mode $totexp[$index]"
	set totalexposure = `echo $totalexposure $totexp[$index] | awk '{printf "%20.15E", $1 + $2}'`
    end

    echo "gnt_infrm (t_cats) the total exposure time for ALL modes is: $totalexposure"

#### determine the best mode to use (mode with longest exposure time) ####

    set bestexp = "0.0"
    set bestmode = "none"

    @ index = 0
    foreach mode ( $modes0 )
	@ index ++
	set diff = `echo $totexp[$index] $bestexp | awk '{print ($1 > $2)}'`
	if ( $diff == 1 ) then     # found a new longest exposure time
	    set bestexp = $totexp[$index]
	    set bestmode = $mode
	endif
    end

    echo "gnt_infrm (t_cats)"
    echo "gnt_infrm (t_cats) best $inst mode and exposure time:   $bestmode $bestexp"

    set totalexposure = `echo $bestexp $totalexposure | awk '{printf "%20.15E", $1/$2}'`

    echo "gnt_infrm (t_cats) fraction of total good exposure time in best $inst mode: $totalexposure"

    echo $bestmode >! $WORK/bestccd${inst}.temp
    echo $totalexposure >! $WORK/bestfrac${inst}.temp

    echo $bestmode >! $WORK/ccdmode.temp          # this file is used by some scripts

########################################################################################
# Hide unscreened files without the best mode
########################################################################################

# the unscreened files that do not have the same CCD clocking mode as those
# for which the longest exposure time was found, will be renamed to hidden.filename where 
# filename is their orignal name.  This will prevent ascascreen from seeing them, so that
# these data will not be included in further analysis.

    cd $UNSCR

#### check that the bestmode file were created ####

    foreach file ( $WORK/bestccd${inst}.temp )
	if ( ! -e $file ) then
	    echo "gnt_fatal (t_cats) file $file was not created"
	    exit 1
	endif
    end

#### list the unscreened files ####

    if ( $inst == sis0 ) set files = ( `ls ad*s0*.unf` )     # sis0 .unf files
    if ( $inst == sis1 ) set files = ( `ls ad*s1*.unf` )     # sis1 .unf files

    if ( $#files == "0" ) then         # if no files
	echo "gnt_fatal (t_cats) no $inst files in $UNSCR - exiting"
	exit 1
    else
	echo "gnt_infrm (t_cats) number of $inst unscreened files: $#files"
    endif

#### check the CCD mode in each unscreened file and hide if required ####

    foreach file ( $files )

# get CCD mode  

	if ( $inst == "sis0" ) then
	    set det = "S0"      # SIS0 uses chip 1
	    set chip = "1"
	else
	    set det = "S1"   
	    set chip = "3"       # SIS1 uses chip 3
	endif

	fkeypar fitsfile=${file}+0 keyword=${det}CCDMOD
	set ccd = `pget fkeypar value`

	if ( ($ccd != $bestmode) ) then
	    echo "gnt_infrm (t_cats) renaming $file to hidden.$file"
	    echo "gnt_warng (t_cats) **** SEQUENCE $obsid - $file was hidden by t_cats (NOT BEST MODE) ****"
	    mv $file hidden.$file
	endif

    end

    echo "gnt_infrm (t_cats)"

########################################################################################
# End foreach inst loop 
########################################################################################

end

########################################################################################
# End if ( "$checklvdl" != "check" ) statement
########################################################################################

endif

########################################################################################
########################################################################################
########################################################################################

# This section of t_cats does some final checking.

########################################################################################
# Check sis0 and sis1 have same best CCD mode
########################################################################################

cd $WORK

set bestccdmode0 = "sis0 - none"
set bestccdmode1 = "sis1 - none"

if ( -e $WORK/bestccdsis0.temp ) set bestccdmode0 = `cat $WORK/bestccdsis0.temp`
if ( -e $WORK/bestccdsis1.temp ) set bestccdmode1 = `cat $WORK/bestccdsis1.temp`

if ( "$bestccdmode0" != "$bestccdmode1" ) then
    echo "gnt_fatal (t_cats) best CCD mode SIS0 : $bestccdmode0"
    echo "gnt_fatal (t_cats) best CCD mode SIS1 : $bestccdmode1"
    echo "gnt_fatal (t_cats) SIS0 and SIS1 CCD modes are different"
    exit 1
else
    echo "gnt_infrm (t_cats) SIS0 and SIS1 have same CCD modes"
endif

########################################################################################
# Confirm that there are non-hidden sis0 and sis1 files in unscreened directory
########################################################################################

cd $UNSCR

set sis0files = ( `ls ad*s0*.unf` )
set sis1files = ( `ls ad*s1*.unf` )

echo "gnt_infrm (t_cats) number of unhidden unscreened SIS0 files: $#sis0files"
echo "gnt_infrm (t_cats) number of unhidden unscreened SIS1 files: $#sis1files"

if ( ($#sis0files == "0") || ($#sis1files == "0") ) then
    echo "gnt_fatal (t_cats) missing some SIS unscreened files - exiting"
    exit 1
endif

########################################################################################
# Confirm that the nominal chips are in the readout list
########################################################################################

# SIS0 data must have chip 1 in the CCD readout list keyword=S0CCDLST
# SIS1 data must have chip 3 in the CCD readout list keyword=S1CCDLST
# also checks that the CCDLST does not change

cd $UNSCR

set sis0files = ( `ls ad*s0*.unf` )
set sis1files = ( `ls ad*s1*.unf` )

#### get the first CCD list - all other must equal this ####

fkeypar fitsfile=$sis0files[1]+0 keyword=S0CCDLST
set ccdlistsis0 = `pget fkeypar value | sed s/"'"//g | sed s/" "//g`

fkeypar fitsfile=$sis1files[1]+0 keyword=S1CCDLST
set ccdlistsis1 = `pget fkeypar value | sed s/"'"//g | sed s/" "//g`

#### check all CCD lists ####

set ccdstatus = "GOOD"

foreach file ( $sis0files )

    fkeypar fitsfile=${file}+0 keyword=S0CCDLST
    set ccdlist = `pget fkeypar value | sed s/"'"//g | sed s/" "//g`

    fkeypar fitsfile=${file}+0 keyword=EXPOSURE
    set exp = `pget fkeypar value`

    echo "gnt_infrm (t_cats) $file S0CCDLST and EXPOSURE: $ccdlist $exp"

    if ( $ccdlist != $ccdlistsis0 ) then
	echo "gnt_fatal (t_cats) SIS0 CCDLST changes"
	set ccdstatus = "ERROR"
    endif

    set ccdlist = `echo $ccdlist | grep "1"`

    if ( $#ccdlist == "0" ) then
	echo "gnt_fatal (t_cats) SIS0 chip 1 not in CCDLST"
	set ccdstatus = "ERROR"
    endif

end

foreach file ( $sis1files )

    fkeypar fitsfile=${file}+0 keyword=S1CCDLST
    set ccdlist = `pget fkeypar value | sed s/"'"//g | sed s/" "//g`

    fkeypar fitsfile=${file}+0 keyword=EXPOSURE
    set exp = `pget fkeypar value`

    echo "gnt_infrm (t_cats) $file S1CCDLST and EXPOSURE: $ccdlist $exp"

    if ( $ccdlist != $ccdlistsis1 ) then
	echo "gnt_fatal (t_cats) SIS1 CCDLST changes"
	set ccdstatus = "ERROR"
    endif

    set ccdlist = `echo $ccdlist | grep "3"`

    if ( $#ccdlist == "0" ) then
	echo "gnt_fatal (t_cats) SIS1 chip 3 not in CCDLST"
	set ccdstatus = "ERROR"
    endif

end

if ( $ccdstatus == "ERROR" ) exit 1

########################################################################################
# Check the required files were created
########################################################################################

foreach file ( datamode.temp brangle.temp cat_sis0.cat ccdmode.temp )
    if ( ! -e $WORK/$file ) then
	echo "gnt_fatal (t_cats) file $file was not created"
	exit 1
    endif
end

foreach inst ( sis0 sis1 )

    if ( "$checklvdl" == "check" ) then 
	set files = ( bestccd${inst}.temp bestefevtr${inst}.temp bestfrac${inst}.temp )
    else
	set files = ( bestccd${inst}.temp bestfrac${inst}.temp )
    endif

    foreach file ( $files )
	if ( ! -e $WORK/$file ) then
	    echo "gnt_fatal (t_cats) file $file was not created"
	    exit 1
	endif
    end

end

########################################################################################
# Remove all unnecessary files
########################################################################################

echo "gnt_infrm (t_cats) Removing unnecessary files"

if ( -e $AUX/awkfile.tmp ) rm -f $AUX/awkfile.tmp

rm -f $AUX/ascii*
rm -f $AUX/comm*.pco
rm -f $AUX/*del
rm -f $WORK/*.tmp
rm -f $WORK/xsel_*.cat

########################################################################################
# Check how many hidden files exist
########################################################################################

# This ensures that a record is available of exactly which files are hidden.

echo "gnt_infrm (t_cats) Checking for hidden files in $AUX"

cd $AUX
set files = `ls *hidden*`
if ( $#files != 0 ) then
    foreach file ( $files )
	echo "gnt_infrm (t_cats) **** SEQUENCE $obsid - found hidden file in $AUX : $file ****"
	echo "gnt_warng (t_cats) **** SEQUENCE $obsid - found hidden file in $AUX : $file ****"
    end
endif

echo "gnt_infrm (t_cats) Checking for hidden files in $UNSCR"
cd $UNSCR
set files = `ls *hidden*`
if ( $#files != 0 ) then
    foreach file ( $files )
	echo "gnt_infrm (t_cats) **** SEQUENCE $obsid - found hidden file in $UNSCR : $file ****"
	echo "gnt_warng (t_cats) **** SEQUENCE $obsid - found hidden file in $UNSCR : $file ****"
    end
endif

echo "gnt_infrm (t_cats) Checking for hidden files in $SCR"
cd $SCR
set files = `ls *hidden*`
if ( $#files != 0 ) then
    foreach file ( $files )
	echo "gnt_infrm (t_cats) **** SEQUENCE $obsid - found hidden file in $SCR : $file ****"
	echo "gnt_warng (t_cats) **** SEQUENCE $obsid - found hidden file in $SCR : $file ****"
    end
endif

########################################################################################
# All done 
########################################################################################

echo ""
echo "gnt_infrm (t_cats) ++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo "gnt_infrm (t_cats)  t_cats 4.0  FINISHED "`date`
echo "gnt_infrm (t_cats) ++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo ""