faint2bright (version 1.3)
You can also look at:
#! /usr/bin/ksh
###########################################################################
#
# SYNTAX: faint2bright
#
# BRIEFLY: Convert FAINT mode files to BRIGHT and BRIGHT2 modes
#
# DESCRIPTION: This routine converts FAINT mode SIS unfiltered event files
# DESCRIPTION: to BRIGHT and BRIGHT2 modes.
# DESCRIPTION: <P>
# DESCRIPTION: An event typically registers on more than one pixel of
# DESCRIPTION: an SIS CCD. The pattern of excited pixels can suggest
# DESCRIPTION: whether the event was an actual photon or not.
# DESCRIPTION: <P>
# DESCRIPTION: In FAINT mode, the nine pixels surrounding an event are
# DESCRIPTION: telemetered to the ground. In BRIGHT mode only a single PHA
# DESCRIPTION: value and a grade describing the pattern of excited pixels
# DESCRIPTION: are sent.
# DESCRIPTION: BRIGHT2 mode files are created only on the ground.
# DESCRIPTION: BRIGHT2 mode is like BRIGHT mode except that the extra
# DESCRIPTION: information from FAINT is used to correct for things
# DESCRIPTION: like dark frame error and echo effect.
# DESCRIPTION: <P>
# DESCRIPTION: The BRIGHT mode conversion is done with zerodef set to 1
# DESCRIPTION: in order to minimize the effect of RDD.
# DESCRIPTION: (see fhelp faintdfe with ftools ver 3.5 or higher )
# DESCRIPTION: <P>
# DESCRIPTION: As a byproduct, the dark frame error information is extracted
# DESCRIPTION: and stored in the dfe files which are later saved as trend
# DESCRIPTION: products. The dark frame error information is also later
# DESCRIPTION: stored in the filter (mkf) files.
#
# VERSION: 1.3
#
# HISTORY: 0.0 -> 1.0 1/19/96
# HISTORY: Fixed a bug in indexing that was pointed out by Koji Mukai.
# HISTORY: The file index for new event files was set to a value higher than
# HISTORY: the index of any existing files. However, the search for the
# HISTORY: highest existing index was not inside a loop over instrument, so
# HISTORY: the only the highest index for SIS1 files was found. Now the index
# HISTORY: for new files is the same as the index for the original file.
# HISTORY: This is possible since the combine script assigns a unique
# HISTORY: index for each unfiltered event file, regardless of mode.
# HISTORY: Originally this was not true for sequences with multiple
# HISTORY: telemetry files, but that was also a bug.
# HISTORY:
# HISTORY: 1.1 -> 1.2 12/29/97
# HISTORY: Added code to extract corner pixel histograms.
# HISTORY:
# HISTORY: 1.2 -> 1.3 4/29/98
# HISTORY: Moved corner pixel extraction to the framedata subroutine to
# HISTORY: allow time filtering and did some minor re-organization of the code.
#
############################################################################
#DEBUG=1
$UTIL/milestone "Converting FAINT mode files to BRIGHT and BRIGHT2 modes"
min_events=$($UTIL/read_parfile $PARFILE sis_events_min)
################################
# Loop through FAINT mode files
################################
list=$($UTIL/generate_filename unfiltered s? any 01 any)
list=$(ls $list 2>/dev/null)
if [ -z "$list" ]; then
$UTIL/log_entry "No FAINT mode data to convert to bright mode"
exit 0
fi
#####################################################################
# Determine split thresholds.
# Nowadays these are always 40, but some early PV phase observations
# may have other values.
# Check an on-board created unfiltered event file to see what split
# threshold was used there
#####################################################################
for inst in s0 s1 ; do
case "$inst" in
s0) caps="S0";;
s1) caps="S1";;
*) $UTIL/exception $0 1 "Unknown instrument $inst ";;
esac
default=$($UTIL/read_parfile $PARFILE ${inst}_split)
list=$($UTIL/generate_filename unfiltered $inst any 02 any)
unfiltered=$(ls $list 2>/dev/null | head -1)
if [ -n "$unfiltered" ]; then
ccd=$($UTIL/read_fits_keyword $unfiltered[0] ${caps}CCDLST \
| awk '{print $1}')
###################
# Debugging output
###################
if [ -n "$DEBUG" ]; then
echo ${0##*/} unfiltered=$unfiltered
echo ${0##*/} caps=$caps
echo ${0##*/} ccd=$ccd
fi
split=$($UTIL/read_fits_keyword $unfiltered[0] \
${caps}_SPTR${ccd})
if [ $split -ne $default ]; then
$UTIL/log_entry \
"Using split threshold $split for $inst to match $unfiltered CCD $ccd"
fi
else
split=$default
fi
case $inst in
s0) split0=$split;;
s1) split1=$split;;
*) $UTIL/exception $0 1 "Unknown instrument $inst";;
esac
done
$UTIL/log_entry "Split threshold for SIS0=$split0, and SIS1=$split1"
###################
# Debugging output
###################
if [ -n "$DEBUG" ]; then
echo ${0##*/}: split0=$split0
echo ${0##*/}: split1=$split1
fi
#########################
# Fetch calibration file
#########################
caldir=$($UTIL/read_parfile $PARFILE caldir )
tblfile=$($UTIL/read_parfile $PARFILE faintdfe)
$UTIL/fetch_file $caldir $tblfile
#######################################
# Remove any old accumulated dfe files
#######################################
for inst in s0 s1; do
cumulated=$($UTIL/generate_filename dfe $inst)
rm -f $cumulated
done
################
# Convert modes
################
list=$($UTIL/generate_filename unfiltered any any 01 any)
list=$(ls $list 2>/dev/null)
for unfiltered in ${list} ; do
inst=$( $UTIL/parse_filename inst $unfiltered)
index=$( $UTIL/parse_filename index $unfiltered)
bitrate=$($UTIL/parse_filename bitrate $unfiltered)
###################
# Debugging output
###################
if [ -n "$DEBUG" ]; then
echo ${0##*/}: unfiltered=$unfiltered
echo ${0##*/}: inst=$inst
echo ${0##*/}: index=$index
echo ${0##*/}: bitrate=$bitrate
fi
case $inst in
s0) split=$split0;;
s1) split=$split1;;
*) $UTIL/exception $0 1 "Unknown instrument $inst";;
esac
cumulated=$($UTIL/generate_filename dfe $inst)
##########################
# Do BRIGHT2, then BRIGHT
##########################
for mode in 12 02; do
bright=$( $UTIL/generate_filename unfiltered \
$inst $index $mode $bitrate)
case "$mode" in
12)
##########
# BRIGHT2
##########
zerodef=1
bright_flag=no;;
02)
#########
# BRIGHT
#########
zerodef=2
bright_flag=yes;;
*)
###############
# Unknown mode
###############
$UTIL/exception $0 1 "Unknown mode $mode";;
esac
#############################
# Calculate dark frame error
#############################
$UTIL/log_entry \
"Calculating DFE values for $unfiltered with zerodef=$zerodef"
$UTIL/setup_parfile $FTOOL/faintdfe.par infile=$unfiltered \
tblfile=$tblfile \
split=$split \
binsec=64 \
zerodef=$zerodef \
outfile=dfe.tmp
rm -f dfe.tmp
$FTOOL/faintdfe >stdout.log 2>stderr.log
code=$?
grep -v 'zero-levels are set to ones expected in bright mode' \
stderr.log >filtered.tmp
if [ ! -s filtered.tmp -a -s stderr.log ]; then
$UTIL/log_entry "Stderr output from faintdfe:"
$UTIL/file_to_log stderr.log
fi
rm -f stderr.log
$UTIL/ftool_test faintdfe $code $0 2 stdout.log filtered.tmp
######################################################
# Store the DFE values used to convert to BRIGHT mode
######################################################
if [ "$mode" = "02" ]; then
cat dfe.tmp >>$cumulated
fi
#################################
# Convert to BRIGHT/BRIGHT2 mode
#################################
$UTIL/log_entry "Converting $unfiltered to $bright"
rm -f $bright
$UTIL/setup_parfile $FTOOL/faint.par infile=$unfiltered \
outfile=$bright \
split=$split \
dfefile=dfe.tmp \
maxgrade=6 \
bright=$bright_flag
$FTOOL/faint >stdout.log 2>stderr.log
$UTIL/ftool_test faint $? $0 2 stdout.log stderr.log
rm -f dfe.tmp
###########################################################
# Remove the file if it has < the minimum number of events
###########################################################
nevents=$($UTIL/read_fits_keyword ${bright}[0] NEVENTS)
if [ $nevents -lt $min_events ]; then
$UTIL/log_entry \
"Removing $bright since it only has $nevents events"
rm -f $bright
fi
done # loop over BRIGHT and BRIGHT2 modes
done # loop over FAINT mode event files
#################################
# Sort the accumulated dfe files
#################################
for inst in s0 s1; do
cumulated=$($UTIL/generate_filename dfe $inst)
if [ -s $cumulated ]; then
sort -n $cumulated |uniq >dfe.tmp
mv dfe.tmp $cumulated
fi
done
exit 0