#!/bin/tcsh -f

# t_scrn 4.0

# Runs ascascreen to produce screened events files 
# Uses code from do_scrn script by K.Nandra/D.Turcan

# Adaptation by Paul O'Neill

# Date in final form: 17 December 2004

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

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

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

#### get parameters ####

set obsid = $1      # sequence number for this observation
set datamode = $2   # dominant data mode for this sequence 
set brangle = $3    # mimimum acceptable bright Earth angle
set SEQ = $4        # root directory of this observing sequence

#### 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

########################################################################################
# Show ccdmode, datamode, brangle
########################################################################################

if ( -e $WORK/ccdmode.temp ) then
    set ccdmode = `cat $WORK/ccdmode.temp`
else
    echo "gnt_fatal (t_scrn) CCD mode not found"
    exit 1
endif

echo "gnt_infrm (t_scrn)"
echo "gnt_infrm (t_scrn) the CCD mode is $ccdmode"
echo "gnt_infrm (t_scrn)"
echo "gnt_infrm (t_scrn) The dominant datamode is  : $datamode"
echo "gnt_infrm (t_scrn)"
echo "gnt_infrm (t_scrn) The bright Earth angle is : $brangle"
echo "gnt_infrm (t_scrn)"

########################################################################################
# Check that files in 1-CCD mode use primary chips
########################################################################################

cd $UNSCR

if ( $ccdmode == 1 ) then
    set st = (1111 3333)

    foreach i ( 0 1 )
	set e = `echo $i+1 | bc -l`
	set files = `ls ad*s${i}*`
      
	foreach file ( $files )
	    set string = `fkeyprint $file S${i}CCDLST | grep readout | sed s/" "/""/g | sed s/"'"/" "/g`
	    if ( "$string[2]" != "$st[$e]" ) then
		echo "gnt_warng (t_scrn) primary chip not used - renaming $file to hidden.$file"
		mv $file hidden.$file
	    endif
	end

    end

endif

########################################################################################
# Write out command files
########################################################################################

cd $WORK

if ( $ccdmode == 4 ) then
    ascascreen -a -d -q -e unf -f "S0CCDMOD==$ccdmode" $UNSCR SIS0 $datamode \
      \!${obsid}_sis0 "H,M"
    ascascreen -a -d -q -e unf -f "S1CCDMOD==$ccdmode" $UNSCR SIS1 $datamode \
      \!${obsid}_sis1 "H,M"
else
    ascascreen -a -d -q -e unf -f "S0CCDMOD==$ccdmode" $UNSCR SIS0 $datamode \
      \!${obsid}_sis0 "H,M,L"
    ascascreen -a -d -q -e unf -f "S1CCDMOD==$ccdmode" $UNSCR SIS1 $datamode \
      \!${obsid}_sis1 "H,M,L"
endif

ascascreen -a -d -q -e unf $UNSCR GIS2 PH \!${obsid}_gis2 "H,M,L"
ascascreen -a -d -q -e unf $UNSCR GIS3 PH \!${obsid}_gis3 "H,M,L"

foreach inst ( sis0 sis1 gis2 gis3 )
    if ( "`cat $WORK/${obsid}_${inst}_mkf.sel`" == "" ) then
	echo "gnt_fatal (t_scrn) the .sel file for $inst contains no data"
	echo "gnt_fatal (t_scrn) no screening criteria"
	exit 1
    endif
end

########################################################################################
# Determine start and stop time of attitude file
########################################################################################

# can only have GTIs in which attitude infomation is available. The attitude file is
# checked for the start and stop times of attitude info. 

# The results of this section of the script are used to incorporate a TIME screen for
# certain sequences that require that screening (see below)

#### get name of attitude file ####

set attfiles = `ls $AUX/fa*`

if ( $#attfiles == "0" ) then
    echo "gnt_fatal (t_scrn) attitude file not found"
    exit 1
endif

#### get bin width in mkf file ###

set mkffiles = `ls $AUX/ft*.mkf`

if ( $#mkffiles == "0" ) then
    echo "gnt_fatal (t_scrn) mkf file not found"
    exit 1
endif

ftabpar fitsfile=$mkffiles[1]+1 column=BN_WIDTH row=1
set mkfbin = `pget ftabpar value`

set mkfbin = `echo $mkfbin | awk '{print $1+0}'`

echo "gnt_infrm (t_scrn) the mkf bin width is: $mkfbin"

#### get first TIME in attitude file ####

echo "gnt_infrm (t_scrn) using attitude file: $attfiles[1]"

ftabpar fitsfile=$attfiles[1]+1 column=TIME row=1
set attstart = `pget ftabpar value`

echo "gnt_infrm (t_scrn) first time in attitude file: $attstart"

set timelow = `echo $attstart | sed s/"E+"/"* 10^"/g | bc`

set timelow = `echo "$timelow + $mkfbin" | bc`  # to avoid interpolation problems
                                                # The GTI will be forced to start exactly 
                                                # between two mkf bins if timelow is
                                                # ANYWHERE between those bins. 

echo "gnt_infrm (t_scrn) lowest acceptable time in mkf file: $timelow"

#### get last TIME in attitude file ####

fkeypar fitsfile=$attfiles[1]+1 keyword=NAXIS2  # number of rows in attitude file
set numattrow = `pget fkeypar value`

ftabpar fitsfile=$attfiles[1]+1 column=TIME row=$numattrow
set attstop = `pget ftabpar value`

echo "gnt_infrm (t_scrn) last time in attitude file: $attstop"

set timehigh = `echo $attstop | sed s/"E+"/"* 10^"/g | bc`

set timehigh = `echo "$timehigh - $mkfbin" | bc`

echo "gnt_infrm (t_scrn) highest acceptable time in mkf file: $timehigh"

########################################################################################
# Fix up all the selection files and command files
########################################################################################

# add some additional constraints for screening

#### SIS0 and SIS1 ####
 
echo "gnt_infrm (t_scrn) fixing selection files"

cat $WORK/${obsid}_sis0_mkf.sel | sed s/275/500/g >! $WORK/sis0_mkf.tmp
cat $WORK/sis0_mkf.tmp | sed s/30/$brangle/g >! $WORK/${obsid}_sis0_mkf.sel
echo '&& S0_SATF1 == 0' >> $WORK/${obsid}_sis0_mkf.sel    # accept non-saturated telemetry only

cat $WORK/${obsid}_sis1_mkf.sel | sed s/275/500/g >! $WORK/sis1_mkf.tmp
mv $WORK/sis1_mkf.tmp $WORK/${obsid}_sis1_mkf.sel
echo '&& S1_SATF3 == 0' >> $WORK/${obsid}_sis1_mkf.sel

########################################################################################
# Screen on TIME for the specific sequences that require it
########################################################################################

# for some sequences, the attitude file has been found not to span the time in the events file.
# for these sequences, additional screening is done so that all the event file GTIs have
# attitude information.

if ( $obsid == 75020000 ) then
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_sis0_mkf.sel # must have attitude data
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_sis1_mkf.sel
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_gis2_mkf.sel
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_gis3_mkf.sel
    echo "gnt_infrm (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - sis0 sis1 gis2 gis3 ****"
    echo "gnt_warng (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - sis0 sis1 gis2 gis3 ****"
endif

if ( $obsid == 77006010 ) then
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_gis2_mkf.sel
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_gis3_mkf.sel
    echo "gnt_infrm (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - gis2 gis3 ****"
    echo "gnt_warng (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - gis2 gis3 ****"
endif

if ( $obsid == 77006060 ) then
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_gis2_mkf.sel
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_gis3_mkf.sel
    echo "gnt_infrm (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - gis2 gis3 ****"
    echo "gnt_warng (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - gis2 gis3 ****"
endif

if ( $obsid == 77049000 ) then
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_sis0_mkf.sel # must have attitude data
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_gis2_mkf.sel
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_gis3_mkf.sel
    echo "gnt_infrm (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - sis0 gis2 gis3 ****"
    echo "gnt_warng (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - sis0 gis2 gis3 ****"
endif

if ( $obsid == 78003001 ) then
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_sis0_mkf.sel # must have attitude data
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_sis1_mkf.sel
    echo "gnt_infrm (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - sis0 sis1 ****"
    echo "gnt_warng (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - sis0 sis1 ****"
endif

if ( $obsid == 78003003 ) then
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_sis0_mkf.sel # must have attitude data
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_sis1_mkf.sel
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_gis2_mkf.sel
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_gis3_mkf.sel
    echo "gnt_infrm (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - sis0 sis1 gis2 gis3 ****"
    echo "gnt_warng (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - sis0 sis1 gis2 gis3 ****"
endif

if ( $obsid == 78009002 ) then
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_sis0_mkf.sel # must have attitude data
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_sis1_mkf.sel
    echo "gnt_infrm (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - sis0 sis1 ****"
    echo "gnt_warng (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - sis0 sis1 ****"
endif

if ( $obsid == 78011004 ) then
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_sis0_mkf.sel # must have attitude data
    echo "&& (TIME > $timelow)" >> $WORK/${obsid}_sis1_mkf.sel
    echo "gnt_infrm (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - sis0 sis1 ****"
    echo "gnt_warng (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - sis0 sis1 ****"
endif

if ( $obsid == 78012002 ) then
    echo "&& (TIME < $timehigh)" >> $WORK/${obsid}_gis2_mkf.sel
    echo "&& (TIME < $timehigh)" >> $WORK/${obsid}_gis3_mkf.sel
    echo "gnt_infrm (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - gis2 gis3 ****"
    echo "gnt_warng (t_scrn) **** SEQUENCE $obsid - screening on attitude file TIME - gis2 gis3 ****"
endif

########################################################################################
# Fix up command files
########################################################################################

grep -v "plot image" $WORK/${obsid}_sis0.xco >! $WORK/sis0.tmp
grep -v "plot image" $WORK/${obsid}_sis1.xco >! $WORK/sis1.tmp
grep -v "plot image" $WORK/${obsid}_gis2.xco >! $WORK/gis2.tmp
grep -v "plot image" $WORK/${obsid}_gis3.xco >! $WORK/gis3.tmp

echo "exit save=no" >! $WORK/quit.tmp

cat $WORK/sis0.tmp $WORK/quit.tmp >! $WORK/${obsid}_sis0.xco
cat $WORK/sis1.tmp $WORK/quit.tmp >! $WORK/${obsid}_sis1.xco
cat $WORK/gis2.tmp $WORK/quit.tmp >! $WORK/${obsid}_gis2.xco
cat $WORK/gis3.tmp $WORK/quit.tmp >! $WORK/${obsid}_gis3.xco

########################################################################################
# Check if good time was found
########################################################################################

cd $AUX
set mkf = `ls -1 *.mkf | sed s/".mkf"//g`

maketime infile="$mkf[1].mkf" outfile="$mkf[1].gti" expr=@$WORK/${obsid}_sis0_mkf.sel \
   name=NAME value=VALUE time=TIME compact=no clobber=yes

fdump infile=$mkf[1].gti+1 outfile=good.time columns=START rows=- clobber=yes \
   prhead=no showcol=no showunit=no showrow=yes

if ( "`cat good.time`" == "" ) then
    echo "gnt_fatal (t_scrn) no good time detected"
    exit 1
endif

########################################################################################
# Run xselect
########################################################################################

echo " "
echo "gnt_infrm (t_scrn) Running extractions"
echo " "

cd $WORK

foreach inst ( sis0 sis1 gis2 gis3 )
    echo "gnt_infrm (t_scrn) Extracting instrument: $inst"
    xselect @$WORK/${obsid}_${inst}.xco
end

########################################################################################
# Check for G3BITFIX problem
########################################################################################

if ( -e $WORK/bitfix.temp ) rm $WORK/bitfix.temp

echo " "
echo "gnt_infrm (t_scrn) Checking for G3BITFIX problem"
echo " "
   
if ( -e $WORK/${obsid}_gis3.evt ) then

    fkeyprint $WORK/${obsid}_gis3.evt+1 G3BITFIX outfile=$WORK/bitfix.temp
    set bitfix = `grep FIXED $WORK/bitfix.temp`

    if ( $#bitfix == "0" ) then
	set bitfix = "n"
    else   
	set bitfix = "y"
	echo "gnt_infrm (t_scrn) **** SEQUENCE $obsid - GIS3 bitfix problem detected ****"
	echo "gnt_warng (t_scrn) **** SEQUENCE $obsid - GIS3 bitfix problem detected ****"
    endif

    rm $WORK.bitfix.temp
    echo $bitfix >! $WORK/bitfix.temp

else

    echo "gnt_fatal (t_scrn) G3BITFIX corr. cannot be determined - $WORK/${obsid}_gis3.evt does not exist"
    exit 1

endif

########################################################################################
# Check if all files were created
########################################################################################

if ( ! -e $WORK/ccdmode.temp ) echo "gnt_warng (t_scrn) file ccdmode.temp was not created"

foreach inst ( sis0 sis1 gis2 gis3 )

    set file = ${obsid}_${inst}.evt
    if ( ! -e $WORK/$file ) then
	echo "gnt_fatal (t_scrn) file $file was not created"
	exit 1
    endif

    set file = ${obsid}_${inst}.img
    if ( ! -e $WORK/$file ) then
	echo "gnt_fatal (t_scrn) file $file was not created"
	exit 1
    else
	mv $WORK/${obsid}_${inst}.img $WORK/${obsid}_${inst}_det.img
    endif

end

########################################################################################
# gzip the screened and unscreened files
########################################################################################

echo "gnt_infrm (t_scrn) looking for files to gzip"

#### screened directory ####

cd $SCR
set unzippedfiles = `ls *.evt`
echo "gnt_infrm (t_scrn) number of unzipped files in ${SCR}: $#unzippedfiles" 
echo "gnt_infrm (t_scrn) gzipping files in ${SCR}"
if ( $#unzippedfiles != 0 ) then
    foreach unzippedfile ( $unzippedfiles )
	gzip $unzippedfile
    end
endif

#### unscreened directory ####

cd $UNSCR
set unzippedfiles = `ls *.unf`
echo "gnt_infrm (t_scrn) number of unzipped files in ${UNSCR}: $#unzippedfiles" 
echo "gnt_infrm (t_scrn) gzipping files in ${UNSCR}"
if ( $#unzippedfiles != 0 ) then
    foreach unzippedfile ( $unzippedfiles )
	gzip $unzippedfile
    end
endif

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

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