framedata (version 1.2)

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

###########################################################################
#
# SYNTAX: framedata
#
# BRIEFLY: Extract FRAME mode data from the telemetry.
#
# DESCRIPTION: This routine extracts FRAME mode data
# DESCRIPTION: from the telemetry using
# DESCRIPTION: a program originally written at ISAS. The frames are processed
# DESCRIPTION: to subtract the bias level and to mimic the on-board dark
# DESCRIPTION: level subtraction. Several pieces of information are
# DESCRIPTION: then extracted from the filter file and added to the frame file
# DESCRIPTION: headers.
# DESCRIPTION: <p>
# DESCRIPTION: In FRAME mode, ASCA dumps the pha values of all of the pixels
# DESCRIPTION: in each operating chip and are primarily useful for studying
# DESCRIPTION: the dark current in the CCDs.
# DESCRIPTION: The FRAME mode data are saved as trend
# DESCRIPTION: products and are used to generate RDD maps and for calibration.
#
#
# VERSION: 1.2
#
# HISTORY: 0.0 -> 1.0 1/6/98
# HISTORY: This code was originally in the readfrfs subroutine, but
# HISTORY: was moved here so that information could be extracted from the
# HISTORY: filter file and put into the FITS headers.
# HISTORY:
# HISTORY: 1.0 -> 1.1 2/9/98
# HISTORY: Now skips trying to add info to headers when the frame file
# HISTORY: could not be levelled, and therefore does not exist.
# HISTORY:
# HISTORY: 1.1 -> 1.2 4/24/98
# HISTORY: Now handles both gzipped and compressed telemetry files.
# HISTORY: Also, added T_DY_NT to the filter parameters added to the frame
# HISTORY: headers.
# HISTORY: Changed the way that the filter parameters are extracted.
# HISTORY: Now the last row between TSTOP and 32 seconds before TSTART
# HISTORY: is used. Before, it was the first row. This will hopefully
# HISTORY: do a better job with things like day-night transitions.
# HISTORY: Imported code to extract corner pixel histograms from the
# HISTORY: faint2bright subroutine. Also modified that code to include
# HISTORY: some time filtering
#
###########################################################################
#DEBUG=1

$UTIL/milestone "Extracting FRAME mode data"

##############################################
# Read number of telemetry files from job par
##############################################
nfiles=$($UTIL/read_parfile $JOBPAR frfcnt)
if [ -z "$nfiles" ]; then
    nfiles=0
fi

if [ $nfiles -eq 0 ]; then
    $UTIL/exception $0 3 "Can't get non-zero number of frf files from $JOBPAR"
fi

###############################################################################
# Extract frame mode data from the telemetry
# These data are collected as trend products and later used for RDD correction
# The software to do this was originally written by Tadayasu Dotani
# dotani@astro.isas.ac.jp
###############################################################################
rddsoft=$($UTIL/read_parfile $PARFILE rddsoft)

#######################################################
# Gather some information needed for the FITS headers
#######################################################
seqnum=$($UTIL/read_parfile $JOBPAR sequence)
seqpnum=$($UTIL/read_parfile $JOBPAR seqprocnum)
object=$($UTIL/read_parfile $JOBPAR object)

##########################################################
# List of columns to be read from filter file and placed
# in frame headers
##########################################################
rm -f mkf_cols.tmp
echo "S0_TEMP\nS1_TEMP\nFOV\nBR_EARTH\nELV\nSUNSHINE\nSAA\nT_SAA" >>mkf_cols.tmp
echo "T_DY_NT" >>mkf_cols.tmp
echo "COR\nRBM_CONT\nZ_ALPHA\nZ_DELTA\nEULER_3" >>mkf_cols.tmp
echo "SAT_ALT\nSAT_LON\nSAT_LAT" >>mkf_cols.tmp
echo "G2_H0\nG2_H2\nG3_H0\nG3_H2" >>mkf_cols.tmp
echo "G2_L1\nG3_L1" >>mkf_cols.tmp

################################
# Loop over all telemetry files
################################
typeset -i i=1
while [ $i -le $nfiles ]; do

    ######################
    # Telemetry file name
    ######################
    number="$(echo $i | nawk '{printf ("%03d", $0)}')"
    full=$($UTIL/read_parfile $JOBPAR frffile_$number)
    tel=${full##*/}
    tel=${tel%.Z}
    tel=${tel%.gz}


    base=${tel#ft}

    ##########################
    # Extract frame mode data
    ##########################
    $UTIL/log_entry "Extracting frame mode data from $tel"

    $rddsoft/tlmFrm -t $tel \
                    -o fr${base} \
                    -f >stdout.log 2>stderr.log
    code=$?

    grep -v "Frame/Integration mode was not completed." stderr.log | \
    grep -v "Wrong sis OS table"  >filtered.log

    $UTIL/stool_test tlmFrm $code $0 1 stdout.log stderr.log filtered.log

    ###############################################################
    # Process each frame mode file to
    # subtract bias and the dark frame, remove overclocked pixels,
    # and change the (h,v) address of a pixel
    ###############################################################
    for file in $(ls fr${base}* 2>/dev/null); do

        $UTIL/log_entry "Processing $file"

        rm -f rdd.tmp
        $rddsoft/mkPixLvl -l -40 \
                          -u 40  \
                          -i $file \
                          -o rdd.tmp >stdout.log 2>stderr.log
        code=$?
        grep -v "Warning: some pixels in the image have bad value" \
                stderr.log >filtered.log

        $UTIL/stool_test mkPixLvl $code $0 1 \
                                  stdout.log stderr.log filtered.log

        if [ -a rdd.tmp ]; then
            mv -f rdd.tmp $file
        else
            # $UTIL/exception $0 1 "$file could not be leveled"
            rm -f $file
            continue
        fi

        ########################################################
        # Put some additional information into the FITS header
        # of each frame mode file
        ########################################################
        $UTIL/log_entry "Adding keywords to header of $file"

        ######################
        # Start and stop time
        ######################
        tstart=$($UTIL/read_fits_keyword $file[0] TSTART)
        tstop=$( $UTIL/read_fits_keyword $file[0] TSTOP )

        ###################################
        # Information from the filter file
        ###################################
        filter=$(echo ft${base} | sed -e's/\./_/g').mkf
        low_time=$($STOOL/equals "$tstart - 32.")

        rm -f row.tmp
        $UTIL/setup_parfile $FTOOL/fselect.par infile=$filter  \
                                               outfile=row.tmp \
                      expr="TIME >= $low_time && TIME<=$tstop" \
                                               histkw=no       \
                                               copyall=no      \
                                               keycopy=yes     \
                                               clobber=yes

        $FTOOL/fselect >stdout.log 2>stderr.log
        $UTIL/ftool_test fselect $? $0 2 stdout.log stderr.log

        ##################################
        # Read info from extracted row(s)
        ##################################
        $UTIL/setup_parfile $FTOOL/fdump.par infile=row.tmp      \
                                             outfile=STDOUT      \
                                             rows=-              \
                                             columns="@mkf_cols.tmp"  \
                                             prhead=no           \
                                             prdata=yes          \
                                             showcol=no          \
                                             showunit=no         \
                                             showscale=no        \
                                             showrow=no          \
                                             tdisp=yes           \
                                             page=no             \
                                             wrap=no             \
                                             align=no            \
                                             pagewidth=256

        $FTOOL/fdump 2>stderr.log | tail -1 | \
                  read s0_temp s1_temp fov br_earth elv sunshine saa t_saa \
                       cor rbm_cont ra dec roll  \
                       sat_alt sat_lon sat_lat   \
                       g2_h0 g2_h2 g3_h0 g3_h2   \
                       g2_l1 g3_l1

        $UTIL/ftool_test fdump $? $0 2 "" stderr.log


        rm -f row.tmp

        #######################################
        # Put the new keywords into the header
        #######################################
        rm -f template.tmp

        echo "SEQNUM=$seqnum / Sequential number from ODB"     >>template.tmp
        echo "SEQPNUM=$seqpnum / Number of times sequence processed" \
                                                               >>template.tmp
        echo "OBJECT='$object' / Name of observed object"      >>template.tmp
        echo "S0_TEMP=$s0_temp / SIS 0 CCD temperature (K)"    >>template.tmp
        echo "S1_TEMP=$s1_temp / SIS 1 CCD temperature (K)"    >>template.tmp

        echo "FOV=$fov / Field of veiw  0:Sky/1:Night Earth/2:Day Earth" \
                                                               >>template.tmp
        echo "BR_EARTH=$br_earth / Angular dist from Bright Earth (deg)" \
                                                               >>template.tmp
        echo "ELV=$elv / Target elevation from the Earth (deg)" \
                                                               >>template.tmp
        echo "SUNSHINE=$sunshine / 1:satellite is in daytime/ 0:night" \
                                                               >>template.tmp

        echo "SAA=$saa / Passage of South Atlantic Anomaly  1:yes/0:no" \
                                                               >>template.tmp
        echo "T_SAA=$t_saa / Time after SAA passage (sec)"     >>template.tmp

        echo "COR=$cor / Cut Off Rigidity (GeV) "              >>template.tmp
        echo "RBM_CONT=$rbm_cont / RBM counting rate (cts/sec)" \
                                                               >>template.tmp

        echo "RA=$ra / R.A.(2000) of Z-Axis (deg)"             >>template.tmp
        echo "DEC=$dec / Dec.(2000) of Z-Axis (deg)"           >>template.tmp
        echo "ROLL=$roll / 3rd Euler Angle (Z-Y-Z) (deg)"      >>template.tmp

        echo "SAT_ALT=$sat_alt / Satellite altitude (km)"      >>template.tmp
        echo "SAT_LON=$sat_lon / Satellite longitude (deg)"    >>template.tmp
        echo "SAT_LAT=$sat_lat / Satellite lattitude (deg)"    >>template.tmp

        echo "G2_H0=$g2_h0 / GIS2 monitor H0 (cts/sec)"        >>template.tmp
        echo "G2_H2=$g2_h2 / GIS2 monitor H2 (cts/sec)"        >>template.tmp
        echo "G3_H0=$g3_h0 / GIS3 monitor H0 (cts/sec)"        >>template.tmp
        echo "G3_H2=$g3_h2 / GIS3 monitor H2 (cts/sec)"        >>template.tmp

        echo "G2_L1=$g2_l1 / GIS2 monitor L1 (cts/sec)"        >>template.tmp
        echo "G3_L1=$g3_l1 / GIS3 monitor L1 (cts/sec)"        >>template.tmp


        $UTIL/setup_parfile $FTOOL/fmodhead.par infile=$file[0] \
                                                tmpfil=template.tmp
        $FTOOL/fmodhead >stdout.log 2>stderr.log
        $UTIL/ftool_test fmodhead $? $0 2 stdout.log stderr.log

        rm -f template.tmp

    done

    let i=$i+1

done

rm -f mkf_cols.tmp

################################################################
# Extract corner pixel histograms
################################################################
list=$($UTIL/generate_filename unfiltered any any 01 any)
list=$(ls $list 2>/dev/null)

if [ -z "$list" ]; then
     $UTIL/log_entry "No FAINT mode files from which to extract corner pixels"
else
     ######################################
     # extract GTIs from filter files
     ######################################


     criteria=$LISTS/corner_pixel.cri
     $UTIL/log_entry "Extracting corner pixel GTIs with the following criteria:"
     $UTIL/file_to_log $criteria

     for filter in $($UTIL/any_filename filter); do
          $UTIL/log_entry "Extracting GTIs from $filter"
          gti=${filter}_gti.tmp

          $UTIL/setup_parfile $FTOOL/maketime.par infile=$filter \
                                                   outfile=$gti \
                                                   expr=@$criteria \
                                                   compact=no \
                                                   copykw=no

          $FTOOL/maketime >stdout.log 2>stderr.log
          $UTIL/ftool_test maketime $? $0 2 stdout.log stderr.log
     done

fi


##################################################################
# loop over FAINT mode files and extract corner pixel histograms
##################################################################
for unfiltered in ${list} ; do

     inst=$(   $UTIL/parse_filename inst    $unfiltered)
     index=$(  $UTIL/parse_filename index   $unfiltered)
     bitrate=$($UTIL/parse_filename bitrate $unfiltered)

     ############################
     # get filter file name
     ############################
     telem=$($UTIL/read_fits_keyword $unfiltered[0] TLM_FILE)
     filter=$(echo $telem | sed -e's/\./_/g')
     filter=${filter}.mkf
     gti=${filter}_gti.tmp

     #######################
     # loop over chips
     #######################
     for chip in $($UTIL/dump_table $unfiltered[1] CCDID | sort -u); do

          corner=$($UTIL/generate_filename corner $inst $index 01 \
                                           $bitrate $chip)

          $UTIL/log_entry "Generating corner pixel histogram $corner"

          rm -f $corner
          $STOOL/corner_pix $corner $unfiltered $chip $gti \
                                                      >stdout.log 2>stderr.log
          $UTIL/stool_test corner_pix $? $0 2 stdout.log stderr.log

     done #loop over chips

done #loop over faint mode files

#######################
# clean up GTI files
#######################
rm -f *_gti.tmp


exit 0