gti_merge (version 2.0)

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

##############################################################################
#
# SYNTAX: gti_merge allgti listfile
#
# DESCRIPTION: This utility merges the GTIs in the event files listed in
# DESCRIPTION: listfile into a single allgti file. The files in listfile
# DESCRIPTION: should have the GTI extensions explicitly indicated
# DESCRIPTION: (e.g. event.unf[2]). The GTIs are assumed not to overlap.
# DESCRIPTION: GTIs in allgti are sorted by TSTART.
#
# VERSION: 2.0
#
# HISTORY: 0.0 -> 1.0 
# HISTORY: Changed the "column" parameter name to "columns" in fmemsort
# HISTORY: to accomodate FTOOLS 4.1
# HISTORY: 
# HISTORY: 1.0 -> 2.0 1999-05-07
# HISTORY: Converted from ksh to perl
#
# CALLS: $UTIL/setup_parfile
# CALLS: $UTIL/ftool_test
# CALLS: $UTIL/setup_parfile
# CALLS: $UTIL/ftool_test
# CALLS: $FTOOL/fmerge
# CALLS: $FTOOL/fmemsort
#
##############################################################################

($allgti, $listfile) = @ARGV;

$UTIL = $ENV{"UTIL"};
$FTOOL = $ENV{"FTOOL"};

$tmp = "out$$.tmp";

if ( -s "$listfile" ) {
    `$UTIL/log_entry "Merging GTIs from the following files:"`;
    `$UTIL/file_to_log $listfile`;
}

unlink $allgti;
unlink $tmp;

`$UTIL/setup_parfile $FTOOL/fmerge.par infiles="\@${listfile}" \\
                                       outfile="$tmp"         \\
                                       columns="START STOP"   \\
                                       mextname="ALLGTI"`;

`$FTOOL/fmerge >stdout.log 2>stderr.log`;
`$UTIL/ftool_test fmerge $? $0 2 stdout.log stderr.log`;

##############################
# Sort the GTIs by start time
##############################
`$UTIL/setup_parfile $FTOOL/fmemsort.par infile="$tmp"   \\
                                         outfile=$allgti \\
                                         columns="START" \\
                                         method=insert   \\
                                         ascend=yes      \\
                                         copyall=no      \\
                                         history=no`;

`$FTOOL/fmemsort >stdout.log 2>stdout.log`;
`$UTIL/ftool_test fmemsort $? $0 2 stdout.log stderr.log`;

unlink $tmp;

exit 0;